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 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.text.NumberFormat; |
25 | |
import java.text.ParseException; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Collection; |
28 | |
import java.util.HashMap; |
29 | |
import java.util.List; |
30 | |
import java.util.Map; |
31 | |
import java.util.StringTokenizer; |
32 | |
|
33 | |
import javax.xml.parsers.ParserConfigurationException; |
34 | |
import javax.xml.parsers.SAXParser; |
35 | |
import javax.xml.parsers.SAXParserFactory; |
36 | |
|
37 | |
import org.xml.sax.Attributes; |
38 | |
import org.xml.sax.SAXException; |
39 | |
import org.xml.sax.helpers.DefaultHandler; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 5 | public class TestSuiteXmlParser |
45 | |
extends DefaultHandler |
46 | |
{ |
47 | |
private ReportTestSuite defaultSuite; |
48 | |
private ReportTestSuite currentSuite; |
49 | |
private Map classesToSuites; |
50 | 5 | private NumberFormat numberFormat = NumberFormat.getInstance(); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
private StringBuffer currentElement; |
56 | |
|
57 | |
private ReportTestCase testCase; |
58 | |
|
59 | |
public Collection parse( String xmlPath ) |
60 | |
throws ParserConfigurationException, SAXException, IOException |
61 | |
{ |
62 | 11 | SAXParserFactory factory = SAXParserFactory.newInstance(); |
63 | |
|
64 | 11 | SAXParser saxParser = factory.newSAXParser(); |
65 | |
|
66 | 11 | classesToSuites = new HashMap(); |
67 | |
|
68 | 11 | saxParser.parse( new File( xmlPath ), this ); |
69 | |
|
70 | 11 | if ( currentSuite != defaultSuite ) |
71 | |
{ |
72 | 1 | if ( defaultSuite.getNumberOfTests() == 0 ) |
73 | |
{ |
74 | 1 | classesToSuites.remove( defaultSuite.getFullClassName() ); |
75 | |
} |
76 | |
} |
77 | |
|
78 | 11 | return classesToSuites.values(); |
79 | |
} |
80 | |
|
81 | |
|
82 | |
public void startElement( String uri, String localName, String qName, Attributes attributes ) |
83 | |
throws SAXException |
84 | |
{ |
85 | |
try |
86 | |
{ |
87 | 674 | if ( "testsuite".equals( qName ) ) |
88 | |
{ |
89 | 11 | currentSuite = defaultSuite = new ReportTestSuite(); |
90 | |
|
91 | |
try |
92 | |
{ |
93 | 11 | Number time = numberFormat.parse( attributes.getValue( "time" ) ); |
94 | |
|
95 | 10 | defaultSuite.setTimeElapsed( time.floatValue() ); |
96 | |
} |
97 | 1 | catch ( NullPointerException npe ) |
98 | |
{ |
99 | 1 | System.err.println("WARNING: no time attribute found on testsuite element"); |
100 | 10 | } |
101 | |
|
102 | |
|
103 | 11 | if ( attributes.getValue( "group" ) != null && !"".equals( attributes.getValue( "group" ) ) ) |
104 | |
{ |
105 | 0 | String packageName = attributes.getValue( "group" ); |
106 | 0 | String name = attributes.getValue( "name" ); |
107 | |
|
108 | 0 | defaultSuite.setFullClassName( packageName + "." + name ); |
109 | 0 | } |
110 | |
else |
111 | |
{ |
112 | 11 | String fullClassName = attributes.getValue( "name" ); |
113 | 11 | defaultSuite.setFullClassName( fullClassName ); |
114 | |
} |
115 | |
|
116 | 11 | classesToSuites.put( defaultSuite.getFullClassName(), defaultSuite ); |
117 | |
} |
118 | 663 | else if ( "testcase".equals( qName ) ) |
119 | |
{ |
120 | 57 | currentElement = new StringBuffer(); |
121 | |
|
122 | 57 | testCase = new ReportTestCase(); |
123 | |
|
124 | 57 | testCase.setName( attributes.getValue( "name" ) ); |
125 | |
|
126 | 57 | String fullClassName = attributes.getValue( "classname" ); |
127 | |
|
128 | |
|
129 | 57 | if ( fullClassName != null ) |
130 | |
{ |
131 | 2 | currentSuite = (ReportTestSuite) classesToSuites.get( fullClassName ); |
132 | 2 | if ( currentSuite == null ) |
133 | |
{ |
134 | 2 | currentSuite = new ReportTestSuite(); |
135 | 2 | currentSuite.setFullClassName( fullClassName ); |
136 | 2 | classesToSuites.put( fullClassName, currentSuite ); |
137 | |
} |
138 | |
} |
139 | |
|
140 | 57 | testCase.setFullClassName( currentSuite.getFullClassName() ); |
141 | 57 | testCase.setClassName( currentSuite.getName() ); |
142 | 57 | testCase.setFullName( currentSuite.getFullClassName() + "." + testCase.getName() ); |
143 | |
|
144 | 57 | String timeAsString = attributes.getValue( "time" ); |
145 | |
|
146 | 57 | Number time = new Integer( 0 ); |
147 | |
|
148 | 57 | if ( timeAsString != null ) |
149 | |
{ |
150 | 54 | time = numberFormat.parse( timeAsString ); |
151 | |
} |
152 | |
|
153 | 57 | testCase.setTime( time.floatValue() ); |
154 | |
|
155 | 57 | if ( currentSuite != defaultSuite ) { |
156 | 2 | currentSuite.setTimeElapsed( time.floatValue() + currentSuite.getTimeElapsed() ); |
157 | |
} |
158 | 57 | } |
159 | 606 | else if ( "failure".equals( qName ) ) |
160 | |
{ |
161 | 9 | testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) ); |
162 | 9 | currentSuite.setNumberOfFailures( 1 + currentSuite.getNumberOfFailures() ); |
163 | |
} |
164 | 597 | else if ( "error".equals( qName ) ) |
165 | |
{ |
166 | 5 | testCase.addFailure( attributes.getValue( "message" ), attributes.getValue( "type" ) ); |
167 | 5 | currentSuite.setNumberOfErrors( 1 + currentSuite.getNumberOfErrors() ); |
168 | |
} |
169 | 592 | else if ( "skipped".equals( qName ) ) |
170 | |
{ |
171 | 0 | testCase.addFailure( "skipped", "skipped" ); |
172 | 0 | currentSuite.setNumberOfSkipped( 1 + currentSuite.getNumberOfSkipped() ); |
173 | |
} |
174 | |
} |
175 | 0 | catch ( ParseException e ) |
176 | |
{ |
177 | 0 | throw new SAXException( e.getMessage(), e ); |
178 | 674 | } |
179 | 674 | } |
180 | |
|
181 | |
|
182 | |
public void endElement( String uri, String localName, String qName ) |
183 | |
throws SAXException |
184 | |
{ |
185 | 674 | if ( "testcase".equals( qName ) ) |
186 | |
{ |
187 | 57 | currentSuite.getTestCases().add( testCase ); |
188 | |
} |
189 | 617 | else if ( "failure".equals( qName ) ) |
190 | |
{ |
191 | 9 | Map failure = testCase.getFailure(); |
192 | |
|
193 | 9 | failure.put( "detail", parseCause( currentElement.toString() ) ); |
194 | 9 | } |
195 | 608 | else if ( "error".equals( qName ) ) |
196 | |
{ |
197 | 5 | Map error = testCase.getFailure(); |
198 | |
|
199 | 5 | error.put( "detail", parseCause( currentElement.toString() ) ); |
200 | 5 | } |
201 | 603 | else if ( "time".equals( qName ) ) |
202 | |
{ |
203 | |
try { |
204 | 2 | Number time = numberFormat.parse( currentElement.toString() ); |
205 | 2 | defaultSuite.setTimeElapsed( time.floatValue() ); |
206 | |
} |
207 | 0 | catch ( ParseException e ) |
208 | |
{ |
209 | 0 | throw new SAXException( e.getMessage(), e ); |
210 | 2 | } |
211 | |
} |
212 | |
|
213 | 674 | } |
214 | |
|
215 | |
|
216 | |
public void characters( char[] ch, int start, int length ) |
217 | |
throws SAXException |
218 | |
{ |
219 | 1558 | String s = new String( ch, start, length ); |
220 | |
|
221 | 1558 | if ( !"".equals( s.trim() ) ) |
222 | |
{ |
223 | 824 | currentElement.append( s ); |
224 | |
} |
225 | 1558 | } |
226 | |
|
227 | |
private List parseCause( String detail ) |
228 | |
{ |
229 | 14 | String fullName = testCase.getFullName(); |
230 | 14 | String name = fullName.substring( fullName.lastIndexOf( "." ) + 1 ); |
231 | 14 | return parseCause( detail, name ); |
232 | |
} |
233 | |
|
234 | |
private List parseCause( String detail, String compareTo ) |
235 | |
{ |
236 | 14 | StringTokenizer stringTokenizer = new StringTokenizer( detail, "\n" ); |
237 | 14 | List parsedDetail = new ArrayList( stringTokenizer.countTokens() ); |
238 | |
|
239 | 61 | while ( stringTokenizer.hasMoreTokens() ) |
240 | |
{ |
241 | 61 | String lineString = stringTokenizer.nextToken().trim(); |
242 | 61 | parsedDetail.add( lineString ); |
243 | 61 | if ( lineString.indexOf( compareTo ) >= 0 ) |
244 | |
{ |
245 | 14 | break; |
246 | |
} |
247 | 47 | } |
248 | |
|
249 | 14 | return parsedDetail; |
250 | |
} |
251 | |
|
252 | |
} |