Clover coverage report - Cactus 1.4.1 for J2EE API 13
Coverage timestamp: Sat Aug 31 2002 22:02:23 BST
file stats: LOC: 229   Methods: 9
NCLOC: 117   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractWebTestCaller.java 50% 72.1% 100% 74.3%
 1   
 /*   Generated by AspectJ version 1.0.5 */
 2   
 package org.apache.cactus.server;
 3   
 import java.io.IOException;
 4   
 import java.io.Writer;
 5   
 import java.lang.reflect.Constructor;
 6   
 import javax.servlet.ServletException;
 7   
 import org.apache.cactus.AbstractTestCase;
 8   
 import org.apache.cactus.HttpServiceDefinition;
 9   
 import org.apache.cactus.WebTestResult;
 10   
 import org.apache.cactus.util.ClassLoaderUtils;
 11   
 import org.apache.commons.logging.Log;
 12   
 import org.apache.commons.logging.LogFactory;
 13   
 
 14   
 /** 
 15   
  * Responsible for instanciating the <code>TestCase</code> class on the server 
 16   
  * side, set up the implicit objects and call the test method. This class 
 17   
  * provides a common abstraction for all test web requests. 
 18   
  * 
 19   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 20   
  * @author <a href="mailto:ndlesiecki@apache.org">Nicholas Lesiecki</a> 
 21   
  * 
 22   
  * @version $Id: AbstractWebTestCaller.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 23   
  */
 24   
 public abstract class AbstractWebTestCaller {
 25   
   /** 
 26   
        * Name of the attribute in the <code>application</code> scope that will 
 27   
        * hold the results of the test. 
 28   
        */
 29   
   protected static final String TEST_RESULTS = "ServletTestRedirector_TestResults";
 30   
   /** 
 31   
        * The logger. 
 32   
        */
 33   
   private static Log LOGGER;
 34   
   /** 
 35   
        * The implicit objects (which will be used to set the test case fields 
 36   
        * in the <code>setTesCaseFields</code> method. 
 37   
        */
 38   
   protected WebImplicitObjects webImplicitObjects;
 39   
   /** 
 40   
        * @param theObjects the implicit objects coming from the redirector 
 41   
        */
 42  675
   public AbstractWebTestCaller(WebImplicitObjects theObjects) {
 43  675
     super();
 44  675
     this.webImplicitObjects = theObjects;
 45   
   } 
 46   
   /** 
 47   
        * Sets the implicit object in the test case class 
 48   
        * 
 49   
        * @param theTestCase the instance of the test case class on which the 
 50   
        *        class variable (implicit objects) should be set 
 51   
        * @exception Exception if an errors occurs when setting the implicit 
 52   
        *            objects 
 53   
        */
 54   
   protected abstract void setTestCaseFields(AbstractTestCase theTestCase) throws Exception;
 55   
 
 56   
   /** 
 57   
        * @return a <code>Writer</code> object that will be used to return the 
 58   
        *         test result to the client side. 
 59   
        * @exception IOException if an error occurs when retrieving the writer 
 60   
        */
 61   
   protected abstract Writer getResponseWriter() throws IOException;
 62   
 
 63   
   /** 
 64   
        * Calls a test method. The parameters needed to call this method are found 
 65   
        * in the HTTP request. Save the results in the <code>application</code> 
 66   
        * scope so that the Get Test Result service can find them. 
 67   
        * 
 68   
        * @exception ServletException if an unexpected error occurred 
 69   
        */
 70  330
   public void doTest() throws ServletException {
 71  330
     WebTestResult result = null;
 72  330
     try {
 73  330
       AbstractTestCase testInstance = this.getTestClassInstance(this.getTestClassName(), 
 74   
           this.getTestMethodName());
 75  330
       this.setTestCaseFields(testInstance);
 76  330
       testInstance.runBareServerTest();
 77  310
       result = new WebTestResult();
 78   
     } catch (Throwable e) {
 79  20
       result = new WebTestResult(e);
 80   
     } 
 81  330
     AbstractWebTestCaller.LOGGER.debug("Test result : [" + result + "]");
 82  330
     this.webImplicitObjects.getServletContext().setAttribute("ServletTestRedirector_TestResults", 
 83   
         result);
 84  330
     AbstractWebTestCaller.LOGGER.debug("Result saved in context scope");
 85   
   } 
 86   
 
 87   
   /** 
 88   
        * Return the last test results in the HTTP response. 
 89   
        * 
 90   
        * @exception ServletException if an unexpected error occurred 
 91   
        */
 92  330
   public void doGetResults() throws ServletException {
 93  330
     WebTestResult result = (WebTestResult)(this.webImplicitObjects.getServletContext(
 94   
         ).getAttribute("ServletTestRedirector_TestResults"));
 95  330
     AbstractWebTestCaller.LOGGER.debug("Test Result = [" + result + "]");
 96  330
     try {
 97  330
       Writer writer = this.getResponseWriter();
 98  330
       writer.write(result.toXml());
 99  330
       writer.close();
 100   
     } catch (IOException e) {
 101  0
       String message = "Error writing WebTestResult instance to output stream";
 102  0
       AbstractWebTestCaller.LOGGER.error(message, e);
 103  0
       throw new ServletException(message, e);
 104   
     } 
 105   
   } 
 106   
 
 107   
   /** 
 108   
        * Run the connection test between client and server. This is just to 
 109   
        * ensure that configuration is set up correctly. 
 110   
        * 
 111   
        * @exception ServletException if an unexpected error occurred 
 112   
        */
 113  15
   public void doRunTest() throws ServletException {
 114   
   } 
 115   
 
 116   
   /** 
 117   
        * @return the class to test class name, extracted from the HTTP request 
 118   
        * @exception ServletException if the class name of the test case is missing 
 119   
        *            from the HTTP request 
 120   
        */
 121  330
   protected String getTestClassName() throws ServletException {
 122  330
     String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
 123  330
     String className = ServletUtil.getQueryStringParameter(queryString, "Cactus_TestClass");
 124  330
     if (className == null) {
 125  0
       String message = "Missing class name parameter [Cactus_TestClass] in HTTP request.";
 126  0
       AbstractWebTestCaller.LOGGER.error(message);
 127  0
       throw new ServletException(message);
 128   
     } 
 129  330
     AbstractWebTestCaller.LOGGER.debug("Class to call = " + className);
 130  330
     return className;
 131   
   } 
 132   
 
 133   
   /** 
 134   
        * @return the class method to call for the current test case, extracted 
 135   
        *         from the HTTP request 
 136   
        * @exception ServletException if the method name of the test case is 
 137   
        *            missing from the HTTP request 
 138   
        */
 139  330
   protected String getTestMethodName() throws ServletException {
 140  330
     String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
 141  330
     String methodName = ServletUtil.getQueryStringParameter(queryString, "Cactus_TestMethod");
 142  330
     if (methodName == null) {
 143  0
       String message = "Missing method name parameter [Cactus_TestMethod] in HTTP request.";
 144  0
       AbstractWebTestCaller.LOGGER.error(message);
 145  0
       throw new ServletException(message);
 146   
     } 
 147  330
     AbstractWebTestCaller.LOGGER.debug("Method to call = " + methodName);
 148  330
     return methodName;
 149   
   } 
 150   
 
 151   
   /** 
 152   
        * @return true if the auto session flag for the Session can be found in 
 153   
        *         the HTTP request 
 154   
        */
 155  305
   protected boolean isAutoSession() {
 156  305
     String queryString = this.webImplicitObjects.getHttpServletRequest().getQueryString();
 157  305
     String autoSession = ServletUtil.getQueryStringParameter(queryString, 
 158   
         "Cactus_AutomaticSession");
 159  305
     boolean isAutomaticSession = Boolean.valueOf(autoSession).booleanValue();
 160  305
     AbstractWebTestCaller.LOGGER.debug("Auto session is " + isAutomaticSession);
 161  305
     return isAutomaticSession;
 162   
   } 
 163   
 
 164   
   /** 
 165   
        * @param theClassName the name of the test class 
 166   
        * @param theTestCaseName the name of the current test case 
 167   
        * @return an instance of the test class to call 
 168   
        * @exception ServletException if the test case instance for the current 
 169   
        *            test fails to be instanciated (for example if some 
 170   
        *            information is missing from the HTTP request) 
 171   
        */
 172  330
   protected AbstractTestCase getTestClassInstance(String theClassName, 
 173   
       String theTestCaseName) throws ServletException {
 174  330
     Class testClass = this.getTestClassClass(theClassName);
 175  330
     AbstractTestCase testInstance = null;
 176  330
     try {
 177  330
       Constructor constructor = testClass.getConstructor(new java.lang.Class[] {String.class});
 178  330
       testInstance = (AbstractTestCase)constructor.newInstance(new java.lang.Object[] {
 179   
           theTestCaseName});
 180   
     } catch (Exception e) {
 181  0
       String message = "Error instantiating class [" + theClassName + "(" + theTestCaseName + ")]";
 182  0
       AbstractWebTestCaller.LOGGER.error(message, e);
 183  0
       throw new ServletException(message, e);
 184   
     } 
 185  330
     return testInstance;
 186   
   } 
 187   
 
 188   
   /** 
 189   
        * @param theClassName the name of the test class 
 190   
        * @return the class object the test class to call 
 191   
        * @exception ServletException if the class of the current test case 
 192   
        *            cannot be loaded in memory (i.e. it is not in the 
 193   
        *            classpath) 
 194   
        */
 195  330
   protected Class getTestClassClass(String theClassName) throws ServletException {
 196  330
     Class testClass = null;
 197  330
     try {
 198  330
       testClass = ClassLoaderUtils.loadClass(theClassName, this.getClass());
 199   
     } catch (Exception e) {
 200  0
       String message = "Error finding class [" + theClassName + 
 201   
           "] using both the Context classloader and the webapp " + 
 202   
           "classloader. Possible causes include:\r\n";
 203  0
       message += "\t- Your webapp does not include your test classes,\r\n";
 204  0
       message += 
 205   
           "\t- The cactus.jar is not located in your WEB-INF/lib directory and your Container has not set the Context classloader to point to the webapp one";
 206  0
       AbstractWebTestCaller.LOGGER.error(message, e);
 207  0
       throw new ServletException(message, e);
 208   
     } 
 209  330
     return testClass;
 210   
   } 
 211   
 
 212   
   /** 
 213   
    * Responsible for instanciating the <code>TestCase</code> class on the server 
 214   
    * side, set up the implicit objects and call the test method. This class 
 215   
    * provides a common abstraction for all test web requests. 
 216   
    * 
 217   
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a> 
 218   
    * @author <a href="mailto:ndlesiecki@apache.org">Nicholas Lesiecki</a> 
 219   
    * 
 220   
    * @version $Id: AbstractWebTestCaller.html,v 1.1 2003/04/14 12:27:31 sinisa Exp $ 
 221   
    */
 222   
   static {
 223   
     /** 
 224   
          * The logger. 
 225   
          */
 226  5
     AbstractWebTestCaller.LOGGER = LogFactory.getLog(AbstractWebTestCaller.class);
 227   
   } 
 228   
 
 229   
 }