KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > TCXMLJUnitFormatter


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.test;
5
6 import org.w3c.dom.Text JavaDoc;
7 import org.w3c.dom.Element JavaDoc;
8 import org.w3c.dom.Document JavaDoc;
9
10 import org.apache.tools.ant.util.DOMElementWriter;
11 import org.apache.tools.ant.taskdefs.optional.junit.XMLConstants;
12 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner;
13 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
14 import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
15 import org.apache.tools.ant.BuildException;
16
17 import junit.framework.TestCase;
18 import junit.framework.Test;
19 import junit.framework.AssertionFailedError;
20
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.io.Writer JavaDoc;
26 import java.io.OutputStreamWriter JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.FileOutputStream JavaDoc;
30 import java.io.FileNotFoundException JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.BufferedWriter JavaDoc;
33
34 /**
35  * This formatter is used to generate a 'pre-condition' log --- if the TestCase shuts the VM down (eg: deliberately
36  * calling System.exit()) then we'll at least have a log file that indicate that the TestSuite did not complete
37  * gracefully, with a record of which TestCase was last run...
38  *
39  * @author Juris Galang
40  */

41 public class TCXMLJUnitFormatter
42    implements JUnitResultFormatter, XMLConstants {
43
44   /**
45    */

46   private JUnitTest suite;
47
48   /**
49    */

50   private File JavaDoc logfile;
51
52   /**
53    * The XML document.
54    */

55   private Document JavaDoc doc;
56
57   /**
58    * The wrapper for the whole testsuite.
59    */

60   private Element JavaDoc rootElement;
61
62   /** constant for unnnamed testsuites/cases */
63   private static final String JavaDoc UNKNOWN = "unknown";
64
65   /**
66    */

67   public void startTest(Test test) {
68     //logfile = new File("TEST-" + this.suite.getName() + "-" + ((TestCase) test).getName() + ".xml");
69
logfile = new File JavaDoc("TEST-" + this.suite.getName() + ".xml");
70
71     loadDocument();
72     Throwable JavaDoc t = new TestSuiteAbortedException("TestSuite '" + suite.getName()
73                                                 + "' abnormally terminated in test case: '"
74                                                 + ((TestCase) test).getName() + "'");
75     Element JavaDoc nested = doc.createElement(FAILURE);
76     Element JavaDoc currentTest = rootElement;
77
78     currentTest.appendChild(nested);
79
80     String JavaDoc message = t.getMessage();
81     if (message != null && message.length() > 0) {
82       nested.setAttribute(ATTR_MESSAGE, t.getMessage());
83     }
84     nested.setAttribute(ATTR_TYPE, t.getClass().getName());
85
86     String JavaDoc strace = JUnitTestRunner.getFilteredTrace(t);
87     Text JavaDoc trace = doc.createTextNode(strace);
88     nested.appendChild(trace);
89     saveDocument();
90   }
91
92   /**
93    */

94   public void endTest(Test test) {
95     logfile.delete();
96   }
97
98   private void loadDocument() {
99     try {
100       doc = logfile.exists() ? getDocumentBuilder().parse(logfile) : getDocumentBuilder().newDocument();
101       if (logfile.exists()) {
102         rootElement = (Element JavaDoc) doc.getElementsByTagName(TESTSUITE).item(0);
103         return;
104       }
105
106       rootElement = doc.createElement(TESTSUITE);
107       String JavaDoc n = suite.getName();
108       rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n);
109
110       Element JavaDoc propsElement = doc.createElement(PROPERTIES);
111       rootElement.appendChild(propsElement);
112       Properties JavaDoc props = suite.getProperties();
113       if (props != null) {
114         Enumeration JavaDoc e = props.propertyNames();
115         while (e.hasMoreElements()) {
116           String JavaDoc name = (String JavaDoc) e.nextElement();
117           Element JavaDoc propElement = doc.createElement(PROPERTY);
118           propElement.setAttribute(ATTR_NAME, name);
119           propElement.setAttribute(ATTR_VALUE, props.getProperty(name));
120           propsElement.appendChild(propElement);
121         }
122       }
123     } catch (java.io.IOException JavaDoc ioe) {
124       throw new BuildException("Unable to append to log file", ioe);
125     } catch (org.xml.sax.SAXException JavaDoc saxe) {
126       throw new BuildException("Unable to append to log file", saxe);
127     }
128   }
129
130   private void saveDocument() {
131     rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount());
132     rootElement.setAttribute(ATTR_FAILURES, "" + (suite.failureCount() + 1));
133     rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount());
134     rootElement.setAttribute(ATTR_TIME, "" + (suite.getRunTime() / 1000.0));
135     try {
136       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(logfile, true);
137       if (out != null) {
138         Writer wri = null;
139         try {
140           wri = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(out, "UTF8"));
141           wri.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
142           (new DOMElementWriter()).write(rootElement, wri, 0, " ");
143           wri.flush();
144           rootElement = null;
145         } catch (IOException JavaDoc exc) {
146           throw new BuildException("Unable to write log file", exc);
147         } finally {
148           if (wri != null) {
149             try {
150               wri.close();
151             } catch (IOException JavaDoc e) {
152               // ignore
153
}
154           }
155         }
156       }
157     } catch (FileNotFoundException JavaDoc fnfe) {
158       throw new BuildException("Unable to create log file", fnfe);
159     }
160   }
161
162   /**
163    */

164   public void startTestSuite(JUnitTest test) {
165     this.suite = test;
166   }
167
168   /**
169    */

170   public void endTestSuite(JUnitTest test) {
171     //
172
}
173
174   private static DocumentBuilder JavaDoc getDocumentBuilder() {
175     try {
176       return DocumentBuilderFactory.newInstance().newDocumentBuilder();
177     } catch (Exception JavaDoc exc) {
178       throw new ExceptionInInitializerError JavaDoc(exc);
179     }
180   }
181
182   private class TestSuiteAbortedException extends Throwable JavaDoc {
183     public TestSuiteAbortedException(String JavaDoc message) {
184       super(message);
185     }
186
187     public String JavaDoc getMessage() {
188       return super.getMessage();
189     }
190
191   }
192
193   public void setOutput(OutputStream JavaDoc out) {
194     //
195
}
196
197   public void setSystemOutput(String JavaDoc out) {
198     //
199
}
200
201   public void setSystemError(String JavaDoc out) {
202     //
203
}
204
205   public void addError(Test test, java.lang.Throwable JavaDoc t) {
206     //
207
}
208
209   public void addFailure(Test test, AssertionFailedError t) {
210     //
211
}
212 } // TCXMLJUnitFormatter
213

214
Popular Tags