2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.internal.server.runner;
22
23 import java.text.NumberFormat;
24 import java.util.Locale;
25
26 import junit.framework.AssertionFailedError;
27 import junit.framework.Test;
28 import junit.framework.TestFailure;
29 import junit.framework.TestListener;
30 import junit.framework.TestResult;
31
32 import org.apache.cactus.internal.util.JUnitVersionHelper;
33 import org.apache.cactus.internal.util.StringUtil;
34
35
36
37
38
39
40 public class XMLFormatter implements XMLConstants, TestListener
41 {
42
43
44
45 private static final String[] DEFAULT_STACK_FILTER_PATTERNS = new String[]
46 {
47 "org.apache.cactus.AbstractTestCase",
48 "org.apache.cactus.AbstractWebTestCase",
49 "org.apache.cactus.FilterTestCase",
50 "org.apache.cactus.JspTestCase",
51 "org.apache.cactus.ServletTestCase",
52 "junit.framework.TestCase",
53 "junit.framework.TestResult",
54 "junit.framework.TestSuite",
55 "junit.framework.Assert.",
56 "java.lang.reflect.Method.invoke("
57 };
58
59
60
61
62
63
64 private String xslFileName;
65
66
67
68
69 private String suiteClassName;
70
71
72
73
74 private long totalDuration;
75
76
77
78
79 private String encoding = "UTF-8";
80
81
82
83
84 private long currentTestStartTime;
85
86
87
88
89
90
91 private NumberFormat durationFormat = NumberFormat.getInstance(Locale.US);
92
93
94
95
96 private StringBuffer currentTestCaseResults = new StringBuffer();
97
98
99
100
101 private String currentTestFailure;
102
103
104
105
106
107
108
109
110 public void setXslFileName(String theXslFileName)
111 {
112 this.xslFileName = theXslFileName;
113 }
114
115
116
117
118 public void setEncoding(String theEncoding)
119 {
120 this.encoding = theEncoding;
121 }
122
123
124
125
126 public String getEncoding()
127 {
128 return this.encoding;
129 }
130
131
132
133
134 public String getSuiteClassName()
135 {
136 return this.suiteClassName;
137 }
138
139
140
141
142
143
144 public void setSuiteClassName(String theSuiteClassName)
145 {
146 this.suiteClassName = theSuiteClassName;
147 }
148
149
150
151
152 public String getTotalDurationAsString()
153 {
154 return getDurationAsString(this.totalDuration);
155 }
156
157
158
159
160
161
162
163 private String getDurationAsString(long theDuration)
164 {
165 return durationFormat.format((double) theDuration / 1000);
166 }
167
168
169
170
171
172
173 public void setTotalDuration(long theDuration)
174 {
175 this.totalDuration = theDuration;
176 }
177
178
179
180
181
182
183
184 public String toXML(TestResult theResult)
185 {
186 StringBuffer xml = new StringBuffer();
187
188 xml.append("<?xml version=\"1.0\" encoding=\"" + getEncoding()
189 + "\"?>");
190
191 if (this.xslFileName != null)
192 {
193 xml.append("<?xml-stylesheet type=\"text/xsl\" " + "href=\""
194 + this.xslFileName + "\"?>");
195 }
196
197 xml.append("<" + TESTSUITES + ">");
198
199 xml.append("<" + TESTSUITE + " " + ATTR_NAME + "=\""
200 + getSuiteClassName() + "\" " + ATTR_TESTS + "=\""
201 + theResult.runCount() + "\" " + ATTR_FAILURES + "=\""
202 + theResult.failureCount() + "\" " + ATTR_ERRORS + "=\""
203 + theResult.errorCount() + "\" " + ATTR_TIME + "=\""
204 + getTotalDurationAsString() + "\">");
205
206 xml.append(this.currentTestCaseResults.toString());
207
208 xml.append("</" + TESTSUITE + ">");
209 xml.append("</" + TESTSUITES + ">");
210
211 return xml.toString();
212 }
213
214
215
216
217
218
219 public void startTest(Test theTest)
220 {
221 this.currentTestStartTime = System.currentTimeMillis();
222 this.currentTestFailure = null;
223 }
224
225
226
227
228
229
230
231 public void addError(Test theTest, Throwable theThrowable)
232 {
233 TestFailure failure = new TestFailure(theTest, theThrowable);
234 StringBuffer xml = new StringBuffer();
235
236 xml.append("<" + ERROR + " " + ATTR_MESSAGE + "=\""
237 + xmlEncode(failure.thrownException().getMessage()) + "\" "
238 + ATTR_TYPE + "=\""
239 + failure.thrownException().getClass().getName() + "\">");
240 xml.append(xmlEncode(StringUtil.exceptionToString(
241 failure.thrownException(), DEFAULT_STACK_FILTER_PATTERNS)));
242 xml.append("</" + ERROR + ">");
243
244 this.currentTestFailure = xml.toString();
245 }
246
247
248
249
250
251
252
253 public void addFailure(Test theTest, AssertionFailedError theError)
254 {
255 TestFailure failure = new TestFailure(theTest, theError);
256 StringBuffer xml = new StringBuffer();
257
258 xml.append("<" + FAILURE + " " + ATTR_MESSAGE + "=\""
259 + xmlEncode(failure.thrownException().getMessage()) + "\" "
260 + ATTR_TYPE + "=\""
261 + failure.thrownException().getClass().getName() + "\">");
262 xml.append(xmlEncode(StringUtil.exceptionToString(
263 failure.thrownException(), DEFAULT_STACK_FILTER_PATTERNS)));
264 xml.append("</" + FAILURE + ">");
265
266 this.currentTestFailure = xml.toString();
267 }
268
269
270
271
272
273
274 public void endTest(Test theTest)
275 {
276 StringBuffer xml = new StringBuffer();
277 String duration = getDurationAsString(System.currentTimeMillis()
278 - this.currentTestStartTime);
279
280 xml.append("<" + TESTCASE + " " + ATTR_NAME + "=\""
281 + JUnitVersionHelper.getTestCaseName(theTest) + "\" "
282 + ATTR_TIME + "=\"" + duration + "\">");
283
284 if (this.currentTestFailure != null)
285 {
286 xml.append(this.currentTestFailure);
287 }
288
289 xml.append("</" + TESTCASE + ">");
290
291 this.currentTestCaseResults.append(xml.toString());
292 }
293
294
295
296
297
298
299
300 private String xmlEncode(String theString)
301 {
302 String newString;
303
304
305
306 newString = StringUtil.replace(theString, '&', "&");
307
308 newString = StringUtil.replace(newString, '<', "<");
309 newString = StringUtil.replace(newString, '>', ">");
310 newString = StringUtil.replace(newString, '\"', """);
311
312 return newString;
313 }
314 }