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: 224451 $
4    * $Date: 2005-07-23 06:23:59 -0400 (Sat, 23 Jul 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  
46  /***
47   * Base class for test cases using 
48   * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based 
49   * testing framework.
50   *
51   * @author Oleg Kalnichevski
52   * 
53   * @version $Id: HttpClientTestBase.java 224451 2005-07-23 10:23:59Z olegk $
54   */
55  public class HttpClientTestBase extends TestCase {
56  
57      protected HttpClient client = null;
58      protected SimpleHttpServer server = null;
59  
60      protected SimpleProxy proxy = null;
61      private boolean useProxy = false;
62      private boolean useSSL = false;
63      
64      // ------------------------------------------------------------ Constructor
65      public HttpClientTestBase(final String testName) throws IOException {
66          super(testName);
67      }
68  
69      // ------------------------------------------------------------------- Main
70      public static void main(String args[]) {
71          String[] testCaseName = { HttpClientTestBase.class.getName() };
72          junit.textui.TestRunner.main(testCaseName);
73      }
74  
75      // ------------------------------------------------------- TestCase Methods
76  
77      public static Test suite() {
78          return new TestSuite(HttpClientTestBase.class);
79      }
80  
81      public void setUseProxy(boolean useProxy) {
82          this.useProxy = useProxy;
83      }
84      
85      public void setUseSSL(boolean b) {
86          this.useSSL = b;
87      }
88      
89      public boolean isUseSSL() {
90          return this.useSSL;
91      }
92      
93      // ------------------------------------------------- TestCase setup/shutdown
94  
95      public void setUp() throws IOException {
96          // Configure socket factories
97          SimpleSocketFactory serversocketfactory = null; 
98          Protocol testhttp = null;
99          if (this.useSSL) {
100             serversocketfactory = new SimpleSSLSocketFactory(); 
101             testhttp = new Protocol("https", 
102                     (ProtocolSocketFactory)new SimpleSSLTestProtocolSocketFactory(), 443);
103         } else {
104             serversocketfactory = new SimplePlainSocketFactory(); 
105             testhttp = Protocol.getProtocol("http"); 
106         }
107 
108         this.server = new SimpleHttpServer(serversocketfactory, 0); // use arbitrary port
109         this.server.setTestname(getName());
110 
111         this.client = new HttpClient();
112 
113         this.client.getHostConfiguration().setHost(
114                 this.server.getLocalAddress(), 
115                 this.server.getLocalPort(),
116                 testhttp);
117         
118         if (this.useProxy) {
119             this.proxy = new SimpleProxy();
120             client.getHostConfiguration().setProxy(
121                 proxy.getLocalAddress(), 
122                 proxy.getLocalPort());                
123         }
124     }
125 
126     public void tearDown() throws IOException {
127         this.client = null;
128         this.server.destroy();
129         this.server = null;
130         if (proxy != null) {
131             proxy.destroy();
132             proxy = null;
133         }
134     }    
135 }