forked from fmueller/sonar-scala
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some surefire unit tests and fixing some errors
- Loading branch information
Showing
11 changed files
with
802 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/test/java/org/sonar/plugins/scala/surefire/SurefireStaxHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/test/java/org/sonar/plugins/scala/surefire/UnitTestClassReportTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.