-
Notifications
You must be signed in to change notification settings - Fork 531
/
TestSuiteXmlParser.java
303 lines (261 loc) · 11.3 KB
/
TestSuiteXmlParser.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.plugins.surefire.report;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Locale.ENGLISH;
import static org.apache.maven.shared.utils.StringUtils.isBlank;
/**
*
*/
public final class TestSuiteXmlParser extends DefaultHandler {
private final NumberFormat numberFormat = NumberFormat.getInstance(ENGLISH);
private final ConsoleLogger consoleLogger;
private ReportTestSuite defaultSuite;
private ReportTestSuite currentSuite;
private Map<String, Integer> classesToSuitesIndex;
private List<ReportTestSuite> suites;
private StringBuilder currentElement;
private ReportTestCase testCase;
private boolean valid;
public TestSuiteXmlParser(ConsoleLogger consoleLogger) {
this.consoleLogger = consoleLogger;
}
public List<ReportTestSuite> parse(String xmlPath) throws ParserConfigurationException, SAXException, IOException {
File f = new File(xmlPath);
try (InputStreamReader stream = new InputStreamReader(new FileInputStream(f), UTF_8)) {
return parse(stream);
}
}
public List<ReportTestSuite> parse(InputStreamReader stream)
throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
valid = true;
classesToSuitesIndex = new HashMap<>();
suites = new ArrayList<>();
saxParser.parse(new InputSource(stream), this);
if (currentSuite != defaultSuite) { // omit the defaultSuite if it's empty and there are alternatives
if (defaultSuite.getNumberOfTests() == 0) {
suites.remove(classesToSuitesIndex
.get(defaultSuite.getFullClassName())
.intValue());
}
}
return suites;
}
/**
* {@inheritDoc}
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (valid) {
try {
switch (qName) {
case "testsuite":
defaultSuite = new ReportTestSuite();
currentSuite = defaultSuite;
try {
Number time = numberFormat.parse(attributes.getValue("time"));
defaultSuite.setTimeElapsed(time.floatValue());
} catch (NullPointerException e) {
consoleLogger.error("WARNING: no time attribute found on testsuite element");
}
final String name = attributes.getValue("name");
final String group = attributes.getValue("group");
defaultSuite.setFullClassName(
isBlank(group)
? /*name is full class name*/ name
: /*group is package name*/ group + "." + name);
suites.add(defaultSuite);
classesToSuitesIndex.put(defaultSuite.getFullClassName(), suites.size() - 1);
break;
case "testcase":
// Although this element does not contain any text, this line must be retained because the
// nested elements do have text content.
currentElement = new StringBuilder();
testCase = new ReportTestCase().setName(attributes.getValue("name"));
String fullClassName = attributes.getValue("classname");
// if the testcase declares its own classname, it may need to belong to its own suite
if (fullClassName != null) {
Integer currentSuiteIndex = classesToSuitesIndex.get(fullClassName);
if (currentSuiteIndex == null) {
currentSuite = new ReportTestSuite().setFullClassName(fullClassName);
suites.add(currentSuite);
classesToSuitesIndex.put(fullClassName, suites.size() - 1);
} else {
currentSuite = suites.get(currentSuiteIndex);
}
}
String timeAsString = attributes.getValue("time");
Number time = isBlank(timeAsString) ? 0 : numberFormat.parse(timeAsString);
testCase.setFullClassName(currentSuite.getFullClassName())
.setClassName(currentSuite.getName())
.setFullName(currentSuite.getFullClassName() + "." + testCase.getName())
.setTime(time.floatValue());
if (currentSuite != defaultSuite) {
currentSuite.setTimeElapsed(testCase.getTime() + currentSuite.getTimeElapsed());
}
break;
case "failure":
currentElement = new StringBuilder();
testCase.setFailure(attributes.getValue("message"), attributes.getValue("type"));
currentSuite.incrementNumberOfFailures();
break;
case "error":
currentElement = new StringBuilder();
testCase.setError(attributes.getValue("message"), attributes.getValue("type"));
currentSuite.incrementNumberOfErrors();
break;
case "skipped":
String message = attributes.getValue("message");
testCase.setSkipped(message != null ? message : "skipped");
currentSuite.incrementNumberOfSkipped();
break;
case "flakyFailure":
case "flakyError":
currentSuite.incrementNumberOfFlakes();
break;
case "failsafe-summary":
valid = false;
break;
case "time":
currentElement = new StringBuilder();
break;
default:
break;
}
} catch (ParseException e) {
throw new SAXException(e.getMessage(), e);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case "testcase":
currentSuite.getTestCases().add(testCase);
break;
case "failure":
case "error":
testCase.setFailureDetail(currentElement.toString())
.setFailureErrorLine(parseErrorLine(currentElement, testCase.getFullClassName()));
break;
case "time":
try {
defaultSuite.setTimeElapsed(
numberFormat.parse(currentElement.toString()).floatValue());
} catch (ParseException e) {
throw new SAXException(e.getMessage(), e);
}
break;
default:
break;
}
// TODO extract real skipped reasons
}
/**
* {@inheritDoc}
*/
@Override
public void characters(char[] ch, int start, int length) {
assert start >= 0;
assert length >= 0;
if (valid && isNotBlank(start, length, ch)) {
currentElement.append(ch, start, length);
}
}
public boolean isValid() {
return valid;
}
static boolean isNotBlank(int from, int len, char... s) {
assert from >= 0;
assert len >= 0;
if (s != null) {
for (int i = 0; i < len; i++) {
char c = s[from++];
if (c != ' ' && c != '\t' && c != '\n' && c != '\r' && c != '\f') {
return true;
}
}
}
return false;
}
static boolean isNumeric(StringBuilder s, final int from, final int to) {
assert from >= 0;
assert from <= to;
for (int i = from; i != to; ) {
if (!Character.isDigit(s.charAt(i++))) {
return false;
}
}
return from != to;
}
static String parseErrorLine(StringBuilder currentElement, String fullClassName) {
final String[] linePatterns = {"at " + fullClassName + '.', "at " + fullClassName + '$'};
int[] indexes = lastIndexOf(currentElement, linePatterns);
int patternStartsAt = indexes[0];
if (patternStartsAt != -1) {
int searchFrom = patternStartsAt + (linePatterns[indexes[1]]).length();
searchFrom = 1 + currentElement.indexOf(":", searchFrom);
int searchTo = currentElement.indexOf(")", searchFrom);
return isNumeric(currentElement, searchFrom, searchTo)
? currentElement.substring(searchFrom, searchTo)
: "";
}
return "";
}
static int[] lastIndexOf(StringBuilder source, String... linePatterns) {
int end = source.indexOf("Caused by:");
if (end == -1) {
end = source.length();
}
int startsAt = -1;
int pattern = -1;
for (int i = 0; i < linePatterns.length; i++) {
String linePattern = linePatterns[i];
int currentStartsAt = source.lastIndexOf(linePattern, end);
if (currentStartsAt > startsAt) {
startsAt = currentStartsAt;
pattern = i;
}
}
return new int[] {startsAt, pattern};
}
}