3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.scanner.genericcoverage;
22 import com.google.common.base.Preconditions;
24 import java.io.FileInputStream;
25 import java.io.InputStream;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.List;
30 import javax.xml.stream.XMLStreamException;
31 import org.codehaus.staxmate.in.SMHierarchicCursor;
32 import org.codehaus.staxmate.in.SMInputCursor;
33 import org.sonar.api.batch.fs.InputFile;
34 import org.sonar.api.batch.sensor.SensorContext;
35 import org.sonar.api.test.MutableTestCase;
36 import org.sonar.api.test.MutableTestPlan;
37 import org.sonar.api.test.TestCase;
38 import org.sonar.api.utils.MessageException;
39 import org.sonar.api.utils.log.Logger;
40 import org.sonar.api.utils.log.Loggers;
41 import org.sonar.scanner.deprecated.test.TestPlanBuilder;
43 import static org.sonar.scanner.genericcoverage.GenericCoverageReportParser.checkElementName;
44 import static org.sonar.scanner.genericcoverage.GenericCoverageReportParser.longValue;
45 import static org.sonar.scanner.genericcoverage.GenericCoverageReportParser.mandatoryAttribute;
47 public class GenericTestExecutionReportParser {
49 private static final String ROOT_ELEMENT = "testExecutions";
51 private static final String OLD_ROOT_ELEMENT = "unitTest";
53 private static final Logger LOG = Loggers.get(GenericTestExecutionReportParser.class);
55 private static final String NAME_ATTR = "name";
56 private static final String DURATION_ATTR = "duration";
57 private static final String MESSAGE_ATTR = "message";
58 public static final String OK = "ok";
59 public static final String ERROR = "error";
60 public static final String FAILURE = "failure";
61 public static final String SKIPPED = "skipped";
63 private static final int MAX_STORED_UNKNOWN_FILE_PATHS = 5;
65 private final TestPlanBuilder testPlanBuilder;
67 private int numberOfUnknownFiles;
68 private final List<String> firstUnknownFiles = new ArrayList<>();
69 private final Set<String> matchedFileKeys = new HashSet<>();
71 public GenericTestExecutionReportParser(TestPlanBuilder testPlanBuilder) {
72 this.testPlanBuilder = testPlanBuilder;
75 public void parse(File reportFile, SensorContext context) {
76 try (InputStream inputStream = new FileInputStream(reportFile)) {
77 parse(inputStream, context);
78 } catch (Exception e) {
79 throw MessageException.of(
80 "Error during parsing of generic test execution report '" + reportFile + "'. Look at the SonarQube documentation to know the expected XML format.", e);
84 private void parse(InputStream inputStream, SensorContext context) throws XMLStreamException {
85 new StaxParser(rootCursor -> {
87 parseRootNode(rootCursor, context);
88 }).parse(inputStream);
91 private void parseRootNode(SMHierarchicCursor rootCursor, SensorContext context) throws XMLStreamException {
92 String elementName = rootCursor.getLocalName();
93 if (!OLD_ROOT_ELEMENT.equals(elementName) && !ROOT_ELEMENT.equals(elementName)) {
94 throw new IllegalStateException(
95 "Unknown XML node, expected \"" + ROOT_ELEMENT + "\" but got \"" + elementName + "\" at line " + rootCursor.getCursorLocation().getLineNumber());
97 if (OLD_ROOT_ELEMENT.equals(elementName)) {
98 LOG.warn("Using '" + OLD_ROOT_ELEMENT + "' as root element of the report is deprecated. Please change to '" + ROOT_ELEMENT + "'.");
100 String version = rootCursor.getAttrValue("version");
101 if (!"1".equals(version)) {
102 throw new IllegalStateException("Unknown report version: " + version + ". This parser only handles version 1.");
104 parseFiles(rootCursor.childElementCursor(), context);
107 private void parseFiles(SMInputCursor fileCursor, SensorContext context) throws XMLStreamException {
108 while (fileCursor.getNext() != null) {
109 checkElementName(fileCursor, "file");
110 String filePath = mandatoryAttribute(fileCursor, "path");
111 InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
112 if (inputFile == null) {
113 numberOfUnknownFiles++;
114 if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
115 firstUnknownFiles.add(filePath);
119 Preconditions.checkState(
120 inputFile.language() != null,
121 "Line %s of report refers to a file with an unknown language: %s",
122 fileCursor.getCursorLocation().getLineNumber(),
124 Preconditions.checkState(
125 inputFile.type() != InputFile.Type.MAIN,
126 "Line %s of report refers to a file which is not configured as a test file: %s",
127 fileCursor.getCursorLocation().getLineNumber(),
129 matchedFileKeys.add(inputFile.absolutePath());
131 MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, inputFile);
132 SMInputCursor testCaseCursor = fileCursor.childElementCursor();
133 while (testCaseCursor.getNext() != null) {
134 parseTestCase(testCaseCursor, testPlan);
139 private void parseTestCase(SMInputCursor cursor, MutableTestPlan testPlan) throws XMLStreamException {
140 checkElementName(cursor, "testCase");
141 MutableTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
142 TestCase.Status status = TestCase.Status.OK;
143 testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
145 SMInputCursor child = cursor.descendantElementCursor();
146 if (child.getNext() != null) {
147 String elementName = child.getLocalName();
148 if (SKIPPED.equals(elementName)) {
149 status = TestCase.Status.SKIPPED;
150 } else if (FAILURE.equals(elementName)) {
151 status = TestCase.Status.FAILURE;
152 } else if (ERROR.equals(elementName)) {
153 status = TestCase.Status.ERROR;
155 if (TestCase.Status.OK != status) {
156 testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
157 testCase.setStackTrace(child.collectDescendantText());
160 testCase.setStatus(status);
164 public int numberOfMatchedFiles() {
165 return matchedFileKeys.size();
168 public int numberOfUnknownFiles() {
169 return numberOfUnknownFiles;
172 public List<String> firstUnknownFiles() {
173 return firstUnknownFiles;