Search in sources :

Example 6 with MutableTestPlan

use of org.sonar.api.test.MutableTestPlan in project sonar-java by SonarSource.

the class JaCoCoSensorTest method test_read_execution_data_for_lines_covered_by_tests.

@Test
public void test_read_execution_data_for_lines_covered_by_tests() throws IOException {
    outputDir = TestUtils.getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest2/");
    jacocoExecutionData = new File(outputDir, "jacoco.exec");
    Files.copy(TestUtils.getResource("/org/sonar/plugins/jacoco/JaCoCoSensorTest2/org/example/App.class.toCopy"), new File(jacocoExecutionData.getParentFile(), "/org/example/App.class"));
    DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(10).build();
    when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
    when(javaClasspath.getBinaryDirs()).thenReturn(ImmutableList.of(outputDir));
    MutableTestable testAbleFile = mock(MutableTestable.class);
    when(perspectives.as(eq(MutableTestable.class), eq(resource))).thenReturn(testAbleFile);
    MutableTestCase testCase = mock(MutableTestCase.class);
    when(testCase.name()).thenReturn("test");
    MutableTestPlan testPlan = mock(MutableTestPlan.class);
    when(testPlan.testCasesByName("test")).thenReturn(newArrayList(testCase));
    when(perspectives.as(eq(MutableTestPlan.class), eq(resource))).thenReturn(testPlan);
    context.settings().setProperty(REPORT_PATH_PROPERTY, jacocoExecutionData.getAbsolutePath());
    SensorContextTester spy = Mockito.spy(context);
    sensor.execute(spy);
    verify(spy, times(1)).newCoverage();
    verify(testCase).setCoverageBlock(testAbleFile, newArrayList(3, 6));
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) MutableTestable(org.sonar.api.test.MutableTestable) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 7 with MutableTestPlan

use of org.sonar.api.test.MutableTestPlan in project sonarqube by SonarSource.

the class MeasuresPublisher method updateTestExecutionFromTestPlan.

private void updateTestExecutionFromTestPlan(final InputFile inputFile) {
    final MutableTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile);
    if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
        return;
    }
    long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count();
    measureCache.put(inputFile.key(), TESTS_KEY, new DefaultMeasure<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
    long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).mapToLong(t -> t.durationInMs() != null ? t.durationInMs().longValue() : 0L).sum();
    measureCache.put(inputFile.key(), TEST_EXECUTION_TIME_KEY, new DefaultMeasure<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
    long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count();
    measureCache.put(inputFile.key(), TEST_ERRORS_KEY, new DefaultMeasure<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
    long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count();
    measureCache.put(inputFile.key(), SKIPPED_TESTS_KEY, new DefaultMeasure<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
    long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count();
    measureCache.put(inputFile.key(), TEST_FAILURES_KEY, new DefaultMeasure<Integer>().forMetric(TEST_FAILURES).withValue((int) failedTests));
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputFile(org.sonar.api.batch.fs.InputFile) Metric(org.sonar.api.batch.measure.Metric) Iterables(com.google.common.collect.Iterables) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) SKIPPED_TESTS_KEY(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) SKIPPED_TESTS(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS) LINES_TO_COVER(org.sonar.api.measures.CoreMetrics.LINES_TO_COVER) Type(org.sonar.api.batch.fs.InputFile.Type) TESTS(org.sonar.api.measures.CoreMetrics.TESTS) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) UNCOVERED_LINES_KEY(org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES_KEY) TEST_ERRORS_KEY(org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY) CONDITIONS_TO_COVER_KEY(org.sonar.api.measures.CoreMetrics.CONDITIONS_TO_COVER_KEY) TEST_FAILURES_KEY(org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY) UNCOVERED_CONDITIONS(org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS) Status(org.sonar.api.test.TestCase.Status) CONDITIONS_TO_COVER(org.sonar.api.measures.CoreMetrics.CONDITIONS_TO_COVER) StringValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue) Map(java.util.Map) TEST_EXECUTION_TIME_KEY(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY) StreamSupport(java.util.stream.StreamSupport) UNCOVERED_CONDITIONS_KEY(org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS_KEY) LINES_TO_COVER_KEY(org.sonar.api.measures.CoreMetrics.LINES_TO_COVER_KEY) InputComponent(org.sonar.api.batch.fs.InputComponent) TEST_EXECUTION_TIME(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME) TestPlanBuilder(org.sonar.scanner.deprecated.test.TestPlanBuilder) IntValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.IntValue) LongValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.LongValue) TEST_ERRORS(org.sonar.api.measures.CoreMetrics.TEST_ERRORS) MeasureCache(org.sonar.scanner.scan.measure.MeasureCache) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) MutableTestPlan(org.sonar.api.test.MutableTestPlan) BoolValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.BoolValue) TEST_FAILURES(org.sonar.api.measures.CoreMetrics.TEST_FAILURES) TESTS_KEY(org.sonar.api.measures.CoreMetrics.TESTS_KEY) UNCOVERED_LINES(org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) Collections(java.util.Collections) DoubleValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.DoubleValue) MutableTestPlan(org.sonar.api.test.MutableTestPlan)

