2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 317   Methods: 4
NCLOC: 161   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ServletTestRunner.java 0% 0% 0% 0%
coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2001-2004 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * You may obtain a copy of the License at
 9   
  * 
 10   
  *   http://www.apache.org/licenses/LICENSE-2.0
 11   
  * 
 12   
  * Unless required by applicable law or agreed to in writing, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 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   
 /**
 46   
  * Helper servlet to start a JUnit Test Runner in a webapp.
 47   
  * 
 48   
  * <p>
 49   
  *   This class currently does a couple of reflection tricks to avoid a direct 
 50   
  *   dependancy on the TraX API (<code>javax.xml.transform.*</code>),
 51   
  *   encapsulated in the 
 52   
  *   {@link org.apache.cactus.internal.server.runner.XMLTransformer} class.
 53   
  * </p>
 54   
  * 
 55   
  * @version $Id: ServletTestRunner.java 239016 2004-06-27 15:23:30Z vmassol $
 56   
  */
 57   
 public class ServletTestRunner extends HttpServlet
 58   
 {
 59   
     /**
 60   
      * HTTP parameter containing name of test suite to execute
 61   
      */
 62   
     private static final String HTTP_SUITE_PARAM = "suite";
 63   
 
 64   
     /**
 65   
      * HTTP parameter that determines whether the XML test results should be 
 66   
      * transformed using the XSLT stylesheet specified as initialization 
 67   
      * parameter.
 68   
      */
 69   
     private static final String HTTP_TRANSFORM_PARAM = "transform";
 70   
 
 71   
     /**
 72   
      * HTTP parameter containing name of the XSL stylesheet to put in the
 73   
      * returned XML test result. It will only work if the browser supports
 74   
      * this feature (IE does, I don't know about others).
 75   
      */
 76   
     private static final String HTTP_XSL_PARAM = "xsl";
 77   
 
 78   
     /**
 79   
      * Name of the servlet initialization parameter that contains the path to
 80   
      * the XSLT stylesheet for transforming the XML report into HTML.
 81   
      */
 82   
     private static final String XSL_STYLESHEET_PARAM = "xsl-stylesheet";
 83   
 
 84   
     /**
 85   
      * Encoding to use for the returned XML.
 86   
      */
 87   
     private static final String ENCODING_PARAM = "encoding";
 88   
     
 89   
     /**
 90   
      * The XML transformer. Avoid direct dependancy by using reflection.
 91   
      */
 92   
     private Object transformer = null;
 93   
 
 94   
     /**
 95   
      * Indicates whether the servlet has sufficient permissions to set a
 96   
      * system property, to be able to set the cactus.contentURL property. This
 97   
      * is set to false if the first attempt to set the property throws a
 98   
      * SecurityException.
 99   
      */
 100   
     private boolean canSetSystemProperty = true;
 101   
 
 102   
     /**
 103   
      * Called by the container when the servlet is initialized.
 104   
      * 
 105   
      * @throws ServletException If an initialization parameter contains an
 106   
      *         illegal value
 107   
      */
 108  0
     public void init() throws ServletException
 109   
     {
 110   
         // Reset the Cactus initialization so that multiple web application can 
 111   
         // work with different Cactus configurations. Otherwise, as the Cactus 
 112   
         // initialization is JVM-wide, the config is not read again.
 113  0
         ConfigurationInitializer.initialize(true);
 114   
 
 115   
         // Check whether XSLT transformations should be done server-side and
 116   
         // build the templates if an XSLT processor is available
 117  0
         String xslStylesheetParam = getInitParameter(XSL_STYLESHEET_PARAM);
 118  0
         if (xslStylesheetParam != null)
 119   
         {
 120  0
             InputStream xslStylesheet =
 121   
                 getServletContext().getResourceAsStream(xslStylesheetParam);
 122  0
             if (xslStylesheet != null)
 123   
             {
 124  0
                 try
 125   
                 {
 126  0
                     Class transformerClass = Class.forName("org.apache.cactus."
 127   
                         + "internal.server.runner.XMLTransformer");
 128  0
                     Constructor transformerCtor = 
 129   
                         transformerClass.getConstructor(
 130   
                         new Class[] {InputStream.class});
 131  0
                     transformer = transformerCtor.newInstance(
 132   
                         new Object[] {xslStylesheet});
 133   
                 }
 134   
                 catch (Throwable t)
 135   
                 {
 136  0
                     log("Could not instantiate XMLTransformer - will not "
 137   
                         + "perform server-side XSLT transformations", t);
 138   
                 }
 139   
             }
 140   
             else
 141   
             {
 142  0
                 throw new UnavailableException(
 143   
                     "The initialization parameter 'xsl-stylesheet' does not "
 144   
                     + "refer to an existing resource");
 145   
             }
 146   
         }
 147   
     }
 148   
 
 149   
     /**
 150   
      * Starts the test suite passed as a HTTP parameter
 151   
      *
 152   
      * @param theRequest the incoming HTTP client request
 153   
      * @param theResponse the outgoing HTTP client request to send back.
 154   
      *
 155   
      * @exception ServletException if an error occurs when servicing the
 156   
      *            request
 157   
      * @exception IOException if an error occurs when servicing the request
 158   
      */
 159  0
     public void doGet(HttpServletRequest theRequest, 
 160   
         HttpServletResponse theResponse) throws ServletException, 
 161   
         IOException
 162   
     {
 163   
         // Verify if a suite parameter exists
 164  0
         String suiteClassName = theRequest.getParameter(HTTP_SUITE_PARAM);
 165   
 
 166   
         // Set up default Cactus System properties so that there is no need
 167   
         // to have a cactus.properties file in WEB-INF/classes
 168  0
         setSystemProperties(theRequest);
 169   
 
 170  0
         if (suiteClassName == null)
 171   
         {
 172  0
             throw new ServletException("Missing HTTP parameter ["
 173   
                 + HTTP_SUITE_PARAM + "] in request");
 174   
         }
 175   
 
 176   
         // Get the XSL stylesheet parameter if any
 177  0
         String xslParam = theRequest.getParameter(HTTP_XSL_PARAM);
 178   
 
 179   
         // Get the transform parameter if any
 180  0
         String transformParam = theRequest.getParameter(HTTP_TRANSFORM_PARAM);
 181   
 
 182   
         // Get the enconding parameter, if any
 183  0
         String encoding = theRequest.getParameter(ENCODING_PARAM);
 184   
         
 185   
         // Run the tests
 186  0
         String xml = run(suiteClassName, xslParam, encoding);
 187   
 
 188   
         // Check if we should do the transformation server side
 189  0
         if ((transformParam != null) && (transformer != null))
 190   
         {
 191   
             // Transform server side
 192  0
             try
 193   
             {
 194  0
                 Method getContentTypeMethod =
 195   
                     transformer.getClass().getMethod(
 196   
                         "getContentType", new Class[0]);
 197  0
                 theResponse.setContentType((String)
 198   
                     getContentTypeMethod.invoke(transformer, new Object[0]));
 199  0
                 PrintWriter out = theResponse.getWriter();
 200  0
                 Method transformMethod =
 201   
                     transformer.getClass().getMethod(
 202   
                         "transform", new Class[] {Reader.class, Writer.class});
 203  0
                 transformMethod.invoke(transformer,
 204   
                     new Object[] {new StringReader(xml), out});
 205   
             }
 206   
             catch (Exception e)
 207   
             {
 208  0
                 throw new ServletException(
 209   
                     "Problem applying the XSLT transformation", e);
 210   
             }
 211   
         }
 212   
         else
 213   
         {
 214   
             // Transform client side (or not at all)
 215  0
             theResponse.setContentType("text/xml");
 216  0
             PrintWriter pw = theResponse.getWriter();
 217  0
             pw.println(xml);
 218   
         }
 219   
     }
 220   
 
 221   
     /**
 222   
      * Set up default Cactus System properties so that there is no need
 223   
      * to have a <code>cactus.properties</code> file in WEB-INF/classes. 
 224   
      * However, if a <code>cactus.properties</code> file is found, the 
 225   
      * properties are read from it.
 226   
      * 
 227   
      * Note: If the JVM security policy prevents setting System properties
 228   
      * you will still need to provide a cactus.properties file.
 229   
      * 
 230   
      * @param theRequest the HTTP request coming from the browser (used
 231   
      *        to extract information about the server name, port, etc) 
 232   
      */
 233  0
     private void setSystemProperties(HttpServletRequest theRequest)
 234   
     {
 235   
         // TODO: We cannot call BaseConfiguration.getContextURL() as it
 236   
         // throws an exception if the context URL property is not defined.
 237   
         // It would be good to change that behavior for a better reuse
 238  0
         String contextURL = System.getProperty(
 239   
             BaseConfiguration.CACTUS_CONTEXT_URL_PROPERTY);
 240   
 
 241   
         // If the context URL propety has not been set, we set a default
 242   
         // value based on the ServletTestRunner mapping in the web app.
 243  0
         if (contextURL == null)
 244   
         {
 245  0
             if (this.canSetSystemProperty)
 246   
             {
 247  0
                 try
 248   
                 {
 249  0
                     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  0
                     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  0
                     this.canSetSystemProperty = false;
 262   
                 }
 263   
             }
 264   
         }
 265   
     }
 266   
     
 267   
     /**
 268   
      * Run the suite tests and return the result.
 269   
      *
 270   
      * @param theSuiteClassName the suite containing the tests to run
 271   
      * @param theXslFileName the name of the XSL stylesheet or null if we don't
 272   
      *        want to apply a stylesheet to the returned XML data
 273   
      * @param theEncoding the encoding to use for the returned XML or null if
 274   
      *        default encoding is to be used
 275   
      * @return the result object
 276   
      * @exception ServletException if the suite failed to be loaded
 277   
      */
 278  0
     protected String run(String theSuiteClassName, String theXslFileName,
 279   
         String theEncoding) throws ServletException
 280   
     {
 281  0
         TestResult result = new TestResult();
 282   
 
 283  0
         XMLFormatter formatter = new XMLFormatter();
 284  0
         formatter.setXslFileName(theXslFileName);
 285  0
         formatter.setSuiteClassName(theSuiteClassName);
 286   
 
 287  0
         if (theEncoding != null)
 288   
         {
 289  0
             formatter.setEncoding(theEncoding);
 290   
         }
 291   
         
 292  0
         result.addListener(formatter);
 293   
 
 294  0
         long startTime = System.currentTimeMillis();
 295   
 
 296  0
         WebappTestRunner testRunner = new WebappTestRunner();
 297   
 
 298  0
         Test suite = testRunner.getTest(theSuiteClassName);
 299   
 
 300  0
         if (suite == null)
 301   
         {
 302  0
             throw new ServletException("Failed to load test suite ["
 303   
                 + theSuiteClassName + "], Reason is ["
 304   
                 + testRunner.getErrorMessage() + "]");
 305   
         }
 306   
 
 307   
         // Run the tests
 308  0
         suite.run(result);
 309   
 
 310  0
         long endTime = System.currentTimeMillis();
 311   
 
 312  0
         formatter.setTotalDuration(endTime - startTime);
 313   
 
 314  0
         return formatter.toXML(result);
 315   
     }
 316   
 }
 317