1   /*
2    * $Header: /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: 1.7 $
4    * $Date: 2004/11/07 12:31:42 $
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.server.SimpleHttpServer;
39  import org.apache.commons.httpclient.server.SimpleProxy;
40  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /***
44   * Base class for test cases using 
45   * {@link org.apache.commons.httpclient.server.SimpleHttpServer} based 
46   * testing framework.
47   *
48   * @author Oleg Kalnichevski
49   * 
50   * @version $Id: HttpClientTestBase.java,v 1.7 2004/11/07 12:31:42 olegk Exp $
51   */
52  public class HttpClientTestBase extends TestCase {
53  
54      private static final Log LOG = LogFactory.getLog(HttpClientTestBase.class);
55      
56      protected HttpClient client = null;
57      protected SimpleHttpServer server = null;
58  
59      protected SimpleProxy proxy = null;
60      private boolean useProxy = false;
61      
62      // ------------------------------------------------------------ Constructor
63      public HttpClientTestBase(final String testName) throws IOException {
64          super(testName);
65      }
66  
67      // ------------------------------------------------------------------- Main
68      public static void main(String args[]) {
69          String[] testCaseName = { HttpClientTestBase.class.getName() };
70          junit.textui.TestRunner.main(testCaseName);
71      }
72  
73      // ------------------------------------------------------- TestCase Methods
74  
75      public static Test suite() {
76          return new TestSuite(HttpClientTestBase.class);
77      }
78  
79      public void setUseProxy(boolean useProxy) {
80          this.useProxy = useProxy;
81      }
82      
83      // ------------------------------------------------- TestCase setup/shutdown
84  
85      public void setUp() throws IOException {
86          this.server = new SimpleHttpServer(); // use arbitrary port
87          this.server.setTestname(getName());
88  
89          this.client = new HttpClient();
90          this.client.getHostConfiguration().setHost(
91              this.server.getLocalAddress(), 
92              this.server.getLocalPort(),
93              Protocol.getProtocol("http"));
94          if (useProxy) {
95              this.proxy = new SimpleProxy();
96              client.getHostConfiguration().setProxy(
97                  proxy.getLocalAddress(), 
98                  proxy.getLocalPort());                
99          }
100     }
101 
102     public void tearDown() throws IOException {
103         this.client = null;
104         this.server.destroy();
105         this.server = null;
106         if (proxy != null) {
107             proxy.destroy();
108             proxy = null;
109         }
110     }    
111 }