SurefireJavaParser { public void collect(SensorContext context, List<File> reportsDirs, boolean reportDirSetByUser) { List<File> xmlFiles = getReports(reportsDirs, reportDirSetByUser); if (!xmlFiles.isEmpty()) { parseFiles(context, xmlFiles); } } SurefireJavaParser(JavaResourceLocator javaResourceLocator); void collect(SensorContext context, List<File> reportsDirs, boolean reportDirSetByUser);  }
@Test void should_store_zero_tests_when_directory_is_null_or_non_existing_or_a_file() throws Exception { SensorContext context = mock(SensorContext.class); context = mock(SensorContext.class); parser.collect(context, getDirs("nonExistingReportsDirectory"), false); verify(context, never()).newMeasure(); context = mock(SensorContext.class); parser.collect(context, getDirs("file.txt"), true); verify(context, never()).newMeasure(); }
@Test void shouldAggregateReports() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("multipleReports"), true); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CloverCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CheckstyleCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.SonarMojoTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JDependsCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JavaNCSSCollectorTest")).hasSize(5); }
@Test void shouldAggregateReportsFromMultipleDirectories() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("multipleDirectories/dir1", "multipleDirectories/dir2"), true); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CloverCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.CheckstyleCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.SonarMojoTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JDependsCollectorTest")).hasSize(5); assertThat(context.measures(":ch.hortis.sonar.mvn.mc.JavaNCSSCollectorTest")).hasSize(5); }
@Test void shouldUseTestSuiteReportIfAlone() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("onlyTestSuiteReport"), true); assertThat(context.measures(":org.sonar.SecondTest")).hasSize(5); assertThat(context.measures(":org.sonar.JavaNCSSCollectorTest")).hasSize(5); }
@Test void shouldInsertZeroWhenNoReports() throws URISyntaxException { SensorContext context = mock(SensorContext.class); parser.collect(context, getDirs("noReports"), true); verify(context, never()).newMeasure(); }
@Test void shouldNotInsertZeroOnFiles() throws URISyntaxException { SensorContext context = mock(SensorContext.class); parser.collect(context, getDirs("noTests"), true); verify(context, never()).newMeasure(); }
@Test void shouldMergeInnerClasses() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("innerClasses"), true); 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 void shouldMergeNestedInnerClasses() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("nestedInnerClasses"), true); assertThat(context.measure(":org.sonar.plugins.surefire.NestedInnerTest", CoreMetrics.TESTS).value()).isEqualTo(3); }
@Test void shouldMergeInnerClassReportInExtraFile() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("innerClassExtraFile"), true); assertThat(context.measure(":com.example.project.CalculatorTests", CoreMetrics.TESTS).value()).isEqualTo(6); }
@Test void should_not_count_negative_tests() throws URISyntaxException { SensorContextTester context = mockContext(); parser.collect(context, getDirs("negativeTestTime"), true); assertThat(context.measure(":java.Foo", CoreMetrics.SKIPPED_TESTS).value()).isZero(); assertThat(context.measure(":java.Foo", CoreMetrics.TESTS).value()).isEqualTo(6); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_ERRORS).value()).isZero(); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_FAILURES).value()).isZero(); assertThat(context.measure(":java.Foo", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(659); }
@Test void should_handle_parameterized_tests() throws URISyntaxException { SensorContextTester context = mockContext(); when(javaResourceLocator.findResourceByClassName(anyString())) .thenAnswer(invocation -> { String className = (String) invocation.getArguments()[0]; if (className.equals("org.foo.Junit4ParameterizedTest") || className.startsWith("org.foo.Junit5_0ParameterizedTest") || className.startsWith("org.foo.Junit5_1ParameterizedTest")) { return new TestInputFileBuilder("", className).build(); } return null; }); parser.collect(context, getDirs("junitParameterizedTests"), true); assertThat(context.measure(":org.foo.Junit4ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(7); assertThat(context.measure(":org.foo.Junit4ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(1); assertThat(context.measure(":org.foo.Junit5_0ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(13); assertThat(context.measure(":org.foo.Junit5_0ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(48); assertThat(context.measure(":org.foo.Junit5_1ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(13); assertThat(context.measure(":org.foo.Junit5_1ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(48); }
@Test void should_log_missing_resource_with_debug_level() throws Exception { SensorContextTester context = mockContext(); parser = new SurefireJavaParser(mock(JavaResourceLocator.class)); parser.collect(context, getDirs("resourceNotFound"), true); assertThat(logTester.logs(LoggerLevel.WARN)).isEmpty(); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Resource not found: org.sonar.Foo"); }