Search in sources :

Example 1 with MutableTestCase

use of org.sonar.api.test.MutableTestCase 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 MutableTestCase

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

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

the class TestExecutionAndCoveragePublisher method toProtobufCoverageDetails.

private CoverageDetail toProtobufCoverageDetails(final ScannerReport.CoverageDetail.Builder builder, final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder, final MutableTestPlan testPlan, String testName) {
    // Take first test with provided name
    MutableTestCase testCase = testPlan.testCasesByName(testName).iterator().next();
    builder.clear();
    builder.setTestName(testName);
    for (CoverageBlock block : testCase.coverageBlocks()) {
        coveredBuilder.clear();
        DefaultInputComponent c = (DefaultInputComponent) componentStore.getByKey(((DefaultTestable) block.testable()).inputFile().key());
        coveredBuilder.setFileRef(c.batchId());
        for (int line : block.lines()) {
            coveredBuilder.addCoveredLine(line);
        }
        builder.addCoveredFile(coveredBuilder.build());
    }
    return builder.build();
}
Also used : MutableTestCase(org.sonar.api.test.MutableTestCase) CoverageBlock(org.sonar.api.test.CoverageBlock) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 4 with MutableTestCase

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

the class GenericTestExecutionReportParserTest method mockMutableTestCase.

private MutableTestCase mockMutableTestCase() {
    MutableTestCase testCase = mock(MutableTestCase.class);
    when(testCase.setDurationInMs(anyLong())).thenReturn(testCase);
    when(testCase.setStatus(any(org.sonar.api.test.TestCase.Status.class))).thenReturn(testCase);
    when(testCase.setMessage(anyString())).thenReturn(testCase);
    when(testCase.setStackTrace(anyString())).thenReturn(testCase);
    when(testCase.setType(anyString())).thenReturn(testCase);
    return testCase;
}
Also used : MutableTestCase(org.sonar.api.test.MutableTestCase)

Example 5 with MutableTestCase

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

the class GenericTestExecutionReportParser method parseTestCase.

private void parseTestCase(SMInputCursor cursor, MutableTestPlan testPlan) throws XMLStreamException {
    checkElementName(cursor, "testCase");
    MutableTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
    TestCase.Status status = TestCase.Status.OK;
    testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
    SMInputCursor child = cursor.descendantElementCursor();
    if (child.getNext() != null) {
        String elementName = child.getLocalName();
        if (SKIPPED.equals(elementName)) {
            status = TestCase.Status.SKIPPED;
        } else if (FAILURE.equals(elementName)) {
            status = TestCase.Status.FAILURE;
        } else if (ERROR.equals(elementName)) {
            status = TestCase.Status.ERROR;
        }
        if (TestCase.Status.OK != status) {
            testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
            testCase.setStackTrace(child.collectDescendantText());
        }
    }
    testCase.setStatus(status);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) MutableTestCase(org.sonar.api.test.MutableTestCase) MutableTestCase(org.sonar.api.test.MutableTestCase) TestCase(org.sonar.api.test.TestCase)

Aggregations

MutableTestCase (org.sonar.api.test.MutableTestCase)6 File (java.io.File)3 InputFile (org.sonar.api.batch.fs.InputFile)3 MutableTestPlan (org.sonar.api.test.MutableTestPlan)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 Before (org.junit.Before)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 CoverageBlock (org.sonar.api.test.CoverageBlock)1 MutableTestable (org.sonar.api.test.MutableTestable)1 TestCase (org.sonar.api.test.TestCase)1 TestPlanBuilder (org.sonar.scanner.deprecated.test.TestPlanBuilder)1