1 4 package com.tc.test; 5 6 import org.w3c.dom.Text ; 7 import org.w3c.dom.Element ; 8 import org.w3c.dom.Document ; 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 ; 22 import javax.xml.parsers.DocumentBuilder ; 23 import java.util.Properties ; 24 import java.util.Enumeration ; 25 import java.io.Writer ; 26 import java.io.OutputStreamWriter ; 27 import java.io.OutputStream ; 28 import java.io.IOException ; 29 import java.io.FileOutputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.File ; 32 import java.io.BufferedWriter ; 33 34 41 public class TCXMLJUnitFormatter 42 implements JUnitResultFormatter, XMLConstants { 43 44 46 private JUnitTest suite; 47 48 50 private File logfile; 51 52 55 private Document doc; 56 57 60 private Element rootElement; 61 62 63 private static final String UNKNOWN = "unknown"; 64 65 67 public void startTest(Test test) { 68 logfile = new File ("TEST-" + this.suite.getName() + ".xml"); 70 71 loadDocument(); 72 Throwable t = new TestSuiteAbortedException("TestSuite '" + suite.getName() 73 + "' abnormally terminated in test case: '" 74 + ((TestCase) test).getName() + "'"); 75 Element nested = doc.createElement(FAILURE); 76 Element currentTest = rootElement; 77 78 currentTest.appendChild(nested); 79 80 String 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 strace = JUnitTestRunner.getFilteredTrace(t); 87 Text trace = doc.createTextNode(strace); 88 nested.appendChild(trace); 89 saveDocument(); 90 } 91 92 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 ) doc.getElementsByTagName(TESTSUITE).item(0); 103 return; 104 } 105 106 rootElement = doc.createElement(TESTSUITE); 107 String n = suite.getName(); 108 rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n); 109 110 Element propsElement = doc.createElement(PROPERTIES); 111 rootElement.appendChild(propsElement); 112 Properties props = suite.getProperties(); 113 if (props != null) { 114 Enumeration e = props.propertyNames(); 115 while (e.hasMoreElements()) { 116 String name = (String ) e.nextElement(); 117 Element 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 ioe) { 124 throw new BuildException("Unable to append to log file", ioe); 125 } catch (org.xml.sax.SAXException 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 out = new FileOutputStream (logfile, true); 137 if (out != null) { 138 Writer wri = null; 139 try { 140 wri = new BufferedWriter (new OutputStreamWriter (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 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 e) { 152 } 154 } 155 } 156 } 157 } catch (FileNotFoundException fnfe) { 158 throw new BuildException("Unable to create log file", fnfe); 159 } 160 } 161 162 164 public void startTestSuite(JUnitTest test) { 165 this.suite = test; 166 } 167 168 170 public void endTestSuite(JUnitTest test) { 171 } 173 174 private static DocumentBuilder getDocumentBuilder() { 175 try { 176 return DocumentBuilderFactory.newInstance().newDocumentBuilder(); 177 } catch (Exception exc) { 178 throw new ExceptionInInitializerError (exc); 179 } 180 } 181 182 private class TestSuiteAbortedException extends Throwable { 183 public TestSuiteAbortedException(String message) { 184 super(message); 185 } 186 187 public String getMessage() { 188 return super.getMessage(); 189 } 190 191 } 192 193 public void setOutput(OutputStream out) { 194 } 196 197 public void setSystemOutput(String out) { 198 } 200 201 public void setSystemError(String out) { 202 } 204 205 public void addError(Test test, java.lang.Throwable t) { 206 } 208 209 public void addFailure(Test test, AssertionFailedError t) { 210 } 212 } 214 | Popular Tags |