diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/BaseStreamFormatter.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/BaseStreamFormatter.java index e3affb172..1149be7cc 100644 --- a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/BaseStreamFormatter.java +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/BaseStreamFormatter.java @@ -86,7 +86,7 @@ public class BaseStreamFormatter extends BaseFormatter { private PrintWriter writer; public void init(Properties props) throws BuildException { - String file = props.getProperty("file"); + String file = props.getProperty(FILE_KEY); OutputStream os = null; if (file != null) { try { diff --git a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/XMLFormatter.java b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/XMLFormatter.java index a3f8bcf8d..23ceae218 100644 --- a/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/XMLFormatter.java +++ b/proposal/sandbox/junit/src/main/org/apache/tools/ant/taskdefs/optional/junit/formatter/XMLFormatter.java @@ -54,6 +54,7 @@ package org.apache.tools.ant.taskdefs.optional.junit.formatter; import java.util.Hashtable; +import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -61,6 +62,9 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Text; +import org.apache.tools.ant.util.DOMElementWriter; +import org.apache.tools.ant.BuildException; + /** * XML Formatter. Due to the nature of the XML we are forced to store * everything in memory until it is finished. It might be resource @@ -68,7 +72,7 @@ import org.w3c.dom.Text; * * @author Stephane Bailliez */ -public class XMLFormatter extends BaseFormatter { +public class XMLFormatter extends BaseStreamFormatter { /** the testsuites element for the aggregate document */ public final static String TESTSUITES = "testsuites"; @@ -125,10 +129,10 @@ public class XMLFormatter extends BaseFormatter { public final static String ATTR_VALUE = "value"; /** The XML document. */ - private Document doc; + private Document doc = getDocumentBuilder().newDocument(); /** The wrapper for the whole testsuite. */ - private Element rootElement; + private Element rootElement = doc.createElement(TESTSUITE); /** Element for the current test. */ private Hashtable testElements = new Hashtable(); @@ -158,8 +162,8 @@ public class XMLFormatter extends BaseFormatter { currentTest.setAttribute(ATTR_TIME, Float.toString(time)); super.onTestEnded(testname); // remove the test objects - testStarts.remove(testname); - testElements.remove(testname); + //testStarts.remove(testname); + //testElements.remove(testname); } public void onTestFailed(int status, String testname, String trace) { @@ -199,6 +203,19 @@ public class XMLFormatter extends BaseFormatter { super.onTestRunStopped(elapsedtime); } + protected void close() { + DOMElementWriter domWriter = new DOMElementWriter(); + // the underlying writer uses UTF8 encoding + getWriter().println(""); + try { + domWriter.write(rootElement, getWriter(), 0, " "); + } catch (IOException e){ + throw new BuildException(e); + } finally { + super.close(); + } + } + private static DocumentBuilder getDocumentBuilder() { try { return DocumentBuilderFactory.newInstance().newDocumentBuilder();