KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > ant > JunitFormatter


1 /**
2  * Copyright 2000-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * --------------------------------------------------------------------------
17  * $Id: JunitFormatter.java,v 1.1 2005/02/16 14:24:38 benoitf Exp $
18  * --------------------------------------------------------------------------
19  */

20 package org.objectweb.carol.ant;
21
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.OutputStreamWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import javax.xml.parsers.DocumentBuilder JavaDoc;
32 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
33
34 import junit.framework.AssertionFailedError;
35 import junit.framework.Test;
36
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.Element JavaDoc;
39 import org.w3c.dom.Text JavaDoc;
40
41 import org.apache.tools.ant.BuildException;
42 import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
43 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
44 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner;
45 import org.apache.tools.ant.taskdefs.optional.junit.JUnitVersionHelper;
46 import org.apache.tools.ant.taskdefs.optional.junit.XMLConstants;
47 import org.apache.tools.ant.util.DOMElementWriter;
48
49 /**
50  * Use our own formatter (based on XML formatter). This is to prefix test name
51  * as all our tests have the same name but we run it with different
52  * configurations Note : cannot extends super class as all is private !!!
53  * Version comes from ANT 1.6.2 version Changes made are prefix by : // ###OW
54  * changes
55  */

56 public class JunitFormatter implements JUnitResultFormatter, XMLConstants {
57
58     private static DocumentBuilder JavaDoc getDocumentBuilder() {
59         try {
60             return DocumentBuilderFactory.newInstance().newDocumentBuilder();
61         } catch (Exception JavaDoc exc) {
62             throw new ExceptionInInitializerError JavaDoc(exc);
63         }
64     }
65
66     /**
67      * The XML document.
68      */

69     private Document JavaDoc doc;
70
71     /**
72      * The wrapper for the whole testsuite.
73      */

74     private Element JavaDoc rootElement;
75
76     /**
77      * Element for the current test.
78      */

79     private Hashtable JavaDoc testElements = new Hashtable JavaDoc();
80
81     /**
82      * tests that failed.
83      */

84     private Hashtable JavaDoc failedTests = new Hashtable JavaDoc();
85
86     /**
87      * Timing helper.
88      */

89     private Hashtable JavaDoc testStarts = new Hashtable JavaDoc();
90
91     // ###OW changes
92
/**
93      * Name of test (test.name) property
94      */

95     private String JavaDoc testName = null;
96
97     /**
98      * Mode of test (test.mode) property (parallel1 or parallel2)
99      */

100     private String JavaDoc testMode = null;
101
102     // #OW changes###
103

104     /**
105      * Where to write the log to.
106      */

107     private OutputStream JavaDoc out;
108
109     public JunitFormatter() {
110     }
111
112     public void setOutput(OutputStream JavaDoc out) {
113         this.out = out;
114     }
115
116     public void setSystemOutput(String JavaDoc out) {
117         formatOutput(SYSTEM_OUT, out);
118     }
119
120     public void setSystemError(String JavaDoc out) {
121         formatOutput(SYSTEM_ERR, out);
122     }
123
124     /**
125      * The whole testsuite started.
126      * @param suite the test suite
127      */

128     public void startTestSuite(JUnitTest suite) {
129         doc = getDocumentBuilder().newDocument();
130         rootElement = doc.createElement(TESTSUITE);
131         rootElement.setAttribute(ATTR_NAME, suite.getName());
132
133         // Output properties
134
Element JavaDoc propsElement = doc.createElement(PROPERTIES);
135         rootElement.appendChild(propsElement);
136         Properties JavaDoc props = suite.getProperties();
137         if (props != null) {
138             // ###OW changes
139
// get test.name property
140
testName = props.getProperty("test.name");
141
142             testMode = props.getProperty("test.mode", "parallel");
143
144             if (testName != null) {
145                 rootElement.setAttribute(ATTR_NAME, suite.getName() + testName.replace('.', '-') + "-" + testMode);
146             }
147             // OW changes###
148

149             Enumeration JavaDoc e = props.propertyNames();
150             while (e.hasMoreElements()) {
151                 String JavaDoc name = (String JavaDoc) e.nextElement();
152                 Element JavaDoc propElement = doc.createElement(PROPERTY);
153                 propElement.setAttribute(ATTR_NAME, name);
154                 propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
155                 propsElement.appendChild(propElement);
156             }
157         }
158     }
159
160     /**
161      * The whole testsuite ended.
162      * @param suite the test suite
163      * @throws BuildException if it fails
164      */

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

199     public void startTest(Test t) {
200         testStarts.put(t, new Long JavaDoc(System.currentTimeMillis()));
201     }
202
203     /**
204      * Interface TestListener.
205      * <p>
206      * A Test is finished.
207      * @param test finished
208      */

209     public void endTest(Test test) {
210         // Fix for bug #5637 - if a junit.extensions.TestSetup is
211
// used and throws an exception during setUp then startTest
212
// would never have been called
213
if (!testStarts.containsKey(test)) {
214             startTest(test);
215         }
216
217         Element JavaDoc currentTest = null;
218         if (!failedTests.containsKey(test)) {
219             currentTest = doc.createElement(TESTCASE);
220
221             // ###OW changes
222
String JavaDoc tmpTestName = JUnitVersionHelper.getTestCaseName(test);
223             if (testName != null) {
224                 tmpTestName = testName + "-" + testMode + "-" + tmpTestName;
225             }
226             currentTest.setAttribute(ATTR_NAME, tmpTestName);
227             // OW changes###
228

229             // a TestSuite can contain Tests from multiple classes,
230
// even tests with the same name - disambiguate them.
231
currentTest.setAttribute(ATTR_CLASSNAME, test.getClass().getName());
232             rootElement.appendChild(currentTest);
233             testElements.put(test, currentTest);
234         } else {
235             currentTest = (Element JavaDoc) testElements.get(test);
236         }
237
238         Long JavaDoc l = (Long JavaDoc) testStarts.get(test);
239         currentTest.setAttribute(ATTR_TIME, "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0));
240     }
241
242     /**
243      * Interface TestListener for JUnit &lt;= 3.4.
244      * <p>
245      * A Test failed.
246      * @param test test which failed
247      * @param t exception
248      */

249     public void addFailure(Test test, Throwable JavaDoc t) {
250         formatError(FAILURE, test, t);
251     }
252
253     /**
254      * Interface TestListener for JUnit &gt; 3.4.
255      * <p>
256      * A Test failed.
257      * @param test test which failed
258      * @param t the failure
259      */

260     public void addFailure(Test test, AssertionFailedError t) {
261         addFailure(test, (Throwable JavaDoc) t);
262     }
263
264     /**
265      * Interface TestListener.
266      * <p>
267      * An error occurred while running the test.
268      * @param test test which failed
269      * @param t the exception
270      */

271     public void addError(Test test, Throwable JavaDoc t) {
272         formatError(ERROR, test, t);
273     }
274
275     /**
276      * Format the given error
277      * @param type of error
278      * @param test test which made the error
279      * @param t the exception
280      */

281     private void formatError(String JavaDoc type, Test test, Throwable JavaDoc t) {
282         if (test != null) {
283             endTest(test);
284             failedTests.put(test, test);
285         }
286
287         Element JavaDoc nested = doc.createElement(type);
288         Element JavaDoc currentTest = null;
289         if (test != null) {
290             currentTest = (Element JavaDoc) testElements.get(test);
291         } else {
292             currentTest = rootElement;
293         }
294
295         currentTest.appendChild(nested);
296
297         String JavaDoc message = t.getMessage();
298         if (message != null && message.length() > 0) {
299             nested.setAttribute(ATTR_MESSAGE, t.getMessage());
300         }
301         nested.setAttribute(ATTR_TYPE, t.getClass().getName());
302
303         String JavaDoc strace = JUnitTestRunner.getFilteredTrace(t);
304         Text JavaDoc trace = doc.createTextNode(strace);
305         nested.appendChild(trace);
306     }
307
308     /**
309      * Format the output for a given type
310      * @param type given type
311      * @param output section to add
312      */

313     private void formatOutput(String JavaDoc type, String JavaDoc output) {
314         Element JavaDoc nested = doc.createElement(type);
315         rootElement.appendChild(nested);
316         nested.appendChild(doc.createCDATASection(output));
317     }
318 }
Popular Tags