Search in sources :

Example 1 with MutableTestPlan

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

the class TestExecutionAndCoveragePublisher method publish.

@Override
public void publish(ScannerReportWriter writer) {
    final ScannerReport.Test.Builder testBuilder = ScannerReport.Test.newBuilder();
    final ScannerReport.CoverageDetail.Builder builder = ScannerReport.CoverageDetail.newBuilder();
    final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder = ScannerReport.CoverageDetail.CoveredFile.newBuilder();
    for (final InputComponent c : componentStore.all()) {
        DefaultInputComponent component = (DefaultInputComponent) c;
        final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component);
        if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
            continue;
        }
        final Set<String> testNamesWithCoverage = new HashSet<>();
        writer.writeTests(component.batchId(), StreamSupport.stream(testPlan.testCases().spliterator(), false).map(testCase -> toProtobufTest(testBuilder, testNamesWithCoverage, testCase)).collect(toList()));
        writer.writeCoverageDetails(component.batchId(), testNamesWithCoverage.stream().map(testName -> toProtobufCoverageDetails(builder, coveredBuilder, testPlan, testName)).collect(toList()));
    }
}
Also used : CoverageDetail(org.sonar.scanner.protocol.output.ScannerReport.CoverageDetail) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputComponent(org.sonar.api.batch.fs.InputComponent) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) MutableTestPlan(org.sonar.api.test.MutableTestPlan) Test(org.sonar.scanner.protocol.output.ScannerReport.Test) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) HashSet(java.util.HashSet)

Example 2 with MutableTestPlan

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

the class GenericTestExecutionReportParserTest method mockMutableTestPlan.

private MutableTestPlan mockMutableTestPlan(MutableTestCase testCase) {
    MutableTestPlan testPlan = mock(MutableTestPlan.class);
    when(testPlan.addTestCase(anyString())).thenReturn(testCase);
    return testPlan;
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan)

Example 3 with MutableTestPlan

use of org.sonar.api.test.MutableTestPlan 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 4 with MutableTestPlan

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

the class TestExecutionSensor 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();
                    String durationStr = split.next();
                    Long duration = StringUtils.isNotBlank(durationStr) ? Long.parseLong(durationStr) : null;
                    String msg = split.next();
                    String stack = split.next();
                    String status = split.next();
                    String type = split.next();
                    MutableTestCase testCase = testPlan.addTestCase(name);
                    testCase.setDurationInMs(duration);
                    testCase.setMessage(msg);
                    testCase.setStackTrace(stack);
                    testCase.setStatus(Status.valueOf(status));
                    testCase.setType(type);
                } catch (Exception e) {
                    throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) IOException(java.io.IOException)

Example 5 with MutableTestPlan

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

the class UnitTestAnalyzer method addCoverage.

private boolean addCoverage(InputFile resource, InputFile testFile, String testName, List<Integer> coveredLines) {
    boolean result = false;
    Testable testAbleFile = perspectives.as(MutableTestable.class, resource);
    if (testAbleFile != null) {
        MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, testFile);
        if (testPlan != null) {
            for (MutableTestCase testCase : testPlan.testCasesByName(testName)) {
                testCase.setCoverageBlock(testAbleFile, coveredLines);
                result = true;
            }
        }
    }
    return result;
}
Also used : MutableTestPlan(org.sonar.api.test.MutableTestPlan) MutableTestCase(org.sonar.api.test.MutableTestCase) Testable(org.sonar.api.test.Testable) MutableTestable(org.sonar.api.test.MutableTestable)

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