@@ -61,7 +61,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
private static DocumentBuilder getDocumentBuilder() {
try {
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (Exception exc) {
} catch (final Exception exc) {
throw new ExceptionInInitializerError(exc);
}
}
@@ -70,10 +70,12 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* The XML document.
*/
private Document doc;
/**
* The wrapper for the whole testsuite.
*/
private Element rootElement;
/**
* Element for the current test.
*
@@ -82,23 +84,25 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* so we can't easily match Test objects without manually iterating over all keys and checking
* individual fields.
*/
private Hashtable<String, Element> testElements = new Hashtable<String, Element>();
private final Hashtable<String, Element> testElements = new Hashtable<String, Element>();
/**
* tests that failed.
*/
private Hashtable failedTests = new Hashtable();
private final Hashtable failedTests = new Hashtable();
/**
* Tests that were skipped.
*/
private Hashtable<String, Test> skippedTests = new Hashtable<String, Test>();
private final Hashtable<String, Test> skippedTests = new Hashtable<String, Test>();
/**
* Tests that were ignored. See the note above about the key being a bit of a hack.
*/
private Hashtable<String, Test> ignoredTests = new Hashtable<String, Test>();
private final Hashtable<String, Test> ignoredTests = new Hashtable<String, Test>();
/**
* Timing helper.
*/
private Hashtable<String, Long> testStarts = new Hashtable<String, Long>();
private final Hashtable<String, Long> testStarts = new Hashtable<String, Long>();
/**
* Where to write the log to.
*/
@@ -109,17 +113,17 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
}
/** {@inheritDoc}. */
public void setOutput(OutputStream out) {
public void setOutput(final OutputStream out) {
this.out = out;
}
/** {@inheritDoc}. */
public void setSystemOutput(String out) {
public void setSystemOutput(final String out) {
formatOutput(SYSTEM_OUT, out);
}
/** {@inheritDoc}. */
public void setSystemError(String out) {
public void setSystemError(final String out) {
formatOutput(SYSTEM_ERR, out);
}
@@ -127,10 +131,10 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* The whole testsuite started.
* @param suite the testsuite.
*/
public void startTestSuite(JUnitTest suite) {
public void startTestSuite(final JUnitTest suite) {
doc = getDocumentBuilder().newDocument();
rootElement = doc.createElement(TESTSUITE);
String n = suite.getName();
final String n = suite.getName();
rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n);
//add the timestamp
@@ -141,14 +145,14 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
rootElement.setAttribute(HOSTNAME, getHostname());
// Output properties
Element propsElement = doc.createElement(PROPERTIES);
final Element propsElement = doc.createElement(PROPERTIES);
rootElement.appendChild(propsElement);
Properties props = suite.getProperties();
final Properties props = suite.getProperties();
if (props != null) {
Enumeration e = props.propertyNames();
final Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
Element propElement = doc.createElement(PROPERTY);
final String name = (String) e.nextElement();
final Element propElement = doc.createElement(PROPERTY);
propElement.setAttribute(ATTR_NAME, name);
propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
propsElement.appendChild(propElement);
@@ -163,11 +167,11 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
private String getHostname() {
String hostname = "localhost";
try {
InetAddress localHost = InetAddress.getLocalHost();
final InetAddress localHost = InetAddress.getLocalHost();
if (localHost != null) {
hostname = localHost.getHostName();
}
} catch (UnknownHostException e) {
} catch (final UnknownHostException e) {
// fall back to default 'localhost'
}
return hostname;
@@ -178,7 +182,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* @param suite the testsuite.
* @throws BuildException on error.
*/
public void endTestSuite(JUnitTest suite) throws BuildException {
public void endTestSuite(final JUnitTest suite) throws BuildException {
rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount());
rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
@@ -191,13 +195,13 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8"));
wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
(new DOMElementWriter()).write(rootElement, wri, 0, " ");
} catch (IOException exc) {
} catch (final IOException exc) {
throw new BuildException("Unable to write log file", exc);
} finally {
if (wri != null) {
try {
wri.flush();
} catch (IOException ex) {
} catch (final IOException ex) {
// ignore
}
}
@@ -214,11 +218,11 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* <p>A new Test is started.
* @param t the test.
*/
public void startTest(Test t) {
public void startTest(final Test t) {
testStarts.put(createDescription(t), System.currentTimeMillis());
}
private static String createDescription(Test test) throws BuildException {
private static String createDescription(final Test test) throws BuildException {
return JUnitVersionHelper.getTestCaseName(test) + "(" + JUnitVersionHelper.getTestCaseClassName(test) + ")";
}
@@ -228,8 +232,8 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* <p>A Test is finished.
* @param test the test.
*/
public void endTest(Test test) {
String testDescription = createDescription(test);
public void endTest(final Test test) {
final String testDescription = createDescription(test);
// Fix for bug #5637 - if a junit.extensions.TestSetup is
// used and throws an exception during setUp then startTest
@@ -240,7 +244,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
Element currentTest;
if (!failedTests.containsKey(test) && !skippedTests.containsKey(testDescription) && !ignoredTests.containsKey(testDescription)) {
currentTest = doc.createElement(TESTCASE);
String n = JUnitVersionHelper.getTestCaseName(test);
final String n = JUnitVersionHelper.getTestCaseName(test);
currentTest.setAttribute(ATTR_NAME,
n == null ? UNKNOWN : n);
// a TestSuite can contain Tests from multiple classes,
@@ -253,7 +257,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
currentTest = testElements.get(testDescription);
}
Long l = testStarts.get(createDescription(test));
final Long l = testStarts.get(createDescription(test));
currentTest.setAttribute(ATTR_TIME,
"" + ((System.currentTimeMillis() - l) / ONE_SECOND));
}
@@ -265,7 +269,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* @param test the test.
* @param t the exception.
*/
public void addFailure(Test test, Throwable t) {
public void addFailure(final Test test, final Throwable t) {
formatError(FAILURE, test, t);
}
@@ -276,7 +280,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* @param test the test.
* @param t the assertion.
*/
public void addFailure(Test test, AssertionFailedError t) {
public void addFailure(final Test test, final AssertionFailedError t) {
addFailure(test, (Throwable) t);
}
@@ -287,17 +291,17 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
* @param test the test.
* @param t the error.
*/
public void addError(Test test, Throwable t) {
public void addError(final Test test, final Throwable t) {
formatError(ERROR, test, t);
}
private void formatError(String type, Test test, Throwable t) {
private void formatError(final String type, final Test test, final Throwable t) {
if (test != null) {
endTest(test);
failedTests.put(test, test);
}
Element nested = doc.createElement(type);
final Element nested = doc.createElement(type);
Element currentTest;
if (test != null) {
currentTest = testElements.get(createDescription(test));
@@ -307,24 +311,24 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
currentTest.appendChild(nested);
String message = t.getMessage();
final String message = t.getMessage();
if (message != null && message.length() > 0) {
nested.setAttribute(ATTR_MESSAGE, t.getMessage());
}
nested.setAttribute(ATTR_TYPE, t.getClass().getName());
String strace = JUnitTestRunner.getFilteredTrace(t);
Text trace = doc.createTextNode(strace);
final String strace = JUnitTestRunner.getFilteredTrace(t);
final Text trace = doc.createTextNode(strace);
nested.appendChild(trace);
}
private void formatOutput(String type, String output) {
Element nested = doc.createElement(type);
private void formatOutput(final String type, final String output) {
final Element nested = doc.createElement(type);
rootElement.appendChild(nested);
nested.appendChild(doc.createCDATASection(output));
}
public void testIgnored(Test test) {
public void testIgnored(final Test test) {
formatSkip(test, JUnitVersionHelper.getIgnoreMessage(test));
if (test != null) {
ignoredTests.put(createDescription(test), test);
@@ -332,12 +336,12 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
}
public void formatSkip(Test test, String message) {
public void formatSkip(final Test test, final String message) {
if (test != null) {
endTest(test);
}
Element nested = doc.createElement("skipped");
final Element nested = doc.createElement("skipped");
if (message != null) {
nested.setAttribute("message", message);
@@ -354,7 +358,7 @@ public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstan
}
public void testAssumptionFailure(Test test, Throwable failure) {
public void testAssumptionFailure(final Test test, final Throwable failure) {
formatSkip(test, failure.getMessage());
skippedTests.put(createDescription(test), test);