001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java,v 1.7 2004/11/07 12:31:42 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: */
029:
030: package org.apache.commons.httpclient;
031:
032: import java.io.IOException;
033:
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: import org.apache.commons.httpclient.protocol.Protocol;
039: import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
040: import org.apache.commons.httpclient.server.SimpleHttpServer;
041: import org.apache.commons.httpclient.server.SimplePlainSocketFactory;
042: import org.apache.commons.httpclient.server.SimpleProxy;
043: import org.apache.commons.httpclient.server.SimpleSocketFactory;
044: import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory;
045: import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory;
046:
047: /**
048: * Base class for test cases using
049: * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based
050: * testing framework.
051: *
052: * @author Oleg Kalnichevski
053: *
054: * @version $Id: HttpClientTestBase.java 480424 2006-11-29 05:56:49Z bayard $
055: */
056: public class HttpClientTestBase extends TestCase {
057:
058: protected HttpClient client = null;
059: protected SimpleHttpServer server = null;
060:
061: protected SimpleProxy proxy = null;
062: private boolean useProxy = false;
063: private boolean useSSL = false;
064:
065: // ------------------------------------------------------------ Constructor
066: public HttpClientTestBase(final String testName) throws IOException {
067: super (testName);
068: }
069:
070: // ------------------------------------------------------------------- Main
071: public static void main(String args[]) {
072: String[] testCaseName = { HttpClientTestBase.class.getName() };
073: junit.textui.TestRunner.main(testCaseName);
074: }
075:
076: // ------------------------------------------------------- TestCase Methods
077:
078: public static Test suite() {
079: return new TestSuite(HttpClientTestBase.class);
080: }
081:
082: public void setUseProxy(boolean useProxy) {
083: this .useProxy = useProxy;
084: }
085:
086: public void setUseSSL(boolean b) {
087: this .useSSL = b;
088: }
089:
090: public boolean isUseSSL() {
091: return this .useSSL;
092: }
093:
094: // ------------------------------------------------- TestCase setup/shutdown
095:
096: public void setUp() throws IOException {
097: // Configure socket factories
098: SimpleSocketFactory serversocketfactory = null;
099: Protocol testhttp = null;
100: if (this .useSSL) {
101: serversocketfactory = new SimpleSSLSocketFactory();
102: testhttp = new Protocol(
103: "https",
104: (ProtocolSocketFactory) new SimpleSSLTestProtocolSocketFactory(),
105: 443);
106: } else {
107: serversocketfactory = new SimplePlainSocketFactory();
108: testhttp = Protocol.getProtocol("http");
109: }
110:
111: this .server = new SimpleHttpServer(serversocketfactory, 0); // use arbitrary port
112: this .server.setTestname(getName());
113:
114: this .client = new HttpClient();
115:
116: this .client.getHostConfiguration().setHost(
117: this .server.getLocalAddress(),
118: this .server.getLocalPort(), testhttp);
119:
120: if (this .useProxy) {
121: this .proxy = new SimpleProxy();
122: client.getHostConfiguration().setProxy(
123: proxy.getLocalAddress(), proxy.getLocalPort());
124: }
125: }
126:
127: public void tearDown() throws IOException {
128: this.client = null;
129: this.server.destroy();
130: this.server = null;
131: if (proxy != null) {
132: proxy.destroy();
133: proxy = null;
134: }
135: }
136: }
|