|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DefaultHttpClient.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.client.connector.http;
|
|
58 |
|
|
59 |
import java.net.HttpURLConnection;
|
|
60 |
|
|
61 |
import org.apache.cactus.HttpServiceDefinition;
|
|
62 |
import org.apache.cactus.RequestDirectives;
|
|
63 |
import org.apache.cactus.ServiceEnumeration;
|
|
64 |
import org.apache.cactus.WebRequest;
|
|
65 |
import org.apache.cactus.WebTestResult;
|
|
66 |
import org.apache.cactus.client.AssertionFailedErrorWrapper;
|
|
67 |
import org.apache.cactus.client.ParsingException;
|
|
68 |
import org.apache.cactus.client.ServletExceptionWrapper;
|
|
69 |
import org.apache.cactus.client.WebTestResultParser;
|
|
70 |
import org.apache.cactus.configuration.WebConfiguration;
|
|
71 |
import org.apache.cactus.util.ChainedRuntimeException;
|
|
72 |
import org.apache.cactus.util.IoUtil;
|
|
73 |
|
|
74 |
/**
|
|
75 |
* Performs the steps necessary to run a test. It involves
|
|
76 |
* opening a first HTTP connection to a server redirector, reading the output
|
|
77 |
* stream and then opening a second HTTP connection to retrieve the test
|
|
78 |
* result.
|
|
79 |
*
|
|
80 |
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
|
|
81 |
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
|
|
82 |
*
|
|
83 |
* @version $Id: DefaultHttpClient.java,v 1.10 2003/07/09 16:36:07 cmlenz Exp $
|
|
84 |
*/
|
|
85 |
public class DefaultHttpClient |
|
86 |
{ |
|
87 |
/**
|
|
88 |
* Cactus configuration.
|
|
89 |
*/
|
|
90 |
protected WebConfiguration configuration;
|
|
91 |
|
|
92 |
/**
|
|
93 |
* Initialize the Http client.
|
|
94 |
*
|
|
95 |
* @param theConfiguration the Cactus configuration
|
|
96 |
*/
|
|
97 | 0 |
public DefaultHttpClient(WebConfiguration theConfiguration)
|
98 |
{ |
|
99 | 0 |
this.configuration = theConfiguration;
|
100 |
} |
|
101 |
|
|
102 |
/**
|
|
103 |
* Calls the test method indirectly by calling the Redirector servlet and
|
|
104 |
* then open a second HTTP connection to retrieve the test results.
|
|
105 |
*
|
|
106 |
* @param theRequest the request containing all data to pass to the
|
|
107 |
* redirector servlet.
|
|
108 |
*
|
|
109 |
* @return the <code>HttpURLConnection</code> that contains the HTTP
|
|
110 |
* response when the test was called.
|
|
111 |
*
|
|
112 |
* @exception Throwable if an error occured in the test method or in the
|
|
113 |
* redirector servlet.
|
|
114 |
*/
|
|
115 | 0 |
public HttpURLConnection doTest(WebRequest theRequest) throws Throwable |
116 |
{ |
|
117 |
// Open the first connection to the redirector to execute the test on
|
|
118 |
// the server side
|
|
119 | 0 |
HttpURLConnection connection = callRunTest(theRequest); |
120 |
|
|
121 |
// Open the second connection to get the test results
|
|
122 | 0 |
WebTestResult result = null;
|
123 |
|
|
124 | 0 |
try
|
125 |
{ |
|
126 | 0 |
result = callGetResult(theRequest); |
127 |
} |
|
128 |
catch (ParsingException e)
|
|
129 |
{ |
|
130 | 0 |
String url = this.configuration.getRedirectorURL(theRequest);
|
131 | 0 |
throw new ChainedRuntimeException("Failed to get the test " |
132 |
+ "results at [" + url + "]", e); |
|
133 |
} |
|
134 |
|
|
135 |
// Check if the returned result object returned contains an error or
|
|
136 |
// not. If yes, we need to raise an exception so that the JUnit
|
|
137 |
// framework can catch it
|
|
138 | 0 |
if (result.hasException())
|
139 |
{ |
|
140 |
// Wrap the exception message and stack trace into a fake
|
|
141 |
// exception class with overloaded <code>printStackTrace()</code>
|
|
142 |
// methods so that when JUnit calls this method it will print the
|
|
143 |
// stack trace that was set on the server side.
|
|
144 |
// If the error was an AssertionFailedError then we use an instance
|
|
145 |
// of AssertionFailedErrorWrapper (so that JUnit recognize it is
|
|
146 |
// an AssertionFailedError exception and print it differently in
|
|
147 |
// it's runner console). Otherwise we use an instance of
|
|
148 |
// ServletExceptionWrapper.
|
|
149 |
|
|
150 |
// Note: We have to test the exceptions by string name as the JUnit
|
|
151 |
// AssertionFailedError class is unfortunately not serializable...
|
|
152 |
|
|
153 | 0 |
if ((result.getExceptionClassName().equals(
|
154 |
"junit.framework.AssertionFailedError"))
|
|
155 |
|| (result.getExceptionClassName().equals( |
|
156 |
"junit.framework.ComparisonFailure")))
|
|
157 |
{ |
|
158 | 0 |
throw new AssertionFailedErrorWrapper( |
159 |
result.getExceptionMessage(), |
|
160 |
result.getExceptionClassName(), |
|
161 |
result.getExceptionStackTrace()); |
|
162 |
} |
|
163 |
else
|
|
164 |
{ |
|
165 | 0 |
throw new ServletExceptionWrapper( |
166 |
result.getExceptionMessage(), |
|
167 |
result.getExceptionClassName(), |
|
168 |
result.getExceptionStackTrace()); |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 | 0 |
return connection;
|
173 |
} |
|
174 |
|
|
175 |
/**
|
|
176 |
* Execute the test by calling the redirector.
|
|
177 |
*
|
|
178 |
* @param theRequest the request containing all data to pass to the
|
|
179 |
* redirector servlet.
|
|
180 |
* @return the <code>HttpURLConnection</code> that contains the HTTP
|
|
181 |
* response when the test was called.
|
|
182 |
*
|
|
183 |
* @exception Throwable if an error occured in the test method or in the
|
|
184 |
* redirector servlet.
|
|
185 |
*/
|
|
186 | 0 |
private HttpURLConnection callRunTest(WebRequest theRequest)
|
187 |
throws Throwable
|
|
188 |
{ |
|
189 |
// Specify the service to call on the redirector side
|
|
190 | 0 |
theRequest.addParameter(HttpServiceDefinition.SERVICE_NAME_PARAM, |
191 |
ServiceEnumeration.CALL_TEST_SERVICE.toString(), |
|
192 |
WebRequest.GET_METHOD); |
|
193 |
|
|
194 |
// Open the first connection to the redirector to execute the test on
|
|
195 |
// the server side
|
|
196 | 0 |
ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper( |
197 |
this.configuration.getRedirectorURL(theRequest),
|
|
198 |
this.configuration);
|
|
199 |
|
|
200 | 0 |
HttpURLConnection connection = |
201 |
helper.connect(theRequest, this.configuration);
|
|
202 |
|
|
203 |
// Wrap the connection to ensure that all servlet output is read
|
|
204 |
// before we ask for results
|
|
205 | 0 |
connection = new AutoReadHttpURLConnection(connection);
|
206 |
|
|
207 |
// Trigger the transfer of data
|
|
208 | 0 |
connection.getInputStream(); |
209 |
|
|
210 | 0 |
return connection;
|
211 |
} |
|
212 |
|
|
213 |
/**
|
|
214 |
* Get the test result from the redirector.
|
|
215 |
*
|
|
216 |
* @param theOriginalRequest the request that was used to run the test
|
|
217 |
* @return the result that was returned by the redirector.
|
|
218 |
*
|
|
219 |
* @exception Throwable if an error occured in the test method or in the
|
|
220 |
* redirector servlet.
|
|
221 |
*/
|
|
222 | 0 |
private WebTestResult callGetResult(WebRequest theOriginalRequest)
|
223 |
throws Throwable
|
|
224 |
{ |
|
225 | 0 |
WebRequest resultsRequest = new WebRequest(this.configuration); |
226 | 0 |
RequestDirectives directives = new RequestDirectives(resultsRequest);
|
227 | 0 |
directives.setService(ServiceEnumeration.GET_RESULTS_SERVICE); |
228 |
|
|
229 |
// Use the same redirector as was used by the original request
|
|
230 | 0 |
resultsRequest.setRedirectorName( |
231 |
theOriginalRequest.getRedirectorName()); |
|
232 |
|
|
233 |
// Add authentication details
|
|
234 | 0 |
if (theOriginalRequest.getAuthentication() != null) |
235 |
{ |
|
236 | 0 |
resultsRequest.setAuthentication( |
237 |
theOriginalRequest.getAuthentication()); |
|
238 |
} |
|
239 |
|
|
240 |
// Open the second connection to get the test results
|
|
241 | 0 |
ConnectionHelper helper = ConnectionHelperFactory.getConnectionHelper( |
242 |
this.configuration.getRedirectorURL(resultsRequest),
|
|
243 |
this.configuration);
|
|
244 |
|
|
245 | 0 |
HttpURLConnection resultConnection = |
246 |
helper.connect(resultsRequest, this.configuration);
|
|
247 | 0 |
if (resultConnection.getResponseCode() != 200)
|
248 |
{ |
|
249 | 0 |
throw new ParsingException("Not a valid response [" |
250 |
+ resultConnection.getResponseCode() + " "
|
|
251 |
+ resultConnection.getResponseMessage() + "]");
|
|
252 |
} |
|
253 |
|
|
254 |
// Read the test result
|
|
255 | 0 |
WebTestResultParser parser = new WebTestResultParser();
|
256 | 0 |
return parser.parse(
|
257 |
IoUtil.getText(resultConnection.getInputStream(), "UTF-8"));
|
|
258 |
} |
|
259 |
} |
|
260 |
|
|