|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ServletTestRunner.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.server.runner;
|
|
58 |
|
|
59 |
import java.io.InputStream;
|
|
60 |
import java.io.IOException;
|
|
61 |
import java.io.Reader;
|
|
62 |
import java.io.PrintWriter;
|
|
63 |
import java.io.StringReader;
|
|
64 |
import java.io.Writer;
|
|
65 |
import java.lang.reflect.Constructor;
|
|
66 |
import java.lang.reflect.Method;
|
|
67 |
|
|
68 |
import javax.servlet.ServletException;
|
|
69 |
import javax.servlet.UnavailableException;
|
|
70 |
import javax.servlet.http.HttpServlet;
|
|
71 |
import javax.servlet.http.HttpServletRequest;
|
|
72 |
import javax.servlet.http.HttpServletResponse;
|
|
73 |
|
|
74 |
import junit.framework.Test;
|
|
75 |
import junit.framework.TestResult;
|
|
76 |
|
|
77 |
import org.apache.cactus.configuration.BaseConfiguration;
|
|
78 |
|
|
79 |
/**
|
|
80 |
* Helper servlet to start a JUnit Test Runner in a webapp.
|
|
81 |
*
|
|
82 |
* <p>
|
|
83 |
* This class currently does a couple of reflection tricks to avoid a direct
|
|
84 |
* dependancy on the TraX API (<code>javax.xml.transform.*</code>),
|
|
85 |
* encapsulated in the {@link XMLTransformer} class.
|
|
86 |
* </p>
|
|
87 |
*
|
|
88 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
89 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
90 |
*
|
|
91 |
* @version $Id: ServletTestRunner.java,v 1.14 2003/02/22 11:31:50 vmassol Exp $
|
|
92 |
*/
|
|
93 |
public class ServletTestRunner extends HttpServlet |
|
94 |
{ |
|
95 |
/**
|
|
96 |
* HTTP parameter containing name of test suite to execute
|
|
97 |
*/
|
|
98 |
private static final String HTTP_SUITE_PARAM = "suite"; |
|
99 |
|
|
100 |
/**
|
|
101 |
* HTTP parameter that determines whether the XML test results should be
|
|
102 |
* transformed using the XSLT stylesheet specified as initialization
|
|
103 |
* parameter.
|
|
104 |
*/
|
|
105 |
private static final String HTTP_TRANSFORM_PARAM = "transform"; |
|
106 |
|
|
107 |
/**
|
|
108 |
* HTTP parameter containing name of the XSL stylesheet to put in the
|
|
109 |
* returned XML test result. It will only work if the browser supports
|
|
110 |
* this feature (IE does, I don't know about others).
|
|
111 |
*/
|
|
112 |
private static final String HTTP_XSL_PARAM = "xsl"; |
|
113 |
|
|
114 |
/**
|
|
115 |
* Name of the servlet initialization parameter that contains the path to
|
|
116 |
* the XSLT stylesheet for transforming the XML report into HTML.
|
|
117 |
*/
|
|
118 |
private static final String XSL_STYLESHEET_PARAM = "xsl-stylesheet"; |
|
119 |
|
|
120 |
/**
|
|
121 |
* The XML transformer. Avoid direct dependancy by using reflection.
|
|
122 |
*/
|
|
123 |
private Object transformer = null; |
|
124 |
|
|
125 |
/**
|
|
126 |
* Indicates whether the servlet has sufficient permissions to set a
|
|
127 |
* system property, to be able to set the cactus.contentURL property. This
|
|
128 |
* is set to false if the first attempt to set the property throws a
|
|
129 |
* SecurityException.
|
|
130 |
*/
|
|
131 |
private boolean canSetSystemProperty = true; |
|
132 |
|
|
133 |
/**
|
|
134 |
* Called by the container when the servlet is initialized.
|
|
135 |
*
|
|
136 |
* @throws ServletException If an initialization parameter contains an
|
|
137 |
* illegal value
|
|
138 |
*/
|
|
139 | 0 |
public void init() throws ServletException |
140 |
{ |
|
141 |
// Check whether XSLT transformations should be done server-side and
|
|
142 |
// build the templates if an XSLT processor is available
|
|
143 | 0 |
String xslStylesheetParam = getInitParameter(XSL_STYLESHEET_PARAM); |
144 | 0 |
if (xslStylesheetParam != null) |
145 |
{ |
|
146 | 0 |
InputStream xslStylesheet = |
147 |
getServletContext().getResourceAsStream(xslStylesheetParam); |
|
148 | 0 |
if (xslStylesheet != null) |
149 |
{ |
|
150 | 0 |
try
|
151 |
{ |
|
152 | 0 |
Class transformerClass = Class.forName( |
153 |
"org.apache.cactus.server.runner.XMLTransformer");
|
|
154 | 0 |
Constructor transformerCtor = |
155 |
transformerClass.getConstructor( |
|
156 |
new Class[] {InputStream.class}); |
|
157 | 0 |
transformer = transformerCtor.newInstance( |
158 |
new Object[] {xslStylesheet});
|
|
159 |
} |
|
160 |
catch (Throwable t)
|
|
161 |
{ |
|
162 | 0 |
log("Could not instantiate XMLTransformer - will not "
|
163 |
+ "perform server-side XSLT transformations", t);
|
|
164 |
} |
|
165 |
} |
|
166 |
else
|
|
167 |
{ |
|
168 | 0 |
throw new UnavailableException( |
169 |
"The initialization parameter 'xsl-stylesheet' does not "
|
|
170 |
+ "refer to an existing resource");
|
|
171 |
} |
|
172 |
} |
|
173 |
} |
|
174 |
|
|
175 |
/**
|
|
176 |
* Starts the test suite passed as a HTTP parameter
|
|
177 |
*
|
|
178 |
* @param theRequest the incoming HTTP client request
|
|
179 |
* @param theResponse the outgoing HTTP client request to send back.
|
|
180 |
*
|
|
181 |
* @exception ServletException if an error occurs when servicing the
|
|
182 |
* request
|
|
183 |
* @exception IOException if an error occurs when servicing the request
|
|
184 |
*/
|
|
185 | 0 |
public void doGet(HttpServletRequest theRequest, |
186 |
HttpServletResponse theResponse) throws ServletException,
|
|
187 |
IOException |
|
188 |
{ |
|
189 |
// Verify if a suite parameter exists
|
|
190 | 0 |
String suiteClassName = theRequest.getParameter(HTTP_SUITE_PARAM); |
191 |
|
|
192 |
// Set up default Cactus System properties so that there is no need
|
|
193 |
// to have a cactus.properties file in WEB-INF/classes
|
|
194 | 0 |
setSystemProperties(theRequest); |
195 |
|
|
196 | 0 |
if (suiteClassName == null) |
197 |
{ |
|
198 | 0 |
throw new ServletException("Missing HTTP parameter [" |
199 |
+ HTTP_SUITE_PARAM + "] in request");
|
|
200 |
} |
|
201 |
|
|
202 |
// Get the XSL stylesheet parameter if any
|
|
203 | 0 |
String xslParam = theRequest.getParameter(HTTP_XSL_PARAM); |
204 |
|
|
205 |
// Get the transform parameter if any
|
|
206 | 0 |
String transformParam = theRequest.getParameter(HTTP_TRANSFORM_PARAM); |
207 |
|
|
208 |
// Run the tests
|
|
209 | 0 |
String xml = run(suiteClassName, xslParam); |
210 |
|
|
211 |
// Check if we should do the transformation server side
|
|
212 | 0 |
if ((transformParam != null) && (transformer != null)) |
213 |
{ |
|
214 |
// Transform server side
|
|
215 | 0 |
try
|
216 |
{ |
|
217 | 0 |
Method getContentTypeMethod = |
218 |
transformer.getClass().getMethod( |
|
219 |
"getContentType", new Class[0]); |
|
220 | 0 |
theResponse.setContentType((String) |
221 |
getContentTypeMethod.invoke(transformer, new Object[0]));
|
|
222 | 0 |
PrintWriter out = theResponse.getWriter(); |
223 | 0 |
Method transformMethod = |
224 |
transformer.getClass().getMethod( |
|
225 |
"transform", new Class[] {Reader.class, Writer.class}); |
|
226 | 0 |
transformMethod.invoke(transformer, |
227 |
new Object[] {new StringReader(xml), out}); |
|
228 |
} |
|
229 |
catch (Exception e)
|
|
230 |
{ |
|
231 | 0 |
throw new ServletException( |
232 |
"Problem applying the XSLT transformation", e);
|
|
233 |
} |
|
234 |
} |
|
235 |
else
|
|
236 |
{ |
|
237 |
// Transform client side (or not at all)
|
|
238 | 0 |
theResponse.setContentType("text/xml");
|
239 | 0 |
PrintWriter pw = theResponse.getWriter(); |
240 | 0 |
pw.println(xml); |
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
/**
|
|
245 |
* Set up default Cactus System properties so that there is no need
|
|
246 |
* to have a cactus.properties file in WEB-INF/classes.
|
|
247 |
*
|
|
248 |
* Note: If the JVM security policy prevents setting System properties
|
|
249 |
* you will still need to provide a cactus.properties file.
|
|
250 |
*
|
|
251 |
* @param theRequest the HTTP request coming from the browser (used
|
|
252 |
* to extract information about the server name, port, etc)
|
|
253 |
*/
|
|
254 | 0 |
private void setSystemProperties(HttpServletRequest theRequest) |
255 |
{ |
|
256 | 0 |
if (this.canSetSystemProperty) |
257 |
{ |
|
258 | 0 |
try
|
259 |
{ |
|
260 | 0 |
System.setProperty( |
261 |
BaseConfiguration.CACTUS_CONTEXT_URL_PROPERTY, |
|
262 |
"http://" + theRequest.getServerName() + ":" |
|
263 |
+ theRequest.getServerPort() |
|
264 |
+ theRequest.getContextPath()); |
|
265 |
} |
|
266 |
catch (SecurityException se)
|
|
267 |
{ |
|
268 | 0 |
log("Could not set the Cactus context URL as system property, "
|
269 |
+ "you will have to include a Cactus properties file in "
|
|
270 |
+ "the class path of the web application", se);
|
|
271 | 0 |
this.canSetSystemProperty = false; |
272 |
} |
|
273 |
} |
|
274 |
} |
|
275 |
|
|
276 |
/**
|
|
277 |
* Run the suite tests and return the result.
|
|
278 |
*
|
|
279 |
* @param theSuiteClassName the suite containing the tests to run
|
|
280 |
* @param theXslFileName the name of the XSL stylesheet or null if we don't
|
|
281 |
* want to apply a stylesheet to the returned XML data
|
|
282 |
* @return the result object
|
|
283 |
* @exception ServletException if the suite failed to be loaded
|
|
284 |
*/
|
|
285 | 0 |
protected String run(String theSuiteClassName, String theXslFileName)
|
286 |
throws ServletException
|
|
287 |
{ |
|
288 | 0 |
TestResult result = new TestResult();
|
289 |
|
|
290 | 0 |
XMLFormatter formatter = new XMLFormatter();
|
291 |
|
|
292 | 0 |
formatter.setXslFileName(theXslFileName); |
293 |
|
|
294 | 0 |
formatter.setSuiteClassName(theSuiteClassName); |
295 |
|
|
296 | 0 |
result.addListener(formatter); |
297 |
|
|
298 | 0 |
long startTime = System.currentTimeMillis();
|
299 |
|
|
300 | 0 |
WebappTestRunner testRunner = new WebappTestRunner();
|
301 |
|
|
302 | 0 |
Test suite = testRunner.getTest(theSuiteClassName); |
303 |
|
|
304 | 0 |
if (suite == null) |
305 |
{ |
|
306 | 0 |
throw new ServletException("Failed to load test suite [" |
307 |
+ theSuiteClassName + "], Reason is ["
|
|
308 |
+ testRunner.getErrorMessage() + "]");
|
|
309 |
} |
|
310 |
|
|
311 |
|
|
312 |
// Run the tests
|
|
313 | 0 |
suite.run(result); |
314 |
|
|
315 | 0 |
long endTime = System.currentTimeMillis();
|
316 |
|
|
317 | 0 |
formatter.setTotalDuration(endTime - startTime); |
318 |
|
|
319 | 0 |
return formatter.toXML(result);
|
320 |
} |
|
321 |
} |
|
322 |
|
|