Example 8 with MutableTestPlan

use of org.sonar.api.test.MutableTestPlan in project sonar-java by SonarSource.

the class SurefireJavaParserTest method should_register_tests.

@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(isNull())).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((ArgumentMatcher<InputFile>) o -> ":ch.hortis.sonar.mvn.mc.MetricsCollectorRegistryTest".equals(o.key())))).thenReturn(testPlan);
    parser.collect(context, getDirs("multipleReports"), true);
    verify(testPlan).addTestCase("testGetUnKnownCollector");
    verify(testPlan).addTestCase("testGetJDependsCollector");
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) ArgumentMatcher(org.mockito.ArgumentMatcher) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.Test)

Example 9 with MutableTestPlan

use of org.sonar.api.test.MutableTestPlan in project sonar-java by SonarSource.

the class JaCoCoSensorTest method testExecutionDataForLinesCoveredByTest.

private void testExecutionDataForLinesCoveredByTest(String path, List<Integer> linesExpected) {
    outputDir = TestUtils.getResource(path);
    jacocoExecutionData = new File(outputDir, "jacoco.exec");
    DefaultInputFile resource = new TestInputFileBuilder("", "").setLines(25).build();
    when(javaResourceLocator.findResourceByClassName(anyString())).thenReturn(resource);
    when(javaClasspath.getBinaryDirs()).thenReturn(ImmutableList.of(outputDir));
    MutableTestable testAbleFile = mock(MutableTestable.class);
    when(perspectives.as(eq(MutableTestable.class), eq(resource))).thenReturn(testAbleFile);
    MutableTestCase testCase = mock(MutableTestCase.class);
    when(testCase.name()).thenReturn("test");
    MutableTestPlan testPlan = mock(MutableTestPlan.class);
    when(testPlan.testCasesByName("testBoth")).thenReturn(newArrayList(testCase));
    when(testPlan.testCasesByName("testFoo")).thenReturn(newArrayList(testCase));
    when(perspectives.as(eq(MutableTestPlan.class), eq(resource))).thenReturn(testPlan);
    context.settings().setProperty(REPORT_PATH_PROPERTY, jacocoExecutionData.getAbsolutePath());
    sensor.execute(context);
    verify(testCase).setCoverageBlock(testAbleFile, linesExpected);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) MutableTestable(org.sonar.api.test.MutableTestable) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File)

Aggregations

MutableTestPlan (org.sonar.api.test.MutableTestPlan)9 MutableTestCase (org.sonar.api.test.MutableTestCase)6 File (java.io.File)5 InputFile (org.sonar.api.batch.fs.InputFile)4 MutableTestable (org.sonar.api.test.MutableTestable)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 InputComponent (org.sonar.api.batch.fs.InputComponent)2 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)2 Iterables (com.google.common.collect.Iterables)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 StreamSupport (java.util.stream.StreamSupport)1