Lines 32-42
Link Here
|
32 |
import javax.xml.parsers.DocumentBuilder; |
32 |
import javax.xml.parsers.DocumentBuilder; |
33 |
import javax.xml.parsers.DocumentBuilderFactory; |
33 |
import javax.xml.parsers.DocumentBuilderFactory; |
34 |
import junit.framework.AssertionFailedError; |
34 |
import junit.framework.AssertionFailedError; |
|
|
35 |
import junit.framework.JUnit4TestCaseFacade; |
35 |
import junit.framework.Test; |
36 |
import junit.framework.Test; |
36 |
import org.apache.tools.ant.BuildException; |
37 |
import org.apache.tools.ant.BuildException; |
37 |
import org.apache.tools.ant.util.DOMElementWriter; |
38 |
import org.apache.tools.ant.util.DOMElementWriter; |
38 |
import org.apache.tools.ant.util.DateUtils; |
39 |
import org.apache.tools.ant.util.DateUtils; |
39 |
import org.apache.tools.ant.util.FileUtils; |
40 |
import org.apache.tools.ant.util.FileUtils; |
|
|
41 |
import org.junit.Ignore; |
42 |
import org.junit.runner.Description; |
43 |
import org.junit.runner.notification.Failure; |
40 |
import org.w3c.dom.Document; |
44 |
import org.w3c.dom.Document; |
41 |
import org.w3c.dom.Element; |
45 |
import org.w3c.dom.Element; |
42 |
import org.w3c.dom.Text; |
46 |
import org.w3c.dom.Text; |
Lines 48-54
Link Here
|
48 |
* @see FormatterElement |
52 |
* @see FormatterElement |
49 |
*/ |
53 |
*/ |
50 |
|
54 |
|
51 |
public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants { |
55 |
public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants, IgnoredTestListener { |
52 |
|
56 |
|
53 |
private static final double ONE_SECOND = 1000.0; |
57 |
private static final double ONE_SECOND = 1000.0; |
54 |
|
58 |
|
Lines 74-88
Link Here
|
74 |
/** |
78 |
/** |
75 |
* Element for the current test. |
79 |
* Element for the current test. |
76 |
*/ |
80 |
*/ |
77 |
private Hashtable testElements = new Hashtable(); |
81 |
private Hashtable<Description, Element> testElements = new Hashtable<Description, Element>(); |
78 |
/** |
82 |
/** |
79 |
* tests that failed. |
83 |
* tests that failed. |
80 |
*/ |
84 |
*/ |
81 |
private Hashtable failedTests = new Hashtable(); |
85 |
private Hashtable failedTests = new Hashtable(); |
82 |
/** |
86 |
/** |
|
|
87 |
* Tests that were skipped. This is a bit of a hack - tests are keyed by caseName(className) since |
88 |
* the Test we get for Test start isn't the same as the Test we get during test assumption fail, |
89 |
* so we can't easily match Test objects |
90 |
*/ |
91 |
private Hashtable<Description, Test> skippedTests = new Hashtable<Description, Test>(); |
92 |
/** |
93 |
* Tests that were skipped. This is a bit of a hack - tests are keyed by caseName(className) since |
94 |
* the Test we get for Test start isn't the same as the Test we get during test assumption fail, |
95 |
* so we can't easily match Test objects |
96 |
*/ |
97 |
private Hashtable<Description, Test> ignoredTests = new Hashtable<Description, Test>(); |
98 |
/** |
83 |
* Timing helper. |
99 |
* Timing helper. |
84 |
*/ |
100 |
*/ |
85 |
private Hashtable testStarts = new Hashtable(); |
101 |
private Hashtable<Description, Long> testStarts = new Hashtable<Description, Long>(); |
86 |
/** |
102 |
/** |
87 |
* Where to write the log to. |
103 |
* Where to write the log to. |
88 |
*/ |
104 |
*/ |
Lines 158-166
Link Here
|
158 |
* @throws BuildException on error. |
174 |
* @throws BuildException on error. |
159 |
*/ |
175 |
*/ |
160 |
public void endTestSuite(JUnitTest suite) throws BuildException { |
176 |
public void endTestSuite(JUnitTest suite) throws BuildException { |
|
|
177 |
/* to bring the tests attribute inline with other runners (e.g. surefire), we actually include ignored tests in |
178 |
* the total test count |
179 |
* we have an issue where tests that were skipped due to Assumption failures are recorded as started despite not |
180 |
* producing a final result (pass, fail, error) so we subtract their count from the overall totals |
181 |
*/ |
161 |
rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); |
182 |
rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); |
162 |
rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); |
183 |
rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); |
163 |
rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); |
184 |
rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); |
|
|
185 |
rootElement.setAttribute(ATTR_SKIPPED, "" + suite.skipCount()); |
164 |
rootElement.setAttribute( |
186 |
rootElement.setAttribute( |
165 |
ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); |
187 |
ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); |
166 |
if (out != null) { |
188 |
if (out != null) { |
Lines 193-201
Link Here
|
193 |
* @param t the test. |
215 |
* @param t the test. |
194 |
*/ |
216 |
*/ |
195 |
public void startTest(Test t) { |
217 |
public void startTest(Test t) { |
196 |
testStarts.put(t, new Long(System.currentTimeMillis())); |
218 |
testStarts.put(createDescription(t), System.currentTimeMillis()); |
197 |
} |
219 |
} |
198 |
|
220 |
|
|
|
221 |
private static Description createDescription(Test test) { |
222 |
return Description.createTestDescription(JUnitVersionHelper.getTestCaseClassName(test), JUnitVersionHelper.getTestCaseName(test)); |
223 |
} |
224 |
|
199 |
/** |
225 |
/** |
200 |
* Interface TestListener. |
226 |
* Interface TestListener. |
201 |
* |
227 |
* |
Lines 203-217
Link Here
|
203 |
* @param test the test. |
229 |
* @param test the test. |
204 |
*/ |
230 |
*/ |
205 |
public void endTest(Test test) { |
231 |
public void endTest(Test test) { |
|
|
232 |
Description testDescription = createDescription(test); |
233 |
|
206 |
// Fix for bug #5637 - if a junit.extensions.TestSetup is |
234 |
// Fix for bug #5637 - if a junit.extensions.TestSetup is |
207 |
// used and throws an exception during setUp then startTest |
235 |
// used and throws an exception during setUp then startTest |
208 |
// would never have been called |
236 |
// would never have been called |
209 |
if (!testStarts.containsKey(test)) { |
237 |
if (!testStarts.containsKey(testDescription)) { |
210 |
startTest(test); |
238 |
startTest(test); |
211 |
} |
239 |
} |
212 |
|
240 |
Element currentTest; |
213 |
Element currentTest = null; |
241 |
if (!failedTests.containsKey(test) && !skippedTests.containsKey(testDescription) && !ignoredTests.containsKey(testDescription)) { |
214 |
if (!failedTests.containsKey(test)) { |
|
|
215 |
currentTest = doc.createElement(TESTCASE); |
242 |
currentTest = doc.createElement(TESTCASE); |
216 |
String n = JUnitVersionHelper.getTestCaseName(test); |
243 |
String n = JUnitVersionHelper.getTestCaseName(test); |
217 |
currentTest.setAttribute(ATTR_NAME, |
244 |
currentTest.setAttribute(ATTR_NAME, |
Lines 221-235
Link Here
|
221 |
currentTest.setAttribute(ATTR_CLASSNAME, |
248 |
currentTest.setAttribute(ATTR_CLASSNAME, |
222 |
JUnitVersionHelper.getTestCaseClassName(test)); |
249 |
JUnitVersionHelper.getTestCaseClassName(test)); |
223 |
rootElement.appendChild(currentTest); |
250 |
rootElement.appendChild(currentTest); |
224 |
testElements.put(test, currentTest); |
251 |
testElements.put(createDescription(test), currentTest); |
225 |
} else { |
252 |
} else { |
226 |
currentTest = (Element) testElements.get(test); |
253 |
currentTest = testElements.get(testDescription); |
227 |
} |
254 |
} |
228 |
|
255 |
|
229 |
Long l = (Long) testStarts.get(test); |
256 |
Long l = testStarts.get(createDescription(test)); |
230 |
currentTest.setAttribute(ATTR_TIME, |
257 |
currentTest.setAttribute(ATTR_TIME, |
231 |
"" + ((System.currentTimeMillis() |
258 |
"" + ((System.currentTimeMillis() - l) / ONE_SECOND)); |
232 |
- l.longValue()) / ONE_SECOND)); |
|
|
233 |
} |
259 |
} |
234 |
|
260 |
|
235 |
/** |
261 |
/** |
Lines 272-280
Link Here
|
272 |
} |
298 |
} |
273 |
|
299 |
|
274 |
Element nested = doc.createElement(type); |
300 |
Element nested = doc.createElement(type); |
275 |
Element currentTest = null; |
301 |
Element currentTest; |
276 |
if (test != null) { |
302 |
if (test != null) { |
277 |
currentTest = (Element) testElements.get(test); |
303 |
currentTest = testElements.get(createDescription(test)); |
278 |
} else { |
304 |
} else { |
279 |
currentTest = rootElement; |
305 |
currentTest = rootElement; |
280 |
} |
306 |
} |
Lines 298-301
Link Here
|
298 |
nested.appendChild(doc.createCDATASection(output)); |
324 |
nested.appendChild(doc.createCDATASection(output)); |
299 |
} |
325 |
} |
300 |
|
326 |
|
|
|
327 |
@Override |
328 |
public void testIgnored(Test test) { |
329 |
String message = null; |
330 |
if (test != null) { |
331 |
if (test instanceof JUnit4TestCaseFacade) { |
332 |
JUnit4TestCaseFacade facade = (JUnit4TestCaseFacade) test; |
333 |
Ignore annotation = facade.getDescription().getAnnotation(Ignore.class); |
334 |
if (annotation != null && annotation.value() != null && !annotation.value().isEmpty()) { |
335 |
message = annotation.value(); |
336 |
} |
337 |
} |
338 |
} |
339 |
formatSkip(test, message); |
340 |
if (test != null) { |
341 |
ignoredTests.put(createDescription(test), test); |
342 |
} |
343 |
} |
344 |
|
345 |
|
346 |
public void formatSkip(Test test, String message) { |
347 |
if (test != null) { |
348 |
endTest(test); |
349 |
} |
350 |
|
351 |
Element nested = doc.createElement("skipped"); |
352 |
|
353 |
if (message != null) { |
354 |
nested.setAttribute("message", message); |
355 |
} |
356 |
|
357 |
Element currentTest; |
358 |
if (test != null) { |
359 |
currentTest = testElements.get(createDescription(test)); |
360 |
} else { |
361 |
currentTest = rootElement; |
362 |
} |
363 |
|
364 |
currentTest.appendChild(nested); |
365 |
|
366 |
} |
367 |
|
368 |
@Override |
369 |
public void testAssumptionFailure(Failure failure) { |
370 |
Test test = CustomJUnit4TestAdapterCache.getDefault().asTest(failure.getDescription()); |
371 |
String message = failure.getMessage(); |
372 |
formatSkip(test, message); |
373 |
skippedTests.put(createDescription(test), test); |
374 |
|
375 |
} |
301 |
} // XMLJUnitResultFormatter |
376 |
} // XMLJUnitResultFormatter |