Skip to content

Commit

Permalink
Add some surefire unit tests and fixing some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
miklein committed Nov 13, 2014
1 parent 8f0e955 commit ad767b0
Show file tree
Hide file tree
Showing 11 changed files with 802 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
*/
package org.sonar.plugins.scala.surefire;

import com.google.common.collect.Lists;

import java.util.Collections;
import java.util.List;

import com.google.common.collect.Lists;

public final class UnitTestClassReport {
private long errors = 0L;
private long failures = 0L;
Expand All @@ -46,8 +47,12 @@ public UnitTestClassReport add(UnitTestResult result) {
errors += 1;
}
tests += 1;
durationMilliseconds += result.getDurationMilliseconds();
return this;
if (result.getDurationMilliseconds() < 0) {
negativeTimeTestNumber += 1;
} else {
durationMilliseconds += result.getDurationMilliseconds();
}
return this;
}

private void initResults() {
Expand Down Expand Up @@ -80,6 +85,13 @@ public long getNegativeTimeTestNumber() {
return negativeTimeTestNumber;
}

public List<UnitTestResult> getResults() {
if (results == null) {
return Collections.emptyList();
}
return results;
}

public String toXml() {
StringBuilder sb = new StringBuilder(256);
sb.append("<tests-details>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
*/
package org.sonar.plugins.scala.surefire;

import com.google.common.collect.Maps;

import java.util.Map;

import com.google.common.collect.Maps;

public class UnitTestIndex {

private Map<String, UnitTestClassReport> indexByClassname;
Expand All @@ -46,5 +46,9 @@ public Map<String, UnitTestClassReport> getIndexByClassname() {
public int size() {
return indexByClassname.size();
}

public UnitTestClassReport get(String classname) {
return indexByClassname.get(classname);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.Before;
import org.junit.Test;
import org.sonar.api.utils.StaxParser;
import org.sonar.test.TestUtils;

import javax.xml.stream.XMLStreamException;
import java.io.File;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;

public class SurefireStaxHandlerTest {

private UnitTestIndex index;

@Before
public void setUp() {
index = new UnitTestIndex();
}

@Test
public void shouldHaveSkippedTests() throws XMLStreamException {
parse("skippedTests.xml");
UnitTestClassReport report = index.get("org.sonar.Foo");
assertThat(report.getTests(), is(3L));
assertThat(report.getSkipped(), is(1L));
}

@Test
public void shouldHaveZeroTests() throws XMLStreamException {
parse("zeroTests.xml");
assertThat(index.size(), is(0));
}

@Test
public void shouldHaveTestOnRootPackage() throws XMLStreamException {
parse("rootPackage.xml");
assertThat(index.size(), is(1));
UnitTestClassReport report = index.get("NoPackagesTest");
assertThat(report.getTests(), is(2L));
}

@Test
public void shouldHaveErrorsAndFailures() throws XMLStreamException {
parse("errorsAndFailures.xml");
UnitTestClassReport report = index.get("org.sonar.Foo");
assertThat(report.getErrors(), is(1L));
assertThat(report.getFailures(), is(1L));
assertThat(report.getResults().size(), is(2));

// failure
UnitTestResult failure = report.getResults().get(0);
assertThat(failure.getDurationMilliseconds(), is(5L));
assertThat(failure.getStatus(), is(UnitTestResult.STATUS_FAILURE));
assertThat(failure.getName(), is("testOne"));
assertThat(failure.getMessage(), startsWith("expected"));

// error
UnitTestResult error = report.getResults().get(1);
assertThat(error.getDurationMilliseconds(), is(0L));
assertThat(error.getStatus(), is(UnitTestResult.STATUS_ERROR));
assertThat(error.getName(), is("testTwo"));
}

@Test
public void shouldSupportMultipleSuitesInSameReport() throws XMLStreamException {
parse("multipleSuites.xml");

assertThat(index.get("org.sonar.JavaNCSSCollectorTest").getTests(), is(11L));
assertThat(index.get("org.sonar.SecondTest").getTests(), is(4L));
}

private void parse(String path) throws XMLStreamException {
File xml = TestUtils.getResource(getClass(), path);
SurefireStaxHandler staxParser = new SurefireStaxHandler(index);
StaxParser parser = new StaxParser(staxParser, false);
parser.parse(xml);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.MatcherAssert.assertThat;

public class UnitTestClassReportTest {

@Test
public void shouldIncrementCounters() {
UnitTestClassReport report = new UnitTestClassReport();
report.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_ERROR).setDurationMilliseconds(500L));
report.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_OK).setDurationMilliseconds(200L));
//Some negative duration can occur due to bug in surefire.
report.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_OK).setDurationMilliseconds(-200L));
report.add(new UnitTestResult().setStatus(UnitTestResult.STATUS_SKIPPED));

assertThat(report.getResults().size(), is(4));
assertThat(report.getSkipped(), is(1L));
assertThat(report.getTests(), is(4L));
assertThat(report.getDurationMilliseconds(), is(500L + 200L));
assertThat(report.getErrors(), is(1L));
assertThat(report.getFailures(), is(0L));
assertThat(report.getNegativeTimeTestNumber(), is(1L));
}

@Test
public void shouldHaveEmptyReport() {
UnitTestClassReport report = new UnitTestClassReport();
assertThat(report.getResults().size(), is(0));
assertThat(report.getSkipped(), is(0L));
assertThat(report.getTests(), is(0L));
assertThat(report.getDurationMilliseconds(), is(0L));
assertThat(report.getErrors(), is(0L));
assertThat(report.getFailures(), is(0L));
}
}
Loading

0 comments on commit ad767b0

Please sign in to comment.