ndlesiecki    2003/06/26 08:07:56

  Modified:    framework/src/java/share/org/apache/cactus/client/connector/http
                        DefaultHttpClient.java
               framework/src/java/share/org/apache/cactus WebRequest.java
                        AbstractWebServerTestCase.java
  Added:       framework/src/java/share/org/apache/cactus/util
                        UniqueGenerator.java
               framework/src/java/share/org/apache/cactus
                        RequestDirectives.java
  Removed:     framework/src/java/share/org/apache/cactus/util NetUtil.java
  Log:
  Added the concept of request directives and moved functionality there.
  CVS: ----------------------------------------------------------------------
  CVS: PR:
  CVS:   If this change addresses a PR in the problem report tracking
  CVS:   database, then enter the PR number(s) here.
  CVS: Obtained from:
  CVS:   If this change has been taken from another system, such as NCSA,
  CVS:   then name the system in this line, otherwise delete it.
  CVS: Submitted by:
  CVS:   If this code has been contributed to Apache by someone else; i.e.,
  CVS:   they sent us a patch or a new module, then include their name/email
  CVS:   address here. If this is your work then delete this line.
  CVS: Reviewed by:
  CVS:   If we are doing pre-commit code reviews and someone else has
  CVS:   reviewed your changes, include their name(s) here.
  CVS:   If you have not had it reviewed then delete this line.
  
  Revision  Changes    Path
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/util/UniqueGenerator.java
  
  Index: UniqueGenerator.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 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.cactus.util;
  
  import org.apache.cactus.AbstractWebServerTestCase;
  
  /**
   * Generates a quasi-unique id for a test case.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]>Nicholas Lesiecki</a>
   *
   * @version $Id: UniqueGenerator.java,v 1.1 2003/06/26 15:07:56 ndlesiecki Exp $
   */
  public class UniqueGenerator
  {
      /**
       * Counter with synchronized access to prevent possibly
       * identical ids from two threads requesting an id in the
       * same millisecond.
       */
      private static int count = 0;
      
      /**
       * Lock for count.
       */
      private static Object lock = new Object();
      
      /**
       * @param theTestCase TestCase to generate a unique id for.
       * @return The unique id.
       */
      public static String generate(AbstractWebServerTestCase theTestCase)
      {
          String id = String.valueOf(System.identityHashCode(theTestCase));
  
          synchronized (lock)
          {
              id += count++;
          }
          id += System.currentTimeMillis();
          id += fullNameHash(theTestCase);
          return id;
      }
  
      /**
       * @param theTestCase The TestCase to generate a hash for.
       * @return The hash code of the full name of the testCase.
       */
      private static String fullNameHash(AbstractWebServerTestCase theTestCase)
      {
          String name;
          if (theTestCase.isWrappingATest())
          {
              name = theTestCase.getWrappedTestName();
          }
          else
          {
              name = theTestCase.getClass().getName();
          }
  
          //the test method
          name += theTestCase.getName();
  
          return String.valueOf(name.hashCode());
      }
  
  
  
  }
  
  
  
  1.8       +8 -6      
jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/DefaultHttpClient.java
  
  Index: DefaultHttpClient.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/client/connector/http/DefaultHttpClient.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DefaultHttpClient.java    22 Jun 2003 03:39:14 -0000      1.7
  +++ DefaultHttpClient.java    26 Jun 2003 15:07:56 -0000      1.8
  @@ -59,6 +59,7 @@
   import java.net.HttpURLConnection;
   
   import org.apache.cactus.HttpServiceDefinition;
  +import org.apache.cactus.RequestDirectives;
   import org.apache.cactus.ServiceEnumeration;
   import org.apache.cactus.WebRequest;
   import org.apache.cactus.WebTestResult;
  @@ -222,17 +223,18 @@
           throws Throwable
       {
           WebRequest resultsRequest = new WebRequest(this.configuration);
  -
  -        resultsRequest.addCactusCommand(
  -            HttpServiceDefinition.SERVICE_NAME_PARAM,
  -            ServiceEnumeration.GET_RESULTS_SERVICE.toString());
  +        RequestDirectives directives = new RequestDirectives(resultsRequest);
  +        directives.setService(ServiceEnumeration.GET_RESULTS_SERVICE);
  +//        resultsRequest.addCactusCommand(
  +//            HttpServiceDefinition.SERVICE_NAME_PARAM,
  +//            ServiceEnumeration.GET_RESULTS_SERVICE.toString());
   
           // Use the same redirector as was used by the original request
           resultsRequest.setRedirectorName(
               theOriginalRequest.getRedirectorName());
           
           //also copy the unique id to get the correct test results
  -        resultsRequest.setUniqueId(theOriginalRequest.getUniqueId());
  +        directives.setId(new RequestDirectives(theOriginalRequest).getId());
            
           // Add authentication details
           if (theOriginalRequest.getAuthentication() != null)
  
  
  
  1.24      +17 -51    
jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java
  
  Index: WebRequest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/WebRequest.java,v
  retrieving revision 1.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- WebRequest.java   22 Jun 2003 17:22:37 -0000      1.23
  +++ WebRequest.java   26 Jun 2003 15:07:56 -0000      1.24
  @@ -288,15 +288,22 @@
               ((WebConfiguration) getConfiguration()).getRedirectorURL(this), 
               getConfiguration());
   
  -        WebRequest request = new WebRequest(
  +        WebRequest obtainSessionIdRequest = new WebRequest(
               (WebConfiguration) getConfiguration());
  -        addCactusCommand(HttpServiceDefinition.SERVICE_NAME_PARAM, 
  -            ServiceEnumeration.CREATE_SESSION_SERVICE.toString());
  +            
  +        
  +        //Not sure whether I should be adding the service parameter to
  +        //this request (this) or to the obtainSessionIdRequest
  +        //seems obvious that it should be the obtainSessionIdRequest
  +        RequestDirectives directives = 
  +            new RequestDirectives(obtainSessionIdRequest);
  +        directives.setService(ServiceEnumeration.CREATE_SESSION_SERVICE);
   
           HttpURLConnection resultConnection;
           try
           {
  -            resultConnection = helper.connect(request, getConfiguration());
  +            resultConnection =
  +                helper.connect(obtainSessionIdRequest, getConfiguration());
           }
           catch (Throwable e)
           {
  @@ -308,9 +315,11 @@
           WebResponse response;
           try
           {
  -            response = (WebResponse) new WebResponseObjectFactory().
  -                getResponseObject(WebResponse.class.getName(), request, 
  -                resultConnection);
  +            response =
  +                (WebResponse) new WebResponseObjectFactory().getResponseObject(
  +                    WebResponse.class.getName(),
  +                    obtainSessionIdRequest,
  +                    resultConnection);
           }
           catch (ClientException e)
           {
  @@ -339,48 +348,5 @@
           return sessionCookie;
       }
   
  -    /**
  -     * Adds a cactus-specific command to the URL
  -     * The URL is used to allow the user to send whatever he wants
  -     * in the request body. For example a file, ...
  -     * 
  -     * @param theCommandName The name of the command to add--must start with 
  -     *                       "Cactus_"
  -     * @param theCommandValue Value of the command
  -     */
  -    public void addCactusCommand(String theCommandName, String theCommandValue)
  -    {
  -        if (!theCommandName.startsWith(HttpServiceDefinition.COMMAND_PREFIX))
  -        {
  -            throw new IllegalArgumentException("Cactus commands must begin"
  -                + " with" + HttpServiceDefinition.COMMAND_PREFIX + ". The"
  -                + " offending command was [" + theCommandName + "]");
  -        }
  -        addParameter(theCommandName, theCommandValue, GET_METHOD);
  -    }
  -
  -    /**
  -     * Sets the unique id of the test case. Also adds
  -     * a cactus command consisting of the id
  -     * to the actual HTTP request.
  -     * @param theUniqueId new uniqueId for the test case associated
  -     *        with this request
  -     */
  -    public void setUniqueId(String theUniqueId)
  -    {
  -        if (this.uniqueId != null)
  -        {
  -            throw new IllegalStateException("uniqueId already set!");
  -        }
  -        this.uniqueId = theUniqueId;
  -        addCactusCommand(HttpServiceDefinition.TEST_ID_PARAM, theUniqueId);
  -    }
   
  -    /**
  -     * @return Gets the unique id of the test case
  -     */
  -    public String getUniqueId()
  -    {
  -        return this.uniqueId;
  -    }
   }
  
  
  
  1.11      +20 -50    
jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebServerTestCase.java
  
  Index: AbstractWebServerTestCase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-cactus/framework/src/java/share/org/apache/cactus/AbstractWebServerTestCase.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- AbstractWebServerTestCase.java    22 Jun 2003 09:29:22 -0000      1.10
  +++ AbstractWebServerTestCase.java    26 Jun 2003 15:07:56 -0000      1.11
  @@ -65,7 +65,7 @@
   
   import org.apache.cactus.client.connector.http.DefaultHttpClient;
   import org.apache.cactus.configuration.WebConfiguration;
  -import org.apache.cactus.util.NetUtil;
  +import org.apache.cactus.util.UniqueGenerator;
   import org.apache.commons.logging.LogFactory;
   
   /**
  @@ -245,28 +245,19 @@
       {
           // Add the class name, the method name, to the request to simulate and
           // automatic session creation flag to the request
  -        addCactusCommand(
  -            HttpServiceDefinition.CLASS_NAME_PARAM,
  -            this.getClass().getName(),
  -            theRequest);
  -        addCactusCommand(
  -            HttpServiceDefinition.METHOD_NAME_PARAM,
  -            this.getCurrentTestMethod(),
  -            theRequest);
  -        addCactusCommand(
  -            HttpServiceDefinition.AUTOSESSION_NAME_PARAM,
  -            theRequest.getAutomaticSession() ? "true" : "false",
  -            theRequest);
  +        RequestDirectives directives = new RequestDirectives(theRequest);
  +        directives.setClassName(this.getClass().getName());
  +        directives.setMethodName(this.getCurrentTestMethod());
  +        directives.setAutoSession(
  +            theRequest.getAutomaticSession() ? "true" : "false");
   
  -        theRequest.setUniqueId(generateUniqueId());
  +
  +        directives.setId(UniqueGenerator.generate(this));
   
           // Add the wrapped test if it is not equal to our current instance
  -        if (wrappingATest())
  +        if (isWrappingATest())
           {
  -            addCactusCommand(
  -                HttpServiceDefinition.WRAPPED_CLASS_NAME_PARAM,
  -                wrappedTestName(),
  -                theRequest);
  +              directives.setWrappedTestName(getWrappedTestName());
           }
           // Add the simulated URL (if one has been defined)
           if (theRequest.getURL() != null)
  @@ -283,47 +274,26 @@
       }
   
       /**
  -     * Shortcut to the wrappedTest's name.
  +     * @return The wrappedTest's name, if any.
        */
  -    private String wrappedTestName()
  +    public String getWrappedTestName()
       {
  -        return getWrappedTest().getClass().getName();
  +        if (isWrappingATest())
  +        {
  +            return getWrappedTest().getClass().getName();
  +        }
  +        return null;
       }
   
       /**
  -     * Shortcut to determine wrapping of status.
  +     * @return whether this test case wraps another
        */
  -    private boolean wrappingATest()
  +    public boolean isWrappingATest()
       {
           return getWrappedTest() != this;
       }
   
  -    /**
  -     * Generates a (possibly) unique id for this testcase.
  -     */
  -    private String generateUniqueId()
  -    {
  -        String id = "testCase:" + this.getClass().getName() + "_";
  -        id += "testMethod:" + this.getCurrentTestMethod() + "_";
  -        if (wrappingATest())
  -        {
  -            id += "wrapping:" + wrappedTestName() + "_";
  -        }
  -        id += "thread:" + Thread.currentThread().toString() + "_";
  -        id += "runtime_hash:" + Runtime.getRuntime().hashCode() + "_";
  -        id += "client_ip:" + NetUtil.getIp() + "_";
  -        id += "time:" + System.currentTimeMillis() + "_";
  -        return id;
  -    }
   
  -    /**
  -     * Shortcut to [EMAIL PROTECTED] WebRequest#addCactusCommand}.
  -     */
  -    private void addCactusCommand(String theCommandName, 
  -        String theCommandValue, WebRequest theRequest)
  -    {
  -        theRequest.addCactusCommand(theCommandName, theCommandValue);
  -    }
   
       /**
        * Runs a test case. This method is overriden from the JUnit
  
  
  
  1.1                  
jakarta-cactus/framework/src/java/share/org/apache/cactus/RequestDirectives.java
  
  Index: RequestDirectives.java
  ===================================================================
  /*
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 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.cactus;
  
  
  /**
   * Encapsulates the Cactus-specific URL parameters added to the WebRequest.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]>Nicholas Lesiecki</a>
   *
   * @version $Id: RequestDirectives.java,v 1.1 2003/06/26 15:07:56 ndlesiecki Exp $
   */
  public class RequestDirectives
  {
  
      /**
       * The WebRequest that the directives modifies.
       */
      private WebRequest underlyingRequest;
  
      /**
       * @param theRequest The WebRequest to read directives from or
       *                   apply directives to.
       */
      public RequestDirectives(WebRequest theRequest)
      {
          this.underlyingRequest = theRequest;
      }
  
      /**
       * Adds a cactus-specific command to the URL of the WebRequest
       * The URL is used to allow the user to send whatever he wants
       * in the request body. For example a file, ...
       * 
       * @param theCommandName The name of the command to add--must start with 
       *                       "Cactus_"
       * @param theCommandValue Value of the command
       */
      public void addCactusCommand(String theCommandName, String theCommandValue)
      {
          if (!theCommandName.startsWith(HttpServiceDefinition.COMMAND_PREFIX))
          {
              throw new IllegalArgumentException(
                  "Cactus commands must begin"
                      + " with" + HttpServiceDefinition.COMMAND_PREFIX
                      + ". The offending command was [" + theCommandName
                      + "]");
          }
          underlyingRequest.addParameter(
              theCommandName,
              theCommandValue,
              WebRequest.GET_METHOD);
      }
  
      /**
       * @param theId new id for the test case associated
       *        with this request
       */
      public void setId(String theId)
      {
          if (getId() != null)
          {
              throw new IllegalStateException("uniqueId already set!");
          }
          addCactusCommand(HttpServiceDefinition.TEST_ID_PARAM, theId);
      }
  
      /**
       * @return Gets the unique id of the test case
       */
      public String getId()
      {
          return underlyingRequest.getParameterGet(
              HttpServiceDefinition.TEST_ID_PARAM);
      }
  
      /**
       * @param theName name of the test class.
       */
      public void setClassName(String theName)
      {
          addCactusCommand(
              HttpServiceDefinition.CLASS_NAME_PARAM,
              theName);
      }
  
  
      /**
       * @param theName The name of the wrapped test.
       */
      public void setWrappedTestName(String theName)
      {
          addCactusCommand(
              HttpServiceDefinition.WRAPPED_CLASS_NAME_PARAM,
              theName);
  
      }
  
      /**
       * @param theMethodName name of the test method to execute.
       */
      public void setMethodName(String theMethodName)
      {
              addCactusCommand(
                  HttpServiceDefinition.METHOD_NAME_PARAM,
                  theMethodName);
      }
  
  
  
      /**
       * @param theService The service to request of the redirector.
       */
      public void setService(ServiceEnumeration theService)
      {
          addCactusCommand(HttpServiceDefinition.SERVICE_NAME_PARAM,
                           theService.toString());
          
      }
  
      /**
       * @param hasAutoSession A "boolean string" indicating
       *                       whether or not to use the 
       *                       autoSession option.
       */
      public void setAutoSession(String hasAutoSession)
      {
          addCactusCommand(
              HttpServiceDefinition.AUTOSESSION_NAME_PARAM,
              hasAutoSession);
          
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to