GroovySurefireParser { public void collect(SensorContext context, File reportsDir) { File[] xmlFiles = getReports(reportsDir); if (xmlFiles.length > 0) { parseFiles(context, xmlFiles); } } GroovySurefireParser(Groovy groovy, ResourcePerspectives perspectives, FileSystem fs); void collect(SensorContext context, File reportsDir);  }
@Test public void should_register_tests() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); MutableTestCase testCase = mock(MutableTestCase.class); when(testCase.setDurationInMs(anyLong())).thenReturn(testCase); when(testCase.setStatus(any(TestCase.Status.class))).thenReturn(testCase); when(testCase.setMessage(ArgumentMatchers.nullable(String.class))).thenReturn(testCase); when(testCase.setStackTrace(anyString())).thenReturn(testCase); when(testCase.setType(anyString())).thenReturn(testCase); MutableTestPlan testPlan = mock(MutableTestPlan.class); when(testPlan.addTestCase(anyString())).thenReturn(testCase); when(perspectives.as(eq(MutableTestPlan.class), argThat(inputFileMatcher(":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest")))).thenReturn(testPlan); parser.collect(context, getDir("multipleReports")); verify(testPlan).addTestCase("testGetUnKnownCollector"); verify(testPlan).addTestCase("testGetJDependsCollector"); }
@Test public void should_store_zero_tests_when_directory_is_null_or_non_existing_or_a_file() throws Exception { parser.collect(context, null); verify(context, never()).newMeasure(); context = mock(SensorContext.class); parser.collect(context, getDir("nonExistingReportsDirectory")); verify(context, never()).newMeasure(); context = mock(SensorContext.class); parser.collect(context, getDir("file.txt")); verify(context, never()).newMeasure(); }
@Test public void shouldAggregateReports() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); parser.collect(context, getDir("multipleReports")); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest")).hasSize(6); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CloverCollectorTest")).hasSize(6); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CheckstyleCollectorTest")).hasSize(6); assertThat(context.measures(":ch.hortis.sonar.mvn.SonarMojoTest")).hasSize(6); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JDependsCollectorTest")).hasSize(6); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JavaNCSSCollectorTest")).hasSize(6); }
@Test public void shouldUseTestSuiteReportIfAlone() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); parser.collect(context, getDir("onlyTestSuiteReport")); assertThat(context.measures(":org.sonar.SecondTest")).hasSize(6); assertThat(context.measures(":org.sonar.JavaNCSSCollectorTest")).hasSize(6); }
@Test public void shouldInsertZeroWhenNoReports() throws URISyntaxException { SensorContext context = mock(SensorContext.class); parser.collect(context, getDir("noReports")); verify(context, never()).newMeasure(); }
@Test public void shouldNotInsertZeroOnFiles() throws URISyntaxException { SensorContext context = mock(SensorContext.class); parser.collect(context, getDir("noTests")); verify(context, never()).newMeasure(); }
@Test public void shouldMergeInnerClasses() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); parser.collect(context, getDir("innerClasses")); assertThat(context.measure(":org.apache.commons.collections.bidimap.AbstractTestBidiMap", CoreMetrics.TESTS).value()).isEqualTo(7); assertThat(context.measure(":org.apache.commons.collections.bidimap.AbstractTestBidiMap", CoreMetrics.TEST_ERRORS).value()).isEqualTo(1); assertThat(context.measures(":org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")).isEmpty(); }
@Test public void shouldMergeNestedInnerClasses() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); parser.collect(context, getDir("nestedInnerClasses")); assertThat(context.measure(":org.sonar.plugins.surefire.NestedInnerTest", CoreMetrics.TESTS).value()).isEqualTo(3); }
@Test public void should_not_count_negative_tests() throws URISyntaxException { SensorContextTester context = SensorContextTester.create(new File("")); parser.collect(context, getDir("negativeTestTime")); assertThat(context.measure(":java.Foo", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(0); assertThat(context.measure(":java.Foo", CoreMetrics.TESTS).value()).isEqualTo(6); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_FAILURES).value()).isEqualTo(0); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(659); }
@Test public void should_generate_correct_predicate() throws URISyntaxException { DefaultFileSystem fs = new DefaultFileSystem(new File(".")); DefaultInputFile inputFile = new DefaultInputFile("", "src/test/org/sonar/JavaNCSSCollectorTest.groovy") .setLanguage(Groovy.KEY) .setType(Type.TEST); fs.add(inputFile); parser = new GroovySurefireParser(groovy, perspectives, fs); SensorContextTester context = SensorContextTester.create(new File("")); context.setFileSystem(fs); parser.collect(context, getDir("onlyTestSuiteReport")); assertThat(context.measure(":src/test/org/sonar/JavaNCSSCollectorTest.groovy", CoreMetrics.TESTS).value()).isEqualTo(11); }