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