xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/XMLReporter.java (revision 278534e3e1f900bdd012e51ba483d2b885a5d2f7)
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 			outputBackupDir.renameTo(new File(outputBackupDir.getAbsolutePath() + "." + System.currentTimeMillis()));
125 			FileUtil.deleteFile(outputBackupDir);
126 		}
127 
128 		outputDir.renameTo(outputBackupDir);
129 	}
130 
131 	@Override
132 	public void testRunStarted(Description description) throws Exception {
133 		suiteName = description.getDisplayName();
134 		FileUtil.deleteFile(outputDir);//clear all old output
135 		startSuite();
136 	}
137 
138 	private void startSuite() {
139 		suiteStart = System.currentTimeMillis();
140 		failures = 0;
141 		errors = 0;
142 		tests = 0;
143 		ignored = 0;
144 
145 		file = new File(outputDir, "result.xml");
146 		doc = FileUtil.newXML();
147 
148 		testsuiteEl = doc.createElement("testsuite");
149 		testsuiteEl.setAttribute("name", suiteName);
150 		testsuiteEl.setAttribute("start", Long.toString(suiteStart));
151 		doc.appendChild(testsuiteEl);
152 	}
153 
154 	private void finishSuite() {
155 		testsuiteEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0));
156 		testsuiteEl.setAttribute("failures", Long.toString(failures));
157 		testsuiteEl.setAttribute("errors", Long.toString(errors));
158 		testsuiteEl.setAttribute("tests", Long.toString(tests));
159 		testsuiteEl.setAttribute("ignored", Long.toString(ignored));
160 		Element props = doc.createElement("properties");
161 		testsuiteEl.appendChild(props);
162 		// Add some extra information
163 		System.setProperty("info.os.name", SystemUtil.getOSName());
164 		System.setProperty("info.os.version", SystemUtil.getOSVersion());
165 		System.setProperty("info.os.arch", SystemUtil.getOSArch());
166 		System.setProperty("info.os.arch", SystemUtil.getOSArch());
167 		System.setProperty("info.ip", SystemUtil.getIPAddress());
168 		System.setProperty("info.hostname", SystemUtil.getHostName());
169 		Set<Entry<Object, Object>> entries = System.getProperties().entrySet();
170 		for (Entry<Object, Object> e : entries) {
171 			Element prop = doc.createElement("property");
172 			prop.setAttribute("name", "" + e.getKey());
173 			prop.setAttribute("value", "" + e.getValue());
174 			props.appendChild(prop);
175 		}
176 
177 		store();
178 	}
179 
180 	private void store() {
181 		if (doc != null) {
182 			FileUtil.storeXML(doc, file);
183 			File htmlFile = new File(outputDir, "result.html");
184 			InputStream is = getClass().getResourceAsStream("XMLReporter.xsl");
185 			if (is != null) {
186 				FileUtil.storeXML(doc, htmlFile, is);
187 			}
188 		}
189 	}
190 
191 }
192