2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

Clover coverage report - Cactus 1.8dev for J2EE API 1.3
Coverage timestamp: Sun Mar 26 2006 18:50:18 BRT
file stats: LOC: 175   Methods: 8
NCLOC: 75   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
HttpProtocolHandler.java 66.7% 95.8% 100% 92.1%
coverage coverage
 1   
 /* 
 2   
  * ========================================================================
 3   
  * 
 4   
  * Copyright 2003-2004 The Apache Software Foundation.
 5   
  *
 6   
  * Licensed under the Apache License, Version 2.0 (the "License");
 7   
  * you may not use this file except in compliance with the License.
 8   
  * You may obtain a copy of the License at
 9   
  * 
 10   
  *   http://www.apache.org/licenses/LICENSE-2.0
 11   
  * 
 12   
  * Unless required by applicable law or agreed to in writing, software
 13   
  * distributed under the License is distributed on an "AS IS" BASIS,
 14   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   
  * See the License for the specific language governing permissions and
 16   
  * limitations under the License.
 17   
  * 
 18   
  * ========================================================================
 19   
  */
 20   
 package org.apache.cactus.internal.client.connector.http;
 21   
 
 22   
 import java.io.IOException;
 23   
 import java.net.HttpURLConnection;
 24   
 
 25   
 import junit.framework.Test;
 26   
 
 27   
 import org.apache.cactus.Request;
 28   
 import org.apache.cactus.WebRequest;
 29   
 import org.apache.cactus.internal.RequestDirectives;
 30   
 import org.apache.cactus.internal.WebRequestImpl;
 31   
 import org.apache.cactus.internal.client.WebResponseObjectFactory;
 32   
 import org.apache.cactus.internal.configuration.WebConfiguration;
 33   
 import org.apache.cactus.internal.util.JUnitVersionHelper;
 34   
 import org.apache.cactus.spi.client.ResponseObjectFactory;
 35   
 import org.apache.cactus.spi.client.connector.ProtocolHandler;
 36   
 import org.apache.cactus.spi.client.connector.ProtocolState;
 37   
 
 38   
 /**
 39   
  * Implementation for the HTTP protocol. It connects to the redirector proxy
 40   
  * using HTTP and passing Cactus information (test case to run, etc) as HTTP
 41   
  * GET parameters. 
 42   
  * 
 43   
  * @version $Id: HttpProtocolHandler.java 238991 2004-05-22 11:34:50Z vmassol $
 44   
  */
 45   
 public class HttpProtocolHandler implements ProtocolHandler
 46   
 {
 47   
     /**
 48   
      * Cactus configuration data to use. In particular contains useful 
 49   
      * configuration data for the HTTP connector (e.g. redirector URL).
 50   
      */
 51   
     private WebConfiguration configuration;
 52   
 
 53   
     /**
 54   
      * @param theConfiguration configuration data
 55   
      */
 56  48
     public HttpProtocolHandler(WebConfiguration theConfiguration)
 57   
     {
 58  48
         this.configuration = theConfiguration;
 59   
     }
 60   
 
 61   
     // Interface methods ----------------------------------------------------
 62   
     
 63   
     /**
 64   
      * @see ProtocolHandler#createRequest()
 65   
      */
 66  24
     public Request createRequest()
 67   
     {
 68  24
         return new WebRequestImpl(getConfiguration());
 69   
     }
 70   
     
 71   
     /**
 72   
      * @see ProtocolHandler#runTest(Test, Test, Request)
 73   
      */
 74  24
     public ProtocolState runTest(Test theDelegatedTest, Test theWrappedTest,
 75   
         Request theRequest) throws Throwable
 76   
     {
 77  24
         WebRequest request = (WebRequest) theRequest;
 78   
 
 79   
         // Run the web test
 80  24
         HttpURLConnection connection = runWebTest(theDelegatedTest, 
 81   
             theWrappedTest, request); 
 82   
 
 83  24
         HttpProtocolState state = new HttpProtocolState();
 84  24
         state.setConnection(connection);
 85  24
         return state;
 86   
     }
 87   
 
 88   
     /**
 89   
      * @see ProtocolHandler#createResponseObjectFactory(ProtocolState)
 90   
      */
 91  48
     public ResponseObjectFactory createResponseObjectFactory(
 92   
         ProtocolState theState)
 93   
     {
 94  48
         HttpProtocolState state = (HttpProtocolState) theState;
 95  48
         return new WebResponseObjectFactory(state.getConnection());
 96   
     }
 97   
     
 98   
     /**
 99   
      * @see ProtocolHandler#afterTest(ProtocolState)
 100   
      */
 101  24
     public void afterTest(ProtocolState theState) throws IOException
 102   
     {
 103  24
         HttpProtocolState state = (HttpProtocolState) theState;
 104   
         
 105   
         // Close the input stream (just in the case the user has not done it
 106   
         // in it's endXXX method (or if it has no endXXX method) ....
 107  24
         state.getConnection().getInputStream().close();       
 108   
     }
 109   
 
 110   
     // Private methods ----------------------------------------------------
 111   
     
 112   
     /**
 113   
      * @return configuration data
 114   
      */
 115  48
     private WebConfiguration getConfiguration()
 116   
     {
 117  48
         return this.configuration;
 118   
     }
 119   
     
 120   
     /**
 121   
      * Run the web test by connecting to the server redirector proxy and
 122   
      * execute the tests on the server side.
 123   
      *
 124   
      * @param theDelegatedTest the Cactus test to execute
 125   
      * @param theWrappedTest optionally specify a pure JUnit test case that is
 126   
      *        being wrapped and will be executed on the server side
 127   
      * @param theRequest the request containing data to connect to the 
 128   
      *        redirector proxy
 129   
      * @return the HTTP connection object that was used to call the server side
 130   
      * @exception Throwable any error that occurred when calling the test method
 131   
      *            for the current test case.
 132   
      */
 133  24
     private HttpURLConnection runWebTest(Test theDelegatedTest, 
 134   
         Test theWrappedTest, WebRequest theRequest) throws Throwable
 135   
     {
 136   
         // Add the class name, the method name, to the request to simulate and
 137   
         // automatic session creation flag to the request
 138  24
         RequestDirectives directives = new RequestDirectives(theRequest);
 139  24
         directives.setClassName(theDelegatedTest.getClass().getName());
 140  24
         directives.setMethodName(getCurrentTestName(theDelegatedTest));
 141  24
         directives.setAutoSession(
 142  24
             theRequest.getAutomaticSession() ? "true" : "false");
 143   
 
 144   
         // Add the wrapped test if it is not equal to our current instance
 145  24
         if (theWrappedTest != null)
 146   
         {
 147  0
             directives.setWrappedTestName(theWrappedTest.getClass().getName());
 148   
         }
 149   
 
 150   
         // Add the simulated URL (if one has been defined)
 151  24
         if (theRequest.getURL() != null)
 152   
         {
 153  1
             theRequest.getURL().saveToRequest(theRequest);
 154   
         }
 155   
 
 156   
         // Open the HTTP connection to the servlet redirector and manage errors
 157   
         // that could be returned in the HTTP response.
 158  24
         DefaultHttpClient client = new DefaultHttpClient(getConfiguration());
 159  24
         HttpURLConnection connection = client.doTest(theRequest);
 160   
 
 161  24
         return connection;
 162   
     }
 163   
 
 164   
     /**
 165   
      * @param theDelegatedTest the Cactus test to execute
 166   
      * @return the name of the current test case being executed (it corresponds
 167   
      *         to the name of the test method with the "test" prefix removed.
 168   
      *         For example, for "testSomeTestOk" would return "someTestOk".
 169   
      */
 170  24
     private String getCurrentTestName(Test theDelegatedTest)
 171   
     {
 172  24
         return JUnitVersionHelper.getTestCaseName(theDelegatedTest);        
 173   
     }
 174   
 }
 175