1 // Copyright (c) 2004 Noa Resare. |
|
2 // Written by Noa Resre <noa@resare.com> |
|
3 |
|
4 // This file is part of Mauve. |
|
5 |
|
6 // Mauve is free software; you can redistribute it and/or modify |
|
7 // it under the terms of the GNU General Public License as published by |
|
8 // the Free Software Foundation; either version 2, or (at your option) |
|
9 // any later version. |
|
10 |
|
11 // Mauve is distributed in the hope that it will be useful, |
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 // GNU General Public License for more details. |
|
15 |
|
16 // You should have received a copy of the GNU General Public License |
|
17 // along with Mauve; see the file COPYING. If not, write to |
|
18 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
19 // Boston, MA 02111-1307, USA. |
|
20 |
|
21 package gnu.testlet; |
|
22 |
|
23 import java.util.*; |
|
24 import java.io.*; |
|
25 |
|
26 /** |
|
27 * A TestReport represents all the results of a test run. The TestReport |
|
28 * can be serialized to xml with the writeXml method. |
|
29 */ |
|
30 public class TestReport |
|
31 { |
|
32 private Properties systemProperties; |
|
33 private List testResults; |
|
34 |
|
35 private static final String ENCODING = "UTF-8"; |
|
36 |
|
37 |
|
38 /** |
|
39 * Creates a new TestReport object with jvmName and jvmVersion set. |
|
40 * |
|
41 * @param systemProperties the Properties object returned from |
|
42 * System.getProperties() of the jvm that is tested. |
|
43 */ |
|
44 public TestReport(Properties systemProperties) |
|
45 { |
|
46 this.systemProperties = systemProperties; |
|
47 this.testResults = new ArrayList(); |
|
48 } |
|
49 |
|
50 /** |
|
51 * Adds a TestResult object to this TestReport. |
|
52 * |
|
53 * @param result the TestResult object to be added |
|
54 */ |
|
55 public void addTestResult(TestResult result) |
|
56 { |
|
57 this.testResults.add(result); |
|
58 } |
|
59 |
|
60 /** |
|
61 * Writes a representation of this TestReport object in xml format. |
|
62 * |
|
63 * @param f the file where the xml stream gets written |
|
64 */ |
|
65 public void writeXml(File f) throws IOException |
|
66 { |
|
67 Writer out = new OutputStreamWriter(new FileOutputStream(f), ENCODING); |
|
68 out.write("<?xml version='1.0' encoding='" + ENCODING + "'?>\n"); |
|
69 out.write("<testreport version='0.1'>\n <jvm name='" |
|
70 + escAttrib(systemProperties.get("java.vm.vendor")) |
|
71 + "'\n version='" |
|
72 + escAttrib(systemProperties.get("java.vm.version")) + "' \n" |
|
73 + " os='" + escAttrib(systemProperties.get("os.name")) + " " |
|
74 + escAttrib(systemProperties.get("os.version")) + " " |
|
75 + escAttrib(systemProperties.get("os.arch")) + "' />\n"); |
|
76 Collections.sort(testResults); |
|
77 Iterator results = testResults.iterator(); |
|
78 while (results.hasNext()) |
|
79 { |
|
80 // Send a message to the Harness to let it know that we are |
|
81 // still writing the XML file. |
|
82 System.out.println("RunnerProcess:restart-timer"); |
|
83 |
|
84 TestResult tr = (TestResult) results.next(); |
|
85 String[] failures = tr.getFailMessags(); |
|
86 String[] passes = tr.getPassMessages(); |
|
87 out.write(" <testresult testlet='" + escAttrib(tr.getTestletName())); |
|
88 if (failures.length > 0 || passes.length > 0 |
|
89 || tr.getException() != null) |
|
90 out.write("'>\n"); |
|
91 else |
|
92 out.write("'/>\n"); |
|
93 |
|
94 for (int i = 0; i < failures.length; i++) { |
|
95 // Restart timer. |
|
96 System.out.println("RunnerProcess:restart-timer"); |
|
97 out.write(" <failure>" + esc(failures[i]) + "</failure>\n"); |
|
98 } |
|
99 |
|
100 if (tr.getException() != null) |
|
101 { |
|
102 Throwable t = tr.getException(); |
|
103 out.write(" <failure>\n <exception class='" |
|
104 + escAttrib(t.getClass().getName()) |
|
105 + "'>\n <reason>" + esc(tr.getExceptionMessage()) |
|
106 + "</reason>\n <message>\n" |
|
107 + esc(tr.getExceptionReason()) |
|
108 + "\n </message>\n </exception>" |
|
109 + "\n </failure>\n"); |
|
110 } |
|
111 |
|
112 for (int i = 0; i < passes.length; i++) { |
|
113 // Restart timer. |
|
114 System.out.println("RunnerProcess:restart-timer"); |
|
115 out.write(" <pass>" + esc(passes[i]) + "</pass>\n"); |
|
116 } |
|
117 |
|
118 if (failures.length > 0 || passes.length > 0 |
|
119 || tr.getException() != null) |
|
120 out.write(" </testresult>\n"); |
|
121 } |
|
122 out.write("</testreport>\n"); |
|
123 out.close(); |
|
124 } |
|
125 |
|
126 /** |
|
127 * Escapes chars < > and & in str so that the result is |
|
128 * suitable for inclusion in an xml stream. |
|
129 */ |
|
130 private String esc(String str) |
|
131 { |
|
132 if (str == null) |
|
133 return null; |
|
134 str = str.replaceAll("&", "&"); |
|
135 str = str.replaceAll("<", "<"); |
|
136 str = str.replaceAll(">", ">"); |
|
137 // This is a workaround for java.util.regex.Pattern.pcrematches. |
|
138 str = str.replace('', '?'); |
|
139 return str; |
|
140 } |
|
141 |
|
142 /** |
|
143 * Escapes single quotes in string by prepending a backslash. |
|
144 */ |
|
145 private String escAttrib(Object obj) |
|
146 { |
|
147 if (obj == null) |
|
148 return null; |
|
149 String str = (String)obj; |
|
150 str = str.replaceAll("'", "\\'"); |
|
151 return str; |
|
152 } |
|
153 } |
|