1 |
|
package org.apache.maven.plugins.surefire.report; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
import org.xml.sax.Attributes; |
20 |
|
import org.xml.sax.SAXException; |
21 |
|
import org.xml.sax.helpers.DefaultHandler; |
22 |
|
|
23 |
|
import javax.xml.parsers.ParserConfigurationException; |
24 |
|
import javax.xml.parsers.SAXParser; |
25 |
|
import javax.xml.parsers.SAXParserFactory; |
26 |
|
import java.io.File; |
27 |
|
import java.io.IOException; |
28 |
|
import java.text.NumberFormat; |
29 |
|
import java.text.ParseException; |
30 |
|
import java.util.ArrayList; |
31 |
|
import java.util.Collections; |
32 |
|
import java.util.List; |
33 |
|
import java.util.Map; |
34 |
|
import java.util.StringTokenizer; |
35 |
|
|
36 |
46 |
public class ReportTestSuite |
37 |
|
extends DefaultHandler |
38 |
|
{ |
39 |
|
private List testCases; |
40 |
|
|
41 |
|
private int numberOfErrors; |
42 |
|
|
43 |
|
private int numberOfFailures; |
44 |
|
|
45 |
|
private int numberOfSkipped; |
46 |
|
|
47 |
|
private int numberOfTests; |
48 |
|
|
49 |
|
private String name; |
50 |
|
|
51 |
|
private String fullClassName; |
52 |
|
|
53 |
|
private String packageName; |
54 |
|
|
55 |
|
private float timeElapsed; |
56 |
|
|
57 |
46 |
private NumberFormat numberFormat = NumberFormat.getInstance(); |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private StringBuffer currentElement; |
63 |
|
|
64 |
|
private ReportTestCase testCase; |
65 |
|
|
66 |
|
public void parse( String xmlPath ) |
67 |
|
throws ParserConfigurationException, SAXException, IOException |
68 |
|
{ |
69 |
16 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
70 |
|
|
71 |
16 |
SAXParser saxParser = factory.newSAXParser(); |
72 |
|
|
73 |
16 |
saxParser.parse( new File( xmlPath ), this ); |
74 |
16 |
} |
75 |
|
|
76 |
|
|
77 |
|
private int getAttributeAsInt( Attributes attributes, String name ) |
78 |
|
{ |
79 |
|
|
80 |
64 |
String valueAsString = attributes.getValue( name ); |
81 |
64 |
if ( valueAsString != null ) |
82 |
|
{ |
83 |
48 |
return Integer.parseInt( valueAsString ); |
84 |
|
} |
85 |
16 |
return 0; |
86 |
|
} |
87 |
|
|
88 |
|
public void startElement( String uri, String localName, String qName, Attributes attributes ) |
89 |
|
throws SAXException |
90 |
|
{ |
91 |
|
try |
92 |
|
{ |
93 |
1094 |
if ( "testsuite".equals( qName ) ) |
94 |
|
{ |
95 |
16 |
numberOfErrors = getAttributeAsInt( attributes, "errors" ); |
96 |
16 |
numberOfFailures = getAttributeAsInt( attributes, "failures" ); |
97 |
16 |
numberOfSkipped = getAttributeAsInt( attributes, "skipped" ); |
98 |
16 |
numberOfTests = getAttributeAsInt( attributes, "tests" ); |
99 |
|
|
100 |
16 |
Number time = numberFormat.parse( attributes.getValue( "time" ) ); |
101 |
|
|
102 |
16 |
timeElapsed = time.floatValue(); |
103 |
|
|
104 |
|
|
105 |
16 |
if ( attributes.getValue( "group" ) != null && !"".equals( attributes.getValue( "group" ) ) ) |
106 |
|
{ |
107 |
0 |
packageName = attributes.getValue( "group" ); |
108 |
|
|
109 |
0 |
name = attributes.getValue( "name" ); |
110 |
|
|
111 |
0 |
fullClassName = packageName + "." + name; |
112 |
0 |
} |
113 |
|
else |
114 |
|
{ |
115 |
16 |
fullClassName = attributes.getValue( "name" ); |
116 |
|
|
117 |
16 |
name = fullClassName.substring( fullClassName.lastIndexOf( "." ) + 1, fullClassName.length() ); |
118 |
|
|
119 |
16 |
int lastDotPosition = fullClassName.lastIndexOf( "." ); |
120 |
16 |
if ( lastDotPosition < 0 ) |
121 |
|
{ |
122 |
|
|
123 |
4 |
packageName = ""; |
124 |
4 |
} |
125 |
|
else |
126 |
|
{ |
127 |
12 |
packageName = fullClassName.substring( 0, lastDotPosition ); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
16 |
testCases = new ArrayList(); |
132 |
16 |
} |
133 |
1078 |
else if ( "testcase".equals( qName ) ) |
134 |
|
{ |
135 |
108 |
currentElement = new StringBuffer(); |
136 |
|
|
137 |
108 |
testCase = new ReportTestCase(); |
138 |
|
|
139 |
108 |
testCase.setFullClassName( fullClassName ); |
140 |
|
|
141 |
108 |
testCase.setName( attributes.getValue( "name" ) ); |
142 |
|
|
143 |
108 |
testCase.setClassName( name ); |
144 |
|
|
145 |
108 |
String timeAsString = attributes.getValue( "time" ); |
146 |
|
|
147 |
108 |
Number time = new Integer( 0 ); |
148 |
|
|
149 |
108 |
if ( timeAsString != null ) |
150 |
|
{ |
151 |
104 |
time = numberFormat.parse( timeAsString ); |
152 |
|
} |
153 |
|
|
154 |
108 |
testCase.setTime( time.floatValue() ); |
155 |
|
|
156 |
108 |
testCase.setFullName( packageName + "." + name + "." + testCase.getName() ); |
157 |
108 |
} |
158 |
970 |
else if ( "failure".equals( qName ) ) |
159 |
|
{ |
160 |
18 |
testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) ); |
161 |
18 |
} |
162 |
952 |
else if ( "error".equals( qName ) ) |
163 |
|
{ |
164 |
10 |
testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) ); |
165 |
|
} |
166 |
|
} |
167 |
0 |
catch ( ParseException e ) |
168 |
|
{ |
169 |
0 |
throw new SAXException( e.getMessage(), e ); |
170 |
1094 |
} |
171 |
1094 |
} |
172 |
|
|
173 |
|
public void endElement( String uri, String localName, String qName ) |
174 |
|
throws SAXException |
175 |
|
{ |
176 |
1094 |
if ( "testcase".equals( qName ) ) |
177 |
|
{ |
178 |
108 |
testCases.add( testCase ); |
179 |
108 |
} |
180 |
986 |
else if ( "failure".equals( qName ) ) |
181 |
|
{ |
182 |
18 |
Map failure = testCase.getFailure(); |
183 |
|
|
184 |
18 |
failure.put( "detail", parseCause( currentElement.toString() ) ); |
185 |
18 |
} |
186 |
968 |
else if ( "error".equals( qName ) ) |
187 |
|
{ |
188 |
10 |
Map error = testCase.getFailure(); |
189 |
|
|
190 |
10 |
error.put( "detail", parseCause( currentElement.toString() ) ); |
191 |
|
} |
192 |
1094 |
} |
193 |
|
|
194 |
|
public void characters( char[] ch, int start, int length ) |
195 |
|
throws SAXException |
196 |
|
{ |
197 |
2830 |
String s = new String( ch, start, length ); |
198 |
|
|
199 |
2830 |
if ( ! "".equals( s.trim() ) ) |
200 |
|
{ |
201 |
1624 |
currentElement.append( s ); |
202 |
|
} |
203 |
2830 |
} |
204 |
|
|
205 |
|
public List getTestCases() |
206 |
|
{ |
207 |
30 |
return this.testCases; |
208 |
|
} |
209 |
|
|
210 |
|
public int getNumberOfErrors() |
211 |
|
{ |
212 |
48 |
return numberOfErrors; |
213 |
|
} |
214 |
|
|
215 |
|
public void setNumberOfErrors( int numberOfErrors ) |
216 |
|
{ |
217 |
6 |
this.numberOfErrors = numberOfErrors; |
218 |
6 |
} |
219 |
|
|
220 |
|
public int getNumberOfFailures() |
221 |
|
{ |
222 |
38 |
return numberOfFailures; |
223 |
|
} |
224 |
|
|
225 |
|
public void setNumberOfFailures( int numberOfFailures ) |
226 |
|
{ |
227 |
6 |
this.numberOfFailures = numberOfFailures; |
228 |
6 |
} |
229 |
|
|
230 |
|
public int getNumberOfSkipped() |
231 |
|
{ |
232 |
38 |
return numberOfSkipped; |
233 |
|
} |
234 |
|
|
235 |
|
public void setNumberOfSkipped( int numberOfSkipped ) |
236 |
|
{ |
237 |
6 |
this.numberOfSkipped = numberOfSkipped; |
238 |
6 |
} |
239 |
|
|
240 |
|
public int getNumberOfTests() |
241 |
|
{ |
242 |
38 |
return numberOfTests; |
243 |
|
} |
244 |
|
|
245 |
|
public void setNumberOfTests( int numberOfTests ) |
246 |
|
{ |
247 |
6 |
this.numberOfTests = numberOfTests; |
248 |
6 |
} |
249 |
|
|
250 |
|
public String getName() |
251 |
|
{ |
252 |
58 |
return name; |
253 |
|
} |
254 |
|
|
255 |
|
public void setName( String name ) |
256 |
|
{ |
257 |
2 |
this.name = name; |
258 |
2 |
} |
259 |
|
|
260 |
|
public String getFName() |
261 |
|
{ |
262 |
0 |
return name; |
263 |
|
} |
264 |
|
|
265 |
|
public void setFName( String name ) |
266 |
|
{ |
267 |
0 |
this.name = name; |
268 |
0 |
} |
269 |
|
|
270 |
|
public String getPackageName() |
271 |
|
{ |
272 |
64 |
return packageName; |
273 |
|
} |
274 |
|
|
275 |
|
public void setPackageName( String packageName ) |
276 |
|
{ |
277 |
8 |
this.packageName = packageName; |
278 |
8 |
} |
279 |
|
|
280 |
|
public float getTimeElapsed() |
281 |
|
{ |
282 |
30 |
return this.timeElapsed; |
283 |
|
} |
284 |
|
|
285 |
|
public void setTimeElapsed( float timeElapsed ) |
286 |
|
{ |
287 |
6 |
this.timeElapsed = timeElapsed; |
288 |
6 |
} |
289 |
|
|
290 |
|
private List parseCause( String detail ) |
291 |
|
{ |
292 |
28 |
String fullName = testCase.getFullName(); |
293 |
28 |
String name = fullName.substring( fullName.lastIndexOf( "." ) + 1 ); |
294 |
28 |
return parseCause( detail, name ); |
295 |
|
} |
296 |
|
|
297 |
|
private List parseCause( String detail, String compareTo ) |
298 |
|
{ |
299 |
28 |
StringTokenizer stringTokenizer = new StringTokenizer( detail, "\n" ); |
300 |
28 |
List parsedDetail = new ArrayList( stringTokenizer.countTokens() ); |
301 |
|
|
302 |
122 |
while ( stringTokenizer.hasMoreTokens() ) |
303 |
|
{ |
304 |
122 |
String lineString = stringTokenizer.nextToken().trim(); |
305 |
122 |
parsedDetail.add( lineString ); |
306 |
122 |
if ( lineString.indexOf( compareTo ) >= 0 ) |
307 |
|
{ |
308 |
28 |
break; |
309 |
|
} |
310 |
94 |
} |
311 |
|
|
312 |
28 |
return parsedDetail; |
313 |
|
} |
314 |
|
|
315 |
|
public void setTestCases( List testCases ) |
316 |
|
{ |
317 |
6 |
this.testCases = Collections.unmodifiableList( testCases ); |
318 |
6 |
} |
319 |
|
} |