xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/XMLReporter.java (revision 5980243063f6840bf3a2e60b2243db5da5e78fa6)
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 import org.w3c.dom.NodeList;
36 
37 /**
38  * Generate XML test report
39  *
40  */
41 public class XMLReporter extends RunListener {
42 
43     private File outputDir = Testspace.getFile("output");
44 
45     private File file = null;
46 
47     private Document doc = null;
48 
49     private Element testsuiteEl = null;
50 
51     private Element testcaseEl = null;
52 
53     private String suiteName = null;
54 
55     private long suiteStart = 0;
56 
57     private long failures = 0;
58 
59     private long errors = 0;
60 
61     private long tests = 0;
62 
63     private long ignored = 0;
64 
65     private long testStart = 0;
66 
67     @Override
68     public void testStarted(Description description) throws Exception {
69 //      if (!description.getClassName().equals(testClassName)) {
70 //          finishSuite();
71 //          startSuite(description);
72 //          testClassName = description.getClassName();
73 //      }
74         testcaseEl = doc.createElement("testcase");
75         testcaseEl.setAttribute("name", description.getDisplayName());
76         testcaseEl.setAttribute("classname", description.getClassName());
77         testcaseEl.setAttribute("methodname", description.getMethodName());
78         testsuiteEl.appendChild(testcaseEl);
79         testStart = System.currentTimeMillis();
80     }
81 
82     @Override
83     public void testAssumptionFailure(Failure failure) {
84 
85     }
86 
87     @Override
88     public void testFailure(Failure failure) throws Exception {
89         if (failure.getException() instanceof AssertionError) {
90             failures++;
91             Element failureEl = doc.createElement("failure");
92             failureEl.setAttribute("message", failure.getMessage());
93             failureEl.setAttribute("type", failure.getTestHeader());
94             failureEl.setTextContent(failure.getTrace());
95             testcaseEl.appendChild(failureEl);
96         } else {
97             errors++;
98             Element errorEl = doc.createElement("error");
99             errorEl.setAttribute("message", failure.getMessage());
100             errorEl.setAttribute("type", failure.getTestHeader());
101             errorEl.setTextContent(failure.getTrace());
102             testcaseEl.appendChild(errorEl);
103         }
104     }
105 
106     @Override
107     public void testFinished(Description description) throws Exception {
108         tests++;
109         testcaseEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0));
110         store();
111     }
112 
113     @Override
114     public void testIgnored(Description description) throws Exception {
115         testStarted(description);
116         ignored++;
117         Ignore ignore = description.getAnnotation(Ignore.class);
118         Element ignoredEl = doc.createElement("ignored");
119         ignoredEl.setAttribute("message", ignore.value());
120         testcaseEl.appendChild(ignoredEl);
121         testFinished(description);
122     }
123 
124     @Override
125     public void testRunFinished(Result result) throws Exception {
126         finishSuite();
127         File outputBackupDir = new File(outputDir.getAbsolutePath() + "." + suiteName);
128         if (outputBackupDir.exists()) {
129             outputBackupDir.renameTo(new File(outputBackupDir.getAbsolutePath() + "." + System.currentTimeMillis()));
130             FileUtil.deleteFile(outputBackupDir);
131         }
132 
133         outputDir.renameTo(outputBackupDir);
134     }
135 
136     @Override
137     public void testRunStarted(Description description) throws Exception {
138         suiteName = description.getDisplayName();
139         FileUtil.deleteFile(outputDir);//clear all old output
140         startSuite();
141     }
142 
143     private void startSuite() {
144         suiteStart = System.currentTimeMillis();
145         failures = 0;
146         errors = 0;
147         tests = 0;
148         ignored = 0;
149 
150         file = new File(outputDir, "result.xml");
151         doc = FileUtil.newXML();
152 
153         testsuiteEl = doc.createElement("testsuite");
154         testsuiteEl.setAttribute("name", suiteName);
155         testsuiteEl.setAttribute("start", Long.toString(suiteStart));
156         doc.appendChild(testsuiteEl);
157     }
158 
159     private void finishSuite() {
160         store();
161     }
162 
163     private void store() {
164         if (doc != null) {
165             testsuiteEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0));
166             testsuiteEl.setAttribute("failures", Long.toString(failures));
167             testsuiteEl.setAttribute("errors", Long.toString(errors));
168             testsuiteEl.setAttribute("tests", Long.toString(tests));
169             testsuiteEl.setAttribute("ignored", Long.toString(ignored));
170             NodeList els = testsuiteEl.getElementsByTagName("properties");
171             if (els.getLength() > 0)
172                 testsuiteEl.removeChild(els.item(0));
173 
174             Element props = doc.createElement("properties");
175             testsuiteEl.appendChild(props);
176             // Add some extra information
177             System.setProperty("info.os.name", SystemUtil.getOSName());
178             System.setProperty("info.os.version", SystemUtil.getOSVersion());
179             System.setProperty("info.os.arch", SystemUtil.getOSArch());
180             String ipaddrStr = SystemUtil.getIPAddress();
181             System.setProperty("info.ip", (ipaddrStr!=null) ? ipaddrStr : "UNKNOWN");
182             String hostnameStr = SystemUtil.getHostName();
183             System.setProperty("info.hostname", (hostnameStr!=null) ? hostnameStr : "UNKNOWN");
184             Set<Entry<Object, Object>> entries = System.getProperties().entrySet();
185             for (Entry<Object, Object> e : entries) {
186                 Element prop = doc.createElement("property");
187                 prop.setAttribute("name", "" + e.getKey());
188                 prop.setAttribute("value", "" + e.getValue());
189                 props.appendChild(prop);
190             }
191 
192             FileUtil.storeXML(doc, file);
193             File htmlFile = new File(outputDir, "result.html");
194             InputStream is = getClass().getResourceAsStream("XMLReporter.xsl");
195             if (is != null) {
196                 FileUtil.storeXML(doc, htmlFile, is);
197             }
198         }
199     }
200 
201 }
202 
203