Search in sources :

Example 1 with MutableTestable

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);
        }
    }
}
Also used : MutableTestCase(org.sonar.api.test.MutableTestCase) ArrayList(java.util.ArrayList) MutableTestable(org.sonar.api.test.MutableTestable) IOException(java.io.IOException) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) MutableTestPlan(org.sonar.api.test.MutableTestPlan) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

Example 2 with MutableTestable

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));
}
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 3 with MutableTestable

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

File (java.io.File)3 MutableTestCase (org.sonar.api.test.MutableTestCase)3 MutableTestPlan (org.sonar.api.test.MutableTestPlan)3 MutableTestable (org.sonar.api.test.MutableTestable)3 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 InputFile (org.sonar.api.batch.fs.InputFile)1 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)1