1 20 package org.objectweb.carol.ant; 21 22 import java.io.BufferedWriter ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.OutputStreamWriter ; 26 import java.io.Writer ; 27 import java.util.Enumeration ; 28 import java.util.Hashtable ; 29 import java.util.Properties ; 30 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.DocumentBuilderFactory ; 33 34 import junit.framework.AssertionFailedError; 35 import junit.framework.Test; 36 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.Text ; 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 56 public class JunitFormatter implements JUnitResultFormatter, XMLConstants { 57 58 private static DocumentBuilder getDocumentBuilder() { 59 try { 60 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 61 } catch (Exception exc) { 62 throw new ExceptionInInitializerError (exc); 63 } 64 } 65 66 69 private Document doc; 70 71 74 private Element rootElement; 75 76 79 private Hashtable testElements = new Hashtable (); 80 81 84 private Hashtable failedTests = new Hashtable (); 85 86 89 private Hashtable testStarts = new Hashtable (); 90 91 95 private String testName = null; 96 97 100 private String testMode = null; 101 102 104 107 private OutputStream out; 108 109 public JunitFormatter() { 110 } 111 112 public void setOutput(OutputStream out) { 113 this.out = out; 114 } 115 116 public void setSystemOutput(String out) { 117 formatOutput(SYSTEM_OUT, out); 118 } 119 120 public void setSystemError(String out) { 121 formatOutput(SYSTEM_ERR, out); 122 } 123 124 128 public void startTestSuite(JUnitTest suite) { 129 doc = getDocumentBuilder().newDocument(); 130 rootElement = doc.createElement(TESTSUITE); 131 rootElement.setAttribute(ATTR_NAME, suite.getName()); 132 133 Element propsElement = doc.createElement(PROPERTIES); 135 rootElement.appendChild(propsElement); 136 Properties props = suite.getProperties(); 137 if (props != null) { 138 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 149 Enumeration e = props.propertyNames(); 150 while (e.hasMoreElements()) { 151 String name = (String ) e.nextElement(); 152 Element 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 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 wri = null; 172 try { 173 wri = new BufferedWriter (new OutputStreamWriter (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 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 e) { 185 } 187 } 188 } 189 } 190 } 191 } 192 193 199 public void startTest(Test t) { 200 testStarts.put(t, new Long (System.currentTimeMillis())); 201 } 202 203 209 public void endTest(Test test) { 210 if (!testStarts.containsKey(test)) { 214 startTest(test); 215 } 216 217 Element currentTest = null; 218 if (!failedTests.containsKey(test)) { 219 currentTest = doc.createElement(TESTCASE); 220 221 String tmpTestName = JUnitVersionHelper.getTestCaseName(test); 223 if (testName != null) { 224 tmpTestName = testName + "-" + testMode + "-" + tmpTestName; 225 } 226 currentTest.setAttribute(ATTR_NAME, tmpTestName); 227 229 currentTest.setAttribute(ATTR_CLASSNAME, test.getClass().getName()); 232 rootElement.appendChild(currentTest); 233 testElements.put(test, currentTest); 234 } else { 235 currentTest = (Element ) testElements.get(test); 236 } 237 238 Long l = (Long ) testStarts.get(test); 239 currentTest.setAttribute(ATTR_TIME, "" + ((System.currentTimeMillis() - l.longValue()) / 1000.0)); 240 } 241 242 249 public void addFailure(Test test, Throwable t) { 250 formatError(FAILURE, test, t); 251 } 252 253 260 public void addFailure(Test test, AssertionFailedError t) { 261 addFailure(test, (Throwable ) t); 262 } 263 264 271 public void addError(Test test, Throwable t) { 272 formatError(ERROR, test, t); 273 } 274 275 281 private void formatError(String type, Test test, Throwable t) { 282 if (test != null) { 283 endTest(test); 284 failedTests.put(test, test); 285 } 286 287 Element nested = doc.createElement(type); 288 Element currentTest = null; 289 if (test != null) { 290 currentTest = (Element ) testElements.get(test); 291 } else { 292 currentTest = rootElement; 293 } 294 295 currentTest.appendChild(nested); 296 297 String 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 strace = JUnitTestRunner.getFilteredTrace(t); 304 Text trace = doc.createTextNode(strace); 305 nested.appendChild(trace); 306 } 307 308 313 private void formatOutput(String type, String output) { 314 Element nested = doc.createElement(type); 315 rootElement.appendChild(nested); 316 nested.appendChild(doc.createCDATASection(output)); 317 } 318 } | Popular Tags |