Search in sources :

Example 31 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonar-java by SonarSource.

the class SurefireStaxHandler method stream.

public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
    SMInputCursor testSuite = rootCursor.constructDescendantCursor(new ElementFilter("testsuite"));
    SMEvent testSuiteEvent;
    for (testSuiteEvent = testSuite.getNext(); testSuiteEvent != null; testSuiteEvent = testSuite.getNext()) {
        if (testSuiteEvent.compareTo(SMEvent.START_ELEMENT) == 0) {
            String testSuiteClassName = testSuite.getAttrValue("name");
            parseTestCase(testSuiteClassName, testSuite.childCursor(new ElementFilter("testcase")));
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) ElementFilter(org.codehaus.staxmate.in.ElementFilter) SMEvent(org.codehaus.staxmate.in.SMEvent)

Example 32 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonar-java by SonarSource.

the class SurefireStaxHandler method parseTestResult.

private static UnitTestResult parseTestResult(SMInputCursor testCaseCursor, String testSuiteClassName) throws XMLStreamException {
    UnitTestResult detail = new UnitTestResult();
    String name = getTestCaseName(testCaseCursor);
    detail.setName(name);
    detail.setTestSuiteClassName(testSuiteClassName);
    String status = UnitTestResult.STATUS_OK;
    String time = testCaseCursor.getAttrValue("time");
    Long duration = null;
    SMInputCursor childNode = testCaseCursor.descendantElementCursor();
    if (childNode.getNext() != null) {
        String elementName = childNode.getLocalName();
        if ("skipped".equals(elementName)) {
            status = UnitTestResult.STATUS_SKIPPED;
            // bug with surefire reporting wrong time for skipped tests
            duration = 0L;
        } else if ("failure".equals(elementName)) {
            status = UnitTestResult.STATUS_FAILURE;
            setStackAndMessage(detail, childNode);
        } else if ("error".equals(elementName)) {
            status = UnitTestResult.STATUS_ERROR;
            setStackAndMessage(detail, childNode);
        }
    }
    while (childNode.getNext() != null) {
    // make sure we loop till the end of the elements cursor
    }
    if (duration == null) {
        duration = getTimeAttributeInMS(time);
    }
    detail.setDurationMilliseconds(duration);
    detail.setStatus(status);
    return detail;
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 33 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonar-python by SonarSource.

the class TestSuiteParser method parseTestCaseTag.

private static TestCase parseTestCaseTag(SMInputCursor testCaseCursor) throws XMLStreamException {
    String name = parseTestCaseName(testCaseCursor);
    Double time = parseTime(testCaseCursor);
    String status = TestCase.STATUS_OK;
    String stack = "";
    String msg = "";
    String file = testCaseCursor.getAttrValue("file");
    String testClassName = testCaseCursor.getAttrValue("classname");
    SMInputCursor childCursor = testCaseCursor.childElementCursor();
    if (childCursor.getNext() != null) {
        String elementName = childCursor.getLocalName();
        if (TestCase.STATUS_SKIPPED.equals(elementName)) {
            status = TestCase.STATUS_SKIPPED;
        } else if (TestCase.STATUS_FAILURE.equals(elementName)) {
            status = TestCase.STATUS_FAILURE;
            msg = getExpectedAttribute(childCursor, "message");
            stack = childCursor.collectDescendantText();
        } else if (TestCase.STATUS_ERROR.equals(elementName)) {
            status = TestCase.STATUS_ERROR;
            msg = getExpectedAttribute(childCursor, "message");
            stack = childCursor.collectDescendantText();
        }
    }
    return new TestCase(name, time.intValue(), status, stack, msg, file, testClassName);
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor)

Example 34 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonar-python by SonarSource.

the class TestSuiteParser method stream.

@Override
public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException {
    SMInputCursor testSuiteCursor = rootCursor.constructDescendantCursor(new ElementFilter("testsuite"));
    while (testSuiteCursor.getNext() != null) {
        String testSuiteClassName = getExpectedAttribute(testSuiteCursor, "name");
        TestSuite testSuite = new TestSuite(testSuiteClassName);
        testSuites.add(testSuite);
        SMInputCursor testCaseCursor = testSuiteCursor.childElementCursor("testcase");
        while (testCaseCursor.getNext() != null) {
            testSuite.addTestCase(parseTestCaseTag(testCaseCursor));
        }
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) ElementFilter(org.codehaus.staxmate.in.ElementFilter)

Example 35 with SMInputCursor

use of org.codehaus.staxmate.in.SMInputCursor in project sonar-python by SonarSource.

the class CoberturaParser method parseReport.

public void parseReport(File xmlFile, SensorContext context, final Map<InputFile, NewCoverage> coverageData) throws XMLStreamException {
    LOG.info("Parsing report '{}'", xmlFile);
    unresolvedFilenameCount = 0;
    StaxParser parser = new StaxParser(rootCursor -> {
        File defaultBaseDirectory = context.fileSystem().baseDir();
        List<File> baseDirectories = Collections.singletonList(defaultBaseDirectory);
        try {
            rootCursor.advance();
        } catch (com.ctc.wstx.exc.WstxEOFException eofExc) {
            LOG.debug("Unexpected end of file is encountered", eofExc);
            throw new EmptyReportException();
        }
        SMInputCursor cursor = rootCursor.childElementCursor();
        while (cursor.getNext() != null) {
            if ("sources".equals(cursor.getLocalName())) {
                baseDirectories = extractBaseDirectories(cursor, defaultBaseDirectory);
            } else if ("packages".equals(cursor.getLocalName())) {
                collectFileMeasures(cursor.descendantElementCursor("class"), context, coverageData, baseDirectories);
            }
        }
    });
    parser.parse(xmlFile);
    if (unresolvedFilenameCount > 1) {
        LOG.error("Cannot resolve {} file paths, ignoring coverage measures for those files", unresolvedFilenameCount);
    }
}
Also used : SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) EmptyReportException(org.sonar.plugins.python.EmptyReportException) StaxParser(org.sonar.plugins.python.parser.StaxParser) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File)

Aggregations

SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)42 XMLStreamException (javax.xml.stream.XMLStreamException)9 SMHierarchicCursor (org.codehaus.staxmate.in.SMHierarchicCursor)8 ArrayList (java.util.ArrayList)7 SMInputFactory (org.codehaus.staxmate.SMInputFactory)5 InputFile (org.sonar.api.batch.fs.InputFile)5 File (java.io.File)3 ElementFilter (org.codehaus.staxmate.in.ElementFilter)3 SonarException (org.sonar.api.utils.SonarException)3 StaxParser (org.sonar.cxx.sensors.utils.StaxParser)3 CxxReportIssue (org.sonar.cxx.utils.CxxReportIssue)3 HashMap (java.util.HashMap)2 RuleKey (org.sonar.api.rule.RuleKey)2 CoverageMeasures (org.sonar.cxx.sensors.coverage.CoverageMeasures)2 EmptyReportException (org.sonar.cxx.sensors.utils.EmptyReportException)2 InvalidReportException (org.sonar.cxx.sensors.utils.InvalidReportException)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1