KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > junit > XMLJUnitResultFormatter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.junit;
20
21 import java.io.BufferedWriter JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.net.InetAddress JavaDoc;
31 import java.net.UnknownHostException JavaDoc;
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import junit.framework.AssertionFailedError;
35 import junit.framework.Test;
36 import org.apache.tools.ant.BuildException;
37 import org.apache.tools.ant.util.DOMElementWriter;
38 import org.apache.tools.ant.util.DateUtils;
39 import org.apache.tools.ant.util.FileUtils;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.Text JavaDoc;
43
44
45 /**
46  * Prints XML output of the test to a specified Writer.
47  *
48  * @see FormatterElement
49  */

50
51 public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants {
52
53     /** constant for unnnamed testsuites/cases */
54     private static final String JavaDoc UNKNOWN = "unknown";
55
56     private static DocumentBuilder JavaDoc getDocumentBuilder() {
57         try {
58             return DocumentBuilderFactory.newInstance().newDocumentBuilder();
59         } catch (Exception JavaDoc exc) {
60             throw new ExceptionInInitializerError JavaDoc(exc);
61         }
62     }
63
64     /**
65      * The XML document.
66      */

67     private Document JavaDoc doc;
68     /**
69      * The wrapper for the whole testsuite.
70      */

71     private Element JavaDoc rootElement;
72     /**
73      * Element for the current test.
74      */

75     private Hashtable JavaDoc testElements = new Hashtable JavaDoc();
76     /**
77      * tests that failed.
78      */

79     private Hashtable JavaDoc failedTests = new Hashtable JavaDoc();
80     /**
81      * Timing helper.
82      */

83     private Hashtable JavaDoc testStarts = new Hashtable JavaDoc();
84     /**
85      * Where to write the log to.
86      */

87     private OutputStream JavaDoc out;
88
89     /** No arg constructor. */
90     public XMLJUnitResultFormatter() {
91     }
92
93     /** {@inheritDoc}. */
94     public void setOutput(OutputStream JavaDoc out) {
95         this.out = out;
96     }
97
98     /** {@inheritDoc}. */
99     public void setSystemOutput(String JavaDoc out) {
100         formatOutput(SYSTEM_OUT, out);
101     }
102
103     /** {@inheritDoc}. */
104     public void setSystemError(String JavaDoc out) {
105         formatOutput(SYSTEM_ERR, out);
106     }
107
108     /**
109      * The whole testsuite started.
110      * @param suite the testsuite.
111      */

112     public void startTestSuite(JUnitTest suite) {
113         doc = getDocumentBuilder().newDocument();
114         rootElement = doc.createElement(TESTSUITE);
115         String JavaDoc n = suite.getName();
116         rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n);
117
118         //add the timestamp
119
final String JavaDoc timestamp = DateUtils.format(new Date JavaDoc(),
120                 DateUtils.ISO8601_DATETIME_PATTERN);
121         rootElement.setAttribute(TIMESTAMP, timestamp);
122         //and the hostname.
123
rootElement.setAttribute(HOSTNAME, getHostname());
124
125         // Output properties
126
Element JavaDoc propsElement = doc.createElement(PROPERTIES);
127         rootElement.appendChild(propsElement);
128         Properties JavaDoc props = suite.getProperties();
129         if (props != null) {
130             Enumeration JavaDoc e = props.propertyNames();
131             while (e.hasMoreElements()) {
132                 String JavaDoc name = (String JavaDoc) e.nextElement();
133                 Element JavaDoc propElement = doc.createElement(PROPERTY);
134                 propElement.setAttribute(ATTR_NAME, name);
135                 propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
136                 propsElement.appendChild(propElement);
137             }
138         }
139     }
140
141     /**
142      * get the local hostname
143      * @return the name of the local host, or "localhost" if we cannot work it out
144      */

145     private String JavaDoc getHostname() {
146         try {
147             return InetAddress.getLocalHost().getHostName();
148         } catch (UnknownHostException JavaDoc e) {
149             return "localhost";
150         }
151     }
152
153     /**
154      * The whole testsuite ended.
155      * @param suite the testsuite.
156      * @throws BuildException on error.
157      */

