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