1   /*
2    * $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 $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    * ====================================================================
6    *
7    *  Copyright 1999-2004 The Apache Software Foundation
8    *
9    *  Licensed under the Apache License, Version 2.0 (the "License");
10   *  you may not use this file except in compliance with the License.
11   *  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   *  Unless required by applicable law or agreed to in writing, software
16   *  distributed under the License is distributed on an "AS IS" BASIS,
17   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   *  See the License for the specific language governing permissions and
19   *  limitations under the License.
20   * ====================================================================
21   *
22   * This software consists of voluntary contributions made by many
23   * individuals on behalf of the Apache Software Foundation.  For more
24   * information on the Apache Software Foundation, please see
25   * <http://www.apache.org/>.
26   *
27   */
28  
29  package org.apache.commons.httpclient;
30  
31  import java.io.IOException;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.httpclient.protocol.Protocol;
38  import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
39  import org.apache.commons.httpclient.server.SimpleHttpServer;
40  import org.apache.commons.httpclient.server.SimplePlainSocketFactory;
41  import org.apache.commons.httpclient.server.SimpleProxy;
42  import org.apache.commons.httpclient.server.SimpleSocketFactory;
43  import org.apache.commons.httpclient.ssl.SimpleSSLSocketFactory;
44  import org.apache.commons.httpclient.ssl.SimpleSSLTestProtocolSocketFactory;
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /***
49   * Base class for test cases using 
50   * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based 
51   * testing framework.
52   *
53   * @author Oleg Kalnichevski
54   * 
55   * @version $Id: HttpClientTestBase.java 155418 2005-02-26 13:01:52Z dirkv $
56   */
57  public class HttpClientTestBase extends TestCase {
58  
59      private static final Log LOG = LogFactory.getLog(HttpClientTestBase.class);
60      
61      protected HttpClient client = null;
62      protected SimpleHttpServer server = null;
63  
64      protected SimpleProxy proxy = null;
65      private boolean useProxy = false;
66      private boolean useSSL = false;
67      
68      // ------------------------------------------------------------ Constructor
69      public HttpClientTestBase(final String testName) throws IOException {
70          super(testName);
71      }
72  
73      // ------------------------------------------------------------------- Main
74      public static void main(String args[]) {
75          String[] testCaseName = { HttpClientTestBase.class.getName() };
76          junit.textui.TestRunner.main(testCaseName);
77      }
78  
79      // ------------------------------------------------------- TestCase Methods
80  
81      public static Test suite() {
82          return new TestSuite(HttpClientTestBase.class);
83      }
84  
85      public void setUseProxy(boolean useProxy) {
86          this.useProxy = useProxy;
87      }
88      
89      public void setUseSSL(boolean b) {
90          this.useSSL = b;
91      }
92      
93      public boolean isUseSSL() {
94          return this.useSSL;
95      }
96      
97      // ------------------------------------------------- TestCase setup/shutdown
98  
99      public void setUp() throws IOException {
100         // Configure socket factories
101         SimpleSocketFactory serversocketfactory = null; 
102         Protocol testhttp = null;
103         if (this.useSSL) {
104             serversocketfactory = new SimpleSSLSocketFactory(); 
105             testhttp = new Protocol("https", 
106                     (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
107         } else {
108             serversocketfactory = new SimplePlainSocketFactory(); 
109             testhttp = Protocol.getProtocol("http"); 
110         }
111 
112         this.server = new SimpleHttpServer(serversocketfactory, 0); // use arbitrary port
113         this.server.setTestname(getName());
114 
115         this.client = new HttpClient();
116 
117         this.client.getHostConfiguration().setHost(
118                 this.server.getLocalAddress(), 
119                 this.server.getLocalPort(),
120                 testhttp);
121         
122         if (this.useProxy) {
123             this.proxy = new SimpleProxy();
124             client.getHostConfiguration().setProxy(
125                 proxy.getLocalAddress(), 
126                 proxy.getLocalPort());                
127         }
128     }
129 
130     public void tearDown() throws IOException {
131         this.client = null;
132         this.server.destroy();
133         this.server = null;
134         if (proxy != null) {
135             proxy.destroy();
136             proxy = null;
137         }
138     }    
139 }