-
Notifications
You must be signed in to change notification settings - Fork 688
/
SurefireJavaParser.java
176 lines (159 loc) · 6.9 KB
/
SurefireJavaParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.plugins.surefire;
import java.io.File;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.CheckForNull;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.java.AnalysisException;
import org.sonar.plugins.java.api.JavaResourceLocator;
import org.sonar.plugins.surefire.data.UnitTestClassReport;
import org.sonar.plugins.surefire.data.UnitTestIndex;
/**
* @since 2.4
*/
@ScannerSide
public class SurefireJavaParser {
private static final Logger LOGGER = LoggerFactory.getLogger(SurefireJavaParser.class);
private final JavaResourceLocator javaResourceLocator;
public SurefireJavaParser(JavaResourceLocator javaResourceLocator) {
this.javaResourceLocator = javaResourceLocator;
}
public void collect(SensorContext context, List<File> reportsDirs, boolean reportDirSetByUser) {
List<File> xmlFiles = getReports(reportsDirs, reportDirSetByUser);
if (!xmlFiles.isEmpty()) {
parseFiles(context, xmlFiles);
}
}
private static List<File> getReports(List<File> dirs, boolean reportDirSetByUser) {
return dirs.stream()
.map(dir -> getReports(dir, reportDirSetByUser))
.flatMap(Arrays::stream)
.toList();
}
private static File[] getReports(File dir, boolean reportDirSetByUser) {
if (!dir.isDirectory()) {
if(reportDirSetByUser) {
LOGGER.error("Reports path not found or is not a directory: {}", dir.getAbsolutePath());
}
return new File[0];
}
File[] unitTestResultFiles = findXMLFilesStartingWith(dir, "TEST-");
if (unitTestResultFiles.length == 0) {
// maybe there's only a test suite result file
unitTestResultFiles = findXMLFilesStartingWith(dir, "TESTS-");
}
if(unitTestResultFiles.length == 0) {
LOGGER.warn("Reports path contains no files matching TEST-.*.xml : {}", dir.getAbsolutePath());
}
return unitTestResultFiles;
}
private static File[] findXMLFilesStartingWith(File dir, final String fileNameStart) {
return dir.listFiles((parentDir, name) -> name.startsWith(fileNameStart) && name.endsWith(".xml"));
}
private void parseFiles(SensorContext context, List<File> reports) {
UnitTestIndex index = new UnitTestIndex();
parseFiles(reports, index);
sanitize(index);
save(index, context);
}
private static void parseFiles(List<File> reports, UnitTestIndex index) {
StaxParser parser = new StaxParser(index);
for (File report : reports) {
try {
parser.parse(report);
} catch (XMLStreamException e) {
throw new AnalysisException("Fail to parse the Surefire report: " + report, e);
}
}
}
private static void sanitize(UnitTestIndex index) {
for (String classname : index.getClassnames()) {
if (StringUtils.contains(classname, "$")) {
// Surefire reports classes whereas sonar supports files
String parentClassName = StringUtils.substringBefore(classname, "$");
index.merge(classname, parentClassName);
}
}
}
private void save(UnitTestIndex index, SensorContext context) {
long negativeTimeTestNumber = 0;
Map<InputFile, UnitTestClassReport> indexByInputFile = mapToInputFile(index.getIndexByClassname());
for (Map.Entry<InputFile, UnitTestClassReport> entry : indexByInputFile.entrySet()) {
UnitTestClassReport report = entry.getValue();
if (report.getTests() > 0) {
negativeTimeTestNumber += report.getNegativeTimeTestNumber();
save(report, entry.getKey(), context);
}
}
if (negativeTimeTestNumber > 0) {
LOGGER.warn("There is {} test(s) reported with negative time by surefire, total duration may not be accurate.", negativeTimeTestNumber);
}
}
private Map<InputFile, UnitTestClassReport> mapToInputFile(Map<String, UnitTestClassReport> indexByClassname) {
Map<InputFile, UnitTestClassReport> result = new HashMap<>();
indexByClassname.forEach((className, index) -> {
InputFile resource = getUnitTestResource(className, index);
if (resource != null) {
UnitTestClassReport report = result.computeIfAbsent(resource, r -> new UnitTestClassReport());
// in case of repeated/parameterized tests (JUnit 5.x) we may end up with tests having the same name
index.getResults().forEach(report::add);
} else {
LOGGER.debug("Resource not found: {}", className);
}
});
return result;
}
private static void save(UnitTestClassReport report, InputFile inputFile, SensorContext context) {
int testsCount = report.getTests() - report.getSkipped();
saveMeasure(context, inputFile, CoreMetrics.SKIPPED_TESTS, report.getSkipped());
saveMeasure(context, inputFile, CoreMetrics.TESTS, testsCount);
saveMeasure(context, inputFile, CoreMetrics.TEST_ERRORS, report.getErrors());
saveMeasure(context, inputFile, CoreMetrics.TEST_FAILURES, report.getFailures());
saveMeasure(context, inputFile, CoreMetrics.TEST_EXECUTION_TIME, report.getDurationMilliseconds());
}
@CheckForNull
private InputFile getUnitTestResource(String className, UnitTestClassReport unitTestClassReport) {
InputFile resource = javaResourceLocator.findResourceByClassName(className);
if (resource == null) {
// fall back on testSuite class name (repeated and parameterized tests from JUnit 5.0 are using test name as classname)
// Was fixed in JUnit 5.0.3 (see: https://github.com/junit-team/junit5/issues/1182)
return unitTestClassReport.getResults().stream()
.map(r -> javaResourceLocator.findResourceByClassName(r.getTestSuiteClassName()))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}
return resource;
}
private static <T extends Serializable> void saveMeasure(SensorContext context, InputFile inputFile, Metric<T> metric, T value) {
context.<T>newMeasure().forMetric(metric).on(inputFile).withValue(value).save();
}
}