158     public void endTestSuite(JUnitTest suite) throws BuildException {
159         rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
160         rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount());
161         rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
162         rootElement.setAttribute(ATTR_TIME, "" + (suite.getRunTime() / 1000.0));
163         if (out != null) {
164             Writer JavaDoc wri = null;
165             try {
166                 wri = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out, "UTF8"));
167                 wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
168                 (new DOMElementWriter()).write(rootElement, wri, 0, " ");
169                 wri.flush();
170             } catch (IOException JavaDoc exc) {
171                 throw new BuildException("Unable to write log file", exc);
172             } finally {
173                 if (out != System.out && out != System.err) {
174                     FileUtils.close(wri);
175                 }
176             }
177         }
178     }
179
180     /**
181      * Interface TestListener.
182      *
183      * <p>A new Test is started.
184      * @param t the test.
185      */

186     public void startTest(Test t) {
187         testStarts.put(t, new Long JavaDoc(System.currentTimeMillis()));
188     }
189
190     /**
191      * Interface TestListener.
192      *
193      * <p>A Test is finished.
194      * @param test the test.
195      */

196     public void endTest(Test test) {
197         // Fix for bug #5637 - if a junit.extensions.TestSetup is
198
// used and throws an exception during setUp then startTest
199
// would never have been called
200
if (!testStarts.containsKey(test)) {
201             startTest(test);
202         }
203
204         Element JavaDoc currentTest = null;
205         if (!failedTests.containsKey(test)) {
206             currentTest = doc.createElement(TESTCASE);
207             String JavaDoc n = JUnitVersionHelper.getTestCaseName(test);
208             currentTest.setAttribute(ATTR_NAME,
209                                      n == null ? UNKNOWN : n);
210             // a TestSuite can contain Tests from multiple classes,
211
// even tests with the same name - disambiguate them.
212
currentTest.setAttribute(ATTR_CLASSNAME,
213                     JUnitVersionHelper.getTestCaseClassName(test));
214             rootElement.appendChild(currentTest);
215             testElements.put(test, currentTest);
216         } else {
217             currentTest = (Element JavaDoc) testElements.get(test);
218         }
219
220         Long JavaDoc l = (Long JavaDoc) testStarts.get(test);
221         currentTest.setAttribute(ATTR_TIME,
222             "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0));
223     }
224
225     /**
226      * Interface TestListener for JUnit &lt;= 3.4.
227      *
228      * <p>A Test failed.
229      * @param test the test.
230      * @param t the exception.
231      */

232     public void addFailure(Test test, Throwable JavaDoc t) {
233         formatError(FAILURE, test, t);
234     }
235
236     /**
237      * Interface TestListener for JUnit &gt; 3.4.
238      *
239      * <p>A Test failed.
240      * @param test the test.
241      * @param t the assertion.
242      */

243     public void addFailure(Test test, AssertionFailedError t) {
244         addFailure(test, (Throwable JavaDoc) t);
245     }
246
247     /**
248      * Interface TestListener.
249      *
250      * <p>An error occurred while running the test.
251      * @param test the test.
252      * @param t the error.
253      */

254     public void addError(Test test, Throwable JavaDoc t) {
255         formatError(ERROR, test, t);
256     }
257
258     private void formatError(String JavaDoc type, Test test, Throwable JavaDoc t) {
259         if (test != null) {
260             endTest(test);
261             failedTests.put(test, test);
262         }
263
264         Element JavaDoc nested = doc.createElement(type);
265         Element JavaDoc currentTest = null;
266         if (test != null) {
267             currentTest = (Element JavaDoc) testElements.get(test);
268         } else {
269             currentTest = rootElement;
270         }
271
272         currentTest.appendChild(nested);
273
274         String JavaDoc message = t.getMessage();
275         if (message != null && message.length() > 0) {
276             nested.setAttribute(ATTR_MESSAGE, t.getMessage());
277         }
278         nested.setAttribute(ATTR_TYPE, t.getClass().getName());
279
280         String JavaDoc strace = JUnitTestRunner.getFilteredTrace(t);
281         Text JavaDoc trace = doc.createTextNode(strace);
282         nested.appendChild(trace);
283     }
284
285     private void formatOutput(String JavaDoc type, String JavaDoc output) {
286         Element JavaDoc nested = doc.createElement(type);
287         rootElement.appendChild(nested);
288         nested.appendChild(doc.createCDATASection(output));
289     }
290
291 } // XMLJUnitResultFormatter
292
Popular Tags