use of org.sonar.api.test.MutableTestable in project sonarqube by SonarSource.
the class CoveragePerTestSensor method processTestFile.
private void processTestFile(InputFile inputFile, SensorContext context) {
File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
if (testExecutionFile.exists()) {
LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
int lineNumber = 0;
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(";").split(line).iterator();
String name = split.next();
while (split.hasNext()) {
String coveredBlockStr = split.next();
Iterator<String> splitCoveredBlock = Splitter.on(",").split(coveredBlockStr).iterator();
String componentPath = splitCoveredBlock.next();
InputFile coveredFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(componentPath));
MutableTestable testable = perspectives.as(MutableTestable.class, coveredFile);
List<Integer> coveredLines = new ArrayList<>();
while (splitCoveredBlock.hasNext()) {
coveredLines.add(Integer.parseInt(splitCoveredBlock.next()));
}
for (MutableTestCase testCase : testPlan.testCasesByName(name)) {
testCase.setCoverageBlock(testable, coveredLines);
}
}
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.sonar.api.test.MutableTestable 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.MutableTestable 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