FileDocCategorySizeDatePackage
XMLJUnitResultFormatter.javaAPI DocApache Ant 1.709291Wed Dec 13 06:16:18 GMT 2006org.apache.tools.ant.taskdefs.optional.junit

XMLJUnitResultFormatter

public class XMLJUnitResultFormatter extends Object implements XMLConstants, JUnitResultFormatter
Prints XML output of the test to a specified Writer.
see
FormatterElement

Fields Summary
private static final String
UNKNOWN
constant for unnnamed testsuites/cases
private Document
doc
The XML document.
private Element
rootElement
The wrapper for the whole testsuite.
private Hashtable
testElements
Element for the current test.
private Hashtable
failedTests
tests that failed.
private Hashtable
testStarts
Timing helper.
private OutputStream
out
Where to write the log to.
Constructors Summary
public XMLJUnitResultFormatter()
No arg constructor.


        
      
    
Methods Summary
public voidaddError(junit.framework.Test test, java.lang.Throwable t)
Interface TestListener.

An error occurred while running the test.

param
test the test.
param
t the error.

        formatError(ERROR, test, t);
    
public voidaddFailure(junit.framework.Test test, java.lang.Throwable t)
Interface TestListener for JUnit <= 3.4.

A Test failed.

param
test the test.
param
t the exception.

        formatError(FAILURE, test, t);
    
public voidaddFailure(junit.framework.Test test, junit.framework.AssertionFailedError t)
Interface TestListener for JUnit > 3.4.

A Test failed.

param
test the test.
param
t the assertion.

        addFailure(test, (Throwable) t);
    
public voidendTest(junit.framework.Test test)
Interface TestListener.

A Test is finished.

param
test the test.

        // Fix for bug #5637 - if a junit.extensions.TestSetup is
        // used and throws an exception during setUp then startTest
        // would never have been called
        if (!testStarts.containsKey(test)) {
            startTest(test);
        }

        Element currentTest = null;
        if (!failedTests.containsKey(test)) {
            currentTest = doc.createElement(TESTCASE);
            String n = JUnitVersionHelper.getTestCaseName(test);
            currentTest.setAttribute(ATTR_NAME,
                                     n == null ? UNKNOWN : n);
            // a TestSuite can contain Tests from multiple classes,
            // even tests with the same name - disambiguate them.
            currentTest.setAttribute(ATTR_CLASSNAME,
                    JUnitVersionHelper.getTestCaseClassName(test));
            rootElement.appendChild(currentTest);
            testElements.put(test, currentTest);
        } else {
            currentTest = (Element) testElements.get(test);
        }

        Long l = (Long) testStarts.get(test);
        currentTest.setAttribute(ATTR_TIME,
            "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0));
    
public voidendTestSuite(JUnitTest suite)
The whole testsuite ended.

param
suite the testsuite.
throws
BuildException on error.

        rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
        rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount());
        rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
        rootElement.setAttribute(ATTR_TIME, "" + (suite.getRunTime() / 1000.0));
        if (out != null) {
            Writer wri = null;
            try {
                wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8"));
                wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
                (new DOMElementWriter()).write(rootElement, wri, 0, "  ");
                wri.flush();
            } catch (IOException exc) {
                throw new BuildException("Unable to write log file", exc);
            } finally {
                if (out != System.out && out != System.err) {
                    FileUtils.close(wri);
                }
            }
        }
    
private voidformatError(java.lang.String type, junit.framework.Test test, java.lang.Throwable t)

        if (test != null) {
            endTest(test);
            failedTests.put(test, test);
        }

        Element nested = doc.createElement(type);
        Element currentTest = null;
        if (test != null) {
            currentTest = (Element) testElements.get(test);
        } else {
            currentTest = rootElement;
        }

        currentTest.appendChild(nested);

        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);
        nested.appendChild(trace);
    
private voidformatOutput(java.lang.String type, java.lang.String output)

        Element nested = doc.createElement(type);
        rootElement.appendChild(nested);
        nested.appendChild(doc.createCDATASection(output));
    
private static javax.xml.parsers.DocumentBuildergetDocumentBuilder()


        
        try {
            return DocumentBuilderFactory.newInstance().newDocumentBuilder();
        } catch (Exception exc) {
            throw new ExceptionInInitializerError(exc);
        }
    
private java.lang.StringgetHostname()
get the local hostname

return
the name of the local host, or "localhost" if we cannot work it out

        try {
            return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "localhost";
        }
    
public voidsetOutput(java.io.OutputStream out)
{@inheritDoc}.

        this.out = out;
    
public voidsetSystemError(java.lang.String out)
{@inheritDoc}.

        formatOutput(SYSTEM_ERR, out);
    
public voidsetSystemOutput(java.lang.String out)
{@inheritDoc}.

        formatOutput(SYSTEM_OUT, out);
    
public voidstartTest(junit.framework.Test t)
Interface TestListener.

A new Test is started.

param
t the test.

        testStarts.put(t, new Long(System.currentTimeMillis()));
    
public voidstartTestSuite(JUnitTest suite)
The whole testsuite started.

param
suite the testsuite.

        doc = getDocumentBuilder().newDocument();
        rootElement = doc.createElement(TESTSUITE);
        String n = suite.getName();
        rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n);

        //add the timestamp
        final String timestamp = DateUtils.format(new Date(),
                DateUtils.ISO8601_DATETIME_PATTERN);
        rootElement.setAttribute(TIMESTAMP, timestamp);
        //and the hostname.
        rootElement.setAttribute(HOSTNAME, getHostname());

        // Output properties
        Element propsElement = doc.createElement(PROPERTIES);
        rootElement.appendChild(propsElement);
        Properties props = suite.getProperties();
        if (props != null) {
            Enumeration e = props.propertyNames();
            while (e.hasMoreElements()) {
                String name = (String) e.nextElement();
                Element propElement = doc.createElement(PROPERTY);
                propElement.setAttribute(ATTR_NAME, name);
                propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
                propsElement.appendChild(propElement);
            }
        }