1 20 package org.apache.cactus.server.runner; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.Reader ; 25 import java.io.PrintWriter ; 26 import java.io.StringReader ; 27 import java.io.Writer ; 28 import java.lang.reflect.Constructor ; 29 import java.lang.reflect.Method ; 30 31 import javax.servlet.ServletException ; 32 import javax.servlet.UnavailableException ; 33 import javax.servlet.http.HttpServlet ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 37 import junit.framework.Test; 38 import junit.framework.TestResult; 39 40 import org.apache.cactus.internal.configuration.BaseConfiguration; 41 import org.apache.cactus.internal.configuration.ConfigurationInitializer; 42 import org.apache.cactus.internal.server.runner.WebappTestRunner; 43 import org.apache.cactus.internal.server.runner.XMLFormatter; 44 45 57 public class ServletTestRunner extends HttpServlet 58 { 59 62 private static final String HTTP_SUITE_PARAM = "suite"; 63 64 69 private static final String HTTP_TRANSFORM_PARAM = "transform"; 70 71 76 private static final String HTTP_XSL_PARAM = "xsl"; 77 78 82 private static final String XSL_STYLESHEET_PARAM = "xsl-stylesheet"; 83 84 87 private static final String ENCODING_PARAM = "encoding"; 88 89 92 private Object transformer = null; 93 94 100 private boolean canSetSystemProperty = true; 101 102 108 public void init() throws ServletException 109 { 110 ConfigurationInitializer.initialize(true); 114 115 String xslStylesheetParam = getInitParameter(XSL_STYLESHEET_PARAM); 118 if (xslStylesheetParam != null) 119 { 120 InputStream xslStylesheet = 121 getServletContext().getResourceAsStream(xslStylesheetParam); 122 if (xslStylesheet != null) 123 { 124 try 125 { 126 Class transformerClass = Class.forName("org.apache.cactus." 127 + "internal.server.runner.XMLTransformer"); 128 Constructor transformerCtor = 129 transformerClass.getConstructor( 130 new Class [] {InputStream .class}); 131 transformer = transformerCtor.newInstance( 132 new Object [] {xslStylesheet}); 133 } 134 catch (Throwable t) 135 { 136 log("Could not instantiate XMLTransformer - will not " 137 + "perform server-side XSLT transformations", t); 138 } 139 } 140 else 141 { 142 throw new UnavailableException ( 143 "The initialization parameter 'xsl-stylesheet' does not " 144 + "refer to an existing resource"); 145 } 146 } 147 } 148 149 159 public void doGet(HttpServletRequest theRequest, 160 HttpServletResponse theResponse) throws ServletException , 161 IOException 162 { 163 String suiteClassName = theRequest.getParameter(HTTP_SUITE_PARAM); 165 166 setSystemProperties(theRequest); 169 170 if (suiteClassName == null) 171 { 172 throw new ServletException ("Missing HTTP parameter [" 173 + HTTP_SUITE_PARAM + "] in request"); 174 } 175 176 String xslParam = theRequest.getParameter(HTTP_XSL_PARAM); 178 179 String transformParam = theRequest.getParameter(HTTP_TRANSFORM_PARAM); 181 182 String encoding = theRequest.getParameter(ENCODING_PARAM); 184 185 String xml = run(suiteClassName, xslParam, encoding); 187 188 if ((transformParam != null) && (transformer != null)) 190 { 191 try 193 { 194 Method getContentTypeMethod = 195 transformer.getClass().getMethod( 196 "getContentType", new Class [0]); 197 theResponse.setContentType((String ) 198 getContentTypeMethod.invoke(transformer, new Object [0])); 199 PrintWriter out = theResponse.getWriter(); 200 Method transformMethod = 201 transformer.getClass().getMethod( 202 "transform", new Class [] {Reader .class, Writer .class}); 203 transformMethod.invoke(transformer, 204 new Object [] {new StringReader (xml), out}); 205 } 206 catch (Exception e) 207 { 208 throw new ServletException ( 209 "Problem applying the XSLT transformation", e); 210 } 211 } 212 else 213 { 214 theResponse.setContentType("text/xml"); 216 PrintWriter pw = theResponse.getWriter(); 217 pw.println(xml); 218 } 219 } 220 221 233 private void setSystemProperties(HttpServletRequest theRequest) 234 { 235 String contextURL = System.getProperty( 239 BaseConfiguration.CACTUS_CONTEXT_URL_PROPERTY); 240 241 if (contextURL == null) 244 { 245 if (this.canSetSystemProperty) 246 { 247 try 248 { 249 System.setProperty( 250 BaseConfiguration.CACTUS_CONTEXT_URL_PROPERTY, 251 "http://" + theRequest.getServerName() + ":" 252 + theRequest.getServerPort() 253 + theRequest.getContextPath()); 254 } 255 catch (SecurityException se) 256 { 257 log("Could not set the Cactus context URL as system " 258 + "property, you will have to include a Cactus " 259 + "properties file in the class path of the web " 260 + "application", se); 261 this.canSetSystemProperty = false; 262 } 263 } 264 } 265 } 266 267 278 protected String run(String theSuiteClassName, String theXslFileName, 279 String theEncoding) throws ServletException 280 { 281 TestResult result = new TestResult(); 282 283 XMLFormatter formatter = new XMLFormatter(); 284 formatter.setXslFileName(theXslFileName); 285 formatter.setSuiteClassName(theSuiteClassName); 286 287 if (theEncoding != null) 288 { 289 formatter.setEncoding(theEncoding); 290 } 291 292 result.addListener(formatter); 293 294 long startTime = System.currentTimeMillis(); 295 296 WebappTestRunner testRunner = new WebappTestRunner(); 297 298 Test suite = testRunner.getTest(theSuiteClassName); 299 300 if (suite == null) 301 { 302 throw new ServletException ("Failed to load test suite [" 303 + theSuiteClassName + "], Reason is [" 304 + testRunner.getErrorMessage() + "]"); 305 } 306 307 suite.run(result); 309 310 long endTime = System.currentTimeMillis(); 311 312 formatter.setTotalDuration(endTime - startTime); 313 314 return formatter.toXML(result); 315 } 316 } 317 | Popular Tags |