ProxyTest.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 dalvik.annotation.TestTargetClass;
19import dalvik.annotation.TestTargets;
20import dalvik.annotation.TestLevel;
21import dalvik.annotation.TestTargetNew;
22
23import java.net.InetSocketAddress;
24import java.net.Proxy;
25import java.net.SocketAddress;
26
27import junit.framework.TestCase;
28
29@TestTargetClass(Proxy.class)
30public class ProxyTest extends TestCase {
31
32    private SocketAddress address = new InetSocketAddress("127.0.0.1", 1234);
33
34    /**
35     * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
36     */
37    @TestTargetNew(
38        level = TestLevel.PARTIAL_COMPLETE,
39        notes = "This is a complete subset of tests for Proxy constructor.",
40        method = "Proxy",
41        args = {java.net.Proxy.Type.class, java.net.SocketAddress.class}
42    )
43    public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_Normal() {
44        // test HTTP type proxy
45        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
46        assertEquals(Proxy.Type.HTTP, proxy.type());
47        assertEquals(address, proxy.address());
48
49        // test SOCKS type proxy
50        proxy = new Proxy(Proxy.Type.SOCKS, address);
51        assertEquals(Proxy.Type.SOCKS, proxy.type());
52        assertEquals(address, proxy.address());
53
54        // test DIRECT type proxy
55        proxy = Proxy.NO_PROXY;
56        assertEquals(Proxy.Type.DIRECT, proxy.type());
57        assertNull(proxy.address());
58    }
59
60    /**
61     * @tests java.net.Proxy#Proxy(java.net.Proxy.Type, SocketAddress)
62     */
63    @TestTargetNew(
64        level = TestLevel.PARTIAL_COMPLETE,
65        notes = "This is a complete subset of tests for Proxy constructor.",
66        method = "Proxy",
67        args = {java.net.Proxy.Type.class, java.net.SocketAddress.class}
68    )
69    public void test_ConstructorLjava_net_ProxyLjava_net_SocketAddress_IllegalAddress() {
70        Proxy proxy = null;
71        // test HTTP type proxy
72        try {
73            proxy = new Proxy(Proxy.Type.HTTP, null);
74            fail("should throw IllegalArgumentException");
75        } catch (IllegalArgumentException e) {
76            // expected
77        }
78        // test SOCKS type proxy
79        try {
80            proxy = new Proxy(Proxy.Type.SOCKS, null);
81            fail("should throw IllegalArgumentException");
82        } catch (IllegalArgumentException e) {
83            // expected
84        }
85        // test DIRECT type proxy
86        try {
87            proxy = new Proxy(Proxy.Type.DIRECT, null);
88            fail("should throw IllegalArgumentException");
89        } catch (IllegalArgumentException e) {
90            // expected
91        }
92        // test DIRECT type proxy, any address is illegal
93        try {
94            proxy = new Proxy(Proxy.Type.DIRECT, address);
95            fail("should throw IllegalArgumentException");
96        } catch (IllegalArgumentException e) {
97            // expected
98        }
99
100    }
101
102    /**
103     * @tests java.net.Proxy#hashCode()
104     * @see test_equalsLjava_lang_Object_Equals
105     */
106    @TestTargetNew(
107        level = TestLevel.COMPLETE,
108        notes = "",
109        method = "hashCode",
110        args = {}
111    )
112    public void test_hashCode() {
113        SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
114
115        Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
116        Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address1);
117        assertTrue(proxy1.hashCode() == proxy2.hashCode());
118
119        Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1);
120        Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address1);
121        assertTrue(proxy1.hashCode() == proxy2.hashCode());
122
123        assertTrue(proxy1.hashCode() != proxy4.hashCode());
124
125        SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
126
127        Proxy proxy5 = new Proxy(Proxy.Type.SOCKS, address1);
128        Proxy proxy6 = new Proxy(Proxy.Type.SOCKS, address2);
129        assertTrue(proxy5.hashCode() != proxy6.hashCode());
130    }
131
132    /**
133     * @tests java.net.Proxy#type()
134     */
135    @TestTargetNew(
136        level = TestLevel.COMPLETE,
137        notes = "",
138        method = "type",
139        args = {}
140    )
141    public void test_type() {
142
143        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
144        assertEquals(Proxy.Type.HTTP, proxy.type());
145
146        proxy = new Proxy(Proxy.Type.SOCKS, address);
147        assertEquals(Proxy.Type.SOCKS, proxy.type());
148
149        proxy = Proxy.NO_PROXY;
150        assertEquals(Proxy.Type.DIRECT, proxy.type());
151    }
152
153    /**
154     * @tests java.net.Proxy#address() This method has been tested in
155     *        Constructor test case.
156     */
157    @TestTargetNew(
158        level = TestLevel.COMPLETE,
159        notes = "",
160        method = "address",
161        args = {}
162    )
163    public void test_address() {
164        Proxy proxy = new Proxy(Proxy.Type.SOCKS, address);
165        assertEquals(address, proxy.address());
166
167        try {
168            new Proxy(Proxy.Type.SOCKS, null);
169            fail("IllegalArgumentException was thrown.");
170        } catch(IllegalArgumentException iae) {
171            //expected
172        }
173    }
174
175    /**
176     * @tests java.net.Proxy#toString()
177     */
178    @TestTargetNew(
179        level = TestLevel.COMPLETE,
180        notes = "",
181        method = "toString",
182        args = {}
183    )
184    public void test_toString() {
185        Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
186        // include type String
187        assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
188        // include address String
189        assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
190
191        proxy = new Proxy(Proxy.Type.SOCKS, address);
192        // include type String
193        assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
194        // include address String
195        assertTrue(proxy.toString().indexOf(proxy.address().toString()) != -1);
196
197        proxy = Proxy.NO_PROXY;
198        // include type String
199        assertTrue(proxy.toString().indexOf(proxy.type().toString()) != -1);
200
201        proxy = new Proxy(null, address);
202        // ensure no NPE is thrown
203        proxy.toString();
204
205    }
206
207    /**
208     * @tests java.net.Proxy#equals(Object)
209     */
210    @TestTargetNew(
211        level = TestLevel.PARTIAL_COMPLETE,
212        notes = "This is a complete subset of tests for equals method.",
213        method = "equals",
214        args = {java.lang.Object.class}
215    )
216    public void test_equalsLjava_lang_Object_Equals() {
217        SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
218        SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1234);
219        // HTTP type
220        Proxy proxy1 = new Proxy(Proxy.Type.HTTP, address1);
221        Proxy proxy2 = new Proxy(Proxy.Type.HTTP, address2);
222        assertTrue(proxy1.equals(proxy2));
223        // assert hashCode
224        assertTrue(proxy1.hashCode() == proxy2.hashCode());
225
226        // SOCKS type
227        Proxy proxy3 = new Proxy(Proxy.Type.SOCKS, address1);
228        Proxy proxy4 = new Proxy(Proxy.Type.SOCKS, address2);
229        assertTrue(proxy3.equals(proxy4));
230        // assert hashCode
231        assertTrue(proxy3.hashCode() == proxy4.hashCode());
232
233        // null type
234        Proxy proxy5 = new Proxy(null, address1);
235        Proxy proxy6 = new Proxy(null, address2);
236        assertTrue(proxy5.equals(proxy6));
237    }
238
239    /**
240     * @tests java.net.Proxy#equals(Object)
241     */
242    @TestTargetNew(
243        level = TestLevel.PARTIAL_COMPLETE,
244        notes = "This is a complete subset of tests for equals method.",
245        method = "equals",
246        args = {java.lang.Object.class}
247    )
248    public void test_equalsLjava_lang_Object_NotEquals() {
249        SocketAddress address1 = new InetSocketAddress("127.0.0.1", 1234);
250        SocketAddress address2 = new InetSocketAddress("127.0.0.1", 1235);
251        Proxy proxy[] = { new Proxy(Proxy.Type.HTTP, address1),
252                new Proxy(Proxy.Type.HTTP, address2),
253                new Proxy(Proxy.Type.SOCKS, address1),
254                new Proxy(Proxy.Type.SOCKS, address2), Proxy.NO_PROXY,
255                new Proxy(null, address1), new Proxy(null, address2) };
256        // All of them are not equals
257        for (int i = 0; i < proxy.length; i++) {
258            for (int j = i + 1; j < proxy.length; j++) {
259                assertFalse(proxy[i].equals(proxy[j]));
260            }
261        }
262        // Not equals to an Object type instance. Ensure no exception is thrown.
263        assertFalse(proxy[0].equals(new Object()));
264    }
265
266    /**
267     * @tests java.net.Proxy.Type#valueOf(String)
268     */
269    @TestTargetNew(
270        level = TestLevel.COMPLETE,
271        notes = "Proxy.Type.DIRECT, Proxy.Type.HTTP, Proxy.Type.SOCKS",
272        method = "!Constants",
273        args = {}
274    )
275    public void test_Type_valueOfLjava_lang_String_Normal() {
276        assertEquals(Proxy.Type.DIRECT, Proxy.Type.valueOf("DIRECT"));
277        assertEquals(Proxy.Type.HTTP, Proxy.Type.valueOf("HTTP"));
278        assertEquals(Proxy.Type.SOCKS, Proxy.Type.valueOf("SOCKS"));
279    }
280
281    /**
282     * @tests java.net.Proxy.Type#valueOf(String)
283     */
284    @TestTargetNew(
285        level = TestLevel.COMPLETE,
286        notes = "",
287        method = "!Constants",
288        args = {}
289    )
290    public void test_Type_valueOfLjava_lang_String_IllegalName() {
291        String[] illegalName = { "Direct", "direct", "http", "socks",
292                "illegalName", "" };
293        for (int i = 0; i < illegalName.length; i++) {
294            try {
295                Proxy.Type.valueOf(illegalName[i]);
296                fail("should throw IllegalArgumentException, illegalName:"
297                        + illegalName);
298            } catch (IllegalArgumentException e) {
299                // expected
300            }
301        }
302    }
303
304    /**
305     * @tests java.net.Proxy.Type#valueOf(String)
306     */
307    @TestTargetNew(
308        level = TestLevel.COMPLETE,
309        notes = "",
310        method = "!Constants",
311        args = {}
312    )
313    public void test_Type_valueOfLjava_lang_String_NullPointerException() {
314        // Some old RIs,which throw IllegalArgumentException.
315        // Latest RIs throw NullPointerException.
316        try {
317            Proxy.Type.valueOf(null);
318            fail("should throw an exception.");
319        } catch (NullPointerException e) {
320            // May be caused by some compilers' code
321        } catch (IllegalArgumentException e) {
322            // other compilers will throw this
323        }
324    }
325
326    /**
327     * @tests java.net.Proxy.Type#values()
328     */
329    @TestTargetNew(
330        level = TestLevel.COMPLETE,
331        notes = "",
332        method = "!Constants",
333        args = {}
334    )
335    public void test_Type_values() {
336        Proxy.Type types[] = Proxy.Type.values();
337        assertEquals(3, types.length);
338        assertEquals(Proxy.Type.DIRECT, types[0]);
339        assertEquals(Proxy.Type.HTTP, types[1]);
340        assertEquals(Proxy.Type.SOCKS, types[2]);
341    }
342
343}
344