1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 import java.io.BufferedWriter ; 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.OutputStreamWriter ; 25 import java.io.Writer ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import java.util.Date ; 30 import java.net.InetAddress ; 31 import java.net.UnknownHostException ; 32 import javax.xml.parsers.DocumentBuilder ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 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 ; 41 import org.w3c.dom.Element ; 42 import org.w3c.dom.Text ; 43 44 45 50 51 public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants { 52 53 54 private static final String UNKNOWN = "unknown"; 55 56 private static DocumentBuilder getDocumentBuilder() { 57 try { 58 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 59 } catch (Exception exc) { 60 throw new ExceptionInInitializerError (exc); 61 } 62 } 63 64 67 private Document doc; 68 71 private Element rootElement; 72 75 private Hashtable testElements = new Hashtable (); 76 79 private Hashtable failedTests = new Hashtable (); 80 83 private Hashtable testStarts = new Hashtable (); 84 87 private OutputStream out; 88 89 90 public XMLJUnitResultFormatter() { 91 } 92 93 94 public void setOutput(OutputStream out) { 95 this.out = out; 96 } 97 98 99 public void setSystemOutput(String out) { 100 formatOutput(SYSTEM_OUT, out); 101 } 102 103 104 public void setSystemError(String out) { 105 formatOutput(SYSTEM_ERR, out); 106 } 107 108 112 public void startTestSuite(JUnitTest suite) { 113 doc = getDocumentBuilder().newDocument(); 114 rootElement = doc.createElement(TESTSUITE); 115 String n = suite.getName(); 116 rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n); 117 118 final String timestamp = DateUtils.format(new Date (), 120 DateUtils.ISO8601_DATETIME_PATTERN); 121 rootElement.setAttribute(TIMESTAMP, timestamp); 122 rootElement.setAttribute(HOSTNAME, getHostname()); 124 125 Element propsElement = doc.createElement(PROPERTIES); 127 rootElement.appendChild(propsElement); 128 Properties props = suite.getProperties(); 129 if (props != null) { 130 Enumeration e = props.propertyNames(); 131 while (e.hasMoreElements()) { 132 String name = (String ) e.nextElement(); 133 Element 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 145 private String getHostname() { 146 try { 147 return InetAddress.getLocalHost().getHostName(); 148 } catch (UnknownHostException e) { 149 return "localhost"; 150 } 151 } 152 153 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 wri = null; 165 try { 166 wri = new BufferedWriter (new OutputStreamWriter (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 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 186 public void startTest(Test t) { 187 testStarts.put(t, new Long (System.currentTimeMillis())); 188 } 189 190 196 public void endTest(Test test) { 197 if (!testStarts.containsKey(test)) { 201 startTest(test); 202 } 203 204 Element currentTest = null; 205 if (!failedTests.containsKey(test)) { 206 currentTest = doc.createElement(TESTCASE); 207 String n = JUnitVersionHelper.getTestCaseName(test); 208 currentTest.setAttribute(ATTR_NAME, 209 n == null ? UNKNOWN : n); 210 currentTest.setAttribute(ATTR_CLASSNAME, 213 JUnitVersionHelper.getTestCaseClassName(test)); 214 rootElement.appendChild(currentTest); 215 testElements.put(test, currentTest); 216 } else { 217 currentTest = (Element ) testElements.get(test); 218 } 219 220 Long l = (Long ) testStarts.get(test); 221 currentTest.setAttribute(ATTR_TIME, 222 "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0)); 223 } 224 225 232 public void addFailure(Test test, Throwable t) { 233 formatError(FAILURE, test, t); 234 } 235 236 243 public void addFailure(Test test, AssertionFailedError t) { 244 addFailure(test, (Throwable ) t); 245 } 246 247 254 public void addError(Test test, Throwable t) { 255 formatError(ERROR, test, t); 256 } 257 258 private void formatError(String type, Test test, Throwable t) { 259 if (test != null) { 260 endTest(test); 261 failedTests.put(test, test); 262 } 263 264 Element nested = doc.createElement(type); 265 Element currentTest = null; 266 if (test != null) { 267 currentTest = (Element ) testElements.get(test); 268 } else { 269 currentTest = rootElement; 270 } 271 272 currentTest.appendChild(nested); 273 274 String 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 strace = JUnitTestRunner.getFilteredTrace(t); 281 Text trace = doc.createTextNode(strace); 282 nested.appendChild(trace); 283 } 284 285 private void formatOutput(String type, String output) { 286 Element nested = doc.createElement(type); 287 rootElement.appendChild(nested); 288 nested.appendChild(doc.createCDATASection(output)); 289 } 290 291 } | Popular Tags |