URITest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.net; 19 20import java.net.MalformedURLException; 21import java.net.URI; 22import java.net.URISyntaxException; 23 24import junit.framework.TestCase; 25 26import org.apache.harmony.testframework.serialization.SerializationTest; 27 28public class URITest extends TestCase { 29 30 private URI[] uris; 31 32 private URI[] getUris() throws URISyntaxException { 33 if (uris != null) { 34 return uris; 35 } 36 37 uris = new URI[] { 38 // single arg constructor 39 new URI( 40 "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag"), 41 // escaped octets for illegal chars 42 new URI( 43 "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g"), 44 // escaped octets for unicode chars 45 new URI( 46 "ascheme://user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g"), 47 // unicode chars equivalent to = new 48 // URI("ascheme://user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g"), 49 50 // multiple arg constructors 51 new URI("http", "user%60%20info", "host", 80, "/a%20path", //$NON-NLS-4$ 52 "qu%60%20ery", "fr%5E%20ag"), 53 // escaped octets for illegal 54 new URI("http", "user%C3%9F%C2%A3info", "host", -1, 55 "/a%E2%82%ACpath", "qu%C2%A9%C2%AEery", 56 "fr%C3%A4%C3%A8g"), 57 // escaped octets for unicode 58 new URI("ascheme", "user\u00DF\u00A3info", "host", 80, 59 "/a\u20ACpath", "qu\u00A9\u00AEery", "fr\u00E4\u00E8g"), 60 // unicode chars equivalent to = new 61 // URI("ascheme", "user\u00df\u00a3info", "host", 80, 62 // "/a\u0080path", "qu\u00a9\u00aeery", "fr\u00e4\u00e8g"), 63 new URI("http", "user` info", "host", 81, "/a path", "qu` ery", 64 "fr^ ag"), // illegal chars 65 new URI("http", "user%info", "host", 0, "/a%path", "que%ry", 66 "f%rag"), 67 // % as illegal char, not escaped octet 68 69 // urls with undefined components 70 new URI("mailto", "user@domain.com", null), 71 // no host, path, query or fragment 72 new URI("../adirectory/file.html#"), 73 // relative path with empty fragment; 74 new URI("news", "comp.infosystems.www.servers.unix", null), // 75 new URI(null, null, null, "fragment"), // only fragment 76 new URI("telnet://server.org"), // only host 77 new URI("http://reg:istry?query"), 78 // malformed hostname, therefore registry-based, 79 // with query 80 new URI("file:///c:/temp/calculate.pl?"), 81 // empty authority, non empty path, empty query 82 }; 83 return uris; 84 } 85 86 /** 87 * @tests java.net.URI#URI(java.lang.String) 88 */ 89 public void test_ConstructorLjava_lang_String() throws URISyntaxException { 90 // tests for public URI(String uri) throws URISyntaxException 91 92 String[] constructorTests = new String[] { 93 "http://user@www.google.com:45/search?q=helpinfo#somefragment", 94 // http with authority, query and fragment 95 "ftp://ftp.is.co.za/rfc/rfc1808.txt", // ftp 96 "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles", // gopher 97 "mailto:mduerst@ifi.unizh.ch", // mailto 98 "news:comp.infosystems.www.servers.unix", // news 99 "telnet://melvyl.ucop.edu/", // telnet 100 "http://123.24.17.98/test", // IPv4 authority 101 "http://www.google.com:80/test",// domain name authority 102 "http://joe@[3ffe:2a00:100:7031::1]:80/test", 103 // IPv6 authority, with userinfo and port 104 "/relative", // relative starting with / 105 "//relative", // relative starting with // 106 "relative", // relative with no / 107 "#fragment",// relative just with fragment 108 "http://user@host:80", // UI, host,port 109 "http://user@host", // ui, host 110 "http://host", // host 111 "http://host:80", // host,port 112 "http://joe@:80", // ui, port (becomes registry-based) 113 "file:///foo/bar", // empty authority, non empty path 114 "ht?tp://hoe@host:80", // miscellaneous tests 115 "mai/lto:hey?joe#man", "http://host/a%20path#frag", 116 // path with an escaped octet for space char 117 "http://host/a%E2%82%ACpath#frag", 118 // path with escaped octet for unicode char, not USASCII 119 "http://host/a\u20ACpath#frag", 120 // path with unicode char, not USASCII equivalent to 121 // = "http://host/a\u0080path#frag", 122 "http://host%20name/", // escaped octets in host (becomes 123 // registry based) 124 "http://host\u00DFname/", // unicodechar in host (becomes 125 // registry based) 126 // equivalent to = "http://host\u00dfname/", 127 "ht123-+tp://www.google.com:80/test", // legal chars in scheme 128 }; 129 130 for (int i = 0; i < constructorTests.length; i++) { 131 try { 132 new URI(constructorTests[i]); 133 } catch (URISyntaxException e) { 134 fail("Failed to construct URI for: " + constructorTests[i] 135 + " : " + e); 136 } 137 } 138 139 String[] constructorTestsInvalid = new String[] { 140 "http:///a path#frag", // space char in path, not in escaped 141 // octet form, with no host 142 "http://host/a[path#frag", // an illegal char, not in escaped 143 // octet form, should throw an 144 // exception 145 "http://host/a%path#frag", // invalid escape sequence in path 146 "http://host/a%#frag", // incomplete escape sequence in path 147 148 "http://host#a frag", // space char in fragment, not in 149 // escaped octet form, no path 150 "http://host/a#fr#ag", // illegal char in fragment 151 "http:///path#fr%ag", // invalid escape sequence in fragment, 152 // with no host 153 "http://host/path#frag%", // incomplete escape sequence in 154 // fragment 155 156 "http://host/path?a query#frag", // space char in query, not 157 // in escaped octet form 158 "http://host?query%ag", // invalid escape sequence in query, no 159 // path 160 "http:///path?query%", // incomplete escape sequence in query, 161 // with no host 162 163 "mailto:user^name@fklkf.com" // invalid char in scheme 164 // specific part 165 }; 166 167 int[] constructorTestsInvalidIndices = new int[] { 9, 13, 13, 13, 13, 168 16, 15, 21, 18, 17, 18, 11 }; 169 170 for (int i = 0; i < constructorTestsInvalid.length; i++) { 171 try { 172 new URI(constructorTestsInvalid[i]); 173 fail("Failed to throw URISyntaxException for: " 174 + constructorTestsInvalid[i]); 175 } catch (URISyntaxException e) { 176 assertTrue("Wrong index in URISytaxException for: " 177 + constructorTestsInvalid[i] + " expected: " 178 + constructorTestsInvalidIndices[i] + ", received: " 179 + e.getIndex(), 180 e.getIndex() == constructorTestsInvalidIndices[i]); 181 } 182 } 183 184 String invalid2[] = { 185 // authority validation 186 "http://user@[3ffe:2x00:100:7031::1]:80/test", // malformed 187 // IPv6 authority 188 "http://[ipv6address]/apath#frag", // malformed ipv6 address 189 "http://[ipv6address/apath#frag", // malformed ipv6 address 190 "http://ipv6address]/apath#frag", // illegal char in host name 191 "http://ipv6[address/apath#frag", 192 "http://ipv6addr]ess/apath#frag", 193 "http://ipv6address[]/apath#frag", 194 // illegal char in username... 195 "http://us[]er@host/path?query#frag", "http://host name/path", // illegal 196 // char 197 // in 198 // authority 199 "http://host^name#fragment", // illegal char in authority 200 "telnet://us er@hostname/", // illegal char in authority 201 // missing components 202 "//", // Authority expected 203 "ascheme://", // Authority expected 204 "ascheme:", // Scheme-specific part expected 205 // scheme validation 206 "a scheme://reg/", // illegal char 207 "1scheme://reg/", // non alpha char as 1st char 208 "asche\u00dfme:ssp", // unicode char , not USASCII 209 "asc%20heme:ssp" // escape octets 210 }; 211 212 for (int i = 0; i < invalid2.length; i++) { 213 try { 214 new URI(invalid2[i]); 215 fail("Failed to throw URISyntaxException for: " + invalid2[i]); 216 } catch (URISyntaxException e) { 217 } 218 } 219 220 // Regression test for HARMONY-23 221 try { 222 new URI("%3"); 223 fail("Assert 0: URI constructor failed to throw exception on invalid input."); 224 } catch (URISyntaxException e) { 225 // Expected 226 assertEquals("Assert 1: Wrong index in URISyntaxException.", 0, e 227 .getIndex()); 228 } 229 230 // Regression test for HARMONY-25 231 // if port value is negative, the authority should be considered 232 // registry-based. 233 URI uri = new URI("http://host:-8096/path/index.html"); 234 assertEquals("Assert 2: returned wrong port value,", -1, uri.getPort()); 235 assertNull("Assert 3: returned wrong host value,", uri.getHost()); 236 try { 237 uri.parseServerAuthority(); 238 fail("Assert 4: Expected URISyntaxException"); 239 } catch (URISyntaxException e) { 240 // Expected 241 } 242 243 uri = new URI("http", "//myhost:-8096", null); 244 assertEquals("Assert 5: returned wrong port value,", -1, uri.getPort()); 245 assertNull("Assert 6: returned wrong host value,", uri.getHost()); 246 try { 247 uri.parseServerAuthority(); 248 fail("Assert 7: Expected URISyntaxException"); 249 } catch (URISyntaxException e) { 250 // Expected 251 } 252 } 253 254 /** 255 * @tests java.net.URI#URI(java.lang.String) 256 */ 257 public void test_URI_String() { 258 try { 259 URI myUri = new URI(":abc@mymail.com"); 260 fail("TestA, URISyntaxException expected, but not received."); 261 } catch (URISyntaxException e) { 262 assertEquals("TestA, Wrong URISyntaxException index, ", 0, e 263 .getIndex()); 264 } 265 266 try { 267 URI uri = new URI("path[one"); 268 fail("TestB, URISyntaxException expected, but not received."); 269 } catch (URISyntaxException e1) { 270 assertEquals("TestB, Wrong URISyntaxException index, ", 4, e1 271 .getIndex()); 272 } 273 274 try { 275 URI uri = new URI(" "); 276 fail("TestC, URISyntaxException expected, but not received."); 277 } catch (URISyntaxException e2) { 278 assertEquals("TestC, Wrong URISyntaxException index, ", 0, e2 279 .getIndex()); 280 } 281 } 282 283 /** 284 * @tests java.net.URI#URI(java.lang.String, java.lang.String, 285 * java.lang.String) 286 */ 287 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String() 288 throws URISyntaxException { 289 URI uri = new URI("mailto", "mduerst@ifi.unizh.ch", null); 290 assertNull("wrong userinfo", uri.getUserInfo()); 291 assertNull("wrong hostname", uri.getHost()); 292 assertNull("wrong authority", uri.getAuthority()); 293 assertEquals("wrong port number", -1, uri.getPort()); 294 assertNull("wrong path", uri.getPath()); 295 assertNull("wrong query", uri.getQuery()); 296 assertNull("wrong fragment", uri.getFragment()); 297 assertEquals("wrong SchemeSpecificPart", "mduerst@ifi.unizh.ch", uri 298 .getSchemeSpecificPart()); 299 300 // scheme specific part can not be null 301 try { 302 uri = new URI("mailto", null, null); 303 fail("Expected URISyntaxException"); 304 } catch (URISyntaxException e) { 305 // Expected 306 } 307 308 // scheme needs to start with an alpha char 309 try { 310 uri = new URI("3scheme", "//authority/path", "fragment"); 311 fail("Expected URISyntaxException"); 312 } catch (URISyntaxException e) { 313 // Expected 314 } 315 316 // scheme can not be empty string 317 try { 318 uri = new URI("", "//authority/path", "fragment"); 319 fail("Expected URISyntaxException"); 320 } catch (URISyntaxException e) { 321 // Expected 322 } 323 } 324 325 /** 326 * @tests java.net.URI#URI(java.lang.String, java.lang.String, 327 * java.lang.String, int, java.lang.String, java.lang.String, 328 * java.lang.String) 329 */ 330 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringILjava_lang_StringLjava_lang_StringLjava_lang_String() { 331 // check for URISyntaxException for invalid Server Authority 332 construct1("http", "user", "host\u00DFname", -1, "/file", "query", 333 "fragment"); // unicode chars in host name 334 // equivalent to construct1("http", "user", "host\u00dfname", -1, 335 // "/file", "query", "fragment"); 336 construct1("http", "user", "host%20name", -1, "/file", "query", 337 "fragment"); // escaped octets in host name 338 construct1("http", "user", "host name", -1, "/file", "query", 339 "fragment"); // illegal char in host name 340 construct1("http", "user", "host]name", -1, "/file", "query", 341 "fragment"); // illegal char in host name 342 343 // missing host name 344 construct1("http", "user", "", 80, "/file", "query", "fragment"); 345 346 // missing host name 347 construct1("http", "user", "", -1, "/file", "query", "fragment"); 348 349 // malformed ipv4 address 350 construct1("telnet", null, "256.197.221.200", -1, null, null, null); 351 352 // malformed ipv4 address 353 construct1("ftp", null, "198.256.221.200", -1, null, null, null); 354 355 // These tests fail on other implementations... 356 // construct1("http", "user", null, 80, "/file", "query", "fragment"); 357 // //missing host name 358 // construct1("http", "user", null, -1, "/file", "query", "fragment"); 359 // //missing host name 360 361 // check for URISyntaxException for invalid scheme 362 construct1("ht\u00DFtp", "user", "hostname", -1, "/file", "query", 363 "fragment"); // unicode chars in scheme 364 // equivalent to construct1("ht\u00dftp", "user", "hostname", -1, 365 // "/file", 366 // "query", "fragment"); 367 368 construct1("ht%20tp", "user", "hostname", -1, "/file", "query", 369 "fragment"); // escaped octets in scheme 370 construct1("ht tp", "user", "hostname", -1, "/file", "query", 371 "fragment"); // illegal char in scheme 372 construct1("ht]tp", "user", "hostname", -1, "/file", "query", 373 "fragment"); // illegal char in scheme 374 375 // relative path with scheme 376 construct1("http", "user", "hostname", -1, "relative", "query", 377 "fragment"); // unicode chars in scheme 378 379 // functional test 380 URI uri; 381 try { 382 uri = new URI("http", "us:e@r", "hostname", 85, "/file/dir#/qu?e/", 383 "qu?er#y", "frag#me?nt"); 384 assertEquals("wrong userinfo", "us:e@r", uri.getUserInfo()); 385 assertEquals("wrong hostname", "hostname", uri.getHost()); 386 assertEquals("wrong port number", 85, uri.getPort()); 387 assertEquals("wrong path", "/file/dir#/qu?e/", uri.getPath()); 388 assertEquals("wrong query", "qu?er#y", uri.getQuery()); 389 assertEquals("wrong fragment", "frag#me?nt", uri.getFragment()); 390 assertEquals("wrong SchemeSpecificPart", 391 "//us:e@r@hostname:85/file/dir#/qu?e/?qu?er#y", uri 392 .getSchemeSpecificPart()); 393 } catch (URISyntaxException e) { 394 fail("Unexpected Exception: " + e); 395 } 396 } 397 398 /* 399 * helper method checking if the 7 arg constructor throws URISyntaxException 400 * for a given set of parameters 401 */ 402 private void construct1(String scheme, String userinfo, String host, 403 int port, String path, String query, String fragment) { 404 try { 405 URI uri = new URI(scheme, userinfo, host, port, path, query, 406 fragment); 407 fail("Expected URISyntaxException not thrown for URI: " 408 + uri.toString()); 409 } catch (URISyntaxException e) { 410 // this constructor throws URISyntaxException for malformed server 411 // based authorities 412 } 413 } 414 415 /** 416 * @throws URISyntaxException 417 * @tests java.net.URI#URI(java.lang.String, java.lang.String, 418 * java.lang.String, java.lang.String) 419 */ 420 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_String() 421 throws URISyntaxException { 422 // relative path 423 try { 424 URI myUri = new URI("http", "www.joe.com", "relative", "jimmy"); 425 fail("URISyntaxException expected but not received."); 426 } catch (URISyntaxException e) { 427 // Expected 428 } 429 430 // valid parameters for this constructor 431 URI uri; 432 433 uri = new URI("http", "www.joe.com", "/path", "jimmy"); 434 435 // illegal char in path 436 uri = new URI("http", "www.host.com", "/path?q", "somefragment"); 437 438 // empty fragment 439 uri = new URI("ftp", "ftp.is.co.za", "/rfc/rfc1808.txt", ""); 440 441 // path with escaped octet for unicode char, not USASCII 442 uri = new URI("http", "host", "/a%E2%82%ACpath", "frag"); 443 444 // frag with unicode char, not USASCII 445 // equivalent to = uri = new URI("http", "host", "/apath", 446 // "\u0080frag"); 447 uri = new URI("http", "host", "/apath", "\u20ACfrag"); 448 449 // Regression test for Harmony-1693 450 new URI(null, null, null, null); 451 452 // regression for Harmony-1346 453 try { 454 uri = new URI("http", ":2:3:4:5:6:7:8", "/apath", "\u20ACfrag"); 455 fail("Should throw URISyntaxException"); 456 } catch (URISyntaxException e) { 457 // Expected 458 } 459 } 460 461 /** 462 * @throws URISyntaxException 463 * @tests java.net.URI#URI(java.lang.String, java.lang.String, 464 * java.lang.String, java.lang.String, java.lang.String) 465 */ 466 public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_String() 467 throws URISyntaxException { 468 // URISyntaxException on relative path 469 try { 470 URI myUri = new URI("http", "www.joe.com", "relative", "query", 471 "jimmy"); 472 fail("URISyntaxException expected but not received."); 473 } catch (URISyntaxException e) { 474 // Expected 475 } 476 477 // test if empty authority is parsed into undefined host, userinfo and 478 // port and if unicode chars and escaped octets in components are 479 // preserved, illegal chars are quoted 480 URI uri = new URI("ht12-3+tp", "", "/p#a%E2%82%ACth", "q^u%25ery", 481 "f/r\u00DFag"); 482 483 assertEquals("wrong scheme", "ht12-3+tp", uri.getScheme()); 484 assertNull("wrong authority", uri.getUserInfo()); 485 assertNull("wrong userinfo", uri.getUserInfo()); 486 assertNull("wrong hostname", uri.getHost()); 487 assertEquals("wrong port number", -1, uri.getPort()); 488 assertEquals("wrong path", "/p#a%E2%82%ACth", uri.getPath()); 489 assertEquals("wrong query", "q^u%25ery", uri.getQuery()); 490 assertEquals("wrong fragment", "f/r\u00DFag", uri.getFragment()); 491 // equivalent to = assertTrue("wrong fragment", 492 // uri.getFragment().equals("f/r\u00dfag")); 493 assertEquals("wrong SchemeSpecificPart", "///p#a%E2%82%ACth?q^u%25ery", 494 uri.getSchemeSpecificPart()); 495 assertEquals("wrong RawSchemeSpecificPart", 496 "///p%23a%25E2%2582%25ACth?q%5Eu%2525ery", uri 497 .getRawSchemeSpecificPart()); 498 assertEquals( 499 "incorrect toString()", 500 "ht12-3+tp:///p%23a%25E2%2582%25ACth?q%5Eu%2525ery#f/r\u00dfag", 501 uri.toString()); 502 assertEquals("incorrect toASCIIString()", 503 504 "ht12-3+tp:///p%23a%25E2%2582%25ACth?q%5Eu%2525ery#f/r%C3%9Fag", uri 505 .toASCIIString()); 506 } 507 508 /** 509 * @throws URISyntaxException 510 * @tests java.net.URI#URI(java.lang.String, java.lang.String, 511 * java.lang.String, java.lang.String, java.lang.String) 512 */ 513 public void test_fiveArgConstructor() throws URISyntaxException { 514 // accept [] as part of valid ipv6 host name 515 URI uri = new URI("ftp", "[0001:1234::0001]", "/dir1/dir2", "query", 516 "frag"); 517 assertEquals("Returned incorrect host", "[0001:1234::0001]", uri 518 .getHost()); 519 520 // do not accept [] as part of invalid ipv6 address 521 try { 522 uri = new URI("ftp", "[www.abc.com]", "/dir1/dir2", "query", "frag"); 523 fail("Expected URISyntaxException for invalid ipv6 address"); 524 } catch (URISyntaxException e) { 525 // Expected 526 } 527 528 // do not accept [] as part of user info 529 try { 530 uri = new URI("ftp", "[user]@host", "/dir1/dir2", "query", "frag"); 531 fail("Expected URISyntaxException invalid user info"); 532 } catch (URISyntaxException e) { 533 // Expected 534 } 535 } 536 537 /** 538 * @tests java.net.URI#compareTo(java.lang.Object) 539 */ 540 public void test_compareToLjava_lang_Object() throws Exception { 541 // compareTo tests 542 543 String[][] compareToData = new String[][] { 544 // scheme tests 545 { "http:test", "" }, // scheme null, scheme not null 546 { "", "http:test" }, // reverse 547 { "http:test", "ftp:test" }, // schemes different 548 { "/test", "/test" }, // schemes null 549 { "http://joe", "http://joe" }, // schemes same 550 { "http://joe", "hTTp://joe" }, // schemes same ignoring case 551 552 // opacity : one opaque, the other not 553 { "http:opaque", "http://nonopaque" }, 554 { "http://nonopaque", "http:opaque" }, 555 { "mailto:abc", "mailto:abc" }, // same ssp 556 { "mailto:abC", "mailto:Abc" }, // different, by case 557 { "mailto:abc", "mailto:def" }, // different by letter 558 { "mailto:abc#ABC", "mailto:abc#DEF" }, 559 { "mailto:abc#ABC", "mailto:abc#ABC" }, 560 { "mailto:abc#DEF", "mailto:abc#ABC" }, 561 562 // hierarchical tests.. 563 564 // different authorities 565 { "//www.test.com/test", "//www.test2.com/test" }, 566 567 { "/nullauth", "//nonnullauth/test" }, // one null authority 568 { "//nonnull", "/null" }, 569 { "/hello", "/hello" }, // both authorities null 570 // different userinfo 571 { "http://joe@test.com:80", "http://test.com" }, 572 { "http://jim@test.com", "http://james@test.com" }, 573 // different hostnames 574 { "http://test.com", "http://toast.com" }, 575 { "http://test.com:80", "test.com:87" }, // different ports 576 { "http://test.com", "http://test.com:80" }, 577 // different paths 578 { "http://test.com:91/dir1", "http://test.com:91/dir2" }, 579 // one null host 580 { "http:/hostless", "http://hostfilled.com/hostless" }, 581 582 // queries 583 { "http://test.com/dir?query", "http://test.com/dir?koory" }, 584 { "/test?query", "/test" }, 585 { "/test", "/test?query" }, 586 { "/test", "/test" }, 587 588 // fragments 589 { "ftp://test.com/path?query#frag", "ftp://test.com/path?query" }, 590 { "ftp://test.com/path?query", "ftp://test.com/path?query#frag" }, 591 { "#frag", "#frag" }, { "p", "" }, 592 593 { "http://www.google.com", "#test" } // miscellaneous 594 }; 595 596 int[] compareToResults = { 1, -1, 2, 0, 0, 0, 1, -1, 0, 32, -3, -3, 0, 597 3, -4, -1, 1, 0, 1, 8, -10, -12, -81, -1, -1, 6, 1, -1, 0, 1, 598 -1, 0, 1, 1, }; 599 600 // test compareTo functionality 601 for (int i = 0; i < compareToResults.length; i++) { 602 URI b = new URI(compareToData[i][0]); 603 URI r = new URI(compareToData[i][1]); 604 if (b.compareTo(r) != compareToResults[i]) { 605 fail("Test " + i + ": " + compareToData[i][0] + " compared to " 606 + compareToData[i][1] + " -> " + b.compareTo(r) 607 + " rather than " + compareToResults[i]); 608 } 609 } 610 } 611 612 /** 613 * @throws URISyntaxException 614 * @tests java.net.URI#compareTo(java.lang.Object) 615 */ 616 public void test_compareTo2() throws URISyntaxException { 617 URI uri, uri2; 618 619 // test URIs with host names with different casing 620 uri = new URI("http://AbC.cOm/root/news"); 621 uri2 = new URI("http://aBc.CoM/root/news"); 622 assertEquals("TestA", 0, uri.compareTo(uri2)); 623 assertEquals("TestB", 0, uri.compareTo(uri2)); 624 625 // test URIs with one undefined component 626 uri = new URI("http://abc.com:80/root/news"); 627 uri2 = new URI("http://abc.com/root/news"); 628 assertTrue("TestC", uri.compareTo(uri2) > 0); 629 assertTrue("TestD", uri2.compareTo(uri) < 0); 630 631 // test URIs with one undefined component 632 uri = new URI("http://user@abc.com/root/news"); 633 uri2 = new URI("http://abc.com/root/news"); 634 assertTrue("TestE", uri.compareTo(uri2) > 0); 635 assertTrue("TestF", uri2.compareTo(uri) < 0); 636 } 637 638 /** 639 * @tests java.net.URI#create(java.lang.String) 640 */ 641 public void test_createLjava_lang_String() { 642 try { 643 URI myUri = URI.create("a scheme://reg/"); 644 fail("IllegalArgumentException expected but not received."); 645 } catch (IllegalArgumentException e) { 646 // Expected 647 } 648 } 649 650 /** 651 * @tests java.net.URI#equals(java.lang.Object) 652 */ 653 public void test_equalsLjava_lang_Object() throws Exception { 654 String[][] equalsData = new String[][] { 655 { "", "" }, // null frags 656 { "/path", "/path#frag" }, 657 { "#frag", "#frag2" }, 658 { "#frag", "#FRag" }, 659 660 // case insensitive on hex escapes 661 { "#fr%4F", "#fr%4f" }, 662 663 { "scheme:test", "scheme2:test" }, // scheme stuff 664 { "test", "http:test" }, 665 { "http:test", "test" }, 666 { "SCheme:test", "schEMe:test" }, 667 668 // hierarchical/opaque mismatch 669 { "mailto:jim", "mailto://jim" }, 670 { "mailto://test", "mailto:test" }, 671 672 // opaque 673 { "mailto:name", "mailto:name" }, 674 { "mailtO:john", "mailto:jim" }, 675 676 // test hex case insensitivity on ssp 677 { "mailto:te%4Fst", "mailto:te%4fst" }, 678 679 { "mailto:john#frag", "mailto:john#frag2" }, 680 681 // hierarchical 682 { "/test", "/test" }, // paths 683 { "/te%F4st", "/te%f4st" }, 684 { "/TEst", "/teSt" }, 685 { "", "/test" }, 686 687 // registry based because they don't resolve properly to 688 // server-based add more tests here 689 { "//host.com:80err", "//host.com:80e" }, 690 { "//host.com:81e%Abrr", "//host.com:81e%abrr" }, 691 692 { "/test", "//auth.com/test" }, 693 { "//test.com", "/test" }, 694 695 { "//test.com", "//test.com" }, // hosts 696 697 // case insensitivity for hosts 698 { "//HoSt.coM/", "//hOsT.cOm/" }, 699 { "//te%ae.com", "//te%aE.com" }, 700 { "//test.com:80", "//test.com:81" }, 701 { "//joe@test.com:80", "//test.com:80" }, 702 { "//jo%3E@test.com:82", "//jo%3E@test.com:82" }, 703 { "//test@test.com:85", "//test@test.com" }, }; 704 705 boolean[] equalsResults = new boolean[] { true, false, false, false, 706 true, false, false, false, true, false, false, true, false, 707 true, false, true, true, false, false, false, true, false, 708 false, true, true, true, false, false, true, false, }; 709 710 // test equals functionality 711 for (int i = 0; i < equalsResults.length; i++) { 712 URI b = new URI(equalsData[i][0]); 713 URI r = new URI(equalsData[i][1]); 714 if (b.equals(r) != equalsResults[i]) { 715 fail("Error: " + equalsData[i][0] + " == " + equalsData[i][1] 716 + "? -> " + b.equals(r) + " expected " 717 + equalsResults[i]); 718 } 719 } 720 721 } 722 723 /** 724 * @throws URISyntaxException 725 * @tests java.net.URI#equals(java.lang.Object) 726 */ 727 public void test_equals2() throws URISyntaxException { 728 // test URIs with empty string authority 729 URI uri = new URI("http:///~/dictionary"); 730 URI uri2 = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), 731 uri.getQuery(), uri.getFragment()); 732 assertTrue(uri2.equals(uri)); 733 734 // test URIs with port number 735 uri = new URI("http://abc.com%E2%82%AC:88/root/news"); 736 uri2 = new URI("http://abc.com%E2%82%AC/root/news"); 737 assertFalse(uri.equals(uri2)); 738 assertFalse(uri2.equals(uri)); 739 740 // test URIs with host names with different casing 741 uri = new URI("http://AbC.cOm/root/news"); 742 uri2 = new URI("http://aBc.CoM/root/news"); 743 assertTrue(uri.equals(uri2)); 744 assertTrue(uri2.equals(uri)); 745 } 746 747 /** 748 * @tests java.net.URI#getAuthority() 749 */ 750 public void test_getAuthority() throws Exception { 751 URI[] uris = getUris(); 752 753 String[] getAuthorityResults = { 754 "user` info@host", 755 "user\u00DF\u00A3info@host:80", // = 756 // "user\u00df\u00a3info@host:80", 757 "user\u00DF\u00A3info@host:0", // = 758 // "user\u00df\u00a3info@host:0", 759 "user%60%20info@host:80", 760 "user%C3%9F%C2%A3info@host", 761 "user\u00DF\u00A3info@host:80", // = 762 // "user\u00df\u00a3info@host:80", 763 "user` info@host:81", "user%info@host:0", null, null, null, 764 null, "server.org", "reg:istry", null, }; 765 766 for (int i = 0; i < uris.length; i++) { 767 String result = uris[i].getAuthority(); 768 if (getAuthorityResults[i] != result 769 && !getAuthorityResults[i].equals(result)) { 770 fail("Error: For URI \"" + uris[i].toString() 771 + "\", getAuthority() returned: " + result 772 + ", expected: " + getAuthorityResults[i]); 773 } 774 } 775 // regression test for HARMONY-1119 776 assertNull(new URI(null, null, null, 127, null, null, null) 777 .getAuthority()); 778 } 779 780 /** 781 * @tests java.net.URI#getAuthority() 782 */ 783 public void test_getAuthority2() throws Exception { 784 // tests for URIs with empty string authority component 785 786 URI uri = new URI("file:///tmp/"); 787 assertNull("Authority not null for URI: " + uri, uri.getAuthority()); 788 assertNull("Host not null for URI " + uri, uri.getHost()); 789 assertEquals("testA, toString() returned incorrect value", 790 "file:///tmp/", uri.toString()); 791 792 uri = new URI("file", "", "/tmp", "frag"); 793 assertNull("Authority not null for URI: " + uri, uri.getAuthority()); 794 assertNull("Host not null for URI " + uri, uri.getHost()); 795 assertEquals("testB, toString() returned incorrect value", 796 "file:///tmp#frag", uri.toString()); 797 798 uri = new URI("file", "", "/tmp", "query", "frag"); 799 assertNull("Authority not null for URI: " + uri, uri.getAuthority()); 800 assertNull("Host not null for URI " + uri, uri.getHost()); 801 assertEquals("test C, toString() returned incorrect value", 802 "file:///tmp?query#frag", uri.toString()); 803 804 // after normalization the host string info may be lost since the 805 // uri string is reconstructed 806 uri = new URI("file", "", "/tmp/a/../b/c", "query", "frag"); 807 URI uri2 = uri.normalize(); 808 assertNull("Authority not null for URI: " + uri2, uri.getAuthority()); 809 assertNull("Host not null for URI " + uri2, uri.getHost()); 810 assertEquals("test D, toString() returned incorrect value", 811 "file:///tmp/a/../b/c?query#frag", uri.toString()); 812 assertEquals("test E, toString() returned incorrect value", 813 "file:/tmp/b/c?query#frag", uri2.toString()); 814 815 // the empty string host will give URISyntaxException 816 // for the 7 arg constructor 817 try { 818 uri = new URI("file", "user", "", 80, "/path", "query", "frag"); 819 fail("Expected URISyntaxException"); 820 } catch (URISyntaxException e) { 821 // Expected 822 } 823 } 824 825 /** 826 * @tests java.net.URI#getFragment() 827 */ 828 public void test_getFragment() throws Exception { 829 URI[] uris = getUris(); 830 831 String[] getFragmentResults = { "fr^ ag", "fr\u00E4\u00E8g", // = 832 // "fr\u00e4\u00e8g", 833 "fr\u00E4\u00E8g", // = "fr\u00e4\u00e8g", 834 "fr%5E%20ag", "fr%C3%A4%C3%A8g", "fr\u00E4\u00E8g", // = 835 // "fr\u00e4\u00e8g", 836 "fr^ ag", "f%rag", null, "", null, "fragment", null, null, null }; 837 838 for (int i = 0; i < uris.length; i++) { 839 String result = uris[i].getFragment(); 840 if (getFragmentResults[i] != result 841 && !getFragmentResults[i].equals(result)) { 842 fail("Error: For URI \"" + uris[i].toString() 843 + "\", getFragment() returned: " + result 844 + ", expected: " + getFragmentResults[i]); 845 } 846 } 847 } 848 849 /** 850 * @tests java.net.URI#getHost() 851 */ 852 public void test_getHost() throws Exception { 853 URI[] uris = getUris(); 854 855 String[] getHostResults = { "host", "host", "host", "host", "host", 856 "host", "host", "host", null, null, null, null, "server.org", 857 null, null }; 858 859 for (int i = 0; i < uris.length; i++) { 860 String result = uris[i].getHost(); 861 if (getHostResults[i] != result 862 && !getHostResults[i].equals(result)) { 863 fail("Error: For URI \"" + uris[i].toString() 864 + "\", getHost() returned: " + result + ", expected: " 865 + getHostResults[i]); 866 } 867 } 868 } 869 870 /** 871 * @tests java.net.URI#getPath() 872 */ 873 public void test_getPath() throws Exception { 874 URI[] uris = getUris(); 875 876 String[] getPathResults = { "/a path", 877 "/a\u20ACpath", // = "/a\u0080path", 878 "/a\u20ACpath", // = "/a\u0080path", 879 "/a%20path", "/a%E2%82%ACpath", 880 "/a\u20ACpath", // = "/a\u0080path", 881 "/a path", "/a%path", null, "../adirectory/file.html", null, 882 "", "", "", "/c:/temp/calculate.pl" }; 883 884 for (int i = 0; i < uris.length; i++) { 885 String result = uris[i].getPath(); 886 if (getPathResults[i] != result 887 && !getPathResults[i].equals(result)) { 888 fail("Error: For URI \"" + uris[i].toString() 889 + "\", getPath() returned: " + result + ", expected: " 890 + getPathResults[i]); 891 } 892 } 893 } 894 895 /** 896 * @tests java.net.URI#getPort() 897 */ 898 public void test_getPort() throws Exception { 899 URI[] uris = getUris(); 900 901 int[] getPortResults = { -1, 80, 0, 80, -1, 80, 81, 0, -1, -1, -1, -1, 902 -1, -1, -1 }; 903 904 for (int i = 0; i < uris.length; i++) { 905 int result = uris[i].getPort(); 906 assertTrue("Error: For URI \"" + uris[i].toString() 907 + "\", getPort() returned: " + result + ", expected: " 908 + getPortResults[i], result == getPortResults[i]); 909 } 910 } 911 912 /** 913 * @tests java.net.URI#getPort() 914 */ 915 public void test_getPort2() throws Exception { 916 // if port value is negative, the authority should be 917 // consider registry based. 918 919 URI uri = new URI("http://myhost:-8096/site/index.html"); 920 assertEquals("TestA, returned wrong port value,", -1, uri.getPort()); 921 assertNull("TestA, returned wrong host value,", uri.getHost()); 922 try { 923 uri.parseServerAuthority(); 924 fail("TestA, Expected URISyntaxException"); 925 } catch (URISyntaxException e) { 926 // Expected 927 } 928 929 uri = new URI("http", "//myhost:-8096", null); 930 assertEquals("TestB returned wrong port value,", -1, uri.getPort()); 931 assertNull("TestB returned wrong host value,", uri.getHost()); 932 try { 933 uri.parseServerAuthority(); 934 fail("TestB, Expected URISyntaxException"); 935 } catch (URISyntaxException e) { 936 // Expected 937 } 938 } 939 940 /** 941 * @tests java.net.URI#getQuery() 942 */ 943 public void test_getQuery() throws Exception { 944 URI[] uris = getUris(); 945 946 String[] getQueryResults = { "qu` ery", "qu\u00A9\u00AEery", // = 947 // "qu\u00a9\u00aeery", 948 "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery", 949 "qu%60%20ery", "qu%C2%A9%C2%AEery", "qu\u00A9\u00AEery", // = 950 // "qu\u00a9\u00aeery", 951 "qu` ery", "que%ry", null, null, null, null, null, "query", "" }; 952 953 for (int i = 0; i < uris.length; i++) { 954 String result = uris[i].getQuery(); 955 if (getQueryResults[i] != result 956 && !getQueryResults[i].equals(result)) { 957 fail("Error: For URI \"" + uris[i].toString() 958 + "\", getQuery() returned: " + result + ", expected: " 959 + getQueryResults[i]); 960 } 961 } 962 } 963 964 /** 965 * @tests java.net.URI#getRawAuthority() 966 */ 967 public void test_getRawAuthority() throws Exception { 968 URI[] uris = getUris(); 969 970 String[] getRawAuthorityResults = { 971 "user%60%20info@host", 972 "user%C3%9F%C2%A3info@host:80", 973 "user\u00DF\u00A3info@host:0", // = 974 // "user\u00df\u00a3info@host:0", 975 "user%2560%2520info@host:80", 976 "user%25C3%259F%25C2%25A3info@host", 977 "user\u00DF\u00A3info@host:80", // = 978 // "user\u00df\u00a3info@host:80", 979 "user%60%20info@host:81", "user%25info@host:0", null, null, 980 null, null, "server.org", "reg:istry", null }; 981 982 for (int i = 0; i < uris.length; i++) { 983 String result = uris[i].getRawAuthority(); 984 if (getRawAuthorityResults[i] != result 985 && !getRawAuthorityResults[i].equals(result)) { 986 fail("Error: For URI \"" + uris[i].toString() 987 + "\", getRawAuthority() returned: " + result 988 + ", expected: " + getRawAuthorityResults[i]); 989 } 990 } 991 } 992 993 /** 994 * @tests java.net.URI#getRawFragment() 995 */ 996 public void test_getRawFragment() throws Exception { 997 URI[] uris = getUris(); 998 999 String[] getRawFragmentResults = { "fr%5E%20ag", 1000 "fr%C3%A4%C3%A8g", 1001 "fr\u00E4\u00E8g", // = "fr\u00e4\u00e8g", 1002 "fr%255E%2520ag", "fr%25C3%25A4%25C3%25A8g", 1003 "fr\u00E4\u00E8g", // = 1004 // "fr\u00e4\u00e8g", 1005 "fr%5E%20ag", "f%25rag", null, "", null, "fragment", null, 1006 null, null }; 1007 1008 for (int i = 0; i < uris.length; i++) { 1009 String result = uris[i].getRawFragment(); 1010 if (getRawFragmentResults[i] != result 1011 && !getRawFragmentResults[i].equals(result)) { 1012 fail("Error: For URI \"" + uris[i].toString() 1013 + "\", getRawFragment() returned: " + result 1014 + ", expected: " + getRawFragmentResults[i]); 1015 } 1016 } 1017 } 1018 1019 /** 1020 * @tests java.net.URI#getRawPath() 1021 */ 1022 public void test_getRawPath() throws Exception { 1023 URI[] uris = getUris(); 1024 1025 String[] getRawPathResults = { "/a%20path", 1026 "/a%E2%82%ACpath", 1027 "/a\u20ACpath", // = "/a\u0080path", 1028 "/a%2520path", "/a%25E2%2582%25ACpath", 1029 "/a\u20ACpath", // = 1030 // "/a\u0080path", 1031 "/a%20path", "/a%25path", null, "../adirectory/file.html", 1032 null, "", "", "", "/c:/temp/calculate.pl" }; 1033 1034 for (int i = 0; i < uris.length; i++) { 1035 String result = uris[i].getRawPath(); 1036 if (getRawPathResults[i] != result 1037 && !getRawPathResults[i].equals(result)) { 1038 fail("Error: For URI \"" + uris[i].toString() 1039 + "\", getRawPath() returned: " + result 1040 + ", expected: " + getRawPathResults[i]); 1041 } 1042 } 1043 } 1044 1045 /** 1046 * @tests java.net.URI#getRawQuery() 1047 */ 1048 public void test_getRawQuery() throws Exception { 1049 URI[] uris = getUris(); 1050 1051 String[] getRawQueryResults = { 1052 "qu%60%20ery", 1053 "qu%C2%A9%C2%AEery", 1054 "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery", 1055 "qu%2560%2520ery", 1056 "qu%25C2%25A9%25C2%25AEery", 1057 "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery", 1058 "qu%60%20ery", "que%25ry", null, null, null, null, null, 1059 "query", "" }; 1060 1061 for (int i = 0; i < uris.length; i++) { 1062 String result = uris[i].getRawQuery(); 1063 if (getRawQueryResults[i] != result 1064 && !getRawQueryResults[i].equals(result)) { 1065 fail("Error: For URI \"" + uris[i].toString() 1066 + "\", getRawQuery() returned: " + result 1067 + ", expected: " + getRawQueryResults[i]); 1068 } 1069 } 1070 1071 } 1072 1073 /** 1074 * @tests java.net.URI#getRawSchemeSpecificPart() 1075 */ 1076 public void test_getRawSchemeSpecificPart() throws Exception { 1077 URI[] uris = getUris(); 1078 1079 String[] getRawSspResults = { 1080 "//user%60%20info@host/a%20path?qu%60%20ery", 1081 "//user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery", 1082 "//user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery", // = 1083 // "//user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery" 1084 "//user%2560%2520info@host:80/a%2520path?qu%2560%2520ery", 1085 "//user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery", 1086 "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // = 1087 // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery" 1088 "//user%60%20info@host:81/a%20path?qu%60%20ery", 1089 "//user%25info@host:0/a%25path?que%25ry", "user@domain.com", 1090 "../adirectory/file.html", "comp.infosystems.www.servers.unix", 1091 "", "//server.org", "//reg:istry?query", 1092 "///c:/temp/calculate.pl?" }; 1093 1094 for (int i = 0; i < uris.length; i++) { 1095 String result = uris[i].getRawSchemeSpecificPart(); 1096 if (!getRawSspResults[i].equals(result)) { 1097 fail("Error: For URI \"" + uris[i].toString() 1098 + "\", getRawSchemeSpecificPart() returned: " + result 1099 + ", expected: " + getRawSspResults[i]); 1100 } 1101 } 1102 } 1103 1104 /** 1105 * @tests java.net.URI#getRawUserInfo() 1106 */ 1107 public void test_getRawUserInfo() throws URISyntaxException { 1108 URI[] uris = getUris(); 1109 1110 String[] getRawUserInfoResults = { 1111 "user%60%20info", 1112 "user%C3%9F%C2%A3info", 1113 "user\u00DF\u00A3info", // = "user\u00df\u00a3info", 1114 "user%2560%2520info", 1115 "user%25C3%259F%25C2%25A3info", 1116 "user\u00DF\u00A3info", // = "user\u00df\u00a3info", 1117 "user%60%20info", "user%25info", null, null, null, null, null, 1118 null, null }; 1119 1120 for (int i = 0; i < uris.length; i++) { 1121 String result = uris[i].getRawUserInfo(); 1122 if (getRawUserInfoResults[i] != result 1123 && !getRawUserInfoResults[i].equals(result)) { 1124 fail("Error: For URI \"" + uris[i].toString() 1125 + "\", getRawUserInfo() returned: " + result 1126 + ", expected: " + getRawUserInfoResults[i]); 1127 } 1128 } 1129 } 1130 1131 /** 1132 * @tests java.net.URI#getScheme() 1133 */ 1134 public void test_getScheme() throws Exception { 1135 URI[] uris = getUris(); 1136 1137 String[] getSchemeResults = { "http", "http", "ascheme", "http", 1138 "http", "ascheme", "http", "http", "mailto", null, "news", 1139 null, "telnet", "http", "file" }; 1140 1141 for (int i = 0; i < uris.length; i++) { 1142 String result = uris[i].getScheme(); 1143 if (getSchemeResults[i] != result 1144 && !getSchemeResults[i].equals(result)) { 1145 fail("Error: For URI \"" + uris[i].toString() 1146 + "\", getScheme() returned: " + result 1147 + ", expected: " + getSchemeResults[i]); 1148 } 1149 } 1150 } 1151 1152 /** 1153 * @tests java.net.URI#getSchemeSpecificPart() 1154 */ 1155 public void test_getSchemeSpecificPart() throws Exception { 1156 URI[] uris = getUris(); 1157 1158 String[] getSspResults = { 1159 "//user` info@host/a path?qu` ery", 1160 "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // = 1161 // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery", 1162 "//user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery", // = 1163 // "//user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery", 1164 "//user%60%20info@host:80/a%20path?qu%60%20ery", 1165 "//user%C3%9F%C2%A3info@host/a%E2%82%ACpath?qu%C2%A9%C2%AEery", 1166 "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // = 1167 // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery", 1168 "//user` info@host:81/a path?qu` ery", 1169 "//user%info@host:0/a%path?que%ry", "user@domain.com", 1170 "../adirectory/file.html", "comp.infosystems.www.servers.unix", 1171 "", "//server.org", "//reg:istry?query", 1172 "///c:/temp/calculate.pl?" }; 1173 1174 for (int i = 0; i < uris.length; i++) { 1175 String result = uris[i].getSchemeSpecificPart(); 1176 if (!getSspResults[i].equals(result)) { 1177 fail("Error: For URI \"" + uris[i].toString() 1178 + "\", getSchemeSpecificPart() returned: " + result 1179 + ", expected: " + getSspResults[i]); 1180 } 1181 } 1182 1183 } 1184 1185 /** 1186 * @tests java.net.URI#getUserInfo() 1187 */ 1188 public void test_getUserInfo() throws Exception { 1189 URI[] uris = getUris(); 1190 1191 String[] getUserInfoResults = { 1192 "user` info", 1193 "user\u00DF\u00A3info", // = 1194 // "user\u00df\u00a3info", 1195 "user\u00DF\u00A3info", // = "user\u00df\u00a3info", 1196 "user%60%20info", 1197 "user%C3%9F%C2%A3info", 1198 "user\u00DF\u00A3info", // = "user\u00df\u00a3info", 1199 "user` info", "user%info", null, null, null, null, null, null, 1200 null }; 1201 1202 for (int i = 0; i < uris.length; i++) { 1203 String result = uris[i].getUserInfo(); 1204 if (getUserInfoResults[i] != result 1205 && !getUserInfoResults[i].equals(result)) { 1206 fail("Error: For URI \"" + uris[i].toString() 1207 + "\", getUserInfo() returned: " + result 1208 + ", expected: " + getUserInfoResults[i]); 1209 } 1210 } 1211 } 1212 1213 /** 1214 * @tests java.net.URI#hashCode() 1215 */ 1216 public void test_hashCode() throws Exception { 1217 String[][] hashCodeData = new String[][] { 1218 { "", "" }, // null frags 1219 { "/path", "/path#frag" }, 1220 { "#frag", "#frag2" }, 1221 { "#frag", "#FRag" }, 1222 1223 { "#fr%4F", "#fr%4F" }, // case insensitive on hex escapes 1224 1225 { "scheme:test", "scheme2:test" }, // scheme 1226 { "test", "http:test" }, 1227 { "http:test", "test" }, 1228 1229 // case insensitivity for scheme 1230 { "SCheme:test", "schEMe:test" }, 1231 1232 // hierarchical/opaque mismatch 1233 { "mailto:jim", "mailto://jim" }, 1234 { "mailto://test", "mailto:test" }, 1235 1236 // opaque 1237 { "mailto:name", "mailto:name" }, 1238 { "mailtO:john", "mailto:jim" }, 1239 { "mailto:te%4Fst", "mailto:te%4Fst" }, 1240 { "mailto:john#frag", "mailto:john#frag2" }, 1241 1242 // hierarchical 1243 { "/test/", "/test/" }, // paths 1244 { "/te%F4st", "/te%F4st" }, 1245 { "/TEst", "/teSt" }, 1246 { "", "/test" }, 1247 1248 // registry based because they don't resolve properly to 1249 // server-based 1250 // add more tests here 1251 { "//host.com:80err", "//host.com:80e" }, 1252 { "//host.com:81e%Abrr", "//host.com:81e%Abrr" }, 1253 { "//Host.com:80e", "//hoSt.com:80e" }, 1254 1255 { "/test", "//auth.com/test" }, 1256 { "//test.com", "/test" }, 1257 1258 { "//test.com", "//test.com" }, // server based 1259 1260 // case insensitivity for host 1261 { "//HoSt.coM/", "//hOsT.cOm/" }, 1262 { "//te%aE.com", "//te%aE.com" }, 1263 { "//test.com:80", "//test.com:81" }, 1264 { "//joe@test.com:80", "//test.com:80" }, 1265 { "//jo%3E@test.com:82", "//jo%3E@test.com:82" }, 1266 { "//test@test.com:85", "//test@test.com" }, }; 1267 1268 boolean[] hashCodeResults = new boolean[] { true, false, false, false, 1269 true, false, false, false, true, false, false, true, false, 1270 true, false, true, true, false, false, false, true, false, 1271 false, false, true, true, true, false, false, true, false, }; 1272 1273 for (int i = 0; i < hashCodeResults.length; i++) { 1274 URI b = new URI(hashCodeData[i][0]); 1275 URI r = new URI(hashCodeData[i][1]); 1276 assertEquals("Error in hashcode equals results for" + b.toString() 1277 + " " + r.toString(), hashCodeResults[i], b.hashCode() == r 1278 .hashCode()); 1279 } 1280 1281 } 1282 1283 /** 1284 * @tests java.net.URI#isAbsolute() 1285 */ 1286 public void test_isAbsolute() throws URISyntaxException { 1287 String[] isAbsoluteData = new String[] { "mailto:user@ca.ibm.com", 1288 "urn:isbn:123498989h", "news:software.ibm.com", 1289 "http://www.amazon.ca", "file:///d:/temp/results.txt", 1290 "scheme:ssp", "calculate.pl?isbn=123498989h", 1291 "?isbn=123498989h", "//www.amazon.ca", "a.html", "#top", 1292 "//pc1/", "//user@host/path/file" }; 1293 1294 boolean results[] = new boolean[] { true, true, true, true, true, true, 1295 false, false, false, false, false, false, false }; 1296 1297 for (int i = 0; i < isAbsoluteData.length; i++) { 1298 boolean result = new URI(isAbsoluteData[i]).isAbsolute(); 1299 assertEquals("new URI(" + isAbsoluteData[i] + ").isAbsolute()", 1300 results[i], result); 1301 } 1302 } 1303 1304 /** 1305 * @tests java.net.URI#isOpaque() 1306 */ 1307 public void test_isOpaque() throws URISyntaxException { 1308 String[] isOpaqueData = new String[] { "mailto:user@ca.ibm.com", 1309 "urn:isbn:123498989h", "news:software.ibm.com", 1310 "http://www.amazon.ca", "file:///d:/temp/results.txt", 1311 "scheme:ssp", "calculate.pl?isbn=123498989h", 1312 "?isbn=123498989h", "//www.amazon.ca", "a.html", "#top", 1313 "//pc1/", "//user@host/path/file" }; 1314 1315 boolean results[] = new boolean[] { true, true, true, false, false, 1316 true, false, false, false, false, false, false, false }; 1317 1318 for (int i = 0; i < isOpaqueData.length; i++) { 1319 boolean result = new URI(isOpaqueData[i]).isOpaque(); 1320 assertEquals("new URI(" + isOpaqueData[i] + ").isOpaque()", 1321 results[i], result); 1322 } 1323 } 1324 1325 /** 1326 * @tests java.net.URI#normalize() 1327 */ 1328 public void test_normalize() throws Exception { 1329 1330 String[] normalizeData = new String[] { 1331 // normal 1332 "/", 1333 "/a", 1334 "/a/b", 1335 "/a/b/c", 1336 // single, '.' 1337 "/.", "/./", "/./.", "/././", 1338 "/./a", 1339 "/./a/", 1340 "/././a", 1341 "/././a/", 1342 "/a/.", 1343 "/a/./", 1344 "/a/./.", 1345 "/a/./b", 1346 // double, '..' 1347 "/a/..", "/a/../", "/a/../b", "/a/../b/..", "/a/../b/../", 1348 "/a/../b/../c", "/..", "/../", "/../..", "/../../", "/../a", 1349 "/../a/", "/../../a", "/../../a/", "/a/b/../../c", 1350 "/a/b/../..", 1351 "/a/b/../../", 1352 "/a/b/../../c", 1353 "/a/b/c/../../../d", 1354 "/a/b/..", 1355 "/a/b/../", 1356 "/a/b/../c", 1357 // miscellaneous 1358 "/a/b/.././../../c/./d/../e", 1359 "/a/../../.c././../././c/d/../g/..", 1360 // '.' in the middle of segments 1361 "/a./b", "/.a/b", "/a.b/c", "/a/b../c", 1362 "/a/..b/c", 1363 "/a/b..c/d", 1364 // no leading slash, miscellaneous 1365 "", "a", "a/b", "a/b/c", "../", ".", "..", "../g", 1366 "g/a/../../b/c/./g", "a/b/.././../../c/./d/../e", 1367 "a/../../.c././../././c/d/../g/..", }; 1368 1369 String[] normalizeResults = new String[] { "/", "/a", "/a/b", "/a/b/c", 1370 "/", "/", "/", "/", "/a", "/a/", "/a", "/a/", "/a/", "/a/", 1371 "/a/", "/a/b", "/", "/", "/b", "/", "/", "/c", "/..", "/../", 1372 "/../..", "/../../", "/../a", "/../a/", "/../../a", 1373 "/../../a/", "/c", "/", "/", "/c", "/d", "/a/", "/a/", "/a/c", 1374 "/../c/e", "/../c/", "/a./b", "/.a/b", "/a.b/c", "/a/b../c", 1375 "/a/..b/c", "/a/b..c/d", "", "a", "a/b", "a/b/c", "../", "", 1376 "..", "../g", "b/c/g", "../c/e", "../c/", }; 1377 1378 for (int i = 0; i < normalizeData.length; i++) { 1379 URI test = new URI(normalizeData[i]); 1380 String result = test.normalize().toString(); 1381 assertEquals("Normalized incorrectly, ", normalizeResults[i], 1382 result.toString()); 1383 } 1384 } 1385 1386 /** 1387 * @tests java.net.URI#normalize() 1388 */ 1389 public void test_normalize2() throws URISyntaxException { 1390 URI uri1 = null, uri2 = null; 1391 uri1 = new URI("file:/D:/one/two/../../three"); 1392 uri2 = uri1.normalize(); 1393 1394 assertEquals("Normalized to incorrect URI", "file:/D:/three", uri2 1395 .toString()); 1396 assertTrue("Resolved URI is not absolute", uri2.isAbsolute()); 1397 assertFalse("Resolved URI is opaque", uri2.isOpaque()); 1398 assertEquals("Resolved URI has incorrect scheme specific part", 1399 "/D:/three", uri2.getRawSchemeSpecificPart()); 1400 } 1401 1402 /** 1403 * @tests java.net.URI#normalize() 1404 */ 1405 public void test_normalize3() throws URISyntaxException { 1406 // return same URI if it has a normalized path already 1407 URI uri1 = null, uri2 = null; 1408 uri1 = new URI("http://host/D:/one/two/three"); 1409 uri2 = uri1.normalize(); 1410 assertSame("Failed to return same URI after normalization", uri1, uri2); 1411 1412 // try with empty path 1413 uri1 = new URI("http", "host", null, "fragment"); 1414 uri2 = uri1.normalize(); 1415 assertSame("Failed to return same URI after normalization", uri1, uri2); 1416 } 1417 1418 /** 1419 * @tests java.net.URI#parseServerAuthority() 1420 */ 1421 public void test_parseServerAuthority() throws URISyntaxException { 1422 // registry based uris 1423 URI[] uris = null; 1424 uris = new URI[] { 1425 // port number not digits 1426 new URI("http://foo:bar/file#fragment"), 1427 new URI("http", "//foo:bar/file", "fragment"), 1428 1429 // unicode char in the hostname = new 1430 // URI("http://host\u00dfname/") 1431 new URI("http://host\u00DFname/"), 1432 1433 new URI("http", "//host\u00DFname/", null), 1434 // = new URI("http://host\u00dfname/", null), 1435 1436 // escaped octets in host name 1437 new URI("http://host%20name/"), 1438 new URI("http", "//host%20name/", null), 1439 1440 // missing host name, port number 1441 new URI("http://joe@:80"), 1442 1443 // missing host name, no port number 1444 new URI("http://user@/file?query#fragment"), 1445 1446 new URI("//host.com:80err"), // malformed port number 1447 new URI("//host.com:81e%Abrr"), 1448 1449 // malformed ipv4 address 1450 new URI("telnet", "//256.197.221.200", null), 1451 1452 new URI("telnet://198.256.221.200"), 1453 new URI("//te%ae.com"), // misc .. 1454 new URI("//:port"), new URI("//:80"), 1455 1456 // last label has to start with alpha char 1457 new URI("//fgj234fkgj.jhj.123."), 1458 1459 new URI("//fgj234fkgj.jhj.123"), 1460 1461 // '-' cannot be first or last character in a label 1462 new URI("//-domain.name"), new URI("//domain.name-"), 1463 new URI("//domain-"), 1464 1465 // illegal char in host name 1466 new URI("//doma*in"), 1467 1468 // host expected 1469 new URI("http://:80/"), new URI("http://user@/"), 1470 1471 // ipv6 address not enclosed in "[]" 1472 new URI("http://3ffe:2a00:100:7031:22:1:80:89/"), 1473 1474 // expected ipv6 addresses to be enclosed in "[]" 1475 new URI("http", "34::56:78", "/path", "query", "fragment"), 1476 1477 // expected host 1478 new URI("http", "user@", "/path", "query", "fragment") }; 1479 // these URIs do not have valid server based authorities, 1480 // but single arg, 3 and 5 arg constructors 1481 // parse them as valid registry based authorities 1482 1483 // exception should occur when parseServerAuthority is 1484 // requested on these uris 1485 for (int i = 0; i < uris.length; i++) { 1486 try { 1487 URI uri = uris[i].parseServerAuthority(); 1488 fail("URISyntaxException expected but not received for URI: " 1489 + uris[i].toString()); 1490 } catch (URISyntaxException e) { 1491 // Expected 1492 } 1493 } 1494 1495 // valid Server based authorities 1496 new URI("http", "3ffe:2a00:100:7031:2e:1:80:80", "/path", "fragment") 1497 .parseServerAuthority(); 1498 new URI("http", "host:80", "/path", "query", "fragment") 1499 .parseServerAuthority(); 1500 new URI("http://[::3abc:4abc]:80/").parseServerAuthority(); 1501 new URI("http", "34::56:78", "/path", "fragment") 1502 .parseServerAuthority(); 1503 new URI("http", "[34:56::78]:80", "/path", "fragment") 1504 .parseServerAuthority(); 1505 1506 // invalid authorities (neither server nor registry) 1507 try { 1508 URI uri = new URI("http://us[er@host:80/"); 1509 fail("Expected URISyntaxException for URI " + uri.toString()); 1510 } catch (URISyntaxException e) { 1511 // Expected 1512 } 1513 1514 try { 1515 URI uri = new URI("http://[ddd::hgghh]/"); 1516 fail("Expected URISyntaxException for URI " + uri.toString()); 1517 } catch (URISyntaxException e) { 1518 // Expected 1519 } 1520 1521 try { 1522 URI uri = new URI("http", "[3ffe:2a00:100:7031:2e:1:80:80]a:80", 1523 "/path", "fragment"); 1524 fail("Expected URISyntaxException for URI " + uri.toString()); 1525 } catch (URISyntaxException e) { 1526 // Expected 1527 } 1528 1529 try { 1530 URI uri = new URI("http", "host:80", "/path", "fragment"); 1531 fail("Expected URISyntaxException for URI " + uri.toString()); 1532 } catch (URISyntaxException e) { 1533 // Expected 1534 } 1535 1536 // regression test for HARMONY-1126 1537 assertNotNull(URI.create("file://C:/1.txt").parseServerAuthority()); 1538 } 1539 1540 /** 1541 * @tests java.net.URI#relativize(java.net.URI) 1542 */ 1543 public void test_relativizeLjava_net_URI() throws URISyntaxException { 1544 // relativization tests 1545 String[][] relativizeData = new String[][] { 1546 // first is base, second is the one to relativize 1547 { "http://www.google.com/dir1/dir2", "mailto:test" }, // rel = 1548 // opaque 1549 { "mailto:test", "http://www.google.com" }, // base = opaque 1550 1551 // different authority 1552 { "http://www.eclipse.org/dir1", 1553 "http://www.google.com/dir1/dir2" }, 1554 1555 // different scheme 1556 { "http://www.google.com", "ftp://www.google.com" }, 1557 1558 { "http://www.google.com/dir1/dir2/", 1559 "http://www.google.com/dir3/dir4/file.txt" }, 1560 { "http://www.google.com/dir1/", 1561 "http://www.google.com/dir1/dir2/file.txt" }, 1562 { "./dir1/", "./dir1/hi" }, 1563 { "/dir1/./dir2", "/dir1/./dir2/hi" }, 1564 { "/dir1/dir2/..", "/dir1/dir2/../hi" }, 1565 { "/dir1/dir2/..", "/dir1/dir2/hi" }, 1566 { "/dir1/dir2/", "/dir1/dir3/../dir2/text" }, 1567 { "//www.google.com", "//www.google.com/dir1/file" }, 1568 { "/dir1", "/dir1/hi" }, { "/dir1/", "/dir1/hi" }, }; 1569 1570 // expected results 1571 String[] relativizeResults = new String[] { "mailto:test", 1572 "http://www.google.com", "http://www.google.com/dir1/dir2", 1573 "ftp://www.google.com", 1574 "http://www.google.com/dir3/dir4/file.txt", "dir2/file.txt", 1575 "hi", "hi", "hi", "dir2/hi", "text", "dir1/file", "hi", "hi", }; 1576 1577 for (int i = 0; i < relativizeData.length; i++) { 1578 try { 1579 URI b = new URI(relativizeData[i][0]); 1580 URI r = new URI(relativizeData[i][1]); 1581 if (!b.relativize(r).toString().equals(relativizeResults[i])) { 1582 fail("Error: relativize, " + relativizeData[i][0] + ", " 1583 + relativizeData[i][1] + " returned: " 1584 + b.relativize(r) + ", expected:" 1585 + relativizeResults[i]); 1586 } 1587 } catch (URISyntaxException e) { 1588 fail("Exception on relativize test on data " 1589 + relativizeData[i][0] + ", " + relativizeData[i][1] 1590 + ": " + e); 1591 } 1592 } 1593 1594 URI a = new URI("http://host/dir"); 1595 URI b = new URI("http://host/dir/file?query"); 1596 assertEquals("Assert 0: URI relativized incorrectly,", new URI( 1597 "file?query"), a.relativize(b)); 1598 1599 // One URI with empty host 1600 a = new URI("file:///~/first"); 1601 b = new URI("file://tools/~/first"); 1602 assertEquals("Assert 1: URI relativized incorrectly,", new URI( 1603 "file://tools/~/first"), a.relativize(b)); 1604 assertEquals("Assert 2: URI relativized incorrectly,", new URI( 1605 "file:///~/first"), b.relativize(a)); 1606 1607 // Both URIs with empty hosts 1608 b = new URI("file:///~/second"); 1609 assertEquals("Assert 3: URI relativized incorrectly,", new URI( 1610 "file:///~/second"), a.relativize(b)); 1611 assertEquals("Assert 4: URI relativized incorrectly,", new URI( 1612 "file:///~/first"), b.relativize(a)); 1613 } 1614 1615 // Regression test for HARMONY-6075 1616 public void test_relativize3() throws Exception { 1617 URI uri = new URI("file", null, "/test/location", null); 1618 1619 URI base = new URI("file", null, "/test", null); 1620 1621 URI relative = base.relativize(uri); 1622 assertEquals("location", relative.getSchemeSpecificPart()); 1623 assertNull(relative.getScheme()); 1624 } 1625 1626 /** 1627 * @tests java.net.URI#relativize(java.net.URI) 1628 */ 1629 public void test_relativize2() throws URISyntaxException { 1630 URI a = new URI("http://host/dir"); 1631 URI b = new URI("http://host/dir/file?query"); 1632 assertEquals("relativized incorrectly,", new URI("file?query"), a 1633 .relativize(b)); 1634 1635 // one URI with empty host 1636 a = new URI("file:///~/dictionary"); 1637 b = new URI("file://tools/~/dictionary"); 1638 assertEquals("relativized incorrectly,", new URI( 1639 "file://tools/~/dictionary"), a.relativize(b)); 1640 assertEquals("relativized incorrectly,", 1641 new URI("file:///~/dictionary"), b.relativize(a)); 1642 1643 // two URIs with empty hosts 1644 b = new URI("file:///~/therasus"); 1645 assertEquals("relativized incorrectly,", new URI("file:///~/therasus"), 1646 a.relativize(b)); 1647 assertEquals("relativized incorrectly,", 1648 new URI("file:///~/dictionary"), b.relativize(a)); 1649 1650 URI one = new URI("file:/C:/test/ws"); 1651 URI two = new URI("file:/C:/test/ws"); 1652 1653 URI empty = new URI(""); 1654 assertEquals(empty, one.relativize(two)); 1655 1656 one = new URI("file:/C:/test/ws"); 1657 two = new URI("file:/C:/test/ws/p1"); 1658 URI result = new URI("p1"); 1659 assertEquals(result, one.relativize(two)); 1660 1661 one = new URI("file:/C:/test/ws/"); 1662 assertEquals(result, one.relativize(two)); 1663 } 1664 1665 /** 1666 * @tests java.net.URI#resolve(java.net.URI) 1667 */ 1668 public void test_resolve() throws URISyntaxException { 1669 URI uri1 = null, uri2 = null; 1670 uri1 = new URI("file:/D:/one/two/three"); 1671 uri2 = uri1.resolve(new URI("..")); 1672 1673 assertEquals("Resolved to incorrect URI", "file:/D:/one/", uri2 1674 .toString()); 1675 assertTrue("Resolved URI is not absolute", uri2.isAbsolute()); 1676 assertFalse("Resolved URI is opaque", uri2.isOpaque()); 1677 assertEquals("Resolved URI has incorrect scheme specific part", 1678 "/D:/one/", uri2.getRawSchemeSpecificPart()); 1679 } 1680 1681 /** 1682 * @tests java.net.URI#resolve(java.net.URI) 1683 */ 1684 public void test_resolveLjava_net_URI() { 1685 // resolution tests 1686 String[][] resolveData = new String[][] { 1687 // authority in given URI 1688 { "http://www.test.com/dir", 1689 "//www.test.com/hello?query#fragment" }, 1690 // no authority, absolute path 1691 { "http://www.test.com/dir", "/abspath/file.txt" }, 1692 // no authority, relative paths 1693 { "/", "dir1/file.txt" }, { "/dir1", "dir2/file.txt" }, 1694 { "/dir1/", "dir2/file.txt" }, { "", "dir1/file.txt" }, 1695 { "dir1", "dir2/file.txt" }, { "dir1/", "dir2/file.txt" }, 1696 // normalization required 1697 { "/dir1/dir2/../dir3/./", "dir4/./file.txt" }, 1698 // allow a standalone fragment to be resolved 1699 { "http://www.google.com/hey/joe?query#fragment", "#frag2" }, 1700 // return given when base is opaque 1701 { "mailto:idontexist@uk.ibm.com", "dir1/dir2" }, 1702 // return given when given is absolute 1703 { "http://www.google.com/hi/joe", "http://www.oogle.com" }, }; 1704 1705 // expected results 1706 String[] resolveResults = new String[] { 1707 "http://www.test.com/hello?query#fragment", 1708 "http://www.test.com/abspath/file.txt", "/dir1/file.txt", 1709 "/dir2/file.txt", "/dir1/dir2/file.txt", "dir1/file.txt", 1710 "dir2/file.txt", "dir1/dir2/file.txt", 1711 "/dir1/dir3/dir4/file.txt", 1712 "http://www.google.com/hey/joe?query#frag2", "dir1/dir2", 1713 "http://www.oogle.com", }; 1714 1715 for (int i = 0; i < resolveResults.length; i++) { 1716 try { 1717 URI b = new URI(resolveData[i][0]); 1718 URI r = new URI(resolveData[i][1]); 1719 URI result = b.resolve(r); 1720 if (!result.toString().equals(resolveResults[i])) { 1721 fail("Error: resolve, " + resolveData[i][0] + ", " 1722 + resolveData[i][1] + " returned: " + b.resolve(r) 1723 + ", expected:" + resolveResults[i]); 1724 } 1725 if (!b.isOpaque()) { 1726 assertEquals(b + " and " + result 1727 + " incorrectly differ in absoluteness", b 1728 .isAbsolute(), result.isAbsolute()); 1729 } 1730 } catch (URISyntaxException e) { 1731 fail("Exception on resolve test on data " + resolveData[i][0] 1732 + ", " + resolveData[i][1] + ": " + e); 1733 } 1734 } 1735 } 1736 1737 /** 1738 * @tests java.net.URI#toASCIIString() 1739 */ 1740 public void test_toASCIIString() throws Exception { 1741 URI[] uris = getUris(); 1742 1743 String[] toASCIIStringResults0 = new String[] { 1744 "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag", 1745 "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g", 1746 "ascheme://user%C3%9F%C2%A3info@host:0/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g", 1747 "http://user%2560%2520info@host:80/a%2520path?qu%2560%2520ery#fr%255E%2520ag", 1748 "http://user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery#fr%25C3%25A4%25C3%25A8g", 1749 "ascheme://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g", 1750 "http://user%60%20info@host:81/a%20path?qu%60%20ery#fr%5E%20ag", 1751 "http://user%25info@host:0/a%25path?que%25ry#f%25rag", 1752 "mailto:user@domain.com", "../adirectory/file.html#", 1753 "news:comp.infosystems.www.servers.unix", "#fragment", 1754 "telnet://server.org", "http://reg:istry?query", 1755 "file:///c:/temp/calculate.pl?" }; 1756 1757 for (int i = 0; i < uris.length; i++) { 1758 String result = uris[i].toASCIIString(); 1759 assertTrue("Error: For URI \"" + uris[i].toString() 1760 + "\", toASCIIString() returned: " + result 1761 + ", expected: " + toASCIIStringResults0[i], result 1762 .equals(toASCIIStringResults0[i])); 1763 } 1764 1765 String[] toASCIIStringData = new String[] { 1766 "http://www.test.com/\u00DF/dir/", 1767 "http://www.test.com/\u20AC/dir", "http://www.\u20AC.com/dir", 1768 "http://www.test.com/\u20AC/dir/file#fragment", 1769 "mailto://user@domain.com", "mailto://user\u00DF@domain.com", }; 1770 1771 String[] toASCIIStringResults = new String[] { 1772 "http://www.test.com/%C3%9F/dir/", 1773 "http://www.test.com/%E2%82%AC/dir", 1774 "http://www.%E2%82%AC.com/dir", 1775 "http://www.test.com/%E2%82%AC/dir/file#fragment", 1776 "mailto://user@domain.com", "mailto://user%C3%9F@domain.com", }; 1777 1778 for (int i = 0; i < toASCIIStringData.length; i++) { 1779 URI test = new URI(toASCIIStringData[i]); 1780 String result = test.toASCIIString(); 1781 assertTrue("Error: new URI(\"" + toASCIIStringData[i] 1782 + "\").toASCIIString() returned: " + result 1783 + ", expected: " + toASCIIStringResults[i], result 1784 .equals(toASCIIStringResults[i])); 1785 } 1786 } 1787 1788 /** 1789 * @tests java.net.URI#toString() 1790 */ 1791 public void test_toString() throws Exception { 1792 URI[] uris = getUris(); 1793 1794 String[] toStringResults = { 1795 "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag", 1796 "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g", 1797 "ascheme://user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g", 1798 // = 1799 // "ascheme://user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g", 1800 "http://user%2560%2520info@host:80/a%2520path?qu%2560%2520ery#fr%255E%2520ag", 1801 "http://user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery#fr%25C3%25A4%25C3%25A8g", 1802 "ascheme://user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g", 1803 // = 1804 // "ascheme://user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g", 1805 "http://user%60%20info@host:81/a%20path?qu%60%20ery#fr%5E%20ag", 1806 "http://user%25info@host:0/a%25path?que%25ry#f%25rag", 1807 "mailto:user@domain.com", "../adirectory/file.html#", 1808 "news:comp.infosystems.www.servers.unix", "#fragment", 1809 "telnet://server.org", "http://reg:istry?query", 1810 "file:///c:/temp/calculate.pl?" }; 1811 1812 for (int i = 0; i < uris.length; i++) { 1813 String result = uris[i].toString(); 1814 assertTrue("Error: For URI \"" + uris[i].toString() 1815 + "\", toString() returned: " + result + ", expected: " 1816 + toStringResults[i], result.equals(toStringResults[i])); 1817 } 1818 } 1819 1820 /** 1821 * @tests java.net.URI#toURL() 1822 */ 1823 public void test_toURL() throws Exception { 1824 String absoluteuris[] = new String[] { "mailto:noreply@apache.org", 1825 "urn:isbn:123498989h", "news:software.ibm.com", 1826 "http://www.apache.org", "file:///d:/temp/results.txt", 1827 "scheme:ssp", }; 1828 1829 String relativeuris[] = new String[] { "calculate.pl?isbn=123498989h", 1830 "?isbn=123498989h", "//www.apache.org", "a.html", "#top", 1831 "//pc1/", "//user@host/path/file" }; 1832 1833 for (int i = 0; i < absoluteuris.length; i++) { 1834 try { 1835 new URI(absoluteuris[i]).toURL(); 1836 } catch (MalformedURLException e) { 1837 // not all the URIs can be translated into valid URLs 1838 } 1839 } 1840 1841 for (int i = 0; i < relativeuris.length; i++) { 1842 try { 1843 new URI(relativeuris[i]).toURL(); 1844 fail("Expected IllegalArgumentException not thrown"); 1845 } catch (IllegalArgumentException e) { 1846 // Expected 1847 } 1848 } 1849 } 1850 1851 /** 1852 * @tests serialization/deserialization. 1853 */ 1854 public void testSerializationSelf() throws Exception { 1855 URI uri = new URI("http://harmony.apache.org/"); 1856 1857 SerializationTest.verifySelf(uri); 1858 } 1859} 1860