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

For more information, please explore the Attic.

View Javadoc

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