1
|
|
/* Generated by AspectJ version 1.0.5 */
|
2
|
|
package org.apache.cactus.server.runner;
|
3
|
|
import java.text.NumberFormat;
|
4
|
|
import junit.framework.Test;
|
5
|
|
import junit.framework.TestResult;
|
6
|
|
import junit.framework.TestFailure;
|
7
|
|
import junit.framework.TestListener;
|
8
|
|
import junit.framework.AssertionFailedError;
|
9
|
|
import org.apache.cactus.util.JUnitVersionHelper;
|
10
|
|
import org.apache.cactus.util.StringUtil;
|
11
|
|
|
12
|
|
/**
|
13
|
|
* Format the test results in XML.
|
14
|
|
*
|
15
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
16
|
|
*
|
17
|
|
* @version $Id: XMLFormatter.html,v 1.1 2003/04/14 12:27:33 sinisa Exp $
|
18
|
|
*/
|
19
|
|
public class XMLFormatter implements XMLConstants, TestListener {
|
20
|
|
/**
|
21
|
|
* (optional) Name of the XSL stylesheet to put in the returned XML string
|
22
|
|
* so that the browser will try to apply it (IE at least, I don't know
|
23
|
|
* about the others).
|
24
|
|
*/
|
25
|
|
private String xslFileName;
|
26
|
|
/**
|
27
|
|
* The name of the test suite class.
|
28
|
|
*/
|
29
|
|
private String suiteClassName;
|
30
|
|
/**
|
31
|
|
* Duration it took to execute all the tests.
|
32
|
|
*/
|
33
|
|
private long totalDuration;
|
34
|
|
/**
|
35
|
|
* Time current test was started
|
36
|
|
*/
|
37
|
|
private long currentTestStartTime;
|
38
|
|
/**
|
39
|
|
* XML string containing executed test case results
|
40
|
|
*/
|
41
|
|
private StringBuffer currentTestCaseResults;
|
42
|
|
/**
|
43
|
|
* Current test failure (XML string) : failure or error.
|
44
|
|
*/
|
45
|
|
private String currentTestFailure;
|
46
|
|
/**
|
47
|
|
* Sets the XSL stylesheet file name to put in the returned XML string
|
48
|
|
* so that the browser will try to apply it (IE at least, I don't know
|
49
|
|
* about the others).
|
50
|
|
*
|
51
|
|
* @param theXslFileName the file name (relative to the webapp root)
|
52
|
|
*/
|
53
|
0
|
public void setXslFileName(String theXslFileName) {
|
54
|
0
|
this.xslFileName = theXslFileName;
|
55
|
|
}
|
56
|
|
|
57
|
|
/**
|
58
|
|
* @return the suite class name
|
59
|
|
*/
|
60
|
0
|
public String getSuiteClassName() {
|
61
|
0
|
return this.suiteClassName;
|
62
|
|
}
|
63
|
|
|
64
|
|
/**
|
65
|
|
* Sets the suite class name that was executed.
|
66
|
|
*
|
67
|
|
* @param theSuiteClassName the suite class name
|
68
|
|
*/
|
69
|
0
|
public void setSuiteClassName(String theSuiteClassName) {
|
70
|
0
|
this.suiteClassName = theSuiteClassName;
|
71
|
|
}
|
72
|
|
|
73
|
|
/**
|
74
|
|
* @return the total duration as a string
|
75
|
|
*/
|
76
|
0
|
public String getTotalDurationAsString() {
|
77
|
0
|
return this.getDurationAsString(this.totalDuration);
|
78
|
|
}
|
79
|
|
|
80
|
|
/**
|
81
|
|
* Comvert a duration expressed as a long into a string.
|
82
|
|
*
|
83
|
|
* @param theDuration the duration to convert to string
|
84
|
|
* @return the total duration as a string
|
85
|
|
*/
|
86
|
0
|
private String getDurationAsString(long theDuration) {
|
87
|
0
|
return NumberFormat.getInstance().format((double)theDuration / 1000);
|
88
|
|
}
|
89
|
|
|
90
|
|
/**
|
91
|
|
* Sets the duration it took to execute all the tests.
|
92
|
|
*
|
93
|
|
* @param theDuration the time it took
|
94
|
|
*/
|
95
|
0
|
public void setTotalDuration(long theDuration) {
|
96
|
0
|
this.totalDuration = theDuration;
|
97
|
|
}
|
98
|
|
|
99
|
|
/**
|
100
|
|
* Formats the test result as an XML string.
|
101
|
|
*
|
102
|
|
* @param theResult the test result object
|
103
|
|
* @return the XML string representation of the test results
|
104
|
|
*/
|
105
|
0
|
public String toXML(TestResult theResult) {
|
106
|
0
|
StringBuffer xml = new StringBuffer();
|
107
|
0
|
xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
|
108
|
0
|
if (this.xslFileName != null) {
|
109
|
0
|
xml.append("<?xml-stylesheet type=\"text/xsl\" href=\"" + this.xslFileName + "\"?>");
|
110
|
|
}
|
111
|
0
|
xml.append("<testsuites>");
|
112
|
0
|
xml.append("<testsuite name=\"" + this.getSuiteClassName() + "\" " + "tests" + "=\"" +
|
113
|
|
theResult.runCount() + "\" " + "failures" + "=\"" + theResult.failureCount() + "\" " +
|
114
|
|
"errors" + "=\"" + theResult.errorCount() + "\" " + "time" + "=\"" +
|
115
|
|
this.getTotalDurationAsString() + "\">");
|
116
|
0
|
xml.append(this.currentTestCaseResults.toString());
|
117
|
0
|
xml.append("</testsuite>");
|
118
|
0
|
xml.append("</testsuites>");
|
119
|
0
|
return xml.toString();
|
120
|
|
}
|
121
|
|
|
122
|
|
/**
|
123
|
|
* Event called by the base test runner when the test starts.
|
124
|
|
*
|
125
|
|
* @param theTest the test object being executed
|
126
|
|
*/
|
127
|
0
|
public void startTest(Test theTest) {
|
128
|
0
|
this.currentTestStartTime = System.currentTimeMillis();
|
129
|
0
|
this.currentTestFailure = null;
|
130
|
|
}
|
131
|
|
|
132
|
|
/**
|
133
|
|
* Event called by the base test runner when the test fails with an error.
|
134
|
|
*
|
135
|
|
* @param theTest the test object that failed
|
136
|
|
* @param theThrowable the exception that was thrown
|
137
|
|
*/
|
138
|
0
|
public void addError(Test theTest, Throwable theThrowable) {
|
139
|
0
|
TestFailure failure = new TestFailure(theTest, theThrowable);
|
140
|
0
|
StringBuffer xml = new StringBuffer();
|
141
|
0
|
xml.append("<error message=\"" + this.xmlEncode(failure.thrownException().getMessage()) +
|
142
|
|
"\" " + "type" + "=\"" + failure.thrownException().getClass().getName() + "\">");
|
143
|
0
|
xml.append(this.xmlEncode(StringUtil.exceptionToString(failure.thrownException())));
|
144
|
0
|
xml.append("</error>");
|
145
|
0
|
this.currentTestFailure = xml.toString();
|
146
|
|
}
|
147
|
|
|
148
|
|
/**
|
149
|
|
* Event called by the base test runner when the test fails with a failure.
|
150
|
|
*
|
151
|
|
* @param theTest the test object that failed
|
152
|
|
* @param theError the exception that was thrown
|
153
|
|
*/
|
154
|
0
|
public void addFailure(Test theTest, AssertionFailedError theError) {
|
155
|
0
|
TestFailure failure = new TestFailure(theTest, theError);
|
156
|
0
|
StringBuffer xml = new StringBuffer();
|
157
|
0
|
xml.append("<failure message=\"" + this.xmlEncode(failure.thrownException().getMessage()) +
|
158
|
|
"\" " + "type" + "=\"" + failure.thrownException().getClass().getName() + "\">");
|
159
|
0
|
xml.append(this.xmlEncode(StringUtil.exceptionToString(failure.thrownException())));
|
160
|
0
|
xml.append("</failure>");
|
161
|
0
|
this.currentTestFailure = xml.toString();
|
162
|
|
}
|
163
|
|
|
164
|
|
/**
|
165
|
|
* Event called by the base test runner when the test ends.
|
166
|
|
*
|
167
|
|
* @param theTest the test object being executed
|
168
|
|
*/
|
169
|
0
|
public void endTest(Test theTest) {
|
170
|
0
|
StringBuffer xml = new StringBuffer();
|
171
|
0
|
String duration = this.getDurationAsString(System.currentTimeMillis() -
|
172
|
|
this.currentTestStartTime);
|
173
|
0
|
xml.append("<testcase name=\"" + JUnitVersionHelper.getTestCaseName(theTest) + "\" " + "time"
|
174
|
|
+ "=\"" + duration + "\">");
|
175
|
0
|
if (this.currentTestFailure != null) {
|
176
|
0
|
xml.append(this.currentTestFailure);
|
177
|
|
}
|
178
|
0
|
xml.append("</testcase>");
|
179
|
0
|
this.currentTestCaseResults.append(xml.toString());
|
180
|
|
}
|
181
|
|
|
182
|
|
/**
|
183
|
|
* Escapes reserved XML characters.
|
184
|
|
*
|
185
|
|
* @param theString the string to escape
|
186
|
|
* @return the escaped string
|
187
|
|
*/
|
188
|
0
|
private String xmlEncode(String theString) {
|
189
|
0
|
String newString;
|
190
|
0
|
newString = XMLFormatter.replace(theString, '&', "&");
|
191
|
0
|
newString = XMLFormatter.replace(newString, '<', "<");
|
192
|
0
|
newString = XMLFormatter.replace(newString, '>', ">");
|
193
|
0
|
newString = XMLFormatter.replace(newString, '\"', """);
|
194
|
0
|
return newString;
|
195
|
|
}
|
196
|
|
|
197
|
|
/**
|
198
|
|
* Replaces a character in a string by a substring.
|
199
|
|
*
|
200
|
|
* @param theBaseString the base string in which to perform replacements
|
201
|
|
* @param theChar the char to look for
|
202
|
|
* @param theNewString the string with which to replace the char
|
203
|
|
* @return the string with replacements done or null if the input string
|
204
|
|
* was null
|
205
|
|
*/
|
206
|
0
|
public static String replace(String theBaseString, char theChar, String theNewString) {
|
207
|
0
|
if (theBaseString == null) {
|
208
|
0
|
return ((String)(null));
|
209
|
|
}
|
210
|
0
|
final int len = theBaseString.length() - 1;
|
211
|
0
|
int pos = -1;
|
212
|
0
|
while ((pos = theBaseString.indexOf(theChar, pos + 1)) > -1){
|
213
|
0
|
if (pos == 0) {
|
214
|
0
|
final String after = theBaseString.substring(1);
|
215
|
0
|
theBaseString = theNewString + after;
|
216
|
0
|
} else if (pos == len) {
|
217
|
0
|
final String before = theBaseString.substring(0, pos);
|
218
|
0
|
theBaseString = before + theNewString;
|
219
|
|
} else {
|
220
|
0
|
final String before = theBaseString.substring(0, pos);
|
221
|
0
|
final String after = theBaseString.substring(pos + 1);
|
222
|
0
|
theBaseString = before + theNewString + after;
|
223
|
|
}
|
224
|
|
}
|
225
|
0
|
return theBaseString;
|
226
|
|
}
|
227
|
|
|
228
|
|
/**
|
229
|
|
* Format the test results in XML.
|
230
|
|
*
|
231
|
|
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
232
|
|
*
|
233
|
|
* @version $Id: XMLFormatter.html,v 1.1 2003/04/14 12:27:33 sinisa Exp $
|
234
|
|
*/
|
235
|
0
|
public XMLFormatter() {
|
236
|
0
|
super();
|
237
|
|
{
|
238
|
|
/**
|
239
|
|
* XML string containing executed test case results
|
240
|
|
*/
|
241
|
0
|
this.currentTestCaseResults = new StringBuffer();
|
242
|
|
}
|
243
|
|
}
|
244
|
|
}
|