HttpClientTestBase xref

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