Skip to content

Commit

Permalink
Add missing unit tests for some surefire classes
Browse files Browse the repository at this point in the history
  • Loading branch information
miklein committed Nov 13, 2014
1 parent ad767b0 commit a46f35a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public final class UnitTestClassReport {

private List<UnitTestResult> 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);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/sonar/plugins/scala/surefire/UnitTestIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
115 changes: 115 additions & 0 deletions src/test/java/org/sonar/plugins/scala/surefire/UnitTestIndexTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* 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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* 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));
}
}

0 comments on commit a46f35a

Please sign in to comment.