1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
63 public HttpClientTestBase(final String testName) throws IOException {
64 super(testName);
65 }
66
67
68 public static void main(String args[]) {
69 String[] testCaseName = { HttpClientTestBase.class.getName() };
70 junit.textui.TestRunner.main(testCaseName);
71 }
72
73
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
84
85 public void setUp() throws IOException {
86 this.server = new SimpleHttpServer();
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 }