jakarta-commons  

cvs commit: jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server AbstractTestCaller.java WebImplicitObjects.java AbstractTestController.java JspTestCaller.java JspTestController.java ServletImplicitObjects.java ServletTestCaller.java ServletTestController.java ServletTestRedirector.java

vmassol
Tue, 04 Sep 2001 07:56:11 -0700

vmassol     01/09/04 08:21:16

  Modified:    cactus/src/framework/share/org/apache/commons/cactus/server
                        AbstractTestController.java JspTestCaller.java
                        JspTestController.java ServletImplicitObjects.java
                        ServletTestCaller.java ServletTestController.java
                        ServletTestRedirector.java
  Added:       cactus/src/framework/share/org/apache/commons/cactus/server
                        AbstractTestCaller.java WebImplicitObjects.java
  Log:
  refactorings
  
  Revision  Changes    Path
  1.4       +7 -7      
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/AbstractTestController.java
  
  Index: AbstractTestController.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/AbstractTestController.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractTestController.java       2001/08/31 16:55:51     1.3
  +++ AbstractTestController.java       2001/09/04 15:21:15     1.4
  @@ -66,13 +66,13 @@
   
   /**
    * Controller that extracts the requested service from the HTTP request and
  - * executes the request by calling a <code>ServletTestCaller</code>. There
  + * executes the request by calling a <code>WebTestCaller</code>. There
    * are 2 services available : one for executing the test and one for returning
    * the test result.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: AbstractTestController.java,v 1.3 2001/08/31 16:55:51 vmassol Exp $
  + * @version $Id: AbstractTestController.java,v 1.4 2001/09/04 15:21:15 vmassol Exp $
    */
   public abstract class AbstractTestController
   {
  @@ -85,17 +85,17 @@
       /**
        * @return the test caller that will be used to execute the test
        */
  -    protected abstract ServletTestCaller getTestCaller(
  -            ServletImplicitObjects theObjects);
  +    protected abstract AbstractTestCaller getTestCaller(
  +            WebImplicitObjects theObjects);
   
       /**
        * Handles the incoming request by extracting the requested service and
  -     * calling the correct method on a <code>ServletTestCaller</code>.
  +     * calling the correct method on a <code>WebTestCaller</code>.
        *
        * @param theObjects the implicit objects (they are different for the
        *                   different redirectors)
        */
  -    public void handleRequest(ServletImplicitObjects theObjects)
  +    public void handleRequest(WebImplicitObjects theObjects)
           throws ServletException
       {
           logger.entry("handleRequest(...)");
  @@ -114,7 +114,7 @@
               String serviceName =
                       getServiceName(theObjects.getHttpServletRequest());
   
  -            ServletTestCaller caller = getTestCaller(theObjects);
  +            AbstractTestCaller caller = getTestCaller(theObjects);
   
               // Is it the call test method service ?
               if (ServiceEnumeration.CALL_TEST_SERVICE.equals(serviceName)) {
  
  
  
  1.9       +9 -9      
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/JspTestCaller.java
  
  Index: JspTestCaller.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/JspTestCaller.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- JspTestCaller.java        2001/08/31 16:55:51     1.8
  +++ JspTestCaller.java        2001/09/04 15:21:15     1.9
  @@ -71,7 +71,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: JspTestCaller.java,v 1.8 2001/08/31 16:55:51 vmassol Exp $
  + * @version $Id: JspTestCaller.java,v 1.9 2001/09/04 15:21:15 vmassol Exp $
    */
   public class JspTestCaller extends ServletTestCaller
   {
  @@ -87,39 +87,39 @@
       public JspTestCaller(JspImplicitObjects theObjects)
       {
           super(theObjects);
  -        this.servletImplicitObjects = theObjects;
       }
   
       /**
        * Sets the test case fields using the implicit objects (using reflection).
        * @param theTestInstance the test class instance
        */
  -    protected void setTestCaseFields(ServletTestCase theTestInstance)
  +    protected void setTestCaseFields(AbstractTestCase theTestInstance)
           throws Exception
       {
           logger.entry("setTestCaseFields([" + theTestInstance + "])");
   
  +        JspTestCase jspInstance = (JspTestCase)theTestInstance;
           JspImplicitObjects jspImplicitObjects =
  -            (JspImplicitObjects)this.servletImplicitObjects;
  +            (JspImplicitObjects)this.webImplicitObjects;
   
           // Sets the Servlet-related implicit objects
           // -----------------------------------------
   
  -        super.setTestCaseFields(theTestInstance);
  +        super.setTestCaseFields(jspInstance);
   
           // Set the page context field of the test case class
           // -------------------------------------------------
   
  -        Field pageContextField = theTestInstance.getClass().
  +        Field pageContextField = jspInstance.getClass().
               getField("pageContext");
  -        pageContextField.set(theTestInstance,
  +        pageContextField.set(jspInstance,
               jspImplicitObjects.getPageContext());
   
           // Set the JSP writer field of the test case class
           // -----------------------------------------------
   
  -        Field outField = theTestInstance.getClass().getField("out");
  -        outField.set(theTestInstance, jspImplicitObjects.getJspWriter());
  +        Field outField = jspInstance.getClass().getField("out");
  +        outField.set(jspInstance, jspImplicitObjects.getJspWriter());
   
           logger.exit("setTestCaseFields");
       }
  
  
  
  1.3       +4 -4      
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/JspTestController.java
  
  Index: JspTestController.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/JspTestController.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- JspTestController.java    2001/08/24 16:23:18     1.2
  +++ JspTestController.java    2001/09/04 15:21:15     1.3
  @@ -67,20 +67,20 @@
   /**
    * JSP Controller that extracts the requested service from the
    * HTTP request and executes the request by calling a
  - * <code>ServletTestCaller</code>. There are 2 services available : one for
  + * <code>JspTestCaller</code>. There are 2 services available : one for
    * executing the test and one for returning the test result.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: JspTestController.java,v 1.2 2001/08/24 16:23:18 vmassol Exp $
  + * @version $Id: JspTestController.java,v 1.3 2001/09/04 15:21:15 vmassol Exp $
    */
   public class JspTestController extends AbstractTestController
   {
       /**
        * @return the test caller that will be used to execute the test
        */
  -    protected ServletTestCaller getTestCaller(
  -            ServletImplicitObjects theObjects)
  +    protected AbstractTestCaller getTestCaller(
  +            WebImplicitObjects theObjects)
       {
           return new JspTestCaller((JspImplicitObjects)theObjects);
       }
  
  
  
  1.4       +2 -45     
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletImplicitObjects.java
  
  Index: ServletImplicitObjects.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletImplicitObjects.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ServletImplicitObjects.java       2001/08/24 16:23:18     1.3
  +++ ServletImplicitObjects.java       2001/09/04 15:21:16     1.4
  @@ -63,21 +63,11 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: ServletImplicitObjects.java,v 1.3 2001/08/24 16:23:18 vmassol Exp $
  + * @version $Id: ServletImplicitObjects.java,v 1.4 2001/09/04 15:21:16 vmassol Exp $
    */
  -public class ServletImplicitObjects
  +public class ServletImplicitObjects extends WebImplicitObjects
   {
       /**
  -     * The HTTP request object.
  -     */
  -    protected HttpServletRequest request;
  -
  -    /**
  -     * The HTTP response object.
  -     */
  -    protected HttpServletResponse response;
  -
  -    /**
        * The Servlet configuration object.
        */
       protected ServletConfig config;
  @@ -97,37 +87,4 @@
       {
           this.config = theConfig;
       }
  -
  -    /**
  -     * @return the <code>HttpServletResponse</code> implicit object
  -     */
  -    public HttpServletResponse getHttpServletResponse()
  -    {
  -        return this.response;
  -    }
  -
  -    /**
  -     * @param theResponse the <code>HttpServletResponse</code> implicit object
  -     */
  -    public void setHttpServletResponse(HttpServletResponse theResponse)
  -    {
  -        this.response = theResponse;
  -    }
  -
  -    /**
  -     * @return the <code>HttpServletRequest</code> implicit object
  -     */
  -    public HttpServletRequest getHttpServletRequest()
  -    {
  -        return this.request;
  -    }
  -
  -    /**
  -     * @param theRequest the <code>HttpServletRequest</code> implicit object
  -     */
  -    public void setHttpServletRequest(HttpServletRequest theRequest)
  -    {
  -        this.request = theRequest;
  -    }
  -
   }
  
  
  
  1.12      +20 -261   
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestCaller.java
  
  Index: ServletTestCaller.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestCaller.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ServletTestCaller.java    2001/08/31 16:55:51     1.11
  +++ ServletTestCaller.java    2001/09/04 15:21:16     1.12
  @@ -70,304 +70,64 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: ServletTestCaller.java,v 1.11 2001/08/31 16:55:51 vmassol Exp $
  + * @version $Id: ServletTestCaller.java,v 1.12 2001/09/04 15:21:16 vmassol Exp $
    */
  -public class ServletTestCaller
  +public class ServletTestCaller extends AbstractTestCaller
   {
       /**
  -     * Name of the attribute in the <code>application</code> scope that will
  -     * hold the results of the test.
  -     */
  -    private final static String TEST_RESULTS =
  -        "ServletTestRedirector_TestResults";
  -
  -    /**
        * The logger
        */
       protected static Log logger =
           LogService.getInstance().getLog(ServletTestCaller.class.getName());
   
       /**
  -     * The implicit objects (which will be used to set the test case fields
  -     * in the <code>setTesCaseFields</code> method.
  -     */
  -    protected ServletImplicitObjects servletImplicitObjects;
  -
  -    /**
        * @param theObjects the implicit objects coming from the redirector
        */
       public ServletTestCaller(ServletImplicitObjects theObjects)
  -    {
  -        this.servletImplicitObjects = theObjects;
  -    }
  -
  -    /**
  -     * Calls a test method. The parameters needed to call this method are found
  -     * in the HTTP request. Save the results in the <code>application</code>
  -     * scope so that the Get Test Result service can find them.
  -     *
  -     * @exception ServletException if an unexpected error occurred
  -     */
  -    public void doTest() throws ServletException
  -    {
  -        logger.entry("doTest()");
  -
  -        WebTestResult result = null;
  -
  -        try {
  -
  -            // Create an instance of the test class
  -            ServletTestCase testInstance = getTestClassInstance(
  -                getTestClassName(), getTestMethodName());
  -
  -            // Set its fields (implicit objects)
  -            setTestCaseFields(testInstance);
  -
  -            // Call it's method corresponding to the current test case
  -            testInstance.runBareServerTest();
  -
  -            // Return an instance of <code>WebTestResult</code> with a
  -            // positive result.
  -            result = new WebTestResult();
  -
  -        } catch (Throwable e) {
  -            // An error occurred, return an instance of
  -            // <code>WebTestResult</code> with an exception.
  -            result = new WebTestResult(e);
  -
  -        }
  -
  -        logger.debug("Test result : [" + result + "]");
  -
  -        // Set the test result.
  -        this.servletImplicitObjects.getServletConfig().getServletContext().
  -            setAttribute(TEST_RESULTS, result);
  -
  -        logger.debug("Result saved in context scope");
  -
  -        logger.exit("doTest");
  -    }
  -
  -    /**
  -     * Return the last test results as a serialized object in the HTTP response.
  -     *
  -     * @exception ServletException if an unexpected error occurred
  -     */
  -    public void doGetResults() throws ServletException
       {
  -        logger.entry("doGetResults()");
  -
  -        // One could think there is a potential risk that the client side of
  -        // Cactus will request the result before it has been written to the
  -        // context scope as the HTTP request will not block in some containers.
  -        // However this will not happend because on the client side, once the
  -        // first request is done to execute the test, all the result is read
  -        // by the AutoReadHttpURLConnection class, thus ensuring that the
  -        // request is fully finished and the resukt has been committed ...
  -
  -        WebTestResult result =
  -            (WebTestResult)(this.servletImplicitObjects.getServletConfig().
  -            getServletContext().getAttribute(TEST_RESULTS));
  -
  -        logger.debug("Test Result = [" + result + "]");
  -
  -        // Write back the results as a serialized object to the outgoing stream.
  -        try {
  -
  -            OutputStream os =
  -                this.servletImplicitObjects.getHttpServletResponse().
  -                getOutputStream();
  -
  -            // Write back the result object as a serialized object
  -            ObjectOutputStream oos = new ObjectOutputStream(os);
  -            oos.writeObject(result);
  -            oos.flush();
  -            oos.close();
  -
  -        } catch (IOException e) {
  -            String message = "Error writing WebTestResult instance to output " +
  -                "stream";
  -            logger.error(message, e);
  -            throw new ServletException(message, e);
  -        }
  -
  -        logger.exit("doGetResults");
  +        super(theObjects);
       }
   
       /**
  -     * @return the class to test class name, extracted from the HTTP request
  -     */
  -    protected String getTestClassName() throws ServletException
  -    {
  -        logger.entry("getTestClassName()");
  -
  -        String className = this.servletImplicitObjects.
  -            getHttpServletRequest().
  -            getParameter(ServiceDefinition.CLASS_NAME_PARAM);
  -
  -        if (className == null) {
  -            String message = "Missing class name parameter [" +
  -                ServiceDefinition.CLASS_NAME_PARAM + "] in HTTP request.";
  -            logger.error(message);
  -            throw new ServletException(message);
  -        }
  -
  -        logger.debug("Class to call = " + className);
  -
  -        logger.exit("getTestClassName");
  -        return className;
  -    }
  -
  -    /**
  -     * @return the class method to call for the current test case, extracted
  -     *         from the HTTP request
  -     */
  -    protected String getTestMethodName() throws ServletException
  -    {
  -        logger.entry("getTestMethodName()");
  -
  -        String methodName = this.servletImplicitObjects.getHttpServletRequest().
  -            getParameter(ServiceDefinition.METHOD_NAME_PARAM);
  -
  -        if (methodName == null) {
  -            String message = "Missing method name parameter [" +
  -                ServiceDefinition.METHOD_NAME_PARAM + "] in HTTP request.";
  -            logger.error(message);
  -            throw new ServletException(message);
  -        }
  -
  -        logger.debug("Method to call = " + methodName);
  -
  -        logger.exit("getTestMethodName");
  -        return methodName;
  -    }
  -
  -    /**
  -     * @return true if the auto session flag for the Session can be found in
  -     *         the HTTP request
  -     */
  -    protected boolean isAutoSession()
  -    {
  -        logger.entry("isAutoSession()");
  -
  -        String autoSession = this.servletImplicitObjects.
  -            getHttpServletRequest().
  -            getParameter(ServiceDefinition.AUTOSESSION_NAME_PARAM);
  -
  -        boolean isAutomaticSession = new Boolean(autoSession).booleanValue();
  -
  -        logger.debug("Auto session is " + isAutomaticSession);
  -
  -        logger.exit("isAutoSession");
  -        return isAutomaticSession;
  -    }
  -
  -    /**
  -     * @param theClassName the name of the test class
  -     * @param theTestCaseName the name of the current test case
  -     * @return an instance of the test class to call
  -     */
  -    protected ServletTestCase getTestClassInstance(String theClassName,
  -        String theTestCaseName) throws ServletException
  -    {
  -        logger.entry("getTestClassInstance([" + theClassName + "], [" +
  -            theTestCaseName + "])");
  -
  -        // Print info on the classloader used to load this class
  -        if (logger.isDebugEnabled()) {
  -            StringBuffer buffer = new StringBuffer("Classloaders = ");
  -            ClassLoader classLoader = this.getClass().getClassLoader();
  -            while (classLoader != null) {
  -                buffer.append(classLoader.toString());
  -                classLoader = classLoader.getParent();
  -                if (classLoader != null) {
  -                    buffer.append(", ");
  -                }
  -            }
  -            logger.debug(buffer.toString());
  -        }
  -
  -        // Get the class to call and build an instance of it.
  -        Class testClass = null;
  -        ServletTestCase testInstance = null;
  -        try {
  -            testClass = getTestClassClass(theClassName);
  -            Constructor constructor = testClass.getConstructor(
  -                new Class[] { String.class });
  -            testInstance = (ServletTestCase)constructor.newInstance(
  -                new Object[] { theTestCaseName });
  -        } catch (Exception e) {
  -            String message = "Error instanciating class [" + theClassName +
  -                "(" + theTestCaseName + ")]";
  -            logger.error(message, e);
  -            throw new ServletException(message, e);
  -        }
  -
  -        logger.exit("getTestClassInstance");
  -        return testInstance;
  -    }
  -
  -    /**
  -     * @param theClassName the name of the test class
  -     * @param theTestCaseName the name of the current test case
  -     * @return the class object the test class to call
  -     */
  -    protected Class getTestClassClass(String theClassName)
  -        throws ServletException
  -    {
  -        logger.entry("getTestClassClass([" + theClassName + "])");
  -
  -        // Get the class to call and build an instance of it.
  -        Class testClass = null;
  -        try {
  -            testClass = Class.forName(theClassName);
  -        } catch (Exception e) {
  -            String message = "Error finding class [" + theClassName +
  -                "] in classpath";
  -            logger.error(message, e);
  -            throw new ServletException(message, e);
  -        }
  -
  -        logger.exit("getTestClassClass");
  -        return testClass;
  -    }
  -
  -    /**
        * Sets the test case fields using the implicit objects (using reflection).
        * @param theTestInstance the test class instance
        */
  -    protected void setTestCaseFields(ServletTestCase theTestInstance)
  +    protected void setTestCaseFields(AbstractTestCase theTestInstance)
           throws Exception
       {
           logger.entry("setTestCaseFields([" + theTestInstance + "])");
   
  +        ServletTestCase servletInstance = (ServletTestCase)theTestInstance;
  +        ServletImplicitObjects servletImplicitObjects =
  +            (ServletImplicitObjects)this.webImplicitObjects;
  +
           // Sets the request field of the test case class
           // ---------------------------------------------
   
           // Extract from the HTTP request the URL to simulate (if any)
           HttpServletRequest request =
  -            this.servletImplicitObjects.getHttpServletRequest();
  +            servletImplicitObjects.getHttpServletRequest();
   
           ServletURL url = ServletURL.loadFromRequest(request);
   
  -        Field requestField = theTestInstance.getClass().getField("request");
  -        requestField.set(theTestInstance,
  +        Field requestField = servletInstance.getClass().getField("request");
  +        requestField.set(servletInstance,
               new HttpServletRequestWrapper(request, url));
   
           // Set the response field of the test case class
           // ---------------------------------------------
   
  -        Field responseField = theTestInstance.getClass().getField("response");
  -        responseField.set(theTestInstance,
  -            this.servletImplicitObjects.getHttpServletResponse());
  +        Field responseField = servletInstance.getClass().getField("response");
  +        responseField.set(servletInstance,
  +            servletImplicitObjects.getHttpServletResponse());
   
           // Set the config field of the test case class
           // -------------------------------------------
   
  -        Field configField = theTestInstance.getClass().getField("config");
  -        configField.set(theTestInstance,
  +        Field configField = servletInstance.getClass().getField("config");
  +        configField.set(servletInstance,
               new ServletConfigWrapper(
  -                this.servletImplicitObjects.getServletConfig()));
  +                servletImplicitObjects.getServletConfig()));
   
           // Set the session field of the test case class
           // --------------------------------------------
  @@ -376,11 +136,10 @@
           if (isAutoSession()) {
   
               HttpSession session =
  -                this.servletImplicitObjects.getHttpServletRequest().
  -                getSession(true);
  +                servletImplicitObjects.getHttpServletRequest().getSession(true);
   
  -            Field sessionField = theTestInstance.getClass().getField("session");
  -            sessionField.set(theTestInstance, session);
  +            Field sessionField = servletInstance.getClass().getField("session");
  +            sessionField.set(servletInstance, session);
   
           }
   
  
  
  
  1.3       +4 -4      
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestController.java
  
  Index: ServletTestController.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestController.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ServletTestController.java        2001/08/24 16:23:18     1.2
  +++ ServletTestController.java        2001/09/04 15:21:16     1.3
  @@ -72,17 +72,17 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: ServletTestController.java,v 1.2 2001/08/24 16:23:18 vmassol Exp $
  + * @version $Id: ServletTestController.java,v 1.3 2001/09/04 15:21:16 vmassol Exp $
    */
   public class ServletTestController extends AbstractTestController
   {
       /**
        * @return the test caller that will be used to execute the test
        */
  -    protected ServletTestCaller getTestCaller(
  -            ServletImplicitObjects theObjects)
  +    protected AbstractTestCaller getTestCaller(
  +            WebImplicitObjects theObjects)
       {
  -        return new ServletTestCaller(theObjects);
  +        return new ServletTestCaller((ServletImplicitObjects)theObjects);
       }
   
   }
  
  
  
  1.8       +2 -1      
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestRedirector.java
  
  Index: ServletTestRedirector.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/ServletTestRedirector.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ServletTestRedirector.java        2001/08/31 16:55:51     1.7
  +++ ServletTestRedirector.java        2001/09/04 15:21:16     1.8
  @@ -69,7 +69,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
    *
  - * @version $Id: ServletTestRedirector.java,v 1.7 2001/08/31 16:55:51 vmassol Exp $
  + * @version $Id: ServletTestRedirector.java,v 1.8 2001/09/04 15:21:16 vmassol Exp $
    * @see ServletTestCaller
    */
   public class ServletTestRedirector extends HttpServlet
  @@ -127,6 +127,7 @@
           ServletImplicitObjects objects = new ServletImplicitObjects();
           objects.setHttpServletRequest(theRequest);
           objects.setHttpServletResponse(theResponse);
  +        objects.setServletContext(getServletContext());
           objects.setServletConfig(getServletConfig());
   
           ServletTestController controller = new ServletTestController();
  
  
  
  1.1                  
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/AbstractTestCaller.java
  
  Index: AbstractTestCaller.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.cactus.server;
  
  import java.io.*;
  import java.lang.reflect.*;
  import java.net.*;
  import java.util.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  import org.apache.commons.cactus.*;
  import org.apache.commons.cactus.util.log.*;
  
  /**
   * Responsible for instanciating the <code>TestCase</code> class on the server
   * side, set up the implicit objects and call the test method. This class
   * provides a common abstraction for all test web requests.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
   *
   * @version $Id: AbstractTestCaller.java,v 1.1 2001/09/04 15:21:15 vmassol Exp $
   */
  public abstract class AbstractTestCaller
  {
      /**
       * Name of the attribute in the <code>application</code> scope that will
       * hold the results of the test.
       */
      private final static String TEST_RESULTS =
          "ServletTestRedirector_TestResults";
  
      /**
       * The logger
       */
      protected static Log logger =
          LogService.getInstance().getLog(AbstractTestCaller.class.getName());
  
      /**
       * The implicit objects (which will be used to set the test case fields
       * in the <code>setTesCaseFields</code> method.
       */
      protected WebImplicitObjects webImplicitObjects;
  
      /**
       * @param theObjects the implicit objects coming from the redirector
       */
      public AbstractTestCaller(WebImplicitObjects theObjects)
      {
          this.webImplicitObjects = theObjects;
      }
  
      /**
       * Sets the implicit object in the test case class
       *
       * @param theTestCase the instance of the test case class on which the
       *        class variable (implicit objects) should be set
       */
      protected abstract void setTestCaseFields(AbstractTestCase theTestCase)
          throws Exception;
  
      /**
       * Calls a test method. The parameters needed to call this method are found
       * in the HTTP request. Save the results in the <code>application</code>
       * scope so that the Get Test Result service can find them.
       *
       * @exception ServletException if an unexpected error occurred
       */
      public void doTest() throws ServletException
      {
          logger.entry("doTest()");
  
          WebTestResult result = null;
  
          try {
  
              // Create an instance of the test class
              AbstractTestCase testInstance = getTestClassInstance(
                  getTestClassName(), getTestMethodName());
  
              // Set its fields (implicit objects)
              setTestCaseFields(testInstance);
  
              // Call it's method corresponding to the current test case
              testInstance.runBareServerTest();
  
              // Return an instance of <code>WebTestResult</code> with a
              // positive result.
              result = new WebTestResult();
  
          } catch (Throwable e) {
              // An error occurred, return an instance of
              // <code>WebTestResult</code> with an exception.
              result = new WebTestResult(e);
  
          }
  
          logger.debug("Test result : [" + result + "]");
  
          // Set the test result.
          this.webImplicitObjects.getServletContext().setAttribute(TEST_RESULTS,
              result);
  
          logger.debug("Result saved in context scope");
  
          logger.exit("doTest");
      }
  
      /**
       * Return the last test results as a serialized object in the HTTP response.
       *
       * @exception ServletException if an unexpected error occurred
       */
      public void doGetResults() throws ServletException
      {
          logger.entry("doGetResults()");
  
          // One could think there is a potential risk that the client side of
          // Cactus will request the result before it has been written to the
          // context scope as the HTTP request will not block in some containers.
          // However this will not happend because on the client side, once the
          // first request is done to execute the test, all the result is read
          // by the AutoReadHttpURLConnection class, thus ensuring that the
          // request is fully finished and the resukt has been committed ...
  
          WebTestResult result =
              (WebTestResult)(this.webImplicitObjects.getServletContext().
                  getAttribute(TEST_RESULTS));
  
          logger.debug("Test Result = [" + result + "]");
  
          // Write back the results as a serialized object to the outgoing stream.
          try {
  
              OutputStream os =
                  this.webImplicitObjects.getHttpServletResponse().
                  getOutputStream();
  
              // Write back the result object as a serialized object
              ObjectOutputStream oos = new ObjectOutputStream(os);
              oos.writeObject(result);
              oos.flush();
              oos.close();
  
          } catch (IOException e) {
              String message = "Error writing WebTestResult instance to output " +
                  "stream";
              logger.error(message, e);
              throw new ServletException(message, e);
          }
  
          logger.exit("doGetResults");
      }
  
      /**
       * @return the class to test class name, extracted from the HTTP request
       */
      protected String getTestClassName() throws ServletException
      {
          logger.entry("getTestClassName()");
  
          String className = this.webImplicitObjects.getHttpServletRequest().
              getParameter(ServiceDefinition.CLASS_NAME_PARAM);
  
          if (className == null) {
              String message = "Missing class name parameter [" +
                  ServiceDefinition.CLASS_NAME_PARAM + "] in HTTP request.";
              logger.error(message);
              throw new ServletException(message);
          }
  
          logger.debug("Class to call = " + className);
  
          logger.exit("getTestClassName");
          return className;
      }
  
      /**
       * @return the class method to call for the current test case, extracted
       *         from the HTTP request
       */
      protected String getTestMethodName() throws ServletException
      {
          logger.entry("getTestMethodName()");
  
          String methodName = this.webImplicitObjects.getHttpServletRequest().
              getParameter(ServiceDefinition.METHOD_NAME_PARAM);
  
          if (methodName == null) {
              String message = "Missing method name parameter [" +
                  ServiceDefinition.METHOD_NAME_PARAM + "] in HTTP request.";
              logger.error(message);
              throw new ServletException(message);
          }
  
          logger.debug("Method to call = " + methodName);
  
          logger.exit("getTestMethodName");
          return methodName;
      }
  
      /**
       * @return true if the auto session flag for the Session can be found in
       *         the HTTP request
       */
      protected boolean isAutoSession()
      {
          logger.entry("isAutoSession()");
  
          String autoSession = this.webImplicitObjects.getHttpServletRequest().
              getParameter(ServiceDefinition.AUTOSESSION_NAME_PARAM);
  
          boolean isAutomaticSession = new Boolean(autoSession).booleanValue();
  
          logger.debug("Auto session is " + isAutomaticSession);
  
          logger.exit("isAutoSession");
          return isAutomaticSession;
      }
  
      /**
       * @param theClassName the name of the test class
       * @param theTestCaseName the name of the current test case
       * @return an instance of the test class to call
       */
      protected AbstractTestCase getTestClassInstance(String theClassName,
          String theTestCaseName) throws ServletException
      {
          logger.entry("getTestClassInstance([" + theClassName + "], [" +
              theTestCaseName + "])");
  
          // Print info on the classloader used to load this class
          if (logger.isDebugEnabled()) {
              StringBuffer buffer = new StringBuffer("Classloaders = ");
              ClassLoader classLoader = this.getClass().getClassLoader();
              while (classLoader != null) {
                  buffer.append(classLoader.toString());
                  classLoader = classLoader.getParent();
                  if (classLoader != null) {
                      buffer.append(", ");
                  }
              }
              logger.debug(buffer.toString());
          }
  
          // Get the class to call and build an instance of it.
          Class testClass = null;
          AbstractTestCase testInstance = null;
          try {
              testClass = getTestClassClass(theClassName);
              Constructor constructor = testClass.getConstructor(
                  new Class[] { String.class });
              testInstance = (AbstractTestCase)constructor.newInstance(
                  new Object[] { theTestCaseName });
          } catch (Exception e) {
              String message = "Error instanciating class [" + theClassName +
                  "(" + theTestCaseName + ")]";
              logger.error(message, e);
              throw new ServletException(message, e);
          }
  
          logger.exit("getTestClassInstance");
          return testInstance;
      }
  
      /**
       * @param theClassName the name of the test class
       * @param theTestCaseName the name of the current test case
       * @return the class object the test class to call
       */
      protected Class getTestClassClass(String theClassName)
          throws ServletException
      {
          logger.entry("getTestClassClass([" + theClassName + "])");
  
          // Get the class to call and build an instance of it.
          Class testClass = null;
          try {
              testClass = Class.forName(theClassName);
          } catch (Exception e) {
              String message = "Error finding class [" + theClassName +
                  "] in classpath";
              logger.error(message, e);
              throw new ServletException(message, e);
          }
  
          logger.exit("getTestClassClass");
          return testClass;
      }
  
  }
  
  
  1.1                  
jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server/WebImplicitObjects.java
  
  Index: WebImplicitObjects.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Cactus", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.cactus.server;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  /**
   * Holder class that contains the instances of the implicit objects that exist
   * for all web requests. Namely they are <code>HttpServletRequest</code>,
   * <code>HttpServletResponse</code> and <code>ServletContext</code>.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Massol</a>
   *
   * @version $Id: WebImplicitObjects.java,v 1.1 2001/09/04 15:21:16 vmassol Exp $
   */
  public class WebImplicitObjects
  {
      /**
       * The HTTP request object.
       */
      protected HttpServletRequest request;
  
      /**
       * The HTTP response object.
       */
      protected HttpServletResponse response;
  
      /**
       * The Context object.
       */
      protected ServletContext context;
  
      /**
       * @return the <code>ServletContext</code> implicit object
       */
      public ServletContext getServletContext()
      {
          return this.context;
      }
  
      /**
       * @param theContext the <code>ServletContext</code> implicit object
       */
      public void setServletContext(ServletContext theContext)
      {
          this.context = theContext;
      }
  
      /**
       * @return the <code>HttpServletResponse</code> implicit object
       */
      public HttpServletResponse getHttpServletResponse()
      {
          return this.response;
      }
  
      /**
       * @param theResponse the <code>HttpServletResponse</code> implicit object
       */
      public void setHttpServletResponse(HttpServletResponse theResponse)
      {
          this.response = theResponse;
      }
  
      /**
       * @return the <code>HttpServletRequest</code> implicit object
       */
      public HttpServletRequest getHttpServletRequest()
      {
          return this.request;
      }
  
      /**
       * @param theRequest the <code>HttpServletRequest</code> implicit object
       */
      public void setHttpServletRequest(HttpServletRequest theRequest)
      {
          this.request = theRequest;
      }
  
  }
  
  
  
  • cvs commit: jakarta-commons/cactus/src/framework/share/org/apache/commons/cactus/server AbstractTestCaller.java WebImplicitObjects.java AbstractTestController.java JspTestCaller.java JspTestController.java ServletImplicitObjects.java ServletTestCaller.java ServletTestController.java ServletTestRedirector.java vmassol