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));
}
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));
}
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");
}
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);
}
Aggregations