KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > internal > client > connector > http > HttpProtocolHandler


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 JavaDoc;
23 import java.net.HttpURLConnection JavaDoc;
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,v 1.1 2004/05/22 11:34:46 vmassol Exp $
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     public HttpProtocolHandler(WebConfiguration theConfiguration)
57     {
58         this.configuration = theConfiguration;
59     }
60
61     // Interface methods ----------------------------------------------------
62

63     /**
64      * @see ProtocolHandler#createRequest()
65      */

66     public Request createRequest()
67     {
68         return new WebRequestImpl(getConfiguration());
69     }
70     
71     /**
72      * @see ProtocolHandler#runTest(Test, Test, Request)
73      */

74     public ProtocolState runTest(Test theDelegatedTest, Test theWrappedTest,
75         Request theRequest) throws Throwable JavaDoc
76     {
77         WebRequest request = (WebRequest) theRequest;
78
79         // Run the web test
80
HttpURLConnection JavaDoc connection = runWebTest(theDelegatedTest,
81             theWrappedTest, request);
82
83         HttpProtocolState state = new HttpProtocolState();
84         state.setConnection(connection);
85         return state;
86     }
87
88     /**
89      * @see ProtocolHandler#createResponseObjectFactory(ProtocolState)
90      */

91     public ResponseObjectFactory createResponseObjectFactory(
92         ProtocolState theState)
93     {
94         HttpProtocolState state = (HttpProtocolState) theState;
95         return new WebResponseObjectFactory(state.getConnection());
96     }
97     
98     /**
99      * @see ProtocolHandler#afterTest(ProtocolState)
100      */

101     public void afterTest(ProtocolState theState) throws IOException JavaDoc
102     {
103         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
state.getConnection().getInputStream().close();
108     }
109
110     // Private methods ----------------------------------------------------
111

112     /**
113      * @return configuration data
114      */

115     private WebConfiguration getConfiguration()
116     {
117         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     private HttpURLConnection JavaDoc runWebTest(Test theDelegatedTest,
134         Test theWrappedTest, WebRequest theRequest) throws Throwable JavaDoc
135     {
136         // Add the class name, the method name, to the request to simulate and
137
// automatic session creation flag to the request
138
RequestDirectives directives = new RequestDirectives(theRequest);
139         directives.setClassName(theDelegatedTest.getClass().getName());
140         directives.setMethodName(getCurrentTestName(theDelegatedTest));
141         directives.setAutoSession(
142             theRequest.getAutomaticSession() ? "true" : "false");
143
144         // Add the wrapped test if it is not equal to our current instance
145
if (theWrappedTest != null)
146         {
147             directives.setWrappedTestName(theWrappedTest.getClass().getName());
148         }
149
150         // Add the simulated URL (if one has been defined)
151
if (theRequest.getURL() != null)
152         {
153             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
DefaultHttpClient client = new DefaultHttpClient(getConfiguration());
159         HttpURLConnection JavaDoc connection = client.doTest(theRequest);
160
161         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     private String JavaDoc getCurrentTestName(Test theDelegatedTest)
171     {
172         return JUnitVersionHelper.getTestCaseName(theDelegatedTest);
173     }
174 }
175
Popular Tags