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