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