1 | |
package org.apache.maven.plugin.surefire.report; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.commons.io.output.DeferredFileOutputStream; |
23 | |
import org.apache.maven.shared.utils.io.IOUtil; |
24 | |
import org.apache.maven.shared.utils.xml.XMLWriter; |
25 | |
import org.apache.maven.surefire.report.ReportEntry; |
26 | |
import org.apache.maven.surefire.report.ReporterException; |
27 | |
import org.apache.maven.surefire.report.SafeThrowable; |
28 | |
|
29 | |
import java.io.File; |
30 | |
import java.io.FileOutputStream; |
31 | |
import java.io.FilterOutputStream; |
32 | |
import java.io.IOException; |
33 | |
import java.io.OutputStream; |
34 | |
import java.io.OutputStreamWriter; |
35 | |
import java.util.Enumeration; |
36 | |
import java.util.Properties; |
37 | |
import java.util.StringTokenizer; |
38 | |
|
39 | |
import static org.apache.maven.plugin.surefire.report.FileReporterUtils.stripIllegalFilenameChars; |
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 | 37 | public class StatelessXmlReporter |
74 | |
{ |
75 | |
|
76 | 1 | private static final byte[] ampBytes = "&#".getBytes(); |
77 | |
|
78 | |
private final File reportsDirectory; |
79 | |
|
80 | |
private final String reportNameSuffix; |
81 | |
|
82 | |
private final boolean trimStackTrace; |
83 | |
|
84 | 3 | private final String encoding = "UTF-8"; |
85 | |
|
86 | |
public StatelessXmlReporter( File reportsDirectory, String reportNameSuffix, boolean trimStackTrace ) |
87 | 3 | { |
88 | 3 | this.reportsDirectory = reportsDirectory; |
89 | 3 | this.reportNameSuffix = reportNameSuffix; |
90 | 3 | this.trimStackTrace = trimStackTrace; |
91 | 3 | } |
92 | |
|
93 | |
public void testSetCompleted( WrappedReportEntry testSetReportEntry, TestSetStats testSetStats ) |
94 | |
throws ReporterException |
95 | |
{ |
96 | |
|
97 | 2 | FileOutputStream outputStream = getOutputStream( testSetReportEntry ); |
98 | 2 | OutputStreamWriter fw = getWriter( outputStream ); |
99 | |
try |
100 | |
{ |
101 | |
|
102 | 2 | org.apache.maven.shared.utils.xml.XMLWriter ppw = |
103 | |
new org.apache.maven.shared.utils.xml.PrettyPrintXMLWriter( fw ); |
104 | 2 | ppw.setEncoding( encoding ); |
105 | |
|
106 | 2 | createTestSuiteElement( ppw, testSetReportEntry, testSetStats, reportNameSuffix ); |
107 | |
|
108 | 2 | showProperties( ppw ); |
109 | |
|
110 | 2 | for ( WrappedReportEntry entry : testSetStats.getReportEntries() ) |
111 | |
{ |
112 | 3 | if ( ReportEntryType.success.equals( entry.getReportEntryType() ) ) |
113 | |
{ |
114 | 2 | startTestElement( ppw, entry, reportNameSuffix ); |
115 | 2 | ppw.endElement(); |
116 | |
} |
117 | |
else |
118 | |
{ |
119 | 1 | getTestProblems( fw, ppw, entry, trimStackTrace, reportNameSuffix, outputStream ); |
120 | |
} |
121 | |
|
122 | |
} |
123 | 2 | ppw.endElement(); |
124 | |
|
125 | |
} |
126 | |
finally |
127 | |
{ |
128 | 2 | IOUtil.close( fw ); |
129 | 2 | } |
130 | 2 | } |
131 | |
|
132 | |
private OutputStreamWriter getWriter( FileOutputStream fos ) |
133 | |
{ |
134 | |
try |
135 | |
{ |
136 | |
|
137 | 2 | return new OutputStreamWriter( fos, encoding ); |
138 | |
} |
139 | 0 | catch ( IOException e ) |
140 | |
{ |
141 | 0 | throw new ReporterException( "When writing report", e ); |
142 | |
} |
143 | |
} |
144 | |
|
145 | |
private FileOutputStream getOutputStream( WrappedReportEntry testSetReportEntry ) |
146 | |
{ |
147 | 2 | File reportFile = getReportFile( testSetReportEntry, reportsDirectory, reportNameSuffix ); |
148 | |
|
149 | 2 | File reportDir = reportFile.getParentFile(); |
150 | |
|
151 | |
|
152 | 2 | reportDir.mkdirs(); |
153 | |
|
154 | |
try |
155 | |
{ |
156 | |
|
157 | 2 | return new FileOutputStream( reportFile ); |
158 | |
} |
159 | 0 | catch ( Exception e ) |
160 | |
{ |
161 | 0 | throw new ReporterException( "When writing report", e ); |
162 | |
} |
163 | |
} |
164 | |
|
165 | |
private File getReportFile( ReportEntry report, File reportsDirectory, String reportNameSuffix ) |
166 | |
{ |
167 | |
File reportFile; |
168 | |
|
169 | 2 | if ( reportNameSuffix != null && reportNameSuffix.length() > 0 ) |
170 | |
{ |
171 | 0 | reportFile = new File( reportsDirectory, stripIllegalFilenameChars( |
172 | |
"TEST-" + report.getName() + "-" + reportNameSuffix + ".xml" ) ); |
173 | |
} |
174 | |
else |
175 | |
{ |
176 | 2 | reportFile = new File( reportsDirectory, stripIllegalFilenameChars( "TEST-" + report.getName() + ".xml" ) ); |
177 | |
} |
178 | |
|
179 | 2 | return reportFile; |
180 | |
} |
181 | |
|
182 | |
private static void startTestElement( XMLWriter ppw, WrappedReportEntry report, String reportNameSuffix ) |
183 | |
{ |
184 | 3 | ppw.startElement( "testcase" ); |
185 | 3 | ppw.addAttribute( "name", report.getReportName() ); |
186 | 3 | if ( report.getGroup() != null ) |
187 | |
{ |
188 | 0 | ppw.addAttribute( "group", report.getGroup() ); |
189 | |
} |
190 | 3 | if ( report.getSourceName() != null ) |
191 | |
{ |
192 | 3 | if ( reportNameSuffix != null && reportNameSuffix.length() > 0 ) |
193 | |
{ |
194 | 0 | ppw.addAttribute( "classname", report.getSourceName() + "(" + reportNameSuffix + ")" ); |
195 | |
} |
196 | |
else |
197 | |
{ |
198 | 3 | ppw.addAttribute( "classname", report.getSourceName() ); |
199 | |
} |
200 | |
} |
201 | 3 | ppw.addAttribute( "time", report.elapsedTimeAsString() ); |
202 | 3 | } |
203 | |
|
204 | |
private static void createTestSuiteElement( XMLWriter ppw, WrappedReportEntry report, TestSetStats testSetStats, |
205 | |
String reportNameSuffix1 ) |
206 | |
{ |
207 | 2 | ppw.startElement( "testsuite" ); |
208 | |
|
209 | 2 | ppw.addAttribute( "name", report.getReportName( reportNameSuffix1 ) ); |
210 | |
|
211 | 2 | if ( report.getGroup() != null ) |
212 | |
{ |
213 | 0 | ppw.addAttribute( "group", report.getGroup() ); |
214 | |
} |
215 | |
|
216 | 2 | ppw.addAttribute( "time", testSetStats.getElapsedForTestSet() ); |
217 | |
|
218 | 2 | ppw.addAttribute( "tests", String.valueOf( testSetStats.getCompletedCount() ) ); |
219 | |
|
220 | 2 | ppw.addAttribute( "errors", String.valueOf( testSetStats.getErrors() ) ); |
221 | |
|
222 | 2 | ppw.addAttribute( "skipped", String.valueOf( testSetStats.getSkipped() ) ); |
223 | |
|
224 | 2 | ppw.addAttribute( "failures", String.valueOf( testSetStats.getFailures() ) ); |
225 | |
|
226 | 2 | } |
227 | |
|
228 | |
|
229 | |
private void getTestProblems( OutputStreamWriter outputStreamWriter, XMLWriter ppw, WrappedReportEntry report, |
230 | |
boolean trimStackTrace, String reportNameSuffix, FileOutputStream fw ) |
231 | |
{ |
232 | |
|
233 | 1 | startTestElement( ppw, report, reportNameSuffix ); |
234 | |
|
235 | 1 | ppw.startElement( report.getReportEntryType().name() ); |
236 | |
|
237 | 1 | String stackTrace = report.getStackTrace( trimStackTrace ); |
238 | |
|
239 | 1 | if ( report.getMessage() != null && report.getMessage().length() > 0 ) |
240 | |
{ |
241 | 1 | ppw.addAttribute( "message", extraEscape( report.getMessage(), true ) ); |
242 | |
} |
243 | |
|
244 | 1 | if ( report.getStackTraceWriter() != null ) |
245 | |
{ |
246 | |
|
247 | 1 | SafeThrowable t = report.getStackTraceWriter().getThrowable(); |
248 | 1 | if ( t != null ) |
249 | |
{ |
250 | 1 | if ( t.getMessage() != null ) |
251 | |
{ |
252 | 1 | ppw.addAttribute( "type", ( stackTrace.contains( ":" ) |
253 | |
? stackTrace.substring( 0, stackTrace.indexOf( ":" ) ) |
254 | |
: stackTrace ) ); |
255 | |
} |
256 | |
else |
257 | |
{ |
258 | 0 | ppw.addAttribute( "type", new StringTokenizer( stackTrace ).nextToken() ); |
259 | |
} |
260 | |
} |
261 | |
} |
262 | |
|
263 | 1 | if ( stackTrace != null ) |
264 | |
{ |
265 | 1 | ppw.writeText( extraEscape( stackTrace, false ) ); |
266 | |
} |
267 | |
|
268 | 1 | ppw.endElement(); |
269 | |
|
270 | 1 | EncodingOutputStream eos = new EncodingOutputStream( fw ); |
271 | |
|
272 | 1 | addOutputStreamElement( outputStreamWriter, fw, eos, ppw, report.getStdout(), "system-out" ); |
273 | |
|
274 | 1 | addOutputStreamElement( outputStreamWriter, fw, eos, ppw, report.getStdErr(), "system-err" ); |
275 | |
|
276 | 1 | ppw.endElement(); |
277 | 1 | } |
278 | |
|
279 | |
private void addOutputStreamElement( OutputStreamWriter outputStreamWriter, OutputStream fw, |
280 | |
EncodingOutputStream eos, XMLWriter xmlWriter, DeferredFileOutputStream stdOut, |
281 | |
String name ) |
282 | |
{ |
283 | 2 | if ( stdOut != null && stdOut.getByteCount() > 0 ) |
284 | |
{ |
285 | |
|
286 | 2 | xmlWriter.startElement( name ); |
287 | |
|
288 | |
try |
289 | |
{ |
290 | 2 | xmlWriter.writeText( "" ); |
291 | 2 | outputStreamWriter.flush(); |
292 | 2 | stdOut.close(); |
293 | 2 | stdOut.writeTo( eos ); |
294 | 2 | eos.flush(); |
295 | |
} |
296 | 0 | catch ( IOException e ) |
297 | |
{ |
298 | 0 | throw new ReporterException( "When writing xml report stdout/stderr", e ); |
299 | 2 | } |
300 | 2 | xmlWriter.endElement(); |
301 | |
} |
302 | 2 | } |
303 | |
|
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
private void showProperties( XMLWriter xmlWriter ) |
311 | |
{ |
312 | 2 | xmlWriter.startElement( "properties" ); |
313 | |
|
314 | 2 | Properties systemProperties = System.getProperties(); |
315 | |
|
316 | 2 | if ( systemProperties != null ) |
317 | |
{ |
318 | 2 | Enumeration<?> propertyKeys = systemProperties.propertyNames(); |
319 | |
|
320 | 114 | while ( propertyKeys.hasMoreElements() ) |
321 | |
{ |
322 | 112 | String key = (String) propertyKeys.nextElement(); |
323 | |
|
324 | 112 | String value = systemProperties.getProperty( key ); |
325 | |
|
326 | 112 | if ( value == null ) |
327 | |
{ |
328 | 0 | value = "null"; |
329 | |
} |
330 | |
|
331 | 112 | xmlWriter.startElement( "property" ); |
332 | |
|
333 | 112 | xmlWriter.addAttribute( "name", key ); |
334 | |
|
335 | 112 | xmlWriter.addAttribute( "value", extraEscape( value, true ) ); |
336 | |
|
337 | 112 | xmlWriter.endElement(); |
338 | |
|
339 | 112 | } |
340 | |
} |
341 | 2 | xmlWriter.endElement(); |
342 | 2 | } |
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
private static String extraEscape( String message, boolean attribute ) |
352 | |
{ |
353 | |
|
354 | 114 | if ( !containsEscapesIllegalnXml10( message ) ) |
355 | |
{ |
356 | 114 | return message; |
357 | |
} |
358 | 0 | return escapeXml( message, attribute ); |
359 | |
} |
360 | |
|
361 | |
private static class EncodingOutputStream |
362 | |
extends FilterOutputStream |
363 | |
{ |
364 | |
|
365 | |
public EncodingOutputStream( OutputStream out ) |
366 | |
{ |
367 | 1 | super( out ); |
368 | 1 | } |
369 | |
|
370 | |
@Override |
371 | |
public void write( int b ) |
372 | |
throws IOException |
373 | |
{ |
374 | 37 | if ( isIllegalEscape( b ) ) |
375 | |
{ |
376 | |
|
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | 0 | out.write( ampBytes ); |
382 | 0 | out.write( b ); |
383 | 0 | out.write( ';' ); |
384 | |
} |
385 | 37 | else if ( '<' == b ) |
386 | |
{ |
387 | 1 | out.write( '&' ); |
388 | 1 | out.write( 'l' ); |
389 | 1 | out.write( 't' ); |
390 | 1 | out.write( ';' ); |
391 | |
} |
392 | 36 | else if ( '>' == b ) |
393 | |
{ |
394 | 1 | out.write( '&' ); |
395 | 1 | out.write( 'g' ); |
396 | 1 | out.write( 't' ); |
397 | 1 | out.write( ';' ); |
398 | |
} |
399 | 35 | else if ( '&' == b ) |
400 | |
{ |
401 | 3 | out.write( '&' ); |
402 | 3 | out.write( 'a' ); |
403 | 3 | out.write( 'm' ); |
404 | 3 | out.write( 'p' ); |
405 | 3 | out.write( ';' ); |
406 | |
} |
407 | |
else |
408 | |
{ |
409 | 32 | out.write( b ); |
410 | |
} |
411 | 37 | } |
412 | |
} |
413 | |
|
414 | |
private static boolean containsEscapesIllegalnXml10( String message ) |
415 | |
{ |
416 | 114 | int size = message.length(); |
417 | 9444 | for ( int i = 0; i < size; i++ ) |
418 | |
{ |
419 | 9330 | if ( isIllegalEscape( message.charAt( i ) ) ) |
420 | |
{ |
421 | 0 | return true; |
422 | |
} |
423 | |
|
424 | |
} |
425 | 114 | return false; |
426 | |
} |
427 | |
|
428 | |
private static boolean isIllegalEscape( char c ) |
429 | |
{ |
430 | 9330 | return c >= 0 && c < 32 && c != '\n' && c != '\r' && c != '\t'; |
431 | |
} |
432 | |
|
433 | |
private static boolean isIllegalEscape( int c ) |
434 | |
{ |
435 | 37 | return c >= 0 && c < 32 && c != '\n' && c != '\r' && c != '\t'; |
436 | |
} |
437 | |
|
438 | |
private static String escapeXml( String text, boolean attribute ) |
439 | |
{ |
440 | 0 | StringBuilder sb = new StringBuilder( text.length() * 2 ); |
441 | 0 | for ( int i = 0; i < text.length(); i++ ) |
442 | |
{ |
443 | 0 | char c = text.charAt( i ); |
444 | 0 | if ( isIllegalEscape( c ) ) |
445 | |
{ |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | 0 | sb.append( attribute ? "&#" : "&#" ).append( (int) c ).append( |
452 | |
';' ); |
453 | |
} |
454 | |
else |
455 | |
{ |
456 | 0 | sb.append( c ); |
457 | |
} |
458 | |
} |
459 | 0 | return sb.toString(); |
460 | |
} |
461 | |
|
462 | |
} |