1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 package org.openoffice.test.common; 22 23 import java.io.File; 24 import java.io.InputStream; 25 import java.util.Map.Entry; 26 import java.util.Set; 27 28 import org.junit.Ignore; 29 import org.junit.runner.Description; 30 import org.junit.runner.Result; 31 import org.junit.runner.notification.Failure; 32 import org.junit.runner.notification.RunListener; 33 import org.w3c.dom.Document; 34 import org.w3c.dom.Element; 35 36 public class XMLReporter extends RunListener { 37 38 private File outputDir = Testspace.getFile("output"); 39 40 private File file = null; 41 42 private Document doc = null; 43 44 private Element testsuiteEl = null; 45 46 private Element testcaseEl = null; 47 48 private String suiteName = null; 49 50 private long suiteStart = 0; 51 52 private long failures = 0; 53 54 private long errors = 0; 55 56 private long tests = 0; 57 58 private long ignored = 0; 59 60 private long testStart = 0; 61 62 @Override 63 public void testStarted(Description description) throws Exception { 64 // if (!description.getClassName().equals(testClassName)) { 65 // finishSuite(); 66 // startSuite(description); 67 // testClassName = description.getClassName(); 68 // } 69 testcaseEl = doc.createElement("testcase"); 70 testcaseEl.setAttribute("name", description.getDisplayName()); 71 testcaseEl.setAttribute("classname", description.getClassName()); 72 testcaseEl.setAttribute("methodname", description.getMethodName()); 73 testsuiteEl.appendChild(testcaseEl); 74 testStart = System.currentTimeMillis(); 75 } 76 77 @Override 78 public void testAssumptionFailure(Failure failure) { 79 80 } 81 82 @Override 83 public void testFailure(Failure failure) throws Exception { 84 if (failure.getException() instanceof AssertionError) { 85 failures++; 86 Element failureEl = doc.createElement("failure"); 87 failureEl.setAttribute("message", failure.getMessage()); 88 failureEl.setAttribute("type", failure.getTestHeader()); 89 failureEl.setTextContent(failure.getTrace()); 90 testcaseEl.appendChild(failureEl); 91 } else { 92 errors++; 93 Element errorEl = doc.createElement("error"); 94 errorEl.setAttribute("message", failure.getMessage()); 95 errorEl.setAttribute("type", failure.getTestHeader()); 96 errorEl.setTextContent(failure.getTrace()); 97 testcaseEl.appendChild(errorEl); 98 } 99 } 100 101 @Override 102 public void testFinished(Description description) throws Exception { 103 tests++; 104 testcaseEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0)); 105 store(); 106 } 107 108 @Override 109 public void testIgnored(Description description) throws Exception { 110 testStarted(description); 111 ignored++; 112 Ignore ignore = description.getAnnotation(Ignore.class); 113 Element ignoredEl = doc.createElement("ignored"); 114 ignoredEl.setAttribute("message", ignore.value()); 115 testcaseEl.appendChild(ignoredEl); 116 testFinished(description); 117 } 118 119 @Override 120 public void testRunFinished(Result result) throws Exception { 121 finishSuite(); 122 File outputBackupDir = new File(outputDir.getAbsolutePath() + "." + suiteName); 123 if (outputBackupDir.exists()) { 124 FileUtil.deleteFile(outputBackupDir); 125 } 126 127 outputDir.renameTo(outputBackupDir); 128 } 129 130 @Override 131 public void testRunStarted(Description description) throws Exception { 132 suiteName = description.getDisplayName(); 133 FileUtil.deleteFile(outputDir);//clear all old output 134 startSuite(); 135 } 136 137 private void startSuite() { 138 suiteStart = System.currentTimeMillis(); 139 failures = 0; 140 errors = 0; 141 tests = 0; 142 ignored = 0; 143 144 file = new File(outputDir, "result.xml"); 145 doc = FileUtil.newXML(); 146 147 testsuiteEl = doc.createElement("testsuite"); 148 testsuiteEl.setAttribute("name", suiteName); 149 testsuiteEl.setAttribute("start", Long.toString(suiteStart)); 150 doc.appendChild(testsuiteEl); 151 } 152 153 private void finishSuite() { 154 testsuiteEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0)); 155 testsuiteEl.setAttribute("failures", Long.toString(failures)); 156 testsuiteEl.setAttribute("errors", Long.toString(errors)); 157 testsuiteEl.setAttribute("tests", Long.toString(tests)); 158 testsuiteEl.setAttribute("ignored", Long.toString(ignored)); 159 Element props = doc.createElement("properties"); 160 testsuiteEl.appendChild(props); 161 Set<Entry<Object, Object>> entries = System.getProperties().entrySet(); 162 for (Entry<Object, Object> e : entries) { 163 Element prop = doc.createElement("property"); 164 prop.setAttribute("name", "" + e.getKey()); 165 prop.setAttribute("value", "" + e.getValue()); 166 props.appendChild(prop); 167 } 168 169 store(); 170 } 171 172 private void store() { 173 if (doc != null) { 174 FileUtil.storeXML(doc, file); 175 File htmlFile = new File(outputDir, "result.html"); 176 InputStream is = getClass().getResourceAsStream("XMLReporter.xsl"); 177 if (is != null) { 178 FileUtil.storeXML(doc, htmlFile, is); 179 } 180 } 181 } 182 183 } 184