001: /*
002: * ========================================================================
003: *
004: * Copyright 2003-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.internal.client.connector.http;
021:
022: import java.io.IOException;
023: import java.net.HttpURLConnection;
024:
025: import junit.framework.Test;
026:
027: import org.apache.cactus.Request;
028: import org.apache.cactus.WebRequest;
029: import org.apache.cactus.internal.RequestDirectives;
030: import org.apache.cactus.internal.WebRequestImpl;
031: import org.apache.cactus.internal.client.WebResponseObjectFactory;
032: import org.apache.cactus.internal.configuration.WebConfiguration;
033: import org.apache.cactus.internal.util.JUnitVersionHelper;
034: import org.apache.cactus.spi.client.ResponseObjectFactory;
035: import org.apache.cactus.spi.client.connector.ProtocolHandler;
036: import org.apache.cactus.spi.client.connector.ProtocolState;
037:
038: /**
039: * Implementation for the HTTP protocol. It connects to the redirector proxy
040: * using HTTP and passing Cactus information (test case to run, etc) as HTTP
041: * GET parameters.
042: *
043: * @version $Id: HttpProtocolHandler.java 238991 2004-05-22 11:34:50Z vmassol $
044: */
045: public class HttpProtocolHandler implements ProtocolHandler {
046: /**
047: * Cactus configuration data to use. In particular contains useful
048: * configuration data for the HTTP connector (e.g. redirector URL).
049: */
050: private WebConfiguration configuration;
051:
052: /**
053: * @param theConfiguration configuration data
054: */
055: public HttpProtocolHandler(WebConfiguration theConfiguration) {
056: this .configuration = theConfiguration;
057: }
058:
059: // Interface methods ----------------------------------------------------
060:
061: /**
062: * @see ProtocolHandler#createRequest()
063: */
064: public Request createRequest() {
065: return new WebRequestImpl(getConfiguration());
066: }
067:
068: /**
069: * @see ProtocolHandler#runTest(Test, Test, Request)
070: */
071: public ProtocolState runTest(Test theDelegatedTest,
072: Test theWrappedTest, Request theRequest) throws Throwable {
073: WebRequest request = (WebRequest) theRequest;
074:
075: // Run the web test
076: HttpURLConnection connection = runWebTest(theDelegatedTest,
077: theWrappedTest, request);
078:
079: HttpProtocolState state = new HttpProtocolState();
080: state.setConnection(connection);
081: return state;
082: }
083:
084: /**
085: * @see ProtocolHandler#createResponseObjectFactory(ProtocolState)
086: */
087: public ResponseObjectFactory createResponseObjectFactory(
088: ProtocolState theState) {
089: HttpProtocolState state = (HttpProtocolState) theState;
090: return new WebResponseObjectFactory(state.getConnection());
091: }
092:
093: /**
094: * @see ProtocolHandler#afterTest(ProtocolState)
095: */
096: public void afterTest(ProtocolState theState) throws IOException {
097: HttpProtocolState state = (HttpProtocolState) theState;
098:
099: // Close the input stream (just in the case the user has not done it
100: // in it's endXXX method (or if it has no endXXX method) ....
101: state.getConnection().getInputStream().close();
102: }
103:
104: // Private methods ----------------------------------------------------
105:
106: /**
107: * @return configuration data
108: */
109: private WebConfiguration getConfiguration() {
110: return this .configuration;
111: }
112:
113: /**
114: * Run the web test by connecting to the server redirector proxy and
115: * execute the tests on the server side.
116: *
117: * @param theDelegatedTest the Cactus test to execute
118: * @param theWrappedTest optionally specify a pure JUnit test case that is
119: * being wrapped and will be executed on the server side
120: * @param theRequest the request containing data to connect to the
121: * redirector proxy
122: * @return the HTTP connection object that was used to call the server side
123: * @exception Throwable any error that occurred when calling the test method
124: * for the current test case.
125: */
126: private HttpURLConnection runWebTest(Test theDelegatedTest,
127: Test theWrappedTest, WebRequest theRequest)
128: throws Throwable {
129: // Add the class name, the method name, to the request to simulate and
130: // automatic session creation flag to the request
131: RequestDirectives directives = new RequestDirectives(theRequest);
132: directives.setClassName(theDelegatedTest.getClass().getName());
133: directives.setMethodName(getCurrentTestName(theDelegatedTest));
134: directives
135: .setAutoSession(theRequest.getAutomaticSession() ? "true"
136: : "false");
137:
138: // Add the wrapped test if it is not equal to our current instance
139: if (theWrappedTest != null) {
140: directives.setWrappedTestName(theWrappedTest.getClass()
141: .getName());
142: }
143:
144: // Add the simulated URL (if one has been defined)
145: if (theRequest.getURL() != null) {
146: theRequest.getURL().saveToRequest(theRequest);
147: }
148:
149: // Open the HTTP connection to the servlet redirector and manage errors
150: // that could be returned in the HTTP response.
151: DefaultHttpClient client = new DefaultHttpClient(
152: getConfiguration());
153: HttpURLConnection connection = client.doTest(theRequest);
154:
155: return connection;
156: }
157:
158: /**
159: * @param theDelegatedTest the Cactus test to execute
160: * @return the name of the current test case being executed (it corresponds
161: * to the name of the test method with the "test" prefix removed.
162: * For example, for "testSomeTestOk" would return "someTestOk".
163: */
164: private String getCurrentTestName(Test theDelegatedTest) {
165: return JUnitVersionHelper.getTestCaseName(theDelegatedTest);
166: }
167: }
|