Clover coverage report - Cactus 1.5 for J2EE API 1.3
Coverage timestamp: Wed Feb 18 2004 09:09:13 EST
file stats: LOC: 338   Methods: 7
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WebClientTestCaseDelegate.java 0% 0% 0% 0%
coverage
 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.internal.client;
 58   
 
 59   
 import java.lang.reflect.InvocationTargetException;
 60   
 import java.lang.reflect.Method;
 61   
 import java.lang.reflect.Modifier;
 62   
 
 63   
 import java.net.HttpURLConnection;
 64   
 
 65   
 import junit.framework.Test;
 66   
 
 67   
 import org.apache.cactus.RequestDirectives;
 68   
 import org.apache.cactus.WebRequest;
 69   
 import org.apache.cactus.client.ClientException;
 70   
 import org.apache.cactus.client.WebResponseObjectFactory;
 71   
 import org.apache.cactus.client.connector.http.DefaultHttpClient;
 72   
 import org.apache.cactus.configuration.Configuration;
 73   
 import org.apache.cactus.configuration.WebConfiguration;
 74   
 
 75   
 /**
 76   
  * Delegator extension to support test cases using the HTTP protocol. It adds 
 77   
  * support for end methods (as they are dependent on the protocol used, which 
 78   
  * is HTTP here).
 79   
  *
 80   
  * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
 81   
  *
 82   
  * @version $Id: WebClientTestCaseDelegate.java,v 1.1.2.1 2003/08/31 14:38:18 vmassol Exp $
 83   
  */
 84   
 public class WebClientTestCaseDelegate extends ClientTestCaseDelegate
 85   
 {
 86   
     /**
 87   
      * @param theDelegatedTest the test we are delegating for
 88   
      * @param theWrappedTest the test being wrapped by this delegator (or null 
 89   
      *        if none)
 90   
      * @param theConfiguration the configuration to use 
 91   
      */
 92  0
     public WebClientTestCaseDelegate(Test theDelegatedTest, 
 93   
         Test theWrappedTest, Configuration theConfiguration)
 94   
     {
 95  0
         super(theDelegatedTest, theWrappedTest, theConfiguration);
 96   
     }
 97   
 
 98   
     /**
 99   
      * Call the global end method. This is the method that is called after
 100   
      * each test if it exists. It is called on the client side only.
 101   
      *
 102   
      * @param theRequest the request data that were used to open the
 103   
      *        connection.
 104   
      * @param theConnection the <code>HttpURLConnection</code> that was used
 105   
      *        to open the connection to the redirection servlet. The response
 106   
      *        codes, headers, cookies can be checked using the get methods of
 107   
      *        this object.
 108   
      * @param theMethodName the name of the end method to call
 109   
      * @param theResponse the Response object if it exists. Can be null in
 110   
      *        which case it is created from the HttpURLConnection
 111   
      * @return the created WebReponse object (either Cactus or HttpClient)
 112   
      * @exception Throwable any error that occurred when calling the end method
 113   
      *            for the current test case.
 114   
      */
 115  0
     private Object callGenericEndMethod(WebRequest theRequest, 
 116   
         HttpURLConnection theConnection, String theMethodName, 
 117   
         Object theResponse) throws Throwable
 118   
     {
 119  0
         Method methodToCall = null;
 120  0
         Object paramObject = null;
 121   
 
 122  0
         Method[] methods = getTest().getClass().getMethods();
 123   
 
 124  0
         for (int i = 0; i < methods.length; i++)
 125   
         {
 126  0
             if (methods[i].getName().equals(theMethodName))
 127   
             {
 128   
                 // Check return type
 129  0
                 if (!methods[i].getReturnType().getName().equals("void"))
 130   
                 {
 131  0
                     fail("The method [" + methods[i].getName()
 132   
                         + "] should return void and not ["
 133   
                         + methods[i].getReturnType().getName() + "]");
 134   
                 }
 135   
 
 136   
                 // Check if method is public
 137  0
                 if (!Modifier.isPublic(methods[i].getModifiers()))
 138   
                 {
 139  0
                     fail("Method [" + methods[i].getName()
 140   
                         + "] should be declared public");
 141   
                 }
 142   
 
 143   
                 // Check parameters
 144  0
                 Class[] parameters = methods[i].getParameterTypes();
 145   
 
 146   
                 // Verify only one parameter is defined
 147  0
                 if (parameters.length != 1)
 148   
                 {
 149  0
                     fail("The method [" + methods[i].getName()
 150   
                         + "] must only have a single parameter");
 151   
                 }
 152   
 
 153  0
                 paramObject = theResponse;
 154   
 
 155  0
                 if (paramObject == null)
 156   
                 {
 157  0
                     try
 158   
                     {
 159  0
                         paramObject = new WebResponseObjectFactory()
 160   
                             .getResponseObject(parameters[0].getName(), 
 161   
                             theRequest, theConnection);
 162   
                     }
 163   
                     catch (ClientException e)
 164   
                     {
 165  0
                         throw new ClientException("The method ["
 166   
                             + methods[i].getName() 
 167   
                             + "] has a bad parameter of type ["
 168   
                             + parameters[0].getName() + "]", e);
 169   
                     }
 170   
                 }
 171   
 
 172   
                 // Has a method to call already been found ?
 173  0
                 if (methodToCall != null)
 174   
                 {
 175  0
                     fail("There can only be one method ["
 176   
                         + methods[i].getName() + "] per test case. "
 177   
                         + "Test case [" + this.getCurrentTestName()
 178   
                         + "] has two at least !");
 179   
                 }
 180   
 
 181  0
                 methodToCall = methods[i];
 182   
             }
 183   
         }
 184   
 
 185  0
         if (methodToCall != null)
 186   
         {
 187  0
             try
 188   
             {
 189  0
                 methodToCall.invoke(getTest(), new Object[] {paramObject});
 190   
             }
 191   
             catch (InvocationTargetException e)
 192   
             {
 193  0
                 e.fillInStackTrace();
 194  0
                 throw e.getTargetException();
 195   
             }
 196   
             catch (IllegalAccessException e)
 197   
             {
 198  0
                 e.fillInStackTrace();
 199  0
                 throw e;
 200   
             }
 201   
         }
 202   
 
 203  0
         return paramObject;
 204   
     }
 205   
 
 206   
     /**
 207   
      * Call the client tear down up method if it exists.
 208   
      *
 209   
      * @param theRequest the request data that were used to open the
 210   
      *                   connection.
 211   
      * @param theConnection the <code>HttpURLConnection</code> that was used
 212   
      *        to open the connection to the redirection servlet. The response
 213   
      *        codes, headers, cookies can be checked using the get methods of
 214   
      *        this object.
 215   
      * @param theResponse the Response object if it exists. Can be null in
 216   
      *        which case it is created from the HttpURLConnection
 217   
      * @exception Throwable any error that occurred when calling the method
 218   
      */
 219  0
     protected void callClientGlobalEnd(WebRequest theRequest, 
 220   
         HttpURLConnection theConnection, Object theResponse) throws Throwable
 221   
     {
 222  0
         callGenericEndMethod(theRequest, theConnection, 
 223   
             CLIENT_GLOBAL_END_METHOD, theResponse);
 224   
     }
 225   
 
 226   
     /**
 227   
      * Call the test case end method
 228   
      *
 229   
      * @param theRequest the request data that were used to open the
 230   
      *                   connection.
 231   
      * @param theConnection the <code>HttpURLConnection</code> that was used
 232   
      *        to open the connection to the redirection servlet. The response
 233   
      *        codes, headers, cookies can be checked using the get methods of
 234   
      *        this object.
 235   
      * @return the created WebReponse object (either Cactus or HttpClient)
 236   
      * @exception Throwable any error that occurred when calling the end method
 237   
      *         for the current test case.
 238   
      */
 239  0
     public Object callEndMethod(WebRequest theRequest, 
 240   
         HttpURLConnection theConnection) throws Throwable
 241   
     {
 242  0
         return callGenericEndMethod(theRequest, theConnection, 
 243   
             getEndMethodName(), null);
 244   
     }
 245   
 
 246   
     /**
 247   
      * Runs a test case. This method is overriden from the JUnit
 248   
      * <code>TestCase</code> class in order to seamlessly call the
 249   
      * Cactus redirection servlet.
 250   
      *
 251   
      * @exception Throwable if any error happens during the execution of
 252   
      *            the test
 253   
      */
 254  0
     public void runTest() throws Throwable
 255   
     {
 256  0
         runGenericTest(new DefaultHttpClient(
 257   
             (WebConfiguration) getConfiguration()));        
 258   
     }
 259   
 
 260   
     /**
 261   
      * Execute the test case begin method, then connect to the server proxy
 262   
      * redirector (where the test case test method is executed) and then
 263   
      * executes the test case end method.
 264   
      *
 265   
      * @param theHttpClient the HTTP client class to use to connect to the
 266   
      *        proxy redirector.
 267   
      * @exception Throwable any error that occurred when calling the test method
 268   
      *            for the current test case.
 269   
      */
 270  0
     protected void runGenericTest(DefaultHttpClient theHttpClient)
 271   
         throws Throwable
 272   
     {
 273  0
         WebRequest request = new WebRequest(
 274   
             (WebConfiguration) getConfiguration());
 275   
 
 276   
         // Call the set up and begin methods to fill the request object
 277  0
         callClientGlobalBegin(request);
 278  0
         callBeginMethod(request);
 279   
 
 280   
         // Run the web test
 281  0
         HttpURLConnection connection = runWebTest(request, theHttpClient);
 282   
 
 283   
         // Call the end method
 284  0
         Object response = callEndMethod(request, connection);
 285   
 
 286   
         // call the tear down method
 287  0
         callClientGlobalEnd(request, connection, response);
 288   
 
 289   
         // Close the input stream (just in the case the user has not done it
 290   
         // in it's endXXX method (or if he has no endXXX method) ....
 291  0
         connection.getInputStream().close();
 292   
     }
 293   
 
 294   
     /**
 295   
      * Run the web test by connecting to the server proxy
 296   
      * redirector (where the test case test method is executed).
 297   
      *
 298   
      * @param theRequest the request object which will contain data that will
 299   
      *        be used to connect to the Cactus server side redirectors.
 300   
      * @param theHttpClient the HTTP client class to use to connect to the
 301   
      *        proxy redirector.
 302   
      * @return the HTTP connection object that was used to call the server side
 303   
      * @exception Throwable any error that occurred when calling the test method
 304   
      *            for the current test case.
 305   
      */
 306  0
     private HttpURLConnection runWebTest(
 307   
         WebRequest theRequest,
 308   
         DefaultHttpClient theHttpClient)
 309   
         throws Throwable
 310   
     {
 311   
         // Add the class name, the method name, to the request to simulate and
 312   
         // automatic session creation flag to the request
 313  0
         RequestDirectives directives = new RequestDirectives(theRequest);
 314  0
         directives.setClassName(getDelegatedTest().getClass().getName());
 315  0
         directives.setMethodName(getCurrentTestName());
 316  0
         directives.setAutoSession(
 317  0
             theRequest.getAutomaticSession() ? "true" : "false");
 318   
 
 319   
         // Add the wrapped test if it is not equal to our current instance
 320  0
         if (isWrappingATest())
 321   
         {
 322  0
               directives.setWrappedTestName(getWrappedTestName());
 323   
         }
 324   
         // Add the simulated URL (if one has been defined)
 325  0
         if (theRequest.getURL() != null)
 326   
         {
 327  0
             theRequest.getURL().saveToRequest(theRequest);
 328   
         }
 329   
 
 330   
         // Open the HTTP connection to the servlet redirector
 331   
         // and manage errors that could be returned in the
 332   
         // HTTP response.
 333  0
         HttpURLConnection connection = theHttpClient.doTest(theRequest);
 334   
 
 335  0
         return connection;
 336   
     }
 337   
 }
 338