OldURLTest.java revision aec2ed4b266b75aaab59c23ca71e355a9336b074
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 */ 16 17package libcore.java.net; 18 19import dalvik.annotation.TestLevel; 20import dalvik.annotation.TestTargetClass; 21import dalvik.annotation.TestTargetNew; 22import java.io.BufferedReader; 23import java.io.BufferedWriter; 24import java.io.File; 25import java.io.FileWriter; 26import java.io.IOException; 27import java.io.InputStream; 28import java.io.InputStreamReader; 29import java.lang.reflect.Field; 30import java.net.MalformedURLException; 31import java.net.Proxy; 32import java.net.ProxySelector; 33import java.net.SocketAddress; 34import java.net.URI; 35import java.net.URISyntaxException; 36import java.net.URL; 37import java.net.URLConnection; 38import java.net.URLStreamHandler; 39import java.net.URLStreamHandlerFactory; 40import java.security.Permission; 41import java.util.ArrayList; 42import java.util.Arrays; 43import java.util.List; 44import junit.framework.TestCase; 45 46@TestTargetClass(URL.class) 47public class OldURLTest extends TestCase { 48 49 private static final String helloWorldString = "Hello World"; 50 51 @Override protected void setUp() throws Exception { 52 super.setUp(); 53 } 54 55 @Override protected void tearDown() throws Exception { 56 super.tearDown(); 57 } 58 59 /** 60 * @tests java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String) 61 */ 62 @TestTargetNew( 63 level = TestLevel.COMPLETE, 64 notes = "Regression test.", 65 method = "URL", 66 args = {java.lang.String.class, java.lang.String.class, int.class, java.lang.String.class} 67 ) 68 public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_String() 69 throws MalformedURLException { 70 // Regression for HARMONY-83 71 new URL("http", "apache.org", 123456789, "file"); 72 try { 73 new URL("http", "apache.org", -123, "file"); 74 fail("Assert 0: Negative port should throw exception"); 75 } catch (MalformedURLException e) { 76 // expected 77 } 78 } 79 80 /** 81 * @tests java.net.URL#URL(java.lang.String, java.lang.String, 82 * java.lang.String) 83 */ 84 @TestTargetNew( 85 level = TestLevel.COMPLETE, 86 notes = "", 87 method = "URL", 88 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 89 ) 90 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() throws MalformedURLException { 91 // Strange behavior in reference, the hostname contains a ':' so it gets wrapped in '[', ']' 92 URL testURL = new URL("http", "www.apache.org:8082", "test.html#anch"); 93 assertEquals("Assert 0: wrong protocol", "http", testURL.getProtocol()); 94 assertEquals("Assert 1: wrong host", "[www.apache.org:8082]", testURL.getHost()); 95 assertEquals("Assert 2: wrong port", -1, testURL.getPort()); 96 assertEquals("Assert 3: wrong file", "test.html", testURL.getFile()); 97 assertEquals("Assert 4: wrong anchor", "anch", testURL.getRef()); 98 99 try { 100 new URL("hftp", "apache.org:8082", "test.html#anch"); 101 fail("Assert 0: Invalid protocol"); 102 } catch (MalformedURLException e) { 103 // expected 104 } 105 } 106 107 /** 108 * @tests java.net.URL#URL(String, String, String) 109 * 110 */ 111 @TestTargetNew( 112 level = TestLevel.PARTIAL, 113 notes = "Regression test.", 114 method = "URL", 115 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 116 ) 117 public void test_java_protocol_handler_pkgs_prop() throws MalformedURLException { 118 // Regression test for Harmony-3094 119 final String HANDLER_PKGS = "java.protocol.handler.pkgs"; 120 System.setProperty(HANDLER_PKGS, "fake|org.apache.harmony.luni.tests.java.net"); 121 122 try { 123 new URL("test_protocol", "", "fake.jar"); 124 } catch (MalformedURLException e) { 125 // expected 126 } 127 } 128 129 /** 130 * Test method for {@link java.net.URL#hashCode()}. 131 */ 132 @TestTargetNew( 133 level = TestLevel.COMPLETE, 134 notes = "", 135 method = "hashCode", 136 args = {} 137 ) 138 public void testHashCode() throws MalformedURLException { 139 URL testURL1 = new URL("http", "www.apache.org:8080", "test.html#anch"); 140 URL testURL2 = new URL("http", "www.apache.org:8080", "test.html#anch"); 141 URL changedURL = new URL("http", "www.apache.org:8082", 142 "test.html#anch"); 143 assertEquals("Assert 0: error in hashCode: not same", 144 testURL1.hashCode(), testURL2.hashCode()); 145 assertFalse("Assert 0: error in hashCode: should be same", testURL1 146 .hashCode() == changedURL.hashCode()); 147 } 148 149 /** 150 * Test method for {@link java.net.URL#setURLStreamHandlerFactory(java.net.URLStreamHandlerFactory)}. 151 */ 152 @TestTargetNew( 153 level = TestLevel.SUFFICIENT, 154 notes = "cannot test since no sophisticated StreamHandlerFactory available.", 155 method = "setURLStreamHandlerFactory", 156 args = {java.net.URLStreamHandlerFactory.class} 157 ) 158 public void testSetURLStreamHandlerFactory() throws MalformedURLException, IOException, IllegalArgumentException, IllegalAccessException { 159 URLStreamHandlerFactory factory = new MyURLStreamHandlerFactory(); 160 Field streamHandlerFactoryField = null; 161 int counter = 0; 162 File sampleFile = createTempHelloWorldFile(); 163 164 URL fileURL = sampleFile.toURL(); 165 166 167 Field[] fields = URL.class.getDeclaredFields(); 168 169 170 for (Field f : fields) { 171 if (URLStreamHandlerFactory.class.equals(f.getType())) { 172 counter++; 173 streamHandlerFactoryField = f; 174 } 175 } 176 177 if (counter != 1) { 178 fail("Error in test setup: not Factory found"); 179 } 180 181 streamHandlerFactoryField.setAccessible(true); 182 URLStreamHandlerFactory old = (URLStreamHandlerFactory) streamHandlerFactoryField.get(null); 183 184 try { 185 streamHandlerFactoryField.set(null, factory); 186 BufferedReader buf = new BufferedReader(new InputStreamReader( 187 fileURL.openStream()), helloWorldString.getBytes().length); 188 String nextline; 189 while ((nextline = buf.readLine()) != null) { 190 assertEquals(helloWorldString, nextline); 191 } 192 193 buf.close(); 194 195 } finally { 196 197 streamHandlerFactoryField.set(null, old); 198 } 199 200 } 201 202 /** 203 * Test method for {@link java.net.URL#URL(java.lang.String)}. 204 */ 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 notes = "", 208 method = "URL", 209 args = {java.lang.String.class} 210 ) 211 public void testURLString() throws MalformedURLException { 212 URL testURL = new URL("ftp://myname@host.dom/etc/motd"); 213 214 assertEquals("Assert 0: wrong protocol", "ftp", testURL.getProtocol()); 215 assertEquals("Assert 1: wrong host", "host.dom", testURL.getHost()); 216 assertEquals("Assert 2: wrong port", -1, testURL.getPort()); 217 assertEquals("Assert 3: wrong userInfo", "myname", testURL 218 .getUserInfo()); 219 assertEquals("Assert 4: wrong path", "/etc/motd", testURL.getPath()); 220 221 try { 222 new URL("ftpmyname@host.dom/etc/motd"); 223 fail("Assert 0: malformed URL should throw exception"); 224 } catch (MalformedURLException e) { 225 // expected 226 } 227 228 } 229 230 /** 231 * Test method for {@link java.net.URL#URL(java.net.URL, java.lang.String)}. 232 */ 233 @TestTargetNew( 234 level = TestLevel.COMPLETE, 235 notes = "", 236 method = "URL", 237 args = {java.net.URL.class, java.lang.String.class} 238 ) 239 public void testURLURLString() throws MalformedURLException { 240 241 URL gamelan = new URL("http://www.gamelan.com/pages/"); 242 243 URL gamelanNetwork = new URL(gamelan, "Gamelan.net.html"); 244 URL gamelanNetworkBottom = new URL(gamelanNetwork, "#BOTTOM"); 245 assertEquals("Assert 0: wrong anchor", "BOTTOM", gamelanNetworkBottom 246 .getRef()); 247 assertEquals("Assert 1: wrong protocol", "http", gamelanNetworkBottom 248 .getProtocol()); 249 250 // same protocol 251 URL gamelanNetworBottom2 = new URL(gamelanNetwork, 252 "http://www.gamelan.com/pages/Gamelan.net.html#BOTTOM"); 253 assertEquals(gamelanNetwork.getProtocol(), gamelanNetworBottom2.getProtocol()); 254 255 // changed protocol 256 URL gamelanNetworkBottom3 = new URL(gamelanNetwork, 257 "ftp://www.gamelan2.com/pages/Gamelan.net.html#BOTTOM"); 258 URL absoluteNew = new URL( 259 "ftp://www.gamelan2.com/pages/Gamelan.net.html#BOTTOM"); 260 assertEquals("Host of context URL instead of new URL", "ftp", 261 gamelanNetworkBottom3.getProtocol()); 262 assertTrue("URL is context URL instead of new URL", 263 gamelanNetworkBottom3.sameFile(absoluteNew)); 264 265 // exception testing 266 try { 267 u = null; 268 u1 = new URL(u, "somefile.java"); 269 fail("didn't throw the expected MalFormedURLException"); 270 } catch (MalformedURLException e) { 271 // ok 272 } 273 274 // Non existing protocol 275 // exception testing 276 try { 277 u = new URL(gamelanNetwork, 278 "someFancyNewProt://www.gamelan2.com/pages/Gamelan.net.html#BOTTOM"); 279 assertTrue("someFancyNewProt".equalsIgnoreCase(u.getProtocol())); 280 } catch (MalformedURLException e) { 281 // ok 282 } 283 } 284 285 /** 286 * Test method for {@link java.net.URL#equals(java.lang.Object)}. 287 */ 288 @TestTargetNew( 289 level = TestLevel.COMPLETE, 290 notes = "", 291 method = "equals", 292 args = {java.lang.Object.class} 293 ) 294 public void testEqualsObject() throws MalformedURLException { 295 URL testURL1 = new URL("http", "www.apache.org", 8080, "test.html"); 296 URL wrongProto = new URL("ftp", "www.apache.org", 8080, "test.html"); 297 URL wrongPort = new URL("http", "www.apache.org", 8082, "test.html"); 298 URL wrongHost = new URL("http", "www.apache2.org", 8080, "test.html"); 299 URL wrongRef = new URL("http", "www.apache.org", 8080, "test2.html#BOTTOM"); 300 URL testURL2 = new URL("http://www.apache.org:8080/test.html"); 301 302 assertFalse("Assert 0: error in equals: not same", testURL1.equals(wrongProto)); 303 assertFalse("Assert 1: error in equals: not same", testURL1.equals(wrongPort)); 304 assertFalse("Assert 2: error in equals: not same", testURL1.equals(wrongHost)); 305 assertFalse("Assert 3: error in equals: not same", testURL1.equals(wrongRef)); 306 assertFalse("Assert 4: error in equals: not same", testURL1.equals(testURL2)); 307 308 URL testURL3 = new URL("http", "www.apache.org", "/test.html"); 309 URL testURL4 = new URL("http://www.apache.org/test.html"); 310 assertTrue("Assert 4: error in equals: same", testURL3.equals(testURL4)); 311 } 312 313 /** 314 * Test method for {@link java.net.URL#sameFile(java.net.URL)}. 315 */ 316 @TestTargetNew( 317 level = TestLevel.COMPLETE, 318 notes = "non trivial reference test fails", 319 method = "sameFile", 320 args = {java.net.URL.class} 321 ) 322 public void testSameFile() throws MalformedURLException { 323 URL gamelan = new URL("file:///pages/index.html"); 324 URL gamelanFalse = new URL("file:///pages/out/index.html"); 325 326 URL gamelanNetwork = new URL(gamelan, "#BOTTOM"); 327 assertTrue(gamelanNetwork.sameFile(gamelan)); 328 329 assertFalse(gamelanNetwork.sameFile(gamelanFalse)); 330 331 // non trivial test 332 URL url = new URL("http://web2.javasoft.com/some+file.html"); 333 URL url1 = new URL("http://web2.javasoft.com/some%20file.html"); 334 335 assertFalse(url.sameFile(url1)); 336 } 337 338 /** 339 * Test method for {@link java.net.URL#getContent()}. 340 */ 341 @TestTargetNew( 342 level = TestLevel.COMPLETE, 343 notes = "image and sound content not testable: mime types", 344 method = "getContent", 345 args = {} 346 ) 347 public void testGetContent() throws MalformedURLException { 348 349 File sampleFile = createTempHelloWorldFile(); 350 351 // read content from file 352 URL fileURL = sampleFile.toURL(); 353 354 try { 355 InputStream output = (InputStream) fileURL.getContent(); 356 assertTrue(output.available() > 0); 357 // ok 358 } catch (Exception e) { 359 fail("Did not get output type from File URL"); 360 } 361 362 //Exception test 363 URL invalidFile = new URL("file:///nonexistenttestdir/tstfile"); 364 365 try { 366 invalidFile.getContent(); 367 fail("Access to invalid file worked"); 368 } catch (IOException e) { 369 //ok 370 } 371 372 } 373 374 /** 375 * Test method for {@link java.net.URL#openStream()}. 376 */ 377 @TestTargetNew( 378 level = TestLevel.COMPLETE, 379 notes = "", 380 method = "openStream", 381 args = {} 382 ) 383 public void testOpenStream() throws MalformedURLException, IOException { 384 385 File sampleFile = createTempHelloWorldFile(); 386 387 // read content from file 388 URL fileURL = sampleFile.toURL(); 389 BufferedReader dis = null; 390 String inputLine; 391 StringBuffer buf = new StringBuffer(32); 392 try { 393 dis = new BufferedReader( 394 new InputStreamReader(fileURL.openStream()), 32); 395 while ((inputLine = dis.readLine()) != null) { 396 buf.append(inputLine); 397 } 398 dis.close(); 399 } catch (IOException e) { 400 fail("Unexpected error in test setup: " + e.getMessage()); 401 } 402 403 assertTrue("Assert 0: Nothing was read from file ", buf.length() > 0); 404 assertEquals("Assert 1: Wrong stream content", "Hello World", buf 405 .toString()); 406 407 // exception test 408 409 URL invalidFile = new URL("file:///nonexistenttestdir/tstfile"); 410 411 try { 412 dis = new BufferedReader( 413 new InputStreamReader(invalidFile.openStream()), 32); 414 while ((inputLine = dis.readLine()) != null) { 415 buf.append(inputLine); 416 } 417 fail("Access to invalid file worked"); 418 } catch (Exception e) { 419 //ok 420 } finally { 421 dis.close(); 422 } 423 } 424 425 /** 426 * Test method for {@link java.net.URL#openConnection()}. 427 */ 428 @TestTargetNew( 429 level = TestLevel.COMPLETE, 430 notes = "", 431 method = "openConnection", 432 args = {} 433 ) 434 public void testOpenConnection() throws MalformedURLException, IOException { 435 436 File sampleFile = createTempHelloWorldFile(); 437 438 byte[] ba; 439 InputStream is; 440 String s; 441 u = sampleFile.toURL(); 442 u.openConnection(); 443 444 is = (InputStream) u.getContent(new Class[] { Object.class }); 445 is.read(ba = new byte[4096]); 446 s = new String(ba); 447 assertTrue("Incorrect content " + u 448 + " does not contain: \"Hello World\"", 449 s.indexOf("Hello World") >= 0); 450 451 try { 452 URL u = new URL("https://a.xy.com/index.html"); 453 URLConnection conn = u.openConnection(); 454 conn.connect(); 455 fail("Should not be able to read from this site."); 456 } catch (IOException e) { 457 //ok 458 } 459 } 460 461 /** 462 * Test method for {@link java.net.URL#toURI()}. 463 */ 464 @TestTargetNew( 465 level = TestLevel.COMPLETE, 466 notes = "", 467 method = "toURI", 468 args = {} 469 ) 470 public void testToURI() throws MalformedURLException, URISyntaxException { 471 String testHTTPURLString = "http://www.gamelan.com/pages/"; 472 String testFTPURLString = "ftp://myname@host.dom/etc/motd"; 473 URL testHTTPURL = new URL(testHTTPURLString); 474 URL testFTPURL = new URL(testFTPURLString); 475 476 URI testHTTPURI = testHTTPURL.toURI(); 477 URI testFTPURI = testFTPURL.toURI(); 478 assertEquals(testHTTPURI.toString(),testHTTPURLString); 479 assertEquals(testFTPURI.toString(),testFTPURLString); 480 481 //Exception test 482 String[] constructorTestsInvalid = new String[] { 483 "http:///a path#frag", // space char in path, not in escaped 484 // octet form, with no host 485 "http://host/a[path#frag", // an illegal char, not in escaped 486 // octet form, should throw an 487 // exception 488 "http://host/a%path#frag", // invalid escape sequence in path 489 "http://host/a%#frag", // incomplete escape sequence in path 490 491 "http://host#a frag", // space char in fragment, not in 492 // escaped octet form, no path 493 "http://host/a#fr#ag", // illegal char in fragment 494 "http:///path#fr%ag", // invalid escape sequence in fragment, 495 // with no host 496 "http://host/path#frag%", // incomplete escape sequence in 497 // fragment 498 499 "http://host/path?a query#frag", // space char in query, not 500 // in escaped octet form 501 "http://host?query%ag", // invalid escape sequence in query, no 502 // path 503 "http:///path?query%", // incomplete escape sequence in query, 504 // with no host 505 506 "mailto:user^name@fklkf.com" // invalid char in scheme 507 }; 508 509 for (String malformedURI : Arrays.asList(constructorTestsInvalid)) { 510 try { 511 URL urlQuery = new URL("http://host/a%path#frag"); 512 urlQuery.toURI(); 513 fail("Exception expected"); 514 } catch (URISyntaxException e) { 515 // ok 516 } 517 } 518 } 519 520 /** 521 * Test method for {@link java.net.URL#toString()}. 522 */ 523 @TestTargetNew( 524 level = TestLevel.COMPLETE, 525 notes = "", 526 method = "toString", 527 args = {} 528 ) 529 public void testToString() throws MalformedURLException { 530 URL testHTTPURL = new URL("http://www.gamelan.com/pages/"); 531 URL testFTPURL = new URL("ftp://myname@host.dom/etc/motd"); 532 533 assertEquals(testHTTPURL.toString(),testHTTPURL.toExternalForm()); 534 assertEquals(testFTPURL.toString(),testFTPURL.toExternalForm()); 535 536 assertEquals("http://www.gamelan.com/pages/", testHTTPURL.toString()); 537 } 538 539 /** 540 * Test method for {@link java.net.URL#toExternalForm()}. 541 */ 542 @TestTargetNew( 543 level = TestLevel.PARTIAL_COMPLETE, 544 notes = "simple tests", 545 method = "toExternalForm", 546 args = {} 547 ) 548 public void testToExternalForm() throws MalformedURLException { 549 URL testHTTPURL = new URL("http://www.gamelan.com/pages/"); 550 URL testFTPURL = new URL("ftp://myname@host.dom/etc/motd"); 551 552 assertEquals(testHTTPURL.toString(),testHTTPURL.toExternalForm()); 553 assertEquals(testFTPURL.toString(),testFTPURL.toExternalForm()); 554 555 assertEquals("http://www.gamelan.com/pages/", testHTTPURL.toExternalForm()); 556 } 557 558 /** 559 * Test method for {@link java.net.URL#getFile()}. 560 */ 561 @TestTargetNew( 562 level = TestLevel.COMPLETE, 563 notes = "", 564 method = "getFile", 565 args = {} 566 ) 567 public void testGetFile() throws MalformedURLException { 568 569 File sampleFile = createTempHelloWorldFile(); 570 571 // read content from file 572 URL fileURL = sampleFile.toURL(); 573 assertNotNull(fileURL); 574 assertEquals(sampleFile.getPath(),fileURL.getFile()); 575 } 576 577 /** 578 * Test method for {@link java.net.URL#getPort()}. 579 */ 580 @TestTargetNew( 581 level = TestLevel.COMPLETE, 582 notes = "", 583 method = "getPort", 584 args = {} 585 ) 586 public void testGetPort() throws MalformedURLException { 587 URL testHTTPURL = new URL("http://www.gamelan.com/pages/"); 588 URL testFTPURL = new URL("ftp://myname@host.dom/etc/motd"); 589 590 assertEquals(-1,testFTPURL.getPort()); 591 assertEquals(-1,testHTTPURL.getPort()); 592 593 URL testHTTPURL8082 = new URL("http://www.gamelan.com:8082/pages/"); 594 assertEquals(8082, testHTTPURL8082.getPort()); 595 596 } 597 598 /** 599 * Test method for {@link java.net.URL#getProtocol()}. 600 */ 601 @TestTargetNew( 602 level = TestLevel.COMPLETE, 603 notes = "", 604 method = "getProtocol", 605 args = {} 606 ) 607 public void testGetProtocol() throws MalformedURLException { 608 URL testHTTPURL = new URL("http://www.gamelan.com/pages/"); 609 URL testHTTPSURL = new URL("https://www.gamelan.com/pages/"); 610 URL testFTPURL = new URL("ftp://myname@host.dom/etc/motd"); 611 URL testFile = new URL("file:///pages/index.html"); 612 URL testJarURL = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class"); 613 614 assertTrue("http".equalsIgnoreCase(testHTTPURL.getProtocol())); 615 assertTrue("https".equalsIgnoreCase(testHTTPSURL.getProtocol())); 616 assertTrue("ftp".equalsIgnoreCase(testFTPURL.getProtocol())); 617 assertTrue("file".equalsIgnoreCase(testFile.getProtocol())); 618 assertTrue("jar".equalsIgnoreCase(testJarURL.getProtocol())); 619 620 } 621 622 /** 623 * Test method for {@link java.net.URL#getRef()}. 624 */ 625 @TestTargetNew( 626 level = TestLevel.COMPLETE, 627 notes = "", 628 method = "getRef", 629 args = {} 630 ) 631 public void testGetRef() throws MalformedURLException { 632 URL gamelan = new URL("http://www.gamelan.com/pages/"); 633 634 String output = gamelan.getRef(); 635 assertTrue(output == null || output.equals("")); 636 637 URL gamelanNetwork = new URL(gamelan, "Gamelan.net.html#BOTTOM"); 638 assertEquals("BOTTOM", gamelanNetwork.getRef()); 639 640 URL gamelanNetwork2 = new URL("http", "www.gamelan.com", 641 "Gamelan.network.html#BOTTOM"); 642 643 assertEquals("BOTTOM", gamelanNetwork2.getRef()); 644 645 } 646 647 /** 648 * Test method for {@link java.net.URL#getQuery()}. 649 */ 650 @TestTargetNew( 651 level = TestLevel.COMPLETE, 652 notes = "", 653 method = "getQuery", 654 args = {} 655 ) 656 public void testGetQuery() throws MalformedURLException { 657 URL urlQuery = new URL( 658 "http://www.example.com/index.html?attrib1=value1&attrib2=value&attrib3#anchor"); 659 URL urlNoQuery = new URL( 660 "http://www.example.com/index.html#anchor"); 661 662 assertEquals("attrib1=value1&attrib2=value&attrib3", urlQuery.getQuery()); 663 664 String output = urlNoQuery.getQuery(); 665 assertTrue(output == null || "".equals(output)); 666 } 667 668 /** 669 * Test method for {@link java.net.URL#getPath()}. 670 */ 671 @TestTargetNew( 672 level = TestLevel.COMPLETE, 673 notes = "", 674 method = "getPath", 675 args = {} 676 ) 677 public void testGetPath() throws MalformedURLException { 678 URL url = new URL("http://www.example.com"); 679 String output = url.getPath(); 680 681 assertTrue("".equals(output) || output == null); 682 683 URL url2 = new URL(url,"/foo/index.html"); 684 assertEquals("/foo/index.html",url2.getPath()); 685 } 686 687 /** 688 * Test method for {@link java.net.URL#getUserInfo()}. 689 */ 690 @TestTargetNew( 691 level = TestLevel.COMPLETE, 692 notes = "", 693 method = "getUserInfo", 694 args = {} 695 ) 696 public void testGetUserInfo() throws MalformedURLException { 697 URL urlNoUserInfo = new URL("http://www.java2s.com:8080"); 698 URL url = new URL("ftp://myUser:password@host.dom/etc/motd"); 699 700 assertEquals("Assert 0: Wrong user","myUser:password",url.getUserInfo()); 701 String userInfo = urlNoUserInfo.getUserInfo(); 702 assertTrue("".equals(userInfo) || null == userInfo); 703 } 704 705 /** 706 * Test method for {@link java.net.URL#getAuthority()}. 707 */ 708 @TestTargetNew( 709 level = TestLevel.COMPLETE, 710 notes = "", 711 method = "getAuthority", 712 args = {} 713 ) 714 public void testGetAuthority() throws MalformedURLException, URISyntaxException { 715 // legal authority information userInfo (user,password),domain,port 716 717 URL url = new URL("http://www.java2s.com:8080"); 718 assertEquals("Assert 0: Wrong authority ", "www.java2s.com:8080", url 719 .getAuthority()); 720 721 URL ftpURL = new URL("ftp://myname@host.dom/etc/motd"); 722 assertEquals("Assert 1: Wrong authority ", "myname@host.dom", ftpURL 723 .getAuthority()); 724 725 URI testURI = new URI("/relative/URI/with/absolute/path/to/resource.txt"); 726 String output = testURI.getAuthority(); 727 assertTrue("".equals(output) || null == output); 728 } 729 730 /** 731 * Test method for {@link java.net.URL#getDefaultPort()}. 732 */ 733 @TestTargetNew( 734 level = TestLevel.COMPLETE, 735 notes = "", 736 method = "getDefaultPort", 737 args = {} 738 ) 739 public void testGetDefaultPort() throws MalformedURLException { 740 URL testHTTPURL = new URL("http://www.gamelan.com/pages/"); 741 URL testFTPURL = new URL("ftp://myname@host.dom/etc/motd"); 742 743 assertEquals(21,testFTPURL.getDefaultPort()); 744 assertEquals(80,testHTTPURL.getDefaultPort()); 745 } 746 747 private File createTempHelloWorldFile() { 748 // create content to read 749 File tmpDir = new File(System.getProperty("java.io.tmpdir")); 750 File sampleFile = null; 751 try { 752 if (tmpDir.isDirectory()) { 753 sampleFile = File.createTempFile("openStreamTest", ".txt", 754 tmpDir); 755 sampleFile.deleteOnExit(); 756 } else { 757 fail("Error in test setup java.io.tmpdir does not exist"); 758 } 759 760 FileWriter fstream = new FileWriter(sampleFile); 761 BufferedWriter out = new BufferedWriter(fstream, 32); 762 out.write(helloWorldString); 763 // Close the output stream 764 out.close(); 765 } catch (Exception e) {// Catch exception if any 766 fail("Error: in test setup" + e.getMessage()); 767 } 768 769 return sampleFile; 770 } 771 772 // start HARMONY branch 773 774 public static class MyHandler extends URLStreamHandler { 775 protected URLConnection openConnection(URL u) 776 throws IOException { 777 return null; 778 } 779 } 780 781 URL u; 782 783 URL u1; 784 785 URL u2; 786 787 boolean caught = false; 788 789 static boolean isSelectCalled; 790 791 792 793 static class MockProxySelector extends ProxySelector { 794 795 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { 796 System.out.println("connection failed"); 797 } 798 799 public List<Proxy> select(URI uri) { 800 isSelectCalled = true; 801 ArrayList<Proxy> proxyList = new ArrayList<Proxy>(1); 802 proxyList.add(Proxy.NO_PROXY); 803 return proxyList; 804 } 805 } 806 807 static class MockSecurityManager extends SecurityManager { 808 809 public void checkConnect(String host, int port) { 810 if ("127.0.0.1".equals(host)) { 811 throw new SecurityException("permission is not allowed"); 812 } 813 } 814 815 public void checkPermission(Permission permission) { 816 if ("setSecurityManager".equals(permission.getName())) { 817 return; 818 } 819 super.checkPermission(permission); 820 } 821 822 } 823 824 825 static class MyURLStreamHandler extends URLStreamHandler { 826 827 @Override 828 protected URLConnection openConnection(URL arg0) throws IOException { 829 try { 830 URLConnection con = arg0.openConnection(); 831 con.setDoInput(true); 832 con.connect(); 833 return con; 834 } catch (Throwable e) { 835 return null; 836 } 837 } 838 839 public void parse(URL url, String spec, int start, int end) { 840 parseURL(url, spec, start, end); 841 } 842 } 843 844 static class MyURLStreamHandlerFactory implements URLStreamHandlerFactory { 845 846 public static MyURLStreamHandler handler = new MyURLStreamHandler(); 847 848 public URLStreamHandler createURLStreamHandler(String arg0) { 849 handler = new MyURLStreamHandler(); 850 return handler; 851 } 852 853 } 854 855 /** 856 * URLStreamHandler implementation class necessary for tests. 857 */ 858 private class TestURLStreamHandler extends URLStreamHandler { 859 public URLConnection openConnection(URL arg0) throws IOException { 860 try { 861 URLConnection con = arg0.openConnection(); 862 con.setDoInput(true); 863 con.connect(); 864 return con; 865 } catch (Throwable e) { 866 return null; 867 } 868 } 869 870 public URLConnection openConnection(URL arg0, Proxy proxy) 871 throws IOException { 872 return super.openConnection(u, proxy); 873 } 874 } 875 876 /** 877 * Test method for {@link java.net.URL#URL(java.lang.String, java.lang.String, int, java.lang.String, java.net.URLStreamHandler)}. 878 */ 879 @TestTargetNew( 880 level = TestLevel.COMPLETE, 881 notes = "From harmony branch. No meaningful MalformedURLException foundwhich doesn't end as a NullPointerException.", 882 method = "URL", 883 args = {java.lang.String.class, java.lang.String.class, int.class, java.lang.String.class, java.net.URLStreamHandler.class} 884 ) 885 public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_StringLjava_net_URLStreamHandler() 886 throws Exception { 887 u = new URL("http", "www.yahoo.com", 8080, "test.html#foo", null); 888 assertEquals("SSISH1 returns a wrong protocol", "http", u.getProtocol()); 889 assertEquals("SSISH1 returns a wrong host", "www.yahoo.com", u 890 .getHost()); 891 assertEquals("SSISH1 returns a wrong port", 8080, u.getPort()); 892 assertEquals("SSISH1 returns a wrong file", "test.html", u.getFile()); 893 assertTrue("SSISH1 returns a wrong anchor: " + u.getRef(), u.getRef() 894 .equals("foo")); 895 896 u = new URL("http", "www.yahoo.com", 8080, "test.html#foo", 897 new MyHandler()); 898 assertEquals("SSISH2 returns a wrong protocol", "http", u.getProtocol()); 899 assertEquals("SSISH2 returns a wrong host", "www.yahoo.com", u 900 .getHost()); 901 assertEquals("SSISH2 returns a wrong port", 8080, u.getPort()); 902 assertEquals("SSISH2 returns a wrong file", "test.html", u.getFile()); 903 assertTrue("SSISH2 returns a wrong anchor: " + u.getRef(), u.getRef() 904 .equals("foo")); 905 906 TestURLStreamHandler lh = new TestURLStreamHandler(); 907 u = new URL("http", "www.yahoo.com", 8080, "test.html#foo", 908 lh); 909 910 try { 911 new URL(null, "1", 0, "file", lh); 912 fail("Exception expected, but nothing was thrown!"); 913 } catch (MalformedURLException e) { 914 // ok 915 } catch (NullPointerException e) { 916 // Expected NullPointerException 917 } 918 919 } 920 921 /** 922 * Test method for {@link java.net.URL#getContent(java.lang.Class[])}. 923 */ 924 @TestTargetNew( 925 level = TestLevel.COMPLETE, 926 notes = "throws unexpected exception: NullPointerException in first execution", 927 method = "getContent", 928 args = {java.lang.Class[].class} 929 ) 930 public void test_getContent_LJavaLangClass() throws Exception { 931 932 File sampleFile = createTempHelloWorldFile(); 933 934 byte[] ba; 935 String s; 936 937 InputStream is = null; 938 939 try { 940 u = new URL("file:///data/tmp/hyts_htmltest.html"); 941 is = (InputStream) u.getContent(new Class[] {InputStream.class}); 942 is.read(ba = new byte[4096]); 943 fail("No error occurred reading from nonexisting file"); 944 } catch (IOException e) { 945 // ok 946 } 947 948 try { 949 u = new URL("file:///data/tmp/hyts_htmltest.html"); 950 is = (InputStream) u.getContent(new Class[] { 951 String.class, InputStream.class}); 952 is.read(ba = new byte[4096]); 953 fail("No error occurred reading from nonexisting file"); 954 } catch (IOException e) { 955 // ok 956 } 957 958 // Check for null 959 u = sampleFile.toURL(); 960 u.openConnection(); 961 assertNotNull(u); 962 963 s = (String) u.getContent(new Class[] {String.class}); 964 assertNull(s); 965 966 } 967 968 /** 969 * Test method for {@link java.net.URL#URL(java.net.URL, java.lang.String, java.net.URLStreamHandler)}. 970 */ 971 @TestTargetNew( 972 level = TestLevel.COMPLETE, 973 notes = "From harmony branch", 974 method = "URL", 975 args = {java.net.URL.class, java.lang.String.class, java.net.URLStreamHandler.class} 976 ) 977 public void testURLURLStringURLStreamHandler() throws MalformedURLException { 978 u = new URL("http://www.yahoo.com"); 979 // basic ones 980 u1 = new URL(u, "file.java", new MyHandler()); 981 assertEquals("1 returns a wrong protocol", "http", u1.getProtocol()); 982 assertEquals("1 returns a wrong host", "www.yahoo.com", u1.getHost()); 983 assertEquals("1 returns a wrong port", -1, u1.getPort()); 984 assertEquals("1 returns a wrong file", "/file.java", u1.getFile()); 985 assertNull("1 returns a wrong anchor", u1.getRef()); 986 987 u1 = new URL(u, "systemresource:/+/FILE0/test.java", new MyHandler()); 988 assertEquals("2 returns a wrong protocol", "systemresource", u1 989 .getProtocol()); 990 assertTrue("2 returns a wrong host", u1.getHost().equals("")); 991 assertEquals("2 returns a wrong port", -1, u1.getPort()); 992 assertEquals("2 returns a wrong file", "/+/FILE0/test.java", u1 993 .getFile()); 994 assertNull("2 returns a wrong anchor", u1.getRef()); 995 996 u1 = new URL(u, "dir1/dir2/../file.java", null); 997 assertEquals("3 returns a wrong protocol", "http", u1.getProtocol()); 998 assertEquals("3 returns a wrong host", "www.yahoo.com", u1.getHost()); 999 assertEquals("3 returns a wrong port", -1, u1.getPort()); 1000 assertEquals("3 returns a wrong file", "/dir1/dir2/../file.java", u1 1001 .getFile()); 1002 assertNull("3 returns a wrong anchor", u1.getRef()); 1003 1004 // test for question mark processing 1005 u = new URL("http://www.foo.com/d0/d1/d2/cgi-bin?foo=bar/baz"); 1006 1007 // test for relative file and out of bound "/../" processing 1008 u1 = new URL(u, "../dir1/dir2/../file.java", new MyHandler()); 1009 assertTrue("A) returns a wrong file: " + u1.getFile(), u1.getFile() 1010 .equals("/d0/d1/dir1/file.java")); 1011 1012 // test for absolute and relative file processing 1013 u1 = new URL(u, "/../dir1/dir2/../file.java", null); 1014 assertEquals("B) returns a wrong file", "/../dir1/dir2/../file.java", 1015 u1.getFile()); 1016 1017 URL one; 1018 try { 1019 one = new URL("http://www.ibm.com"); 1020 } catch (MalformedURLException ex) { 1021 // Should not happen. 1022 throw new RuntimeException(ex.getMessage()); 1023 } 1024 try { 1025 new URL(one, (String) null, null); 1026 fail("Specifying null spec on URL constructor should throw MalformedURLException"); 1027 } catch (MalformedURLException e) { 1028 // expected 1029 } 1030 1031 } 1032 1033 /** 1034 * Test method for {@link java.net.URL#toExternalForm()}. 1035 */ 1036 @TestTargetNew( 1037 level = TestLevel.PARTIAL_COMPLETE, 1038 notes = "From harmony branch", 1039 method = "toExternalForm", 1040 args = {} 1041 ) 1042 public void test_toExternalForm_Relative() throws MalformedURLException { 1043 String strURL = "http://a/b/c/d;p?q"; 1044 String ref = "?y"; 1045 URL url = new URL(new URL(strURL), ref); 1046 assertEquals("http://a/b/c/?y", url.toExternalForm()); 1047 } 1048 1049 @TestTargetNew( 1050 level = TestLevel.PARTIAL_COMPLETE, 1051 notes = "From harmony branch", 1052 method = "toExternalForm", 1053 args = {} 1054 ) 1055 public void test_toExternalForm_Absolute() throws MalformedURLException { 1056 String strURL = "http://localhost?name=value"; 1057 URL url = new URL(strURL); 1058 assertEquals(strURL, url.toExternalForm()); 1059 1060 strURL = "http://localhost?name=value/age=12"; 1061 url = new URL(strURL); 1062 assertEquals(strURL, url.toExternalForm()); 1063 } 1064} 1065