1 package org.openoffice.test.common; 2 3 import java.io.File; 4 import java.io.InputStream; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 import org.junit.Ignore; 9 import org.junit.runner.Description; 10 import org.junit.runner.Result; 11 import org.junit.runner.notification.Failure; 12 import org.junit.runner.notification.RunListener; 13 import org.w3c.dom.Document; 14 import org.w3c.dom.Element; 15 16 public class XMLReporter extends RunListener { 17 18 private File outputDir = Testspace.getFile("output"); 19 20 private File reportDir = new File(outputDir, "result"); 21 22 private File file = null; 23 24 private Document doc = null; 25 26 private Element testsuiteEl = null; 27 28 private Element testcaseEl = null; 29 30 private String suiteName = null; 31 32 private long suiteStart = 0; 33 34 private long failures = 0; 35 36 private long errors = 0; 37 38 private long tests = 0; 39 40 private long ignored = 0; 41 42 private long testStart = 0; 43 44 @Override 45 public void testStarted(Description description) throws Exception { 46 // if (!description.getClassName().equals(testClassName)) { 47 // finishSuite(); 48 // startSuite(description); 49 // testClassName = description.getClassName(); 50 // } 51 testcaseEl = doc.createElement("testcase"); 52 testcaseEl.setAttribute("name", description.getDisplayName()); 53 testcaseEl.setAttribute("classname", description.getClassName()); 54 testcaseEl.setAttribute("methodname", description.getMethodName()); 55 testsuiteEl.appendChild(testcaseEl); 56 testStart = System.currentTimeMillis(); 57 } 58 59 @Override 60 public void testAssumptionFailure(Failure failure) { 61 62 } 63 64 @Override 65 public void testFailure(Failure failure) throws Exception { 66 if (failure.getException() instanceof AssertionError) { 67 failures++; 68 Element failureEl = doc.createElement("failure"); 69 failureEl.setAttribute("message", failure.getMessage()); 70 failureEl.setAttribute("type", failure.getTestHeader()); 71 failureEl.setTextContent(failure.getTrace()); 72 testcaseEl.appendChild(failureEl); 73 } else { 74 errors++; 75 Element errorEl = doc.createElement("error"); 76 errorEl.setAttribute("message", failure.getMessage()); 77 errorEl.setAttribute("type", failure.getTestHeader()); 78 errorEl.setTextContent(failure.getTrace()); 79 testcaseEl.appendChild(errorEl); 80 } 81 } 82 83 @Override 84 public void testFinished(Description description) throws Exception { 85 tests++; 86 testcaseEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0)); 87 store(); 88 } 89 90 @Override 91 public void testIgnored(Description description) throws Exception { 92 testStarted(description); 93 ignored++; 94 Ignore ignore = description.getAnnotation(Ignore.class); 95 Element ignoredEl = doc.createElement("ignored"); 96 ignoredEl.setAttribute("message", ignore.value()); 97 testcaseEl.appendChild(ignoredEl); 98 testFinished(description); 99 } 100 101 @Override 102 public void testRunFinished(Result result) throws Exception { 103 finishSuite(); 104 File outputBackupDir = new File(outputDir.getAbsolutePath() + "." + suiteName); 105 if (outputBackupDir.exists()) { 106 FileUtil.deleteFile(outputBackupDir); 107 } 108 109 outputDir.renameTo(outputBackupDir); 110 } 111 112 @Override 113 public void testRunStarted(Description description) throws Exception { 114 suiteName = description.getDisplayName(); 115 FileUtil.deleteFile(outputDir);//clear all old output 116 startSuite(); 117 } 118 119 private void startSuite() { 120 suiteStart = System.currentTimeMillis(); 121 failures = 0; 122 errors = 0; 123 tests = 0; 124 ignored = 0; 125 126 file = new File(reportDir, suiteName + ".xml"); 127 doc = FileUtil.newXML(); 128 129 testsuiteEl = doc.createElement("testsuite"); 130 testsuiteEl.setAttribute("name", suiteName); 131 testsuiteEl.setAttribute("start", Long.toString(suiteStart)); 132 doc.appendChild(testsuiteEl); 133 } 134 135 private void finishSuite() { 136 testsuiteEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0)); 137 testsuiteEl.setAttribute("failures", Long.toString(failures)); 138 testsuiteEl.setAttribute("errors", Long.toString(errors)); 139 testsuiteEl.setAttribute("tests", Long.toString(tests)); 140 testsuiteEl.setAttribute("ignored", Long.toString(ignored)); 141 Element props = doc.createElement("properties"); 142 testsuiteEl.appendChild(props); 143 Set<Entry<Object, Object>> entries = System.getProperties().entrySet(); 144 for (Entry<Object, Object> e : entries) { 145 Element prop = doc.createElement("property"); 146 prop.setAttribute("name", "" + e.getKey()); 147 prop.setAttribute("value", "" + e.getValue()); 148 props.appendChild(prop); 149 } 150 151 store(); 152 } 153 154 private void store() { 155 if (doc != null) { 156 FileUtil.storeXML(doc, file); 157 File htmlFile = new File(reportDir, file.getName() + ".html"); 158 InputStream is = getClass().getResourceAsStream("XMLReporter.xsl"); 159 if (is != null) { 160 FileUtil.storeXML(doc, htmlFile, is); 161 } 162 } 163 } 164 165 } 166