Lines 23-42
Link Here
|
23 |
import java.io.OutputStream; |
23 |
import java.io.OutputStream; |
24 |
import java.io.OutputStreamWriter; |
24 |
import java.io.OutputStreamWriter; |
25 |
import java.io.Writer; |
25 |
import java.io.Writer; |
|
|
26 |
import java.lang.reflect.Method; |
27 |
import java.net.InetAddress; |
28 |
import java.net.UnknownHostException; |
29 |
import java.util.Date; |
26 |
import java.util.Enumeration; |
30 |
import java.util.Enumeration; |
27 |
import java.util.Hashtable; |
31 |
import java.util.Hashtable; |
28 |
import java.util.Properties; |
32 |
import java.util.Properties; |
29 |
import java.util.Date; |
33 |
|
30 |
import java.net.InetAddress; |
|
|
31 |
import java.net.UnknownHostException; |
32 |
import javax.xml.parsers.DocumentBuilder; |
34 |
import javax.xml.parsers.DocumentBuilder; |
33 |
import javax.xml.parsers.DocumentBuilderFactory; |
35 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
|
36 |
|
34 |
import junit.framework.AssertionFailedError; |
37 |
import junit.framework.AssertionFailedError; |
|
|
38 |
import junit.framework.JUnit4TestCaseFacade; |
35 |
import junit.framework.Test; |
39 |
import junit.framework.Test; |
|
|
40 |
|
36 |
import org.apache.tools.ant.BuildException; |
41 |
import org.apache.tools.ant.BuildException; |
37 |
import org.apache.tools.ant.util.DOMElementWriter; |
42 |
import org.apache.tools.ant.util.DOMElementWriter; |
38 |
import org.apache.tools.ant.util.DateUtils; |
43 |
import org.apache.tools.ant.util.DateUtils; |
39 |
import org.apache.tools.ant.util.FileUtils; |
44 |
import org.apache.tools.ant.util.FileUtils; |
|
|
45 |
import org.junit.Ignore; |
40 |
import org.w3c.dom.Document; |
46 |
import org.w3c.dom.Document; |
41 |
import org.w3c.dom.Element; |
47 |
import org.w3c.dom.Element; |
42 |
import org.w3c.dom.Text; |
48 |
import org.w3c.dom.Text; |
Lines 48-54
Link Here
|
48 |
* @see FormatterElement |
54 |
* @see FormatterElement |
49 |
*/ |
55 |
*/ |
50 |
|
56 |
|
51 |
public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants { |
57 |
public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants, IgnoredTestListener { |
52 |
|
58 |
|
53 |
private static final double ONE_SECOND = 1000.0; |
59 |
private static final double ONE_SECOND = 1000.0; |
54 |
|
60 |
|
Lines 73-88
Link Here
|
73 |
private Element rootElement; |
79 |
private Element rootElement; |
74 |
/** |
80 |
/** |
75 |
* Element for the current test. |
81 |
* Element for the current test. |
|
|
82 |
* |
83 |
* The keying of this map is a bit of a hack: tests are keyed by caseName(className) since |
84 |
* the Test we get for Test-start isn't the same as the Test we get during test-assumption-fail, |
85 |
* so we can't easily match Test objects without manually iterating over all keys and checking |
86 |
* individual fields. |
76 |
*/ |
87 |
*/ |
77 |
private Hashtable testElements = new Hashtable(); |
88 |
private Hashtable<String, Element> testElements = new Hashtable<String, Element>(); |
78 |
/** |
89 |
/** |
79 |
* tests that failed. |
90 |
* tests that failed. |
80 |
*/ |
91 |
*/ |
81 |
private Hashtable failedTests = new Hashtable(); |
92 |
private Hashtable failedTests = new Hashtable(); |
82 |
/** |
93 |
/** |
|
|
94 |
* Tests that were skipped. |
95 |
*/ |
96 |
private Hashtable<String, Test> skippedTests = new Hashtable<String, Test>(); |
97 |
/** |
98 |
* Tests that were ignored. See the note above about the key being a bit of a hack. |
99 |
*/ |
100 |
private Hashtable<String, Test> ignoredTests = new Hashtable<String, Test>(); |
101 |
/** |
83 |
* Timing helper. |
102 |
* Timing helper. |
84 |
*/ |
103 |
*/ |
85 |
private Hashtable testStarts = new Hashtable(); |
104 |
private Hashtable<String, Long> testStarts = new Hashtable<String, Long>(); |
86 |
/** |
105 |
/** |
87 |
* Where to write the log to. |
106 |
* Where to write the log to. |
88 |
*/ |
107 |
*/ |
Lines 161-166
Link Here
|
161 |
rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); |
180 |
rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); |
162 |
rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); |
181 |
rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); |
163 |
rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); |
182 |
rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); |
|
|
183 |
rootElement.setAttribute(ATTR_SKIPPED, "" + suite.skipCount()); |
164 |
rootElement.setAttribute( |
184 |
rootElement.setAttribute( |
165 |
ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); |
185 |
ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); |
166 |
if (out != null) { |
186 |
if (out != null) { |
Lines 193-201
Link Here
|
193 |
* @param t the test. |
213 |
* @param t the test. |
194 |
*/ |
214 |
*/ |
195 |
public void startTest(Test t) { |
215 |
public void startTest(Test t) { |
196 |
testStarts.put(t, new Long(System.currentTimeMillis())); |
216 |
testStarts.put(createDescription(t), System.currentTimeMillis()); |
197 |
} |
217 |
} |
198 |
|
218 |
|
|
|
219 |
private static String createDescription(Test test) throws BuildException { |
220 |
return JUnitVersionHelper.getTestCaseName(test) + "(" + JUnitVersionHelper.getTestCaseClassName(test) + ")"; |
221 |
} |
222 |
|
199 |
/** |
223 |
/** |
200 |
* Interface TestListener. |
224 |
* Interface TestListener. |
201 |
* |
225 |
* |
Lines 203-217
Link Here
|
203 |
* @param test the test. |
227 |
* @param test the test. |
204 |
*/ |
228 |
*/ |
205 |
public void endTest(Test test) { |
229 |
public void endTest(Test test) { |
|
|
230 |
String testDescription = createDescription(test); |
231 |
|
206 |
// Fix for bug #5637 - if a junit.extensions.TestSetup is |
232 |
// Fix for bug #5637 - if a junit.extensions.TestSetup is |
207 |
// used and throws an exception during setUp then startTest |
233 |
// used and throws an exception during setUp then startTest |
208 |
// would never have been called |
234 |
// would never have been called |
209 |
if (!testStarts.containsKey(test)) { |
235 |
if (!testStarts.containsKey(testDescription)) { |
210 |
startTest(test); |
236 |
startTest(test); |
211 |
} |
237 |
} |
212 |
|
238 |
Element currentTest; |
213 |
Element currentTest = null; |
239 |
if (!failedTests.containsKey(test) && !skippedTests.containsKey(testDescription) && !ignoredTests.containsKey(testDescription)) { |
214 |
if (!failedTests.containsKey(test)) { |
|
|
215 |
currentTest = doc.createElement(TESTCASE); |
240 |
currentTest = doc.createElement(TESTCASE); |
216 |
String n = JUnitVersionHelper.getTestCaseName(test); |
241 |
String n = JUnitVersionHelper.getTestCaseName(test); |
217 |
currentTest.setAttribute(ATTR_NAME, |
242 |
currentTest.setAttribute(ATTR_NAME, |
Lines 221-235
Link Here
|
221 |
currentTest.setAttribute(ATTR_CLASSNAME, |
246 |
currentTest.setAttribute(ATTR_CLASSNAME, |
222 |
JUnitVersionHelper.getTestCaseClassName(test)); |
247 |
JUnitVersionHelper.getTestCaseClassName(test)); |
223 |
rootElement.appendChild(currentTest); |
248 |
rootElement.appendChild(currentTest); |
224 |
testElements.put(test, currentTest); |
249 |
testElements.put(createDescription(test), currentTest); |
225 |
} else { |
250 |
} else { |
226 |
currentTest = (Element) testElements.get(test); |
251 |
currentTest = testElements.get(testDescription); |
227 |
} |
252 |
} |
228 |
|
253 |
|
229 |
Long l = (Long) testStarts.get(test); |
254 |
Long l = testStarts.get(createDescription(test)); |
230 |
currentTest.setAttribute(ATTR_TIME, |
255 |
currentTest.setAttribute(ATTR_TIME, |
231 |
"" + ((System.currentTimeMillis() |
256 |
"" + ((System.currentTimeMillis() - l) / ONE_SECOND)); |
232 |
- l.longValue()) / ONE_SECOND)); |
|
|
233 |
} |
257 |
} |
234 |
|
258 |
|
235 |
/** |
259 |
/** |
Lines 272-280
Link Here
|
272 |
} |
296 |
} |
273 |
|
297 |
|
274 |
Element nested = doc.createElement(type); |
298 |
Element nested = doc.createElement(type); |
275 |
Element currentTest = null; |
299 |
Element currentTest; |
276 |
if (test != null) { |
300 |
if (test != null) { |
277 |
currentTest = (Element) testElements.get(test); |
301 |
currentTest = testElements.get(createDescription(test)); |
278 |
} else { |
302 |
} else { |
279 |
currentTest = rootElement; |
303 |
currentTest = rootElement; |
280 |
} |
304 |
} |
Lines 298-301
Link Here
|
298 |
nested.appendChild(doc.createCDATASection(output)); |
322 |
nested.appendChild(doc.createCDATASection(output)); |
299 |
} |
323 |
} |
300 |
|
324 |
|
|
|
325 |
@Override |
326 |
public void testIgnored(Test test) { |
327 |
String message = null; |
328 |
if (test != null && test instanceof JUnit4TestCaseFacade) { |
329 |
//try and get the message coded as part of the ignore |
330 |
/* |
331 |
* org.junit.runner.Description contains a getAnnotation(Class) method... but this |
332 |
* wasn't in older versions of JUnit4 so we have to try and do this by reflection |
333 |
*/ |
334 |
try { |
335 |
Class<?> testClass = Class.forName(JUnitVersionHelper.getTestCaseClassName(test)); |
336 |
|
337 |
Method testMethod = testClass.getMethod(JUnitVersionHelper.getTestCaseName(test)); |
338 |
Ignore annotation = testMethod.getAnnotation(Ignore.class); |
339 |
if (annotation != null && annotation.value() != null && !annotation.value().isEmpty()) { |
340 |
message = annotation.value(); |
341 |
} |
342 |
} catch (NoSuchMethodException e) { |
343 |
// silently ignore - we'll report a skip with no message |
344 |
} catch (ClassNotFoundException e) { |
345 |
// silently ignore - we'll report a skip with no message |
346 |
} |
347 |
} |
348 |
formatSkip(test, message); |
349 |
if (test != null) { |
350 |
ignoredTests.put(createDescription(test), test); |
351 |
} |
352 |
} |
353 |
|
354 |
|
355 |
public void formatSkip(Test test, String message) { |
356 |
if (test != null) { |
357 |
endTest(test); |
358 |
} |
359 |
|
360 |
Element nested = doc.createElement("skipped"); |
361 |
|
362 |
if (message != null) { |
363 |
nested.setAttribute("message", message); |
364 |
} |
365 |
|
366 |
Element currentTest; |
367 |
if (test != null) { |
368 |
currentTest = testElements.get(createDescription(test)); |
369 |
} else { |
370 |
currentTest = rootElement; |
371 |
} |
372 |
|
373 |
currentTest.appendChild(nested); |
374 |
|
375 |
} |
376 |
|
377 |
@Override |
378 |
public void testAssumptionFailure(Test test, Throwable failure) { |
379 |
String message = failure.getMessage(); |
380 |
formatSkip(test, message); |
381 |
skippedTests.put(createDescription(test), test); |
382 |
|
383 |
} |
301 |
} // XMLJUnitResultFormatter |
384 |
} // XMLJUnitResultFormatter |