3 * Copyright (C) 2009-2022 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;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.util.ArrayList;
26 import java.util.HashSet;
27 import java.util.List;
29 import javax.xml.stream.XMLStreamException;
30 import org.codehaus.staxmate.in.SMHierarchicCursor;
31 import org.codehaus.staxmate.in.SMInputCursor;
32 import org.sonar.api.batch.fs.InputFile;
33 import org.sonar.api.batch.sensor.SensorContext;
34 import org.sonar.api.utils.MessageException;
35 import org.sonar.api.utils.log.Logger;
36 import org.sonar.api.utils.log.Loggers;
37 import org.sonar.scanner.deprecated.test.DefaultTestCase;
38 import org.sonar.scanner.deprecated.test.DefaultTestCase.Status;
39 import org.sonar.scanner.deprecated.test.DefaultTestPlan;
40 import org.sonar.scanner.deprecated.test.TestPlanBuilder;
42 import static org.sonar.api.utils.Preconditions.checkState;
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 || inputFile.language() == null) {
113 numberOfUnknownFiles++;
114 if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) {
115 firstUnknownFiles.add(filePath);
117 if (inputFile != null) {
118 LOG.debug("Skipping file '{}' in the generic test execution report because it doesn't have a known language", filePath);
123 inputFile.type() != InputFile.Type.MAIN,
124 "Line %s of report refers to a file which is not configured as a test file: %s",
125 fileCursor.getCursorLocation().getLineNumber(),
127 matchedFileKeys.add(inputFile.absolutePath());
129 DefaultTestPlan testPlan = testPlanBuilder.getTestPlan(inputFile);
130 SMInputCursor testCaseCursor = fileCursor.childElementCursor();
131 while (testCaseCursor.getNext() != null) {
132 parseTestCase(testCaseCursor, testPlan);
137 private static void parseTestCase(SMInputCursor cursor, DefaultTestPlan testPlan) throws XMLStreamException {
138 checkElementName(cursor, "testCase");
139 DefaultTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
140 Status status = Status.OK;
141 testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
143 SMInputCursor child = cursor.descendantElementCursor();
144 if (child.getNext() != null) {
145 String elementName = child.getLocalName();
146 if (SKIPPED.equals(elementName)) {
147 status = Status.SKIPPED;
148 } else if (FAILURE.equals(elementName)) {
149 status = Status.FAILURE;
150 } else if (ERROR.equals(elementName)) {
151 status = Status.ERROR;
153 if (Status.OK != status) {
154 testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
157 testCase.setStatus(status);
161 public int numberOfMatchedFiles() {
162 return matchedFileKeys.size();
165 public int numberOfUnknownFiles() {
166 return numberOfUnknownFiles;
169 public List<String> firstUnknownFiles() {
170 return firstUnknownFiles;