xref: /AOO41X/test/testcommon/source/org/openoffice/test/common/XMLReporter.java (revision f325a5bfe95c01f05d7a63b8d98c3bae4115aa37)
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 import org.w3c.dom.ProcessingInstruction;
16 
17 public class XMLReporter extends RunListener {
18 
19     private File reportDir = Testspace.getFile("output/result");
20 
21     private File file = null;
22 
23     private Document doc = null;
24 
25     private Element testsuiteEl = null;
26 
27     private Element testcaseEl = null;
28 
29     private String testClassName = null;
30 
31     private long suiteStart = 0;
32 
33     private long failures = 0;
34 
35     private long errors = 0;
36 
37     private long tests = 0;
38 
39     private long ignored = 0;
40 
41     private long testStart = 0;
42 
43     @Override
44     public void testStarted(Description description) throws Exception {
45         System.out.println("testStarted");
46         if (!description.getClassName().equals(testClassName)) {
47             finishSuite();
48             startSuite(description);
49         }
50         testcaseEl = doc.createElement("testcase");
51         testcaseEl.setAttribute("name", description.getDisplayName());
52         testcaseEl.setAttribute("classname", description.getClassName());
53         testcaseEl.setAttribute("methodname", description.getMethodName());
54         testsuiteEl.appendChild(testcaseEl);
55         testStart = System.currentTimeMillis();
56     }
57 
58     @Override
59     public void testAssumptionFailure(Failure failure) {
60 
61     }
62 
63     @Override
64     public void testFailure(Failure failure) throws Exception {
65         System.out.println("testFailure");
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         System.out.println("testFinished");
86         tests++;
87         testcaseEl.setAttribute("time", Double.toString((System.currentTimeMillis() - testStart) / 1000.0));
88         store();
89     }
90 
91     @Override
92     public void testIgnored(Description description) throws Exception {
93         testStarted(description);
94         System.out.println("testIgnored");
95         ignored++;
96         Ignore ignore = description.getAnnotation(Ignore.class);
97         Element ignoredEl = doc.createElement("ignored");
98         ignoredEl.setAttribute("message", ignore.value());
99         testcaseEl.appendChild(ignoredEl);
100 
101         testFinished(description);
102     }
103 
104     @Override
105     public void testRunFinished(Result result) throws Exception {
106         System.out.println("testRunFinished");
107         finishSuite();
108     }
109 
110     @Override
111     public void testRunStarted(Description description) throws Exception {
112         System.out.println("testRunStarted");
113     }
114 
115     private void startSuite(Description description) {
116         testClassName = description.getClassName();
117         suiteStart = System.currentTimeMillis();
118         failures = 0;
119         errors = 0;
120         tests = 0;
121         ignored = 0;
122 
123         file = new File(reportDir, testClassName + ".xml");
124         doc = FileUtil.newXML();
125 
126         testsuiteEl = doc.createElement("testsuite");
127         testsuiteEl.setAttribute("name", testClassName);
128         testsuiteEl.setAttribute("start", Long.toString(suiteStart));
129         doc.appendChild(testsuiteEl);
130     }
131 
132     private void finishSuite() {
133         if (testClassName == null)
134             return;
135 
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, testClassName + ".html");
158             InputStream is = getClass().getResourceAsStream("XMLReporter.xsl");
159             if (is != null) {
160                 FileUtil.storeXML(doc, htmlFile, is);
161             }
162         }
163     }
164 
165 }
166