You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2003/12/06 18:33:42 UTC

cvs commit: jakarta-cactus/framework/src/java/share/org/apache/cactus/internal WebRequestImpl.java

vmassol     2003/12/06 09:33:42

  Modified:    framework/src/java/share/org/apache/cactus/internal/client
                        WebClientTestCaseDelegate.java
                        AbstractClientTestCaseDelegate.java
               framework/src/java/share/org/apache/cactus/internal
                        WebRequestImpl.java
  Log:
  - Made call to endXXX methods generic. 
  - Moved them from WebClientTestCaseDelegate to AbstractClientTestCaseDelegate
  
  Revision  Changes    Path
  1.5       +9 -164    jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/WebClientTestCaseDelegate.java
  
  Index: WebClientTestCaseDelegate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/WebClientTestCaseDelegate.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- WebClientTestCaseDelegate.java	30 Nov 2003 20:37:08 -0000	1.4
  +++ WebClientTestCaseDelegate.java	6 Dec 2003 17:33:42 -0000	1.5
  @@ -56,17 +56,12 @@
    */
   package org.apache.cactus.internal.client;
   
  -import java.lang.reflect.InvocationTargetException;
  -import java.lang.reflect.Method;
  -import java.lang.reflect.Modifier;
  -
   import java.net.HttpURLConnection;
   
   import junit.framework.Test;
   
   import org.apache.cactus.RequestDirectives;
   import org.apache.cactus.WebRequest;
  -import org.apache.cactus.client.ClientException;
   import org.apache.cactus.client.WebResponseObjectFactory;
   import org.apache.cactus.client.connector.http.DefaultHttpClient;
   import org.apache.cactus.configuration.Configuration;
  @@ -74,9 +69,7 @@
   import org.apache.cactus.internal.WebRequestImpl;
   
   /**
  - * Delegator extension to support test cases using the HTTP protocol. It adds 
  - * support for end methods (as they are dependent on the protocol used, which 
  - * is HTTP here).
  + * Delegator extension to support test cases using the HTTP protocol.
    *
    * @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
    *
  @@ -97,154 +90,6 @@
       }
   
       /**
  -     * Call the global end method. This is the method that is called after
  -     * each test if it exists. It is called on the client side only.
  -     *
  -     * @param theRequest the request data that were used to open the
  -     *        connection.
  -     * @param theConnection the <code>HttpURLConnection</code> that was used
  -     *        to open the connection to the redirection servlet. The response
  -     *        codes, headers, cookies can be checked using the get methods of
  -     *        this object.
  -     * @param theMethodName the name of the end method to call
  -     * @param theResponse the Response object if it exists. Can be null in
  -     *        which case it is created from the HttpURLConnection
  -     * @return the created WebReponse object (either Cactus or HttpClient)
  -     * @exception Throwable any error that occurred when calling the end method
  -     *            for the current test case.
  -     */
  -    private Object callGenericEndMethod(WebRequest theRequest, 
  -        HttpURLConnection theConnection, String theMethodName, 
  -        Object theResponse) throws Throwable
  -    {
  -        Method methodToCall = null;
  -        Object paramObject = null;
  -
  -        Method[] methods = getTest().getClass().getMethods();
  -
  -        for (int i = 0; i < methods.length; i++)
  -        {
  -            if (methods[i].getName().equals(theMethodName))
  -            {
  -                // Check return type
  -                if (!methods[i].getReturnType().getName().equals("void"))
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                        + "] should return void and not ["
  -                        + methods[i].getReturnType().getName() + "]");
  -                }
  -
  -                // Check if method is public
  -                if (!Modifier.isPublic(methods[i].getModifiers()))
  -                {
  -                    fail("Method [" + methods[i].getName()
  -                        + "] should be declared public");
  -                }
  -
  -                // Check parameters
  -                Class[] parameters = methods[i].getParameterTypes();
  -
  -                // Verify only one parameter is defined
  -                if (parameters.length != 1)
  -                {
  -                    fail("The method [" + methods[i].getName()
  -                        + "] must only have a single parameter");
  -                }
  -
  -                paramObject = theResponse;
  -
  -                if (paramObject == null)
  -                {
  -                    try
  -                    {
  -                        paramObject = new WebResponseObjectFactory()
  -                            .getResponseObject(parameters[0].getName(), 
  -                            theRequest, theConnection);
  -                    }
  -                    catch (ClientException e)
  -                    {
  -                        throw new ClientException("The method ["
  -                            + methods[i].getName() 
  -                            + "] has a bad parameter of type ["
  -                            + parameters[0].getName() + "]", e);
  -                    }
  -                }
  -
  -                // Has a method to call already been found ?
  -                if (methodToCall != null)
  -                {
  -                    fail("There can only be one method ["
  -                        + methods[i].getName() + "] per test case. "
  -                        + "Test case [" + this.getCurrentTestName()
  -                        + "] has two at least !");
  -                }
  -
  -                methodToCall = methods[i];
  -            }
  -        }
  -
  -        if (methodToCall != null)
  -        {
  -            try
  -            {
  -                methodToCall.invoke(getTest(), new Object[] {paramObject});
  -            }
  -            catch (InvocationTargetException e)
  -            {
  -                e.fillInStackTrace();
  -                throw e.getTargetException();
  -            }
  -            catch (IllegalAccessException e)
  -            {
  -                e.fillInStackTrace();
  -                throw e;
  -            }
  -        }
  -
  -        return paramObject;
  -    }
  -
  -    /**
  -     * Call the client tear down up method if it exists.
  -     *
  -     * @param theRequest the request data that were used to open the
  -     *                   connection.
  -     * @param theConnection the <code>HttpURLConnection</code> that was used
  -     *        to open the connection to the redirection servlet. The response
  -     *        codes, headers, cookies can be checked using the get methods of
  -     *        this object.
  -     * @param theResponse the Response object if it exists. Can be null in
  -     *        which case it is created from the HttpURLConnection
  -     * @exception Throwable any error that occurred when calling the method
  -     */
  -    protected void callClientGlobalEnd(WebRequest theRequest, 
  -        HttpURLConnection theConnection, Object theResponse) throws Throwable
  -    {
  -        callGenericEndMethod(theRequest, theConnection, 
  -            CLIENT_GLOBAL_END_METHOD, theResponse);
  -    }
  -
  -    /**
  -     * Call the test case end method
  -     *
  -     * @param theRequest the request data that were used to open the
  -     *                   connection.
  -     * @param theConnection the <code>HttpURLConnection</code> that was used
  -     *        to open the connection to the redirection servlet. The response
  -     *        codes, headers, cookies can be checked using the get methods of
  -     *        this object.
  -     * @return the created WebReponse object (either Cactus or HttpClient)
  -     * @exception Throwable any error that occurred when calling the end method
  -     *         for the current test case.
  -     */
  -    public Object callEndMethod(WebRequest theRequest, 
  -        HttpURLConnection theConnection) throws Throwable
  -    {
  -        return callGenericEndMethod(theRequest, theConnection, 
  -            getEndMethodName(), null);
  -    }
  -
  -    /**
        * Runs a test case. This method is overriden from the JUnit
        * <code>TestCase</code> class in order to seamlessly call the
        * Cactus redirection servlet.
  @@ -275,17 +120,19 @@
               (WebConfiguration) getConfiguration());
   
           // Call the set up and begin methods to fill the request object
  -        callClientGlobalBegin(request);
  +        callGlobalBeginMethod(request);
           callBeginMethod(request);
   
           // Run the web test
           HttpURLConnection connection = runWebTest(request, theHttpClient);
   
           // Call the end method
  -        Object response = callEndMethod(request, connection);
  +        Object response = callEndMethod(request, 
  +            new WebResponseObjectFactory(connection));
   
           // call the tear down method
  -        callClientGlobalEnd(request, connection, response);
  +        callGlobalEndMethod(request, new WebResponseObjectFactory(connection), 
  +            response);
   
           // Close the input stream (just in the case the user has not done it
           // in it's endXXX method (or if he has no endXXX method) ....
  @@ -304,10 +151,8 @@
        * @exception Throwable any error that occurred when calling the test method
        *            for the current test case.
        */
  -    private HttpURLConnection runWebTest(
  -        WebRequest theRequest,
  -        DefaultHttpClient theHttpClient)
  -        throws Throwable
  +    private HttpURLConnection runWebTest(WebRequest theRequest,
  +        DefaultHttpClient theHttpClient) throws Throwable
       {
           // Add the class name, the method name, to the request to simulate and
           // automatic session creation flag to the request
  
  
  
  1.2       +146 -6    jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/AbstractClientTestCaseDelegate.java
  
  Index: AbstractClientTestCaseDelegate.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/client/AbstractClientTestCaseDelegate.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- AbstractClientTestCaseDelegate.java	30 Nov 2003 20:37:08 -0000	1.1
  +++ AbstractClientTestCaseDelegate.java	6 Dec 2003 17:33:42 -0000	1.2
  @@ -64,6 +64,8 @@
   import junit.framework.Test;
   
   import org.apache.cactus.Request;
  +import org.apache.cactus.client.ClientException;
  +import org.apache.cactus.client.ResponseObjectFactory;
   import org.apache.cactus.configuration.Configuration;
   import org.apache.cactus.util.JUnitVersionHelper;
   import org.apache.commons.logging.Log;
  @@ -76,9 +78,8 @@
    * JMS, etc). Subclasses will define additional behaviour that depends on the 
    * protocol.
    *  
  - * It provides the ability to run common code before each test on the client 
  - * side (note that calling common tear down code is delegated to child classes 
  - * as the method signature depends on the protocol used).
  + * It provides the ability to run common code before and after each test on the
  + * client side.
    *
    * In addition it provides the ability to execute some one time (per-JVM)
    * initialisation code (a pity this is not provided in JUnit). It can be 
  @@ -371,7 +372,7 @@
               }
           }
       }
  -
  +    
       /**
        * Call the global begin method. This is the method that is called before
        * each test if it exists. It is called on the client side only.
  @@ -380,7 +381,7 @@
        *        be used to connect to the Cactus server side redirectors.
        * @exception Throwable any error that occurred when calling the method
        */
  -    protected void callClientGlobalBegin(Request theRequest) throws Throwable
  +    protected void callGlobalBeginMethod(Request theRequest) throws Throwable
       {
           callGenericBeginMethod(theRequest, CLIENT_GLOBAL_BEGIN_METHOD);
       }
  @@ -397,6 +398,145 @@
           callGenericBeginMethod(theRequest, getBeginMethodName());
       }
   
  +    /**
  +     * Call the global end method. This is the method that is called after
  +     * each test if it exists. It is called on the client side only.
  +     *
  +     * @param theRequest the request data that were used to open the
  +     *        connection.
  +     * @param theResponseFactory the factory to use to return response objects.
  +     * @param theMethodName the name of the end method to call
  +     * @param theResponse the Response object if it exists. Can be null in
  +     *        which case it is created from the response object factory
  +     * @return the created Reponse object
  +     * @exception Throwable any error that occurred when calling the end method
  +     *            for the current test case.
  +     */
  +    private Object callGenericEndMethod(Request theRequest,
  +        ResponseObjectFactory theResponseFactory, String theMethodName, 
  +        Object theResponse) throws Throwable
  +    {
  +        Method methodToCall = null;
  +        Object paramObject = null;
  +
  +        Method[] methods = getTest().getClass().getMethods();
  +
  +        for (int i = 0; i < methods.length; i++)
  +        {
  +            if (methods[i].getName().equals(theMethodName))
  +            {
  +                // Check return type
  +                if (!methods[i].getReturnType().getName().equals("void"))
  +                {
  +                    fail("The method [" + methods[i].getName()
  +                       + "] should return void and not ["
  +                       + methods[i].getReturnType().getName() + "]");
  +                }
  +
  +                // Check if method is public
  +                if (!Modifier.isPublic(methods[i].getModifiers()))
  +                {
  +                    fail("Method [" + methods[i].getName()
  +                       + "] should be declared public");
  +                }
  +
  +                // Check parameters
  +                Class[] parameters = methods[i].getParameterTypes();
  +
  +                // Verify only one parameter is defined
  +                if (parameters.length != 1)
  +                {
  +                    fail("The method [" + methods[i].getName()
  +                       + "] must only have a single parameter");
  +                }
  +
  +                paramObject = theResponse;
  +
  +                if (paramObject == null)
  +                {
  +                    try
  +                    {
  +                        paramObject = theResponseFactory.getResponseObject(
  +                            parameters[0].getName(), theRequest);
  +                    }
  +                    catch (ClientException e)
  +                    {
  +                        throw new ClientException("The method ["
  +                            + methods[i].getName() 
  +                            + "] has a bad parameter of type ["
  +                            + parameters[0].getName() + "]", e);
  +                    }
  +                }
  +
  +                // Has a method to call already been found ?
  +                if (methodToCall != null)
  +                {
  +                    fail("There can only be one method ["
  +                       + methods[i].getName() + "] per test case. "
  +                       + "Test case [" + this.getCurrentTestName()
  +                       + "] has two at least !");
  +                }
  +
  +                methodToCall = methods[i];
  +            }
  +        }
  +
  +        if (methodToCall != null)
  +        {
  +            try
  +            {
  +                methodToCall.invoke(getTest(), new Object[] {paramObject});
  +            }
  +            catch (InvocationTargetException e)
  +            {
  +                e.fillInStackTrace();
  +                throw e.getTargetException();
  +            }
  +            catch (IllegalAccessException e)
  +            {
  +                e.fillInStackTrace();
  +                throw e;
  +            }
  +        }
  +
  +        return paramObject;
  +    }
  +
  +    /**
  +     * Call the client tear down up method if it exists.
  +     *
  +     * @param theRequest the request data that were used to open the
  +     *                   connection.
  +     * @param theResponseFactory the factory to use to return response objects.
  +     * @param theResponse the Response object if it exists. Can be null in
  +     *        which case it is created from the response object factory
  +     * @exception Throwable any error that occurred when calling the method
  +     */
  +    protected void callGlobalEndMethod(Request theRequest, 
  +        ResponseObjectFactory theResponseFactory, Object theResponse) 
  +        throws Throwable
  +    {
  +        callGenericEndMethod(theRequest, theResponseFactory,
  +            CLIENT_GLOBAL_END_METHOD, theResponse);
  +    }
  +
  +    /**
  +     * Call the test case end method
  +     *
  +     * @param theRequest the request data that were used to open the
  +     *                   connection.
  +     * @param theResponseFactory the factory to use to return response objects.
  +     * @return the created Reponse object
  +     * @exception Throwable any error that occurred when calling the end method
  +     *         for the current test case.
  +     */
  +    public Object callEndMethod(Request theRequest, 
  +        ResponseObjectFactory theResponseFactory) throws Throwable
  +    {
  +        return callGenericEndMethod(theRequest, theResponseFactory,
  +            getEndMethodName(), null);
  +    }
  +    
       /**
        * @see #getCurrentTestName()
        * @deprecated Use {@link #getCurrentTestName()} instead
  
  
  
  1.2       +4 -5      jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/WebRequestImpl.java
  
  Index: WebRequestImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/internal/WebRequestImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- WebRequestImpl.java	2 Nov 2003 23:41:43 -0000	1.1
  +++ WebRequestImpl.java	6 Dec 2003 17:33:42 -0000	1.2
  @@ -266,11 +266,10 @@
           WebResponse response;
           try
           {
  -            response =
  -                (WebResponse) new WebResponseObjectFactory().getResponseObject(
  +            response = (WebResponse) new WebResponseObjectFactory(
  +                resultConnection).getResponseObject(
                       WebResponse.class.getName(),
  -                    obtainSessionIdRequest,
  -                    resultConnection);
  +                    obtainSessionIdRequest);
           }
           catch (ClientException e)
           {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org