From a46f35a1d68a2134f1b75db9b2c9b8e190c69668 Mon Sep 17 00:00:00 2001 From: Michael Klein Date: Thu, 13 Nov 2014 17:31:56 +0100 Subject: [PATCH] Add missing unit tests for some surefire classes --- .../scala/surefire/UnitTestClassReport.java | 7 ++ .../plugins/scala/surefire/UnitTestIndex.java | 15 +++ .../scala/surefire/UnitTestIndexTest.java | 115 ++++++++++++++++++ .../scala/surefire/UnitTestResultTest.java | 52 ++++++++ 4 files changed, 189 insertions(+) create mode 100644 src/test/java/org/sonar/plugins/scala/surefire/UnitTestIndexTest.java create mode 100644 src/test/java/org/sonar/plugins/scala/surefire/UnitTestResultTest.java diff --git a/src/main/java/org/sonar/plugins/scala/surefire/UnitTestClassReport.java b/src/main/java/org/sonar/plugins/scala/surefire/UnitTestClassReport.java index 06b920c..f734135 100644 --- a/src/main/java/org/sonar/plugins/scala/surefire/UnitTestClassReport.java +++ b/src/main/java/org/sonar/plugins/scala/surefire/UnitTestClassReport.java @@ -34,6 +34,13 @@ public final class UnitTestClassReport { private List results = null; + public UnitTestClassReport add(UnitTestClassReport other) { + for (UnitTestResult otherResult : other.getResults()) { + add(otherResult); + } + return this; + } + public UnitTestClassReport add(UnitTestResult result) { initResults(); results.add(result); diff --git a/src/main/java/org/sonar/plugins/scala/surefire/UnitTestIndex.java b/src/main/java/org/sonar/plugins/scala/surefire/UnitTestIndex.java index 0c33c76..9c2e773 100644 --- a/src/main/java/org/sonar/plugins/scala/surefire/UnitTestIndex.java +++ b/src/main/java/org/sonar/plugins/scala/surefire/UnitTestIndex.java @@ -51,4 +51,19 @@ public UnitTestClassReport get(String classname) { return indexByClassname.get(classname); } + public UnitTestClassReport merge(String classname, String intoClassname) { + UnitTestClassReport from = indexByClassname.get(classname); + if (from!=null) { + UnitTestClassReport to = index(intoClassname); + to.add(from); + indexByClassname.remove(classname); + return to; + } + return null; + } + + public void remove(String classname) { + indexByClassname.remove(classname); + } + } diff --git a/src/test/java/org/sonar/plugins/scala/surefire/UnitTestIndexTest.java b/src/test/java/org/sonar/plugins/scala/surefire/UnitTestIndexTest.java new file mode 100644 index 0000000..8375876 --- /dev/null +++ b/src/test/java/org/sonar/plugins/scala/surefire/UnitTestIndexTest.java @@ -0,0 +1,115 @@ +/* + * Sonar Scala Plugin + * Copyright (C) 2011 - 2014 All contributors + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.scala.surefire; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertSame; + +public class UnitTestIndexTest { + + @Test + public void shouldIndexNewClassname() { + UnitTestIndex index = new UnitTestIndex(); + + UnitTestClassReport report = index.index("org.sonar.Foo"); + + assertThat(report.getTests(), is(0L)); + assertThat(index.size(), is(1)); + assertSame(index.get("org.sonar.Foo"), report); + } + + @Test + public void shouldNotReIndex() { + UnitTestIndex index = new UnitTestIndex(); + + UnitTestClassReport report1 = index.index("org.sonar.Foo"); + UnitTestClassReport report2 = index.index("org.sonar.Foo"); + + assertSame(report1, report2); + assertThat(report1.getTests(), is(0L)); + assertThat(index.size(), is(1)); + assertSame(index.get("org.sonar.Foo"), report1); + } + + @Test + public void shouldRemoveClassname() { + UnitTestIndex index = new UnitTestIndex(); + + index.index("org.sonar.Foo"); + index.remove("org.sonar.Foo"); + + assertThat(index.size(), is(0)); + assertThat(index.get("org.sonar.Foo"), nullValue()); + } + + @Test + public void shouldMergeClasses() { + UnitTestIndex index = new UnitTestIndex(); + UnitTestClassReport innerClass = index.index("org.sonar.Foo$Bar"); + innerClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_ERROR).setDurationMilliseconds(500L)); + innerClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_OK).setDurationMilliseconds(200L)); + UnitTestClassReport publicClass = index.index("org.sonar.Foo"); + publicClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_ERROR).setDurationMilliseconds(1000L)); + publicClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_FAILURE).setDurationMilliseconds(350L)); + + index.merge("org.sonar.Foo$Bar", "org.sonar.Foo"); + + assertThat(index.size(), is(1)); + UnitTestClassReport report = index.get("org.sonar.Foo"); + assertThat(report.getTests(), is(4L)); + assertThat(report.getFailures(), is(1L)); + assertThat(report.getErrors(), is(2L)); + assertThat(report.getSkipped(), is(0L)); + assertThat(report.getResults().size(), is(4)); + assertThat(report.getDurationMilliseconds(), is(500L + 200L + 1000L + 350L)); + } + + @Test + public void shouldRenameClassWhenMergingToNewClass() { + UnitTestIndex index = new UnitTestIndex(); + UnitTestClassReport innerClass = index.index("org.sonar.Foo$Bar"); + innerClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_ERROR).setDurationMilliseconds(500L)); + innerClass.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_OK).setDurationMilliseconds(200L)); + + index.merge("org.sonar.Foo$Bar", "org.sonar.Foo"); + + assertThat(index.size(), is(1)); + UnitTestClassReport report = index.get("org.sonar.Foo"); + assertThat(report.getTests(), is(2L)); + assertThat(report.getFailures(), is(0L)); + assertThat(report.getErrors(), is(1L)); + assertThat(report.getSkipped(), is(0L)); + assertThat(report.getResults().size(), is(2)); + assertThat(report.getDurationMilliseconds(), is(500L + 200L)); + } + + @Test + public void shouldNotFailWhenMergingUnknownClass() { + UnitTestIndex index = new UnitTestIndex(); + + index.merge("org.sonar.Foo$Bar", "org.sonar.Foo"); + + assertThat(index.size(), is(0)); + } +} diff --git a/src/test/java/org/sonar/plugins/scala/surefire/UnitTestResultTest.java b/src/test/java/org/sonar/plugins/scala/surefire/UnitTestResultTest.java new file mode 100644 index 0000000..f14a689 --- /dev/null +++ b/src/test/java/org/sonar/plugins/scala/surefire/UnitTestResultTest.java @@ -0,0 +1,52 @@ +/* + * Sonar Scala Plugin + * Copyright (C) 2011 - 2014 All contributors + * dev@sonar.codehaus.org + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.scala.surefire; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class UnitTestResultTest { + + @Test + public void shouldBeError() { + UnitTestResult result = new UnitTestResult().setStatus(UnitTestResult.STATUS_ERROR); + assertThat(result.getStatus(), is(UnitTestResult.STATUS_ERROR)); + assertThat(result.isError(), is(true)); + assertThat(result.isErrorOrFailure(), is(true)); + } + + @Test + public void shouldBeFailure() { + UnitTestResult result = new UnitTestResult().setStatus(UnitTestResult.STATUS_FAILURE); + assertThat(result.getStatus(), is(UnitTestResult.STATUS_FAILURE)); + assertThat(result.isError(), is(false)); + assertThat(result.isErrorOrFailure(), is(true)); + } + + @Test + public void shouldBeSuccess() { + UnitTestResult result = new UnitTestResult().setStatus(UnitTestResult.STATUS_OK); + assertThat(result.getStatus(), is(UnitTestResult.STATUS_OK)); + assertThat(result.isError(), is(false)); + assertThat(result.isErrorOrFailure(), is(false)); + } +}