ProxyTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package tests.api.java.net;
17
18import java.net.InetSocketAddress;
19import java.net.Proxy;
20import java.net.SocketAddress;
21
22import junit.framework.TestCase;
23
24public class ProxyTest extends TestCase {
25
26	private SocketAddress address = new InetSocketAddress("127.0.0.1", 1234);
27
28	/**
29	 * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
30	 */
31	public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal() {
32		// test HTTP type proxy
33		Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
34		assertEquals(Proxy.Type.HTTP, proxy.type());
35		assertEquals(address, proxy.address());
36
37		// test SOCKS type proxy
38		proxy = new Proxy(Proxy.Type.SOCKS, address);
39		assertEquals(Proxy.Type.SOCKS, proxy.type());
40		assertEquals(address, proxy.address());
41
42		// test DIRECT type proxy
43		proxy = Proxy.NO_PROXY;
44		assertEquals(Proxy.Type.DIRECT, proxy.type());
45		assertNull(proxy.address());
46	}
47
48	/**
49	 * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
50	 */
51	public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_IllegalAddress() {
52		Proxy proxy = null;
53		// test HTTP type proxy
54		try {
55			proxy = new Proxy(Proxy.Type.HTTP, null);
56			fail("should throw IllegalArgumentException");
57		} catch (IllegalArgumentException e) {
58			// expected
59		}
60		// test SOCKS type proxy
61		try {
62			proxy = new Proxy(Proxy.Type.SOCKS, null);
63			fail("should throw IllegalArgumentException");
64		} catch (IllegalArgumentException e) {
65			// expected
66		}
67		// test DIRECT type proxy
68		try {
69			proxy = new Proxy(Proxy.Type.DIRECT, null);
70			fail("should throw IllegalArgumentException");
71		} catch (IllegalArgumentException e) {
72			// expected
73		}
74		// test DIRECT type proxy, any address is illegal
75		try {
76			proxy = new Proxy(Proxy.Type.DIRECT, address);
77			fail("should throw IllegalArgumentException");
78		} catch (IllegalArgumentException e) {
79			// expected
80		}
81
82	}
83
84	/**
85	 * @tests java.net.Proxy#hashCode()
86	 * @see also see test_equalsLjava_lang_Object_Equals
87	 */
88	public void test_hashCode() {
89		// This method has been tested in test_equalsLjava_lang_Object_Equals.
90	}
91
92	/**
93	 * @tests java.net.Proxy#type()
94	 */
95	public void test_type() {
96		// This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
97	}
98
99	/**
100	 * @tests java.net.Proxy#address() This method has been tested in
101	 *        Constructor test case.
102	 */
103	public void test_address() {
104		// This method has been tested in test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal.
105	}
106
107	/**
108	 * @tests java.net.Proxy#toString()
109	 */
110	public void test_toString() {
111		Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
112		// include type String
113		assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
114		// include address String
115		assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
116
117		proxy = new Proxy(Proxy.Type.SOCKS, address);
118		// include type String
119		assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
120		// include address String
121		assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
122
123		proxy = Proxy.NO_PROXY;
124		// include type String
125		assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
126
127		proxy = new Proxy(null, address);
128		// ensure no NPE is thrown
129		proxy.toString();
130
131	}
132
133	/**
134	 * @tests java.net.Proxy#equals(Object)
135	 */
136	public void test_equalsLjava_lang_Object_Equals() {
137		SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
138		SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1234);
139		// HTTP type
140		Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
141		Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address2);
142		assertTrue(proxy1.equals(proxy2));
143		// assert hashCode
144		assertTrue(proxy1.hashCode() == proxy2.hashCode());
145
146		// SOCKS type
147		Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1);
148		Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address2);
149		assertTrue(proxy3.equals(proxy4));
150		// assert hashCode
151		assertTrue(proxy3.hashCode() == proxy4.hashCode());
152
153		// null type
154		Proxy proxy5 = new Proxy(null, address1);
155		Proxy proxy6 = new Proxy(null, address2);
156		assertTrue(proxy5.equals(proxy6));
157	}
158
159	/**
160	 * @tests java.net.Proxy#equals(Object)
161	 */
162	public void test_equalsLjava_lang_Object_NotEquals() {
163		SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
164		SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
165		Proxy proxy[] = { new Proxy(Proxy.Type.HTTP, address1),
166				new Proxy(Proxy.Type.HTTP, address2),
167				new Proxy(Proxy.Type.SOCKS, address1),
168				new Proxy(Proxy.Type.SOCKS, address2), Proxy.NO_PROXY,
169				new Proxy(null, address1), new Proxy(null, address2) };
170		// All of them are not equals
171		for (int i = 0; i < proxy.length; i++) {
172			for (int j = i + 1; j < proxy.length; j++) {
173				assertFalse(proxy[i].equals(proxy[j]));
174			}
175		}
176		// Not equals to an Object type instance. Ensure no exception is thrown.
177		assertFalse(proxy[0].equals(new Object()));
178	}
179
180	/**
181	 * @tests java.net.Proxy.Type#valueOf(String)
182	 */
183	public void test_Type_valueOfLjava_lang_String_Normal() {
184		assertEquals(Proxy.Type.DIRECT, Proxy.Type.valueOf("DIRECT"));
185		assertEquals(Proxy.Type.HTTP, Proxy.Type.valueOf("HTTP"));
186		assertEquals(Proxy.Type.SOCKS, Proxy.Type.valueOf("SOCKS"));
187	}
188
189	/**
190	 * @tests java.net.Proxy.Type#valueOf(String)
191	 */
192	public void test_Type_valueOfLjava_lang_String_IllegalName() {
193		String[] illegalName = { "Direct", "direct", "http", "socks",
194				"illegalName", "" };
195		for (int i = 0; i < illegalName.length; i++) {
196			try {
197				Proxy.Type.valueOf(illegalName[i]);
198				fail("should throw IllegalArgumentException, illegalName:"
199						+ illegalName);
200			} catch (IllegalArgumentException e) {
201				// expected
202			}
203		}
204	}
205
206	/**
207	 * @tests java.net.Proxy.Type#valueOf(String)
208	 */
209	public void test_Type_valueOfLjava_lang_String_NullPointerException() {
210		// Some old RIs,which throw IllegalArgumentException.
211        // Latest RIs throw NullPointerException.
212		try {
213			Proxy.Type.valueOf(null);
214			fail("should throw an exception.");
215        } catch (NullPointerException e) {
216            // May be caused by some compilers' code
217        } catch (IllegalArgumentException e) {
218            // other compilers will throw this
219        }
220	}
221
222	/**
223	 * @tests java.net.Proxy.Type#values()
224	 */
225	public void test_Type_values() {
226		Proxy.Type types[] = Proxy.Type.values();
227		assertEquals(3, types.length);
228		assertEquals(Proxy.Type.DIRECT, types[0]);
229		assertEquals(Proxy.Type.HTTP, types[1]);
230		assertEquals(Proxy.Type.SOCKS, types[2]);
231	}
232
233}
234