ProxySelectorTest.java revision dafd6348b2e1b46d10e1dbfa64fd2dd44bcfedf2
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.io.IOException;
24import java.net.InetSocketAddress;
25import java.net.NetPermission;
26import java.net.Proxy;
27import java.net.ProxySelector;
28import java.net.SocketAddress;
29import java.net.SocketException;
30import java.net.URI;
31import java.net.URISyntaxException;
32import java.security.Permission;
33import java.util.List;
34import java.util.Properties;
35
36import junit.framework.TestCase;
37import tests.util.TestEnvironment;
38
39@TestTargetClass(ProxySelector.class)
40public class ProxySelectorTest extends TestCase {
41
42    private static final String HTTP_PROXY_HOST = "127.0.0.1";
43
44    private static final int HTTP_PROXY_PORT = 80;
45
46    private static final String HTTPS_PROXY_HOST = "127.0.0.2";
47
48    private static final int HTTPS_PROXY_PORT = 443;
49
50    private static final String FTP_PROXY_HOST = "127.0.0.3";
51
52    private static final int FTP_PROXY_PORT = 80;
53
54    private static final String SOCKS_PROXY_HOST = "127.0.0.4";
55
56    private static final int SOCKS_PROXY_PORT = 1080;
57
58    private static URI httpUri;
59
60    private static URI ftpUri;
61
62    private static URI httpsUri;
63
64    private static URI tcpUri;
65
66    private List proxyList;
67
68    private ProxySelector selector = ProxySelector.getDefault();
69
70    static {
71        try {
72            httpUri = new URI("http://test.com");
73            ftpUri = new URI("ftp://test.com");
74            httpsUri = new URI("https://test.com");
75            tcpUri = new URI("socket://host.com");
76        } catch (URISyntaxException e) {
77
78        }
79    }
80
81    /**
82     * @tests java.net.ProxySelector#getDefault()
83     */
84    @TestTargetNew(
85        level = TestLevel.PARTIAL_COMPLETE,
86        notes = "This is a complete subset of tests for getDefault method.",
87        method = "getDefault",
88        args = {}
89    )
90    public void test_getDefault() {
91        ProxySelector selector1 = ProxySelector.getDefault();
92        assertNotNull(selector1);
93
94        ProxySelector selector2 = ProxySelector.getDefault();
95        assertSame(selector1, selector2);
96    }
97
98    /**
99     * @tests java.net.ProxySelector#getDefault()
100     */
101    @TestTargetNew(
102        level = TestLevel.PARTIAL_COMPLETE,
103        notes = "This is a complete subset of tests for getDefault method.",
104        method = "getDefault",
105        args = {}
106    )
107    public void test_getDefault_Security() {
108        SecurityManager orignalSecurityManager = System.getSecurityManager();
109        try {
110            System.setSecurityManager(new MockSecurityManager());
111        } catch (SecurityException e) {
112            System.err.println("No setSecurityManager permission.");
113            System.err.println("test_getDefault_Security is not tested");
114            return;
115        }
116        try {
117            ProxySelector.getDefault();
118            fail("should throw SecurityException");
119        } catch (SecurityException e) {
120            // expected
121        } finally {
122            System.setSecurityManager(orignalSecurityManager);
123        }
124    }
125
126    /**
127     * @tests java.net.ProxySelector#setDefault(ProxySelector)}
128     */
129    @TestTargets({
130        @TestTargetNew(
131            level = TestLevel.PARTIAL_COMPLETE,
132            notes = "This is a complete subset of tests for setDefault method.",
133            method = "setDefault",
134            args = {java.net.ProxySelector.class}
135        ),
136        @TestTargetNew(
137            level = TestLevel.COMPLETE,
138            notes = "",
139            method = "ProxySelector",
140            args = {}
141        )
142    })
143    public void test_setDefaultLjava_net_ProxySelector() {
144        ProxySelector originalSelector = ProxySelector.getDefault();
145        try {
146            ProxySelector newSelector = new MockProxySelector();
147            ProxySelector.setDefault(newSelector);
148            assertSame(newSelector, ProxySelector.getDefault());
149            // use null to unset
150            ProxySelector.setDefault(null);
151            assertSame(null, ProxySelector.getDefault());
152        } finally {
153            ProxySelector.setDefault(originalSelector);
154        }
155    }
156
157    /**
158     * @tests java.net.ProxySelector#setDefault(ProxySelector)}
159     */
160    @TestTargetNew(
161        level = TestLevel.PARTIAL_COMPLETE,
162        notes = "This is a complete subset of tests for setDefault method.",
163        method = "setDefault",
164        args = {java.net.ProxySelector.class}
165    )
166    public void test_setDefaultLjava_net_ProxySelector_Security() {
167        ProxySelector originalSelector = ProxySelector.getDefault();
168        SecurityManager orignalSecurityManager = System.getSecurityManager();
169        try {
170            System.setSecurityManager(new MockSecurityManager());
171        } catch (SecurityException e) {
172            System.err.println("No setSecurityManager permission.");
173            System.err
174                    .println("test_setDefaultLjava_net_ProxySelector_Security is not tested");
175            return;
176        }
177        try {
178            ProxySelector.setDefault(new MockProxySelector());
179            fail("should throw SecurityException");
180        } catch (SecurityException e) {
181            // expected
182        } finally {
183            System.setSecurityManager(orignalSecurityManager);
184            ProxySelector.setDefault(originalSelector);
185        }
186    }
187
188    /**
189     * @tests java.net.ProxySelector#select(URI)
190     */
191    @TestTargetNew(
192        level = TestLevel.PARTIAL_COMPLETE,
193        notes = "This is a complete subset of tests for select method.",
194        method = "select",
195        args = {java.net.URI.class}
196    )
197    public void test_selectLjava_net_URI_SelectExact()
198            throws URISyntaxException {
199        // no proxy, return a proxyList only contains NO_PROXY
200        proxyList = selector.select(httpUri);
201        assertProxyEquals(proxyList,Proxy.NO_PROXY);
202
203        // set http proxy
204        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
205        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
206        // set https proxy
207        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
208        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
209        // set ftp proxy
210        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
211        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
212        // set socks proxy
213        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
214        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
215
216        proxyList = selector.select(httpUri);
217        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
218
219        proxyList = selector.select(httpsUri);
220        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
221
222        proxyList = selector.select(ftpUri);
223        assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
224
225        proxyList = selector.select(tcpUri);
226        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
227
228    }
229
230    /**
231     * @tests java.net.ProxySelector#select(URI)
232     */
233    @TestTargetNew(
234        level = TestLevel.PARTIAL_COMPLETE,
235        notes = "This is a complete subset of tests for select method.",
236        method = "select",
237        args = {java.net.URI.class}
238    )
239    public void test_selectLjava_net_URI_SelectExact_NullHost()
240            throws URISyntaxException {
241        // regression test for Harmony-1063
242        httpUri = new URI("http://a@");
243        ftpUri = new URI("ftp://a@");
244        httpsUri = new URI("https://a@");
245        tcpUri = new URI("socket://a@");
246        // no proxy, return a proxyList only contains NO_PROXY
247        proxyList = selector.select(httpUri);
248        assertProxyEquals(proxyList, Proxy.NO_PROXY);
249
250        // set http proxy
251        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
252        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
253        // set https proxy
254        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
255        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
256        // set ftp proxy
257        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
258        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
259        // set socks proxy
260        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
261        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
262
263        proxyList = selector.select(httpUri);
264        assertProxyEquals(proxyList, Proxy.Type.HTTP, HTTP_PROXY_HOST,
265                HTTP_PROXY_PORT);
266
267        proxyList = selector.select(httpsUri);
268        assertProxyEquals(proxyList, Proxy.Type.HTTP, HTTPS_PROXY_HOST,
269                HTTPS_PROXY_PORT);
270
271        proxyList = selector.select(ftpUri);
272        assertProxyEquals(proxyList, Proxy.Type.HTTP, FTP_PROXY_HOST,
273                FTP_PROXY_PORT);
274
275        proxyList = selector.select(tcpUri);
276        assertProxyEquals(proxyList, Proxy.Type.SOCKS, SOCKS_PROXY_HOST,
277                SOCKS_PROXY_PORT);
278
279    }
280
281    /**
282     * @tests java.net.ProxySelector#select(URI)
283     */
284    @TestTargetNew(
285        level = TestLevel.PARTIAL_COMPLETE,
286        notes = "This is a complete subset of tests for select method.",
287        method = "select",
288        args = {java.net.URI.class}
289    )
290    public void test_selectLjava_net_URI_SelectExact_DefaultPort()
291            throws URISyntaxException {
292        // set http proxy
293        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
294
295        // set https proxy
296        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
297        // set ftp proxy
298        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
299        // set socks proxy
300        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
301
302        proxyList = selector.select(httpUri);
303        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
304
305        proxyList = selector.select(httpsUri);
306        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
307
308        proxyList = selector.select(ftpUri);
309        assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
310
311        proxyList = selector.select(tcpUri);
312        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
313
314    }
315
316    /**
317     * @tests java.net.ProxySelector#select(URI)
318     */
319    @TestTargetNew(
320        level = TestLevel.PARTIAL_COMPLETE,
321        notes = "This is a complete subset of tests for select method.",
322        method = "select",
323        args = {java.net.URI.class}
324    )
325    public void test_selectLjava_net_URI_SelectExact_InvalidPort()
326            throws URISyntaxException {
327        final String INVALID_PORT = "abc";
328
329        // set http proxy
330        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
331        System.setProperty("http.proxyPort", INVALID_PORT);
332        // set https proxy
333        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
334        System.setProperty("https.proxyPort", INVALID_PORT);
335        // set ftp proxy
336        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
337        System.setProperty("ftp.proxyPort", INVALID_PORT);
338        // set socks proxy
339        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
340        System.setProperty("socksproxyPort", INVALID_PORT);
341
342        proxyList = selector.select(httpUri);
343        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTP_PROXY_HOST,HTTP_PROXY_PORT);
344
345        proxyList = selector.select(httpsUri);
346        assertProxyEquals(proxyList,Proxy.Type.HTTP,HTTPS_PROXY_HOST,HTTPS_PROXY_PORT);
347
348        proxyList = selector.select(ftpUri);
349        assertProxyEquals(proxyList,Proxy.Type.HTTP,FTP_PROXY_HOST,FTP_PROXY_PORT);
350
351        proxyList = selector.select(tcpUri);
352        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
353    }
354
355    /**
356     * @tests java.net.ProxySelector#select(URI)
357     */
358    // RI may fail this test case.
359    // Uncomment this test case when regex.jar is ready.
360    /*
361    public void test_selectLjava_net_URI_Select_NonProxyHosts()
362            throws URISyntaxException {
363        // RI's bug. Some RIs may fail this test case.
364        URI[] httpUris = { new URI("http://test.com"),
365                new URI("http://10.10.1.2"), new URI("http://a"),
366                new URI("http://def.abc.com") };
367        URI[] ftpUris = { new URI("ftp://test.com"),
368                new URI("ftp://10.10.1.2"), new URI("ftp://a"),
369                new URI("ftp://def.abc.com") };
370
371        // set http proxy
372        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
373        System.setProperty("http.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
374        // set ftp proxy
375        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
376        System.setProperty("ftp.nonProxyHosts", "a|b|tes*|10.10.*|*.abc.com");
377
378        for (int i = 0; i < httpUris.length; i++) {
379            proxyList = selector.select(httpUris[i]);
380            assertProxyEquals(proxyList,Proxy.NO_PROXY);
381        }
382
383        for (int i = 0; i < ftpUris.length; i++) {
384            proxyList = selector.select(ftpUris[i]);
385            assertProxyEquals(proxyList,Proxy.NO_PROXY);
386        }
387    }*/
388
389    /**
390     * @tests java.net.ProxySelector#select(URI)
391     */
392    @TestTargetNew(
393        level = TestLevel.PARTIAL_COMPLETE,
394        notes = "This is a complete subset of tests for select method.",
395        method = "select",
396        args = {java.net.URI.class}
397    )
398    public void test_selectLjava_net_URI_SelectLikeHTTP()
399            throws URISyntaxException {
400        System.setProperty("http.proxyHost", "");
401        // set https proxy
402        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
403        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
404        // set ftp proxy
405        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
406        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
407        // set socks proxy
408        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
409        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
410
411        proxyList = selector.select(httpUri);
412        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
413    }
414
415    /**
416     * @tests java.net.ProxySelector#select(URI)
417     */
418    @TestTargetNew(
419        level = TestLevel.PARTIAL_COMPLETE,
420        notes = "This is a complete subset of tests for select method.",
421        method = "select",
422        args = {java.net.URI.class}
423    )
424    public void test_selectLjava_net_URI_SelectNoHTTP()
425            throws URISyntaxException {
426        // set https proxy
427        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
428        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
429        // set ftp proxy
430        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
431        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
432
433        proxyList = selector.select(httpUri);
434        assertProxyEquals(proxyList,Proxy.NO_PROXY);
435    }
436
437    /**
438     * @tests java.net.ProxySelector#select(URI)
439     */
440    @TestTargetNew(
441        level = TestLevel.PARTIAL_COMPLETE,
442        notes = "This is a complete subset of tests for select method.",
443        method = "select",
444        args = {java.net.URI.class}
445    )
446    public void test_selectLjava_net_URI_SelectLikeHTTPS()
447            throws URISyntaxException {
448        // set http proxy
449        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
450        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
451        // set https proxy host empty
452        System.setProperty("http.proxyHost", "");
453        // set ftp proxy
454        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
455        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
456        // set socks proxy
457        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
458        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
459
460        proxyList = selector.select(httpsUri);
461        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
462    }
463
464    /**
465     * @tests java.net.ProxySelector#select(URI)
466     */
467    @TestTargetNew(
468        level = TestLevel.PARTIAL_COMPLETE,
469        notes = "This is a complete subset of tests for select method.",
470        method = "select",
471        args = {java.net.URI.class}
472    )
473    public void test_selectLjava_net_URI_SelectNoHTTPS()
474            throws URISyntaxException {
475        // set https proxy
476        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
477        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
478        // set ftp proxy
479        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
480        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
481
482        proxyList = selector.select(httpsUri);
483        assertProxyEquals(proxyList,Proxy.NO_PROXY);
484    }
485
486    /**
487     * @tests java.net.ProxySelector#select(URI)
488     */
489    @TestTargetNew(
490        level = TestLevel.PARTIAL_COMPLETE,
491        notes = "This is a complete subset of tests for select method.",
492        method = "select",
493        args = {java.net.URI.class}
494    )
495    public void test_selectLjava_net_URI_SelectLikeFTP()
496            throws URISyntaxException {
497        // set http proxy
498        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
499        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
500        // set ftp host empty
501        System.setProperty("ftp.proxyHost", "");
502        // set https proxy
503        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
504        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
505        // set socks proxy
506        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
507        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
508
509        proxyList = selector.select(ftpUri);
510        assertProxyEquals(proxyList,Proxy.Type.SOCKS,SOCKS_PROXY_HOST,SOCKS_PROXY_PORT);
511    }
512
513    /**
514     * @tests java.net.ProxySelector#select(URI)
515     */
516    @TestTargetNew(
517        level = TestLevel.PARTIAL_COMPLETE,
518        notes = "This is a complete subset of tests for select method.",
519        method = "select",
520        args = {java.net.URI.class}
521    )
522    public void test_selectLjava_net_URI_SelectNoFTP()
523            throws URISyntaxException {
524        // set http proxy
525        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
526        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
527        // set https proxy
528        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
529        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
530
531        proxyList = selector.select(ftpUri);
532        assertProxyEquals(proxyList,Proxy.NO_PROXY);
533    }
534
535    /**
536     * @tests java.net.ProxySelector#select(URI)
537     */
538    @TestTargetNew(
539        level = TestLevel.PARTIAL_COMPLETE,
540        notes = "This is a complete subset of tests for select method.",
541        method = "select",
542        args = {java.net.URI.class}
543    )
544    public void test_selectLjava_net_URI_SelectNoSOCKS()
545            throws URISyntaxException {
546        // set http proxy
547        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
548        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
549        // set https proxy
550        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
551        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
552        // set socks proxy
553        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
554        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
555
556        proxyList = selector.select(tcpUri);
557        assertProxyEquals(proxyList,Proxy.NO_PROXY);
558    }
559
560    /**
561     * @tests java.net.ProxySelector#select(URI)
562     */
563    @TestTargetNew(
564        level = TestLevel.PARTIAL_COMPLETE,
565        notes = "This is a complete subset of tests for connectFailed method.",
566        method = "connectFailed",
567        args = {java.net.URI.class, java.net.SocketAddress.class, java.io.IOException.class}
568    )
569    public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException()
570            throws URISyntaxException {
571        // set http proxy
572        System.setProperty("http.proxyHost", HTTP_PROXY_HOST);
573        System.setProperty("http.proxyPort", String.valueOf(HTTP_PROXY_PORT));
574        // set https proxy
575        System.setProperty("https.proxyHost", HTTPS_PROXY_HOST);
576        System.setProperty("https.proxyPort", String.valueOf(HTTPS_PROXY_PORT));
577        // set ftp proxy
578        System.setProperty("ftp.proxyHost", FTP_PROXY_HOST);
579        System.setProperty("ftp.proxyPort", String.valueOf(FTP_PROXY_PORT));
580        // set socks proxy
581        System.setProperty("socksProxyHost", SOCKS_PROXY_HOST);
582        System.setProperty("socksProxyPort", String.valueOf(SOCKS_PROXY_PORT));
583
584        List proxyList1 = selector.select(httpUri);
585        assertNotNull(proxyList1);
586        assertEquals(1, proxyList1.size());
587        Proxy proxy1 = (Proxy) proxyList1.get(0);
588        selector
589                .connectFailed(httpUri, proxy1.address(), new SocketException());
590
591        List proxyList2 = selector.select(httpUri);
592        assertNotNull(proxyList2);
593        assertEquals(1, proxyList2.size());
594        Proxy proxy2 = (Proxy) proxyList2.get(0);
595        // Default implemention doesn't change the proxy list
596        assertEquals(proxy1, proxy2);
597    }
598
599    /**
600     * @tests java.net.ProxySelector#select(URI)
601     */
602    @TestTargetNew(
603        level = TestLevel.PARTIAL_COMPLETE,
604        notes = "This is a complete subset of tests for connectFailed method.",
605        method = "connectFailed",
606        args = {java.net.URI.class, java.net.SocketAddress.class, java.io.IOException.class}
607    )
608    public void test_connectionFailedLjava_net_URILjava_net_SocketAddressLjava_io_IOException_IllegalArguement()
609            throws URISyntaxException {
610        SocketAddress sa = InetSocketAddress.createUnresolved("127.0.0.1", 0);
611        try {
612            selector.connectFailed(null, sa, new SocketException());
613            fail("should throw IllegalArgumentException if any argument is null.");
614        } catch (IllegalArgumentException e) {
615            // expected
616        }
617        try {
618            selector.connectFailed(httpUri, null, new SocketException());
619            fail("should throw IllegalArgumentException if any argument is null.");
620        } catch (IllegalArgumentException e) {
621            // expected
622        }
623        try {
624            selector.connectFailed(httpUri, sa, null);
625            fail("should throw IllegalArgumentException if any argument is null.");
626        } catch (IllegalArgumentException e) {
627            // expected
628        }
629
630    }
631
632    /**
633     * @tests java.net.ProxySelector#select(URI)
634     */
635    @TestTargetNew(
636        level = TestLevel.PARTIAL_COMPLETE,
637        notes = "This is a complete subset of tests for select method.",
638        method = "select",
639        args = {java.net.URI.class}
640    )
641    public void test_selectLjava_net_URI_IllegalArgument()
642            throws URISyntaxException {
643        URI[] illegalUris = { new URI("abc"), new URI("http"), null };
644        for (int i = 0; i < illegalUris.length; i++) {
645            try {
646                selector.select(illegalUris[i]);
647                fail("should throw IllegalArgumentException");
648            } catch (IllegalArgumentException e) {
649                // expected
650            }
651        }
652    }
653
654    /*
655     * asserts whether selectedProxyList contains one and only one element,
656     * and the element equals proxy.
657     */
658    private void assertProxyEquals(List selectedProxyList, Proxy proxy) {
659        assertNotNull(selectedProxyList);
660        assertEquals(1, selectedProxyList.size());
661        assertEquals((Proxy) selectedProxyList.get(0), proxy);
662    }
663
664    /*
665     * asserts whether selectedProxyList contains one and only one element,
666     * and the element equals proxy which is represented by arguments "type",
667     * "host","port".
668     */
669    private void assertProxyEquals(List selectedProxyList, Proxy.Type type,
670            String host, int port) {
671        SocketAddress sa = InetSocketAddress.createUnresolved(host, port);
672        Proxy proxy = new Proxy(type, sa);
673        assertProxyEquals(selectedProxyList, proxy);
674    }
675
676    /*
677     * Mock selector for setDefault test
678     */
679    static class MockProxySelector extends ProxySelector {
680
681        public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
682
683        }
684
685        public List <Proxy> select(URI uri) {
686            return null;
687        }
688    }
689
690    /*
691     * MockSecurityMaanger. It denies NetPermission("getProxySelector") and
692     * NetPermission("setProxySelector").
693     */
694    class MockSecurityManager extends SecurityManager {
695        public void checkPermission(Permission permission) {
696            if (permission instanceof NetPermission) {
697                if ("getProxySelector".equals(permission.getName())) {
698                    throw new SecurityException();
699                }
700            }
701
702            if (permission instanceof NetPermission) {
703                if ("setProxySelector".equals(permission.getName())) {
704                    throw new SecurityException();
705                }
706            }
707
708            if (permission instanceof RuntimePermission) {
709                if ("setSecurityManager".equals(permission.getName())) {
710                    return;
711                }
712            }
713        }
714    }
715
716    /*
717     * @see junit.framework.TestCase#setUp()
718     */
719    protected void setUp() throws Exception {
720        super.setUp();
721        TestEnvironment.reset();
722    }
723
724    /*
725     * @see junit.framework.TestCase#tearDown()
726     */
727    protected void tearDown() throws Exception {
728        TestEnvironment.reset();
729        super.tearDown();
730    }
731}
732