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