Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/test/java/net/URI/Test.java
+++ new/test/java/net/URI/Test.java
1 1 /*
2 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 */
23 23
24 24 /* @test
25 25 * @summary Unit test for java.net.URI
26 - * @bug 4464135 4505046 4503239 4438319 4991359 4866303
26 + * @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363
27 27 * @author Mark Reinhold
28 28 */
29 29
30 30 import java.io.ByteArrayInputStream;
31 31 import java.io.ByteArrayOutputStream;
32 32 import java.io.IOException;
33 33 import java.io.ObjectInputStream;
34 34 import java.io.ObjectOutputStream;
35 35 import java.io.PrintStream;
36 36 import java.net.URI;
37 37 import java.net.URISyntaxException;
38 38 import java.net.URL;
39 39 import java.net.MalformedURLException;
40 40
41 41
42 42 public class Test {
43 43
44 44 static PrintStream out = System.out;
45 45 static int testCount = 0;
46 46
47 47 // Properties that we check
48 48 static final int PARSEFAIL = 1 << 0;
49 49 static final int SCHEME = 1 << 1;
50 50 static final int SSP = 1 << 2;
51 51 static final int SSP_D = 1 << 3; // Decoded form
52 52 static final int OPAQUEPART = 1 << 4; // SSP, and URI is opaque
53 53 static final int USERINFO = 1 << 5;
54 54 static final int USERINFO_D = 1 << 6; // Decoded form
55 55 static final int HOST = 1 << 7;
56 56 static final int PORT = 1 << 8;
57 57 static final int REGISTRY = 1 << 9;
58 58 static final int REGISTRY_D = 1 << 10; // Decoded form
59 59 static final int PATH = 1 << 11;
60 60 static final int PATH_D = 1 << 12; // Decoded form
61 61 static final int QUERY = 1 << 13;
62 62 static final int QUERY_D = 1 << 14; // Decoded form
63 63 static final int FRAGMENT = 1 << 15;
64 64 static final int FRAGMENT_D = 1 << 16; // Decoded form
65 65 static final int TOASCII = 1 << 17;
66 66 static final int IDENT_STR = 1 << 18; // Identities
67 67 static final int IDENT_URI1 = 1 << 19;
68 68 static final int IDENT_URI3 = 1 << 20;
69 69 static final int IDENT_URI5 = 1 << 21;
70 70 static final int IDENT_URI7 = 1 << 22;
71 71 static final int TOSTRING = 1 << 23;
72 72
73 73 String input;
74 74 URI uri = null;
75 75 URI originalURI;
76 76 URI base = null; // Base for resolution/relativization
77 77 String op = null; // Op performed if uri != originalURI
78 78 int checked = 0; // Mask for checked properties
79 79 int failed = 0; // Mask for failed properties
80 80 Exception exc = null;
81 81
82 82 private Test(String s) {
83 83 testCount++;
84 84 input = s;
85 85 try {
86 86 uri = new URI(s);
87 87 } catch (URISyntaxException x) {
88 88 exc = x;
89 89 }
90 90 originalURI = uri;
91 91 }
92 92
93 93 static Test test(String s) {
94 94 return new Test(s);
95 95 }
96 96
97 97 private Test(String s, String u, String h, int n,
98 98 String p, String q, String f)
99 99 {
100 100 testCount++;
101 101 try {
102 102 uri = new URI(s, u, h, n, p, q, f);
103 103 } catch (URISyntaxException x) {
104 104 exc = x;
105 105 input = x.getInput();
106 106 }
107 107 if (uri != null)
108 108 input = uri.toString();
109 109 originalURI = uri;
110 110 }
111 111
112 112 static Test test(String s, String u, String h, int n,
113 113 String p, String q, String f) {
114 114 return new Test(s, u, h, n, p, q, f);
115 115 }
116 116
117 117 private Test(String s, String a,
118 118 String p, String q, String f)
119 119 {
120 120 testCount++;
121 121 try {
122 122 uri = new URI(s, a, p, q, f);
123 123 } catch (URISyntaxException x) {
124 124 exc = x;
125 125 input = x.getInput();
126 126 }
127 127 if (uri != null)
128 128 input = uri.toString();
129 129 originalURI = uri;
130 130 }
131 131
132 132 static Test test(String s, String a,
133 133 String p, String q, String f) {
134 134 return new Test(s, a, p, q, f);
135 135 }
136 136
137 137 private Test(String s, String h, String p, String f) {
138 138 testCount++;
139 139 try {
140 140 uri = new URI(s, h, p, f);
141 141 } catch (URISyntaxException x) {
142 142 exc = x;
143 143 input = x.getInput();
144 144 }
145 145 if (uri != null)
146 146 input = uri.toString();
147 147 originalURI = uri;
148 148 }
149 149
150 150 static Test test(String s, String h, String p, String f) {
151 151 return new Test(s, h, p, f);
152 152 }
153 153
154 154 private Test(String s, String ssp, String f) {
155 155 testCount++;
156 156 try {
157 157 uri = new URI(s, ssp, f);
158 158 } catch (URISyntaxException x) {
159 159 exc = x;
160 160 input = x.getInput();
161 161 }
162 162 if (uri != null)
163 163 input = uri.toString();
164 164 originalURI = uri;
165 165 }
166 166
167 167 static Test test(String s, String ssp, String f) {
168 168 return new Test(s, ssp, f);
169 169 }
170 170
171 171 private Test(String s, boolean xxx) {
172 172 testCount++;
173 173 try {
174 174 uri = URI.create(s);
175 175 } catch (IllegalArgumentException x) {
176 176 exc = x;
177 177 }
178 178 if (uri != null)
179 179 input = uri.toString();
180 180 originalURI = uri;
181 181 }
182 182
183 183 static Test testCreate(String s) {
184 184 return new Test(s, false);
185 185 }
186 186
187 187 boolean parsed() {
188 188 return uri != null;
189 189 }
190 190
191 191 boolean resolved() {
192 192 return base != null;
193 193 }
194 194
195 195 URI uri() {
196 196 return uri;
197 197 }
198 198
199 199
200 200 // Operations on Test instances
201 201 //
202 202 // These are short so as to make test cases compact.
203 203 //
204 204 // s Scheme
205 205 // sp Scheme-specific part
206 206 // spd Scheme-specific part, decoded
207 207 // o Opaque part (isOpaque() && ssp matches)
208 208 // g reGistry (authority matches, and host is not defined)
209 209 // gd reGistry, decoded
210 210 // u User info
211 211 // ud User info, decoded
212 212 // h Host
213 213 // n port Number
214 214 // p Path
215 215 // pd Path, decoded
216 216 // q Query
217 217 // qd Query, decoded
218 218 // f Fragment
219 219 // fd Fragment, decoded
220 220 //
221 221 // rslv Resolve against given base
222 222 // rtvz Relativize
223 223 // psa Parse server Authority
224 224 // norm Normalize
225 225 // ta ASCII form
226 226 //
227 227 // x Check that parse failed as expected
228 228 // z End -- ensure that unchecked components are null
229 229
230 230 private boolean check1(int prop) {
231 231 checked |= prop;
232 232 if (!parsed()) {
233 233 failed |= prop;
234 234 return false;
235 235 }
236 236 return true;
237 237 }
238 238
239 239 private void check2(String s, String ans, int prop) {
240 240 if ((s == null) || !s.equals(ans))
241 241 failed |= prop;
242 242 }
243 243
244 244 Test s(String s) {
245 245 if (check1(SCHEME)) check2(uri.getScheme(), s, SCHEME);
246 246 return this;
247 247 }
248 248
249 249 Test u(String s) {
250 250 if (check1(USERINFO)) check2(uri.getRawUserInfo(), s, USERINFO);
251 251 return this;
252 252 }
253 253
254 254 Test ud(String s) {
255 255 if (check1(USERINFO_D)) {
256 256 check2(uri.getUserInfo(), s, USERINFO_D);
257 257 }
258 258 return this;
259 259 }
260 260
261 261 Test h(String s) {
262 262 if (check1(HOST)) check2(uri.getHost(), s, HOST);
263 263 return this;
264 264 }
265 265
266 266 Test g(String s) {
267 267 if (check1(REGISTRY)) {
268 268 if (uri.getHost() != null)
269 269 failed |= REGISTRY;
270 270 else
271 271 check2(uri.getRawAuthority(), s, REGISTRY);
272 272 }
273 273 return this;
274 274 }
275 275
276 276 Test gd(String s) {
277 277 if (check1(REGISTRY_D)) {
278 278 if (uri.getHost() != null)
279 279 failed |= REGISTRY_D;
280 280 else
281 281 check2(uri.getAuthority(), s, REGISTRY_D);
282 282 }
283 283 return this;
284 284 }
285 285
286 286 Test n(int n) {
287 287 checked |= PORT;
288 288 if (!parsed() || (uri.getPort() != n))
289 289 failed |= PORT;
290 290 return this;
291 291 }
292 292
293 293 Test p(String s) {
294 294 if (check1(PATH)) check2(uri.getRawPath(), s, PATH);
295 295 return this;
296 296 }
297 297
298 298 Test pd(String s) {
299 299 if (check1(PATH_D)) check2(uri.getPath(), s, PATH_D);
300 300 return this;
301 301 }
302 302
303 303 Test o(String s) {
304 304 if (check1(OPAQUEPART)) {
305 305 if (!uri.isOpaque())
306 306 failed |= OPAQUEPART;
307 307 else
308 308 check2(uri.getSchemeSpecificPart(), s, OPAQUEPART);
309 309 }
310 310 return this;
311 311 }
312 312
313 313 Test sp(String s) {
314 314 if (check1(SSP)) check2(uri.getRawSchemeSpecificPart(), s, SSP);
315 315 return this;
316 316 }
317 317
318 318 Test spd(String s) {
319 319 if (check1(SSP_D)) check2(uri.getSchemeSpecificPart(), s, SSP_D);
320 320 return this;
321 321 }
322 322
323 323 Test q(String s) {
324 324 if (check1(QUERY)) check2(uri.getRawQuery(), s, QUERY);
325 325 return this;
326 326 }
327 327
328 328 Test qd(String s) {
329 329 if (check1(QUERY_D)) check2(uri.getQuery(), s, QUERY_D);
330 330 return this;
331 331 }
332 332
333 333 Test f(String s) {
334 334 if (check1(FRAGMENT)) check2(uri.getRawFragment(), s, FRAGMENT);
335 335 return this;
336 336 }
337 337
338 338 Test fd(String s) {
339 339 if (check1(FRAGMENT_D)) check2(uri.getFragment(), s, FRAGMENT_D);
340 340 return this;
341 341 }
342 342
343 343 Test ta(String s) {
344 344 if (check1(TOASCII))
345 345 check2(uri.toASCIIString(), s, TOASCII);
346 346 return this;
347 347 }
348 348
349 349 Test ts(String s) {
350 350 if (check1(TOSTRING))
351 351 check2(uri.toString(), s, TOSTRING);
352 352 return this;
353 353 }
354 354
355 355 Test x() {
356 356 checked |= PARSEFAIL;
357 357 if (parsed())
358 358 failed |= PARSEFAIL;
359 359 return this;
360 360 }
361 361
362 362 Test rslv(URI base) {
363 363 if (!parsed())
364 364 return this;
365 365 this.base = base;
366 366 op = "rslv";
367 367 URI u = uri;
368 368 uri = null;
369 369 try {
370 370 this.uri = base.resolve(u);
371 371 } catch (IllegalArgumentException x) {
372 372 exc = x;
373 373 }
374 374 checked = 0;
375 375 failed = 0;
376 376 return this;
377 377 }
378 378
379 379 Test norm() {
380 380 if (!parsed())
381 381 return this;
382 382 op = "norm";
383 383 uri = uri.normalize();
384 384 return this;
385 385 }
386 386
387 387 Test rtvz(URI base) {
388 388 if (!parsed())
389 389 return this;
390 390 this.base = base;
391 391 op = "rtvz";
392 392 uri = base.relativize(uri);
393 393 checked = 0;
394 394 failed = 0;
395 395 return this;
396 396 }
397 397
398 398 Test psa() {
399 399 try {
400 400 uri.parseServerAuthority();
401 401 } catch (URISyntaxException x) {
402 402 exc = x;
403 403 uri = null;
404 404 }
405 405 checked = 0;
406 406 failed = 0;
407 407 return this;
408 408 }
409 409
410 410 private void checkEmpty(String s, int prop) {
411 411 if (((checked & prop) == 0) && (s != null))
412 412 failed |= prop;
413 413 }
414 414
415 415 // Check identity for the seven-argument URI constructor
416 416 //
417 417 void checkURI7() {
418 418 // Only works on hierarchical URIs
419 419 if (uri.isOpaque())
420 420 return;
421 421 // Only works with server-based authorities
422 422 if ((uri.getAuthority() == null)
423 423 != ((uri.getUserInfo() == null) && (uri.getHost() == null)))
424 424 return;
425 425 // Not true if non-US-ASCII chars are encoded unnecessarily
426 426 if (uri.getPath().indexOf('\u20AC') >= 0)
427 427 return;
428 428 try {
429 429 URI u2 = new URI(uri.getScheme(), uri.getUserInfo(),
430 430 uri.getHost(), uri.getPort(), uri.getPath(),
431 431 uri.getQuery(), uri.getFragment());
432 432 if (!uri.equals(u2))
433 433 failed |= IDENT_URI7;
434 434 } catch (URISyntaxException x) {
435 435 failed |= IDENT_URI7;
436 436 }
437 437 }
438 438
439 439 // Check identity for the five-argument URI constructor
440 440 //
441 441 void checkURI5() {
442 442 // Only works on hierarchical URIs
443 443 if (uri.isOpaque())
444 444 return;
445 445 try {
446 446 URI u2 = new URI(uri.getScheme(), uri.getAuthority(),
447 447 uri.getPath(), uri.getQuery(), uri.getFragment());
448 448 if (!uri.equals(u2))
449 449 failed |= IDENT_URI5;
450 450 } catch (URISyntaxException x) {
451 451 failed |= IDENT_URI5;
452 452 }
453 453 }
454 454
455 455 // Check identity for the three-argument URI constructor
456 456 //
457 457 void checkURI3() {
458 458 try {
459 459 URI u2 = new URI(uri.getScheme(),
460 460 uri.getSchemeSpecificPart(),
461 461 uri.getFragment());
462 462 if (!uri.equals(u2))
463 463 failed |= IDENT_URI3;
464 464 } catch (URISyntaxException x) {
465 465 failed |= IDENT_URI3;
466 466 }
467 467 }
468 468
469 469 // Check all identities mentioned in the URI class specification
470 470 //
471 471 void checkIdentities() {
472 472 if (input != null) {
473 473 if (!uri.toString().equals(input))
474 474 failed |= IDENT_STR;
475 475 }
476 476 try {
477 477 if (!(new URI(uri.toString())).equals(uri))
478 478 failed |= IDENT_URI1;
479 479 } catch (URISyntaxException x) {
480 480 failed |= IDENT_URI1;
481 481 }
482 482
483 483 // Remaining identities fail if "//" given but authority is undefined
484 484 if ((uri.getAuthority() == null)
485 485 && (uri.getSchemeSpecificPart() != null)
486 486 && (uri.getSchemeSpecificPart().startsWith("///")
487 487 || uri.getSchemeSpecificPart().startsWith("//?")
488 488 || uri.getSchemeSpecificPart().equals("//")))
489 489 return;
490 490
491 491 // Remaining identities fail if ":" given but port is undefined
492 492 if ((uri.getHost() != null)
493 493 && (uri.getAuthority() != null)
494 494 && (uri.getAuthority().equals(uri.getHost() + ":")))
495 495 return;
496 496
497 497 // Remaining identities fail if non-US-ASCII chars are encoded
498 498 // unnecessarily
499 499 if ((uri.getPath() != null) && uri.getPath().indexOf('\u20AC') >= 0)
500 500 return;
501 501
502 502 checkURI3();
503 503 checkURI5();
504 504 checkURI7();
505 505 }
506 506
507 507 // Check identities, check that unchecked component properties are not
508 508 // defined, and report any failures
509 509 //
510 510 Test z() {
511 511 if (!parsed()) {
512 512 report();
513 513 return this;
514 514 }
515 515
516 516 if (op == null)
517 517 checkIdentities();
518 518
519 519 // Check that unchecked components are undefined
520 520 checkEmpty(uri.getScheme(), SCHEME);
521 521 checkEmpty(uri.getUserInfo(), USERINFO);
522 522 checkEmpty(uri.getHost(), HOST);
523 523 if (((checked & PORT) == 0) && (uri.getPort() != -1)) failed |= PORT;
524 524 checkEmpty(uri.getPath(), PATH);
525 525 checkEmpty(uri.getQuery(), QUERY);
526 526 checkEmpty(uri.getFragment(), FRAGMENT);
527 527
528 528 // Report failures
529 529 report();
530 530 return this;
531 531 }
532 532
533 533
534 534 // Summarization and reporting
535 535
536 536 static void header(String s) {
537 537 out.println();
538 538 out.println();
539 539 out.println("-- " + s + " --");
540 540 }
541 541
542 542 static void show(String prefix, URISyntaxException x) {
543 543 out.println(uquote(x.getInput()));
544 544 if (x.getIndex() >= 0) {
545 545 for (int i = 0; i < x.getIndex(); i++) {
546 546 if (x.getInput().charAt(i) >= '\u0080')
547 547 out.print(" "); // Skip over \u1234
548 548 else
549 549 out.print(" ");
550 550 }
551 551 out.println("^");
552 552 }
553 553 out.println(prefix + ": " + x.getReason());
554 554 }
555 555
556 556 private void summarize() {
557 557 out.println();
558 558 StringBuffer sb = new StringBuffer();
559 559 if (input.length() == 0)
560 560 sb.append("\"\"");
561 561 else
562 562 sb.append(input);
563 563 if (base != null) {
564 564 sb.append(" ");
565 565 sb.append(base);
566 566 }
567 567 if (!parsed()) {
568 568 String s = (((checked & PARSEFAIL) != 0)
569 569 ? "Correct exception" : "UNEXPECTED EXCEPTION");
570 570 if (exc instanceof URISyntaxException)
571 571 show(s, (URISyntaxException)exc);
572 572 else {
573 573 out.println(uquote(sb.toString()));
574 574 out.print(s + ": ");
575 575 exc.printStackTrace(out);
576 576 }
577 577 } else {
578 578 if (uri != originalURI) {
579 579 sb.append(" ");
580 580 sb.append(op);
581 581 sb.append(" --> ");
582 582 sb.append(uri);
583 583 }
584 584 out.println(uquote(sb.toString()));
585 585 }
586 586 }
587 587
588 588 public static String uquote(String str) {
589 589 if (str == null)
590 590 return str;
591 591 StringBuffer sb = new StringBuffer();
592 592 int n = str.length();
593 593 for (int i = 0; i < n; i++) {
594 594 char c = str.charAt(i);
595 595 if ((c >= ' ') && (c < 0x7f)) {
596 596 sb.append(c);
597 597 continue;
598 598 }
599 599 sb.append("\\u");
600 600 String s = Integer.toHexString(c).toUpperCase();
601 601 while (s.length() < 4)
602 602 s = "0" + s;
603 603 sb.append(s);
604 604 }
605 605 return sb.toString();
606 606 }
607 607
608 608 static void show(String n, String v) {
609 609 out.println(" " + n
610 610 + " = ".substring(n.length())
611 611 + uquote(v));
612 612 }
613 613
614 614 static void show(String n, String v, String vd) {
615 615 if ((v == null) || v.equals(vd))
616 616 show(n, v);
617 617 else {
618 618 out.println(" " + n
619 619 + " = ".substring(n.length())
620 620 + uquote(v)
621 621 + " = " + uquote(vd));
622 622 }
623 623 }
624 624
625 625 public static void show(URI u) {
626 626 show("opaque", "" + u.isOpaque());
627 627 show("scheme", u.getScheme());
628 628 show("ssp", u.getRawSchemeSpecificPart(), u.getSchemeSpecificPart());
629 629 show("authority", u.getRawAuthority(), u.getAuthority());
630 630 show("userinfo", u.getRawUserInfo(), u.getUserInfo());
631 631 show("host", u.getHost());
632 632 show("port", "" + u.getPort());
633 633 show("path", u.getRawPath(), u.getPath());
634 634 show("query", u.getRawQuery(), u.getQuery());
635 635 show("fragment", u.getRawFragment(), u.getFragment());
636 636 if (!u.toString().equals(u.toASCIIString()))
637 637 show("toascii", u.toASCIIString());
638 638 }
639 639
640 640 private void report() {
641 641 summarize();
642 642 if (failed == 0) return;
643 643 StringBuffer sb = new StringBuffer();
644 644 sb.append("FAIL:");
645 645 if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");
646 646 if ((failed & SCHEME) != 0) sb.append(" scheme");
647 647 if ((failed & SSP) != 0) sb.append(" ssp");
648 648 if ((failed & OPAQUEPART) != 0) sb.append(" opaquepart");
649 649 if ((failed & USERINFO) != 0) sb.append(" userinfo");
650 650 if ((failed & USERINFO_D) != 0) sb.append(" userinfod");
651 651 if ((failed & HOST) != 0) sb.append(" host");
652 652 if ((failed & PORT) != 0) sb.append(" port");
653 653 if ((failed & REGISTRY) != 0) sb.append(" registry");
654 654 if ((failed & PATH) != 0) sb.append(" path");
655 655 if ((failed & PATH_D) != 0) sb.append(" pathd");
656 656 if ((failed & QUERY) != 0) sb.append(" query");
657 657 if ((failed & QUERY_D) != 0) sb.append(" queryd");
658 658 if ((failed & FRAGMENT) != 0) sb.append(" fragment");
659 659 if ((failed & FRAGMENT_D) != 0) sb.append(" fragmentd");
660 660 if ((failed & TOASCII) != 0) sb.append(" toascii");
661 661 if ((failed & IDENT_STR) != 0) sb.append(" ident-str");
662 662 if ((failed & IDENT_URI1) != 0) sb.append(" ident-uri1");
663 663 if ((failed & IDENT_URI3) != 0) sb.append(" ident-uri3");
664 664 if ((failed & IDENT_URI5) != 0) sb.append(" ident-uri5");
665 665 if ((failed & IDENT_URI7) != 0) sb.append(" ident-uri7");
666 666 if ((failed & TOSTRING) != 0) sb.append(" tostring");
667 667 out.println(sb.toString());
668 668 if (uri != null) show(uri);
669 669 throw new RuntimeException("Test failed");
670 670 }
671 671
672 672
673 673
674 674 // -- Tests --
675 675
676 676 static void rfc2396() {
677 677
678 678
679 679 header("RFC2396: Basic examples");
680 680
681 681 test("ftp://ftp.is.co.za/rfc/rfc1808.txt")
682 682 .s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();
683 683
684 684 test("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")
685 685 .s("gopher").h("spinaltap.micro.umn.edu")
686 686 .p("/00/Weather/California/Los%20Angeles").z();
687 687
688 688 test("http://www.math.uio.no/faq/compression-faq/part1.html")
689 689 .s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();
690 690
691 691 test("mailto:mduerst@ifi.unizh.ch")
692 692 .s("mailto").o("mduerst@ifi.unizh.ch").z();
693 693
694 694 test("news:comp.infosystems.www.servers.unix")
695 695 .s("news").o("comp.infosystems.www.servers.unix").z();
696 696
697 697 test("telnet://melvyl.ucop.edu/")
698 698 .s("telnet").h("melvyl.ucop.edu").p("/").z();
699 699
700 700 test("http://www.w3.org/Addressing/")
701 701 .s("http").h("www.w3.org").p("/Addressing/").z();
702 702
703 703 test("ftp://ds.internic.net/rfc/")
704 704 .s("ftp").h("ds.internic.net").p("/rfc/").z();
705 705
706 706 test("http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING")
707 707 .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/historical.html")
708 708 .f("WARNING").z();
709 709
710 710 test("http://www.ics.uci.edu/pub/ietf/uri/#Related")
711 711 .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/")
712 712 .f("Related").z();
713 713
714 714
715 715 header("RFC2396: Normal relative-URI examples (appendix C)");
716 716
717 717 URI base = (test("http://a/b/c/d;p?q")
718 718 .s("http").h("a").p("/b/c/d;p").q("q").z().uri());
719 719
720 720 // g:h g:h
721 721 test("g:h")
722 722 .s("g").o("h").z()
723 723 .rslv(base).s("g").o("h").z();
724 724
725 725 // g http://a/b/c/g
726 726 test("g")
727 727 .p("g").z()
728 728 .rslv(base).s("http").h("a").p("/b/c/g").z();
729 729
730 730 // ./g http://a/b/c/g
731 731 test("./g")
732 732 .p("./g").z()
733 733 .rslv(base).s("http").h("a").p("/b/c/g").z();
734 734
735 735 // g/ http://a/b/c/g/
736 736 test("g/")
737 737 .p("g/").z()
738 738 .rslv(base).s("http").h("a").p("/b/c/g/").z();
739 739
740 740 // /g http://a/g
741 741 test("/g")
742 742 .p("/g").z()
743 743 .rslv(base).s("http").h("a").p("/g").z();
744 744
745 745 // //g http://g
746 746 test("//g")
747 747 .h("g").p("").z()
748 748 .rslv(base).s("http").h("g").p("").z();
749 749
750 750 // ?y http://a/b/c/?y
751 751 test("?y")
752 752 .p("").q("y").z()
753 753 .rslv(base).s("http").h("a").p("/b/c/").q("y").z();
754 754
755 755 // g?y http://a/b/c/g?y
756 756 test("g?y")
757 757 .p("g").q("y").z()
758 758 .rslv(base).s("http").h("a").p("/b/c/g").q("y").z();
759 759
760 760 // #s (current document)#s
761 761 // DEVIATION: Lone fragment parses as relative URI with empty path
762 762 test("#s")
763 763 .p("").f("s").z()
764 764 .rslv(base).s("http").h("a").p("/b/c/d;p").f("s").q("q").z();
765 765
766 766 // g#s http://a/b/c/g#s
767 767 test("g#s")
768 768 .p("g").f("s").z()
769 769 .rslv(base).s("http").h("a").p("/b/c/g").f("s").z();
770 770
771 771 // g?y#s http://a/b/c/g?y#s
772 772 test("g?y#s")
773 773 .p("g").q("y").f("s").z()
774 774 .rslv(base).s("http").h("a").p("/b/c/g").q("y").f("s").z();
775 775
776 776 // ;x http://a/b/c/;x
777 777 test(";x")
778 778 .p(";x").z()
779 779 .rslv(base).s("http").h("a").p("/b/c/;x").z();
780 780
781 781 // g;x http://a/b/c/g;x
782 782 test("g;x")
783 783 .p("g;x").z()
784 784 .rslv(base).s("http").h("a").p("/b/c/g;x").z();
785 785
786 786 // g;x?y#s http://a/b/c/g;x?y#s
787 787 test("g;x?y#s")
788 788 .p("g;x").q("y").f("s").z()
789 789 .rslv(base).s("http").h("a").p("/b/c/g;x").q("y").f("s").z();
790 790
791 791 // . http://a/b/c/
792 792 test(".")
793 793 .p(".").z()
794 794 .rslv(base).s("http").h("a").p("/b/c/").z();
795 795
796 796 // ./ http://a/b/c/
797 797 test("./")
798 798 .p("./").z()
799 799 .rslv(base).s("http").h("a").p("/b/c/").z();
800 800
801 801 // .. http://a/b/
802 802 test("..")
803 803 .p("..").z()
804 804 .rslv(base).s("http").h("a").p("/b/").z();
805 805
806 806 // ../ http://a/b/
807 807 test("../")
808 808 .p("../").z()
809 809 .rslv(base).s("http").h("a").p("/b/").z();
810 810
811 811 // ../g http://a/b/g
812 812 test("../g")
813 813 .p("../g").z()
814 814 .rslv(base).s("http").h("a").p("/b/g").z();
815 815
816 816 // ../.. http://a/
817 817 test("../..")
818 818 .p("../..").z()
819 819 .rslv(base).s("http").h("a").p("/").z();
820 820
821 821 // ../../ http://a/
822 822 test("../../")
823 823 .p("../../").z()
824 824 .rslv(base).s("http").h("a").p("/").z();
825 825
826 826 // ../../g http://a/g
827 827 test("../../g")
828 828 .p("../../g").z()
829 829 .rslv(base).s("http").h("a").p("/g").z();
830 830
831 831
832 832 header("RFC2396: Abnormal relative-URI examples (appendix C)");
833 833
834 834 // ../../../g = http://a/../g
835 835 test("../../../g")
836 836 .p("../../../g").z()
837 837 .rslv(base).s("http").h("a").p("/../g").z();
838 838
839 839 // ../../../../g = http://a/../../g
840 840 test("../../../../g")
841 841 .p("../../../../g").z()
842 842 .rslv(base).s("http").h("a").p("/../../g").z();
843 843
844 844
845 845 // /./g = http://a/./g
846 846 test("/./g")
847 847 .p("/./g").z()
848 848 .rslv(base).s("http").h("a").p("/./g").z();
849 849
850 850 // /../g = http://a/../g
851 851 test("/../g")
852 852 .p("/../g").z()
853 853 .rslv(base).s("http").h("a").p("/../g").z();
854 854
855 855 // g. = http://a/b/c/g.
856 856 test("g.")
857 857 .p("g.").z()
858 858 .rslv(base).s("http").h("a").p("/b/c/g.").z();
859 859
860 860 // .g = http://a/b/c/.g
861 861 test(".g")
862 862 .p(".g").z()
863 863 .rslv(base).s("http").h("a").p("/b/c/.g").z();
864 864
865 865 // g.. = http://a/b/c/g..
866 866 test("g..")
867 867 .p("g..").z()
868 868 .rslv(base).s("http").h("a").p("/b/c/g..").z();
869 869
870 870 // ..g = http://a/b/c/..g
871 871 test("..g")
872 872 .p("..g").z()
873 873 .rslv(base).s("http").h("a").p("/b/c/..g").z();
874 874
875 875 // ./../g = http://a/b/g
876 876 test("./../g")
877 877 .p("./../g").z()
878 878 .rslv(base).s("http").h("a").p("/b/g").z();
879 879
880 880 // ./g/. = http://a/b/c/g/
881 881 test("./g/.")
882 882 .p("./g/.").z()
883 883 .rslv(base).s("http").h("a").p("/b/c/g/").z();
884 884
885 885 // g/./h = http://a/b/c/g/h
886 886 test("g/./h")
887 887 .p("g/./h").z()
888 888 .rslv(base).s("http").h("a").p("/b/c/g/h").z();
889 889
890 890 // g/../h = http://a/b/c/h
891 891 test("g/../h")
892 892 .p("g/../h").z()
893 893 .rslv(base).s("http").h("a").p("/b/c/h").z();
894 894
895 895 // g;x=1/./y = http://a/b/c/g;x=1/y
896 896 test("g;x=1/./y")
897 897 .p("g;x=1/./y").z()
898 898 .rslv(base).s("http").h("a").p("/b/c/g;x=1/y").z();
899 899
900 900 // g;x=1/../y = http://a/b/c/y
901 901 test("g;x=1/../y")
902 902 .p("g;x=1/../y").z()
903 903 .rslv(base).s("http").h("a").p("/b/c/y").z();
904 904
905 905 // g?y/./x = http://a/b/c/g?y/./x
906 906 test("g?y/./x")
907 907 .p("g").q("y/./x").z()
908 908 .rslv(base).s("http").h("a").p("/b/c/g").q("y/./x").z();
909 909
910 910 // g?y/../x = http://a/b/c/g?y/../x
911 911 test("g?y/../x")
912 912 .p("g").q("y/../x").z()
913 913 .rslv(base).s("http").h("a").p("/b/c/g").q("y/../x").z();
914 914
915 915 // g#s/./x = http://a/b/c/g#s/./x
916 916 test("g#s/./x")
917 917 .p("g").f("s/./x").z()
918 918 .rslv(base).s("http").h("a").p("/b/c/g").f("s/./x").z();
919 919
920 920 // g#s/../x = http://a/b/c/g#s/../x
921 921 test("g#s/../x")
922 922 .p("g").f("s/../x").z()
923 923 .rslv(base).s("http").h("a").p("/b/c/g").f("s/../x").z();
924 924
925 925 // http:g = http:g
926 926 test("http:g")
927 927 .s("http").o("g").z()
928 928 .rslv(base).s("http").o("g").z();
929 929
930 930 }
931 931
932 932
933 933 static void ip() {
934 934
935 935 header("IP addresses");
936 936
937 937 test("http://1.2.3.4:5")
938 938 .s("http").h("1.2.3.4").n(5).p("").z();
939 939
940 940 // From RFC2732
941 941
942 942 test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")
943 943 .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")
944 944 .n(80).p("/index.html").z();
945 945
946 946 test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]:80/index.html")
947 947 .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]")
948 948 .n(80).p("/index.html").z();
949 949
950 950 test("http://[1080:0:0:0:8:800:200C:417A]/index.html")
951 951 .s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();
952 952
953 953 test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")
954 954 .s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();
955 955
956 956 test("http://[3ffe:2a00:100:7031::1]")
957 957 .s("http").h("[3ffe:2a00:100:7031::1]").p("").z();
958 958
959 959 test("http://[1080::8:800:200C:417A]/foo")
960 960 .s("http").h("[1080::8:800:200C:417A]").p("/foo").z();
961 961
962 962 test("http://[::192.9.5.5]/ipng")
963 963 .s("http").h("[::192.9.5.5]").p("/ipng").z();
964 964
965 965 test("http://[::192.9.5.5%interface]/ipng")
966 966 .s("http").h("[::192.9.5.5%interface]").p("/ipng").z();
967 967
968 968 test("http://[::FFFF:129.144.52.38]:80/index.html")
969 969 .s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();
970 970
971 971 test("http://[2010:836B:4179::836B:4179]")
972 972 .s("http").h("[2010:836B:4179::836B:4179]").p("").z();
973 973
974 974 // From RFC2373
975 975
976 976 test("http://[FF01::101]")
977 977 .s("http").h("[FF01::101]").p("").z();
978 978
979 979 test("http://[::1]")
980 980 .s("http").h("[::1]").p("").z();
981 981
982 982 test("http://[::]")
983 983 .s("http").h("[::]").p("").z();
984 984
985 985 test("http://[::%hme0]")
986 986 .s("http").h("[::%hme0]").p("").z();
987 987
988 988 test("http://[0:0:0:0:0:0:13.1.68.3]")
989 989 .s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();
990 990
991 991 test("http://[0:0:0:0:0:FFFF:129.144.52.38]")
992 992 .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();
993 993
994 994 test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")
995 995 .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();
996 996
997 997 test("http://[0:0:0:0:0:ffff:1.2.3.4]")
998 998 .s("http").h("[0:0:0:0:0:ffff:1.2.3.4]").p("").z();
999 999
1000 1000 test("http://[::13.1.68.3]")
1001 1001 .s("http").h("[::13.1.68.3]").p("").z();
1002 1002
1003 1003 // Optional IPv6 brackets in constructors
1004 1004
1005 1005 test("s", null, "1:2:3:4:5:6:7:8", -1, null, null, null)
1006 1006 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1007 1007
1008 1008 test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)
1009 1009 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1010 1010
1011 1011 test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)
1012 1012 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1013 1013
1014 1014 test("s", "1:2:3:4:5:6:7:8", null, null)
1015 1015 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1016 1016
1017 1017 test("s", "1:2:3:4:5:6:7:8%hme0", null, null)
1018 1018 .s("s").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();
1019 1019
1020 1020 test("s", "1:2:3:4:5:6:7:8%1", null, null)
1021 1021 .s("s").h("[1:2:3:4:5:6:7:8%1]").p("").z();
1022 1022
1023 1023 test("s", "[1:2:3:4:5:6:7:8]", null, null)
1024 1024 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1025 1025
1026 1026 test("s", "[1:2:3:4:5:6:7:8]", null, null, null)
1027 1027 .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
1028 1028
1029 1029 test("s", "1:2:3:4:5:6:7:8", null, null, null)
1030 1030 .s("s").g("1:2:3:4:5:6:7:8").p("").z();
1031 1031
1032 1032 // Error cases
1033 1033
1034 1034 test("http://[ff01:234/foo").x().z();
1035 1035 test("http://[ff01:234:zzz]/foo").x().z();
1036 1036 test("http://[foo]").x().z();
1037 1037 test("http://[]").x().z();
1038 1038 test("http://[129.33.44.55]").x().z();
1039 1039 test("http://[ff:ee:dd:cc:bb::aa:9:8]").x().z();
1040 1040 test("http://[fffff::1]").x().z();
1041 1041 test("http://[ff::ee::8]").x().z();
1042 1042 test("http://[1:2:3:4::5:6:7:8]").x().z();
↓ open down ↓ |
1006 lines elided |
↑ open up ↑ |
1043 1043 test("http://[1:2]").x().z();
1044 1044 test("http://[1:2:3:4:5:6:7:8:9]").x().z();
1045 1045 test("http://[1:2:3:4:5:6:7:8%]").x().z();
1046 1046 test("http://[1:2:3:4:5:6:7:8%!/]").x().z();
1047 1047 test("http://[::1.2.3.300]").x().z();
1048 1048 test("http://1.2.3").psa().x().z();
1049 1049 test("http://1.2.3.300").psa().x().z();
1050 1050 test("http://1.2.3.4.5").psa().x().z();
1051 1051 test("http://[1.2.3.4:5]").x().z();
1052 1052 test("http://1:2:3:4:5:6:7:8").psa().x().z();
1053 + test("http://[1.2.3.4]/").x().z();
1054 + test("http://[1.2.3.4/").x().z();
1055 + test("http://[foo]/").x().z();
1056 + test("http://[foo/").x().z();
1057 + test("s", "[foo]", "/", null, null).x().z();
1058 + test("s", "[foo", "/", null, null).x().z();
1059 + test("s", "[::foo", "/", null, null).x().z();
1053 1060
1054 1061 // Test hostnames that might initially look like IPv4 addresses
1055 1062
1056 1063 test("s://1.2.3.com").psa().s("s").h("1.2.3.com").p("").z();
1057 1064 test("s://1.2.3.4me.com").psa().s("s").h("1.2.3.4me.com").p("").z();
1058 1065
1059 1066 test("s://7up.com").psa().s("s").h("7up.com").p("").z();
1060 1067 test("s://7up.com/p").psa().s("s").h("7up.com").p("/p").z();
1061 1068 test("s://7up").psa().s("s").h("7up").p("").z();
1062 1069 test("s://7up/p").psa().s("s").h("7up").p("/p").z();
1063 1070 test("s://7up.").psa().s("s").h("7up.").p("").z();
1064 1071 test("s://7up./p").psa().s("s").h("7up.").p("/p").z();
1065 1072 }
1066 1073
1067 1074
1068 1075 static void misc() throws URISyntaxException {
1069 1076
1070 1077 URI base = new URI("s://h/a/b");
1071 1078 URI rbase = new URI("a/b/c/d");
1072 1079
1073 1080
1074 1081 header("Corner cases");
1075 1082
1076 1083 // The empty URI parses as a relative URI with an empty path
1077 1084 test("").p("").z()
1078 1085 .rslv(base).s("s").h("h").p("/a/").z();
1079 1086
1080 1087 // Resolving solo queries and fragments
1081 1088 test("#f").p("").f("f").z()
1082 1089 .rslv(base).s("s").h("h").p("/a/b").f("f").z();
1083 1090 test("?q").p("").q("q").z()
1084 1091 .rslv(base).s("s").h("h").p("/a/").q("q").z();
1085 1092
1086 1093 // Fragment is not part of ssp
1087 1094 test("p#f").p("p").f("f").sp("p").z();
1088 1095 test("s:p#f").s("s").o("p").f("f").z();
1089 1096 test("p#f")
1090 1097 .rslv(base).s("s").h("h").p("/a/p").f("f").sp("//h/a/p").z();
1091 1098 test("").p("").sp("").z();
1092 1099
1093 1100
1094 1101
1095 1102 header("Emptiness");
1096 1103
1097 1104 // Components that may be empty
1098 1105 test("///p").p("/p").z(); // Authority (w/ path)
1099 1106 test("//@h/p").u("").h("h").p("/p").z(); // User info
1100 1107 test("//h:/p").h("h").p("/p").z(); // Port
1101 1108 test("//h").h("h").p("").z(); // Path
1102 1109 test("//h?q").h("h").p("").q("q").z(); // Path (w/query)
1103 1110 test("//?q").p("").q("q").z(); // Authority (w/query)
1104 1111 test("//#f").p("").f("f").z(); // Authority (w/fragment)
1105 1112 test("p?#").p("p").q("").f("").z(); // Query & fragment
1106 1113
1107 1114 // Components that may not be empty
1108 1115 test(":").x().z(); // Scheme
1109 1116 test("x:").x().z(); // Hier/opaque
1110 1117 test("//").x().z(); // Authority (w/o path)
1111 1118
1112 1119
1113 1120 header("Resolution, normalization, and relativization");
1114 1121
1115 1122 // Resolving relative paths
1116 1123 test("../e/f").p("../e/f").z()
1117 1124 .rslv(rbase).p("a/b/e/f").z();
1118 1125 test("../../../../d").p("../../../../d").z()
1119 1126 .rslv(rbase).p("../d").z();
1120 1127 test("../../../d:e").p("../../../d:e").z()
1121 1128 .rslv(rbase).p("./d:e").z();
1122 1129 test("../../../d:e/f").p("../../../d:e/f").z()
1123 1130 .rslv(rbase).p("./d:e/f").z();
1124 1131
1125 1132 // Normalization
1126 1133 test("a/./c/../d/f").p("a/./c/../d/f").z()
1127 1134 .norm().p("a/d/f").z();
1128 1135 test("http://a/./b/c/../d?q#f")
1129 1136 .s("http").h("a").p("/./b/c/../d").q("q").f("f").z()
1130 1137 .norm().s("http").h("a").p("/b/d").q("q").f("f").z();
1131 1138 test("a/../b").p("a/../b").z().
1132 1139 norm().p("b");
1133 1140 test("a/../b:c").p("a/../b:c").z()
1134 1141 .norm().p("./b:c").z();
1135 1142
1136 1143 // Normalization of already normalized URI should yield the
1137 1144 // same URI
1138 1145 URI u1 = URI.create("s://h/../p");
1139 1146 URI u2 = u1.normalize();
1140 1147 eq(u1, u2);
1141 1148 eqeq(u1, u2);
1142 1149
1143 1150 // Relativization
1144 1151 test("/a/b").p("/a/b").z()
1145 1152 .rtvz(new URI("/a")).p("b").z();
1146 1153 test("/a/b").p("/a/b").z()
1147 1154 .rtvz(new URI("/a/")).p("b").z();
1148 1155 test("a/b").p("a/b").z()
1149 1156 .rtvz(new URI("a")).p("b").z();
1150 1157 test("/a/b").p("/a/b").z()
1151 1158 .rtvz(new URI("/a/b")).p("").z(); // Result is empty path
1152 1159 test("a/../b:c/d").p("a/../b:c/d").z()
1153 1160 .rtvz(new URI("./b:c/")).p("d").z();
1154 1161
1155 1162 test("http://a/b/d/e?q#f")
1156 1163 .s("http").h("a").p("/b/d/e").q("q").f("f").z()
1157 1164 .rtvz(new URI("http://a/b/?r#g"))
1158 1165 .p("d/e").q("q").f("f").z();
1159 1166
1160 1167 // parseServerAuthority
1161 1168 test("/a/b").psa().p("/a/b").z();
1162 1169 test("s://u@h:1/p")
1163 1170 .psa().s("s").u("u").h("h").n(1).p("/p").z();
1164 1171 test("s://u@h:-foo/p").s("s").g("u@h:-foo").p("/p").z()
1165 1172 .psa().x().z();
1166 1173 test("s://h:999999999999999999999999").psa().x().z();
1167 1174 test("s://:/b").psa().x().z();
1168 1175
1169 1176
1170 1177 header("Constructors and factories");
1171 1178
1172 1179 test("s", null, null, -1, "p", null, null).x().z();
1173 1180 test(null, null, null, -1, null, null, null).p("").z();
1174 1181 test(null, null, null, -1, "p", null, null).p("p").z();
1175 1182 test(null, null, "foo%20bar", -1, null, null, null).x().z();
1176 1183 test(null, null, "foo", -100, null, null, null).x().z();
1177 1184 test("s", null, null, -1, "", null, null).x().z();
1178 1185 test("s", null, null, -1, "/p", null, null).s("s").p("/p").z();
1179 1186 test("s", "u", "h", 10, "/p", "q", "f")
1180 1187 .s("s").u("u").h("h").n(10).p("/p").q("q").f("f").z();
1181 1188 test("s", "a:b", "/p", "q", "f")
1182 1189 .s("s").g("a:b").p("/p").q("q").f("f").z();
1183 1190 test("s", "h", "/p", "f")
1184 1191 .s("s").h("h").p("/p").f("f").z();
1185 1192 test("s", "p", "f").s("s").o("p").f("f").z();
1186 1193 test("s", "/p", "f").s("s").p("/p").f("f").z();
1187 1194 testCreate("s://u@h/p?q#f")
1188 1195 .s("s").u("u").h("h").p("/p").q("q").f("f").z();
1189 1196 }
1190 1197
1191 1198 static void npes() throws URISyntaxException {
1192 1199
1193 1200 header("NullPointerException");
1194 1201
1195 1202 URI base = URI.create("mailto:root@foobar.com");
1196 1203
1197 1204 out.println();
1198 1205
1199 1206 try {
1200 1207 base.resolve((URI)null);
1201 1208 throw new RuntimeException("NullPointerException not thrown");
1202 1209 } catch (NullPointerException x) {
1203 1210 out.println("resolve((URI)null) -->");
1204 1211 out.println("Correct exception: " + x);
1205 1212 }
1206 1213
1207 1214 out.println();
1208 1215
1209 1216 try {
1210 1217 base.resolve((String)null);
1211 1218 throw new RuntimeException("NullPointerException not thrown");
1212 1219 } catch (NullPointerException x) {
1213 1220 out.println("resolve((String)null) -->");
1214 1221 out.println("Correct exception: " + x);
1215 1222 }
1216 1223
1217 1224 out.println();
1218 1225
1219 1226 try {
1220 1227 base.relativize((URI)null);
1221 1228 throw new RuntimeException("NullPointerException not thrown");
1222 1229 } catch (NullPointerException x) {
1223 1230 out.println("relativize((String)null) -->");
1224 1231 out.println("Correct exception: " + x);
1225 1232 }
1226 1233
1227 1234 testCount += 3;
1228 1235 }
1229 1236
1230 1237
1231 1238 static void chars() throws URISyntaxException {
1232 1239
1233 1240 header("Escapes and non-US-ASCII characters");
1234 1241
1235 1242 URI uri;
1236 1243
1237 1244 // Escape pairs
1238 1245 test("%0a%0A%0f%0F%01%09zz")
1239 1246 .p("%0a%0A%0f%0F%01%09zz").z();
1240 1247 test("foo%1").x().z();
1241 1248 test("foo%z").x().z();
1242 1249 test("foo%9z").x().z();
1243 1250
1244 1251 // Escapes not permitted in scheme, host
1245 1252 test("s%20t://a").x().z();
1246 1253 test("//a%20b").g("a%20b").p("").z(); // Parses as registry
1247 1254
1248 1255 // Escapes permitted in opaque part, userInfo, registry, path,
1249 1256 // query, and fragment
1250 1257 test("//u%20v@a").u("u%20v").h("a").p("").z();
1251 1258 test("/p%20q").p("/p%20q").z();
1252 1259 test("/p?q%20").p("/p").q("q%20").z();
1253 1260 test("/p#%20f").p("/p").f("%20f").z();
1254 1261
1255 1262 // Non-US-ASCII chars
1256 1263 test("s\u00a7t://a").x().z();
1257 1264 test("//\u00a7/b").g("\u00a7").p("/b").z(); // Parses as registry
1258 1265 test("//u\u00a7v@a").u("u\u00a7v").h("a").p("").z();
1259 1266 test("/p\u00a7q").p("/p\u00a7q").z();
1260 1267 test("/p?q\u00a7").p("/p").q("q\u00a7").z();
1261 1268 test("/p#\u00a7f").p("/p").f("\u00a7f").z();
1262 1269
1263 1270 // 4648111 - Escapes quoted by toString after resolution
1264 1271 uri = new URI("http://a/b/c/d;p?q");
1265 1272 test("/p%20p")
1266 1273 .rslv(uri).s("http").h("a").p("/p%20p").ts("http://a/p%20p").z();
1267 1274
1268 1275 // 4464135: Forbid unwise characters throughout opaque part
1269 1276 test("foo:x{bar").x().z();
1270 1277 test("foo:{bar").x().z();
1271 1278
1272 1279 // 4438319: Single-argument constructor requires quotation,
1273 1280 // preserves escapes
1274 1281 test("//u%01@h/a/b/%02/c?q%03#f%04")
1275 1282 .u("u%01").ud("u\1")
1276 1283 .h("h")
1277 1284 .p("/a/b/%02/c").pd("/a/b/\2/c")
1278 1285 .q("q%03").qd("q\3")
1279 1286 .f("f%04").fd("f\4")
1280 1287 .z();
1281 1288 test("/a/b c").x().z();
1282 1289
1283 1290 // 4438319: Multi-argument constructors quote illegal chars and
1284 1291 // preserve legal non-ASCII chars
1285 1292 // \uA001-\uA009 are visible characters, \u2000 is a space character
1286 1293 test(null, "u\uA001\1", "h", -1,
1287 1294 "/p% \uA002\2\u2000",
1288 1295 "q% \uA003\3\u2000",
1289 1296 "f% \uA004\4\u2000")
1290 1297 .u("u\uA001%01").h("h")
1291 1298 .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
1292 1299 .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")
1293 1300 .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
1294 1301 test(null, "g\uA001\1",
1295 1302 "/p% \uA002\2\u2000",
1296 1303 "q% \uA003\3\u2000",
1297 1304 "f% \uA004\4\u2000")
1298 1305 .g("g\uA001%01")
1299 1306 .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
1300 1307 .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")
1301 1308 .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
1302 1309 test(null, null, "/p% \uA002\2\u2000", "f% \uA004\4\u2000")
1303 1310 .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
1304 1311 .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
1305 1312 test(null, "/sp% \uA001\1\u2000", "f% \uA004\4\u2000")
1306 1313 .sp("/sp%25%20\uA001%01%E2%80%80").spd("/sp% \uA001\1\u2000")
1307 1314 .p("/sp%25%20\uA001%01%E2%80%80").pd("/sp% \uA001\1\u2000")
1308 1315 .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
1309 1316
1310 1317 // 4438319: Non-raw accessors decode all escaped octets
1311 1318 test("/%25%20%E2%82%AC%E2%80%80")
1312 1319 .p("/%25%20%E2%82%AC%E2%80%80").pd("/% \u20Ac\u2000").z();
1313 1320
1314 1321 // 4438319: toASCIIString
1315 1322 test("/\uCAFE\uBABE")
1316 1323 .p("/\uCAFE\uBABE").ta("/%EC%AB%BE%EB%AA%BE").z();
1317 1324
1318 1325 // 4991359 and 4866303: bad quoting by defineSchemeSpecificPart()
1319 1326 URI base = new URI ("http://host/foo%20bar/a/b/c/d");
1320 1327 test ("resolve")
1321 1328 .rslv(base).spd("//host/foo bar/a/b/c/resolve")
1322 1329 .sp("//host/foo%20bar/a/b/c/resolve").s("http")
1323 1330 .pd("/foo bar/a/b/c/resolve").h("host")
1324 1331 .p("/foo%20bar/a/b/c/resolve").z();
1325 1332
1326 1333 // 6773270: java.net.URI fails to escape u0000
1327 1334 test("s", "a", "/\u0000", null)
1328 1335 .s("s").p("/%00").h("a")
1329 1336 .ta("s://a/%00").z();
1330 1337 }
1331 1338
1332 1339
1333 1340 static void eq0(Comparable u, Comparable v) throws URISyntaxException {
1334 1341 testCount++;
1335 1342 if (!u.equals(v))
1336 1343 throw new RuntimeException("Not equal: " + u + " " + v);
1337 1344 int uh = u.hashCode();
1338 1345 int vh = v.hashCode();
1339 1346 if (uh != vh)
1340 1347 throw new RuntimeException("Hash codes not equal: "
1341 1348 + u + " " + Integer.toHexString(uh) + " "
1342 1349 + v + " " + Integer.toHexString(vh));
1343 1350 out.println();
1344 1351 out.println(u + " == " + v
1345 1352 + " [" + Integer.toHexString(uh) + "]");
1346 1353 }
1347 1354
1348 1355 static void cmp0(Comparable u, Comparable v, boolean same)
1349 1356 throws URISyntaxException
1350 1357 {
1351 1358 int c = u.compareTo(v);
1352 1359 if ((c == 0) != same)
1353 1360 throw new RuntimeException("Comparison inconsistent: " + u + " " + v
1354 1361 + " " + c);
1355 1362 }
1356 1363
1357 1364 static void eq(Comparable u, Comparable v) throws URISyntaxException {
1358 1365 eq0(u, v);
1359 1366 cmp0(u, v, true);
1360 1367 }
1361 1368
1362 1369 static void eqeq(Comparable u, Comparable v) {
1363 1370 testCount++;
1364 1371 if (u != v)
1365 1372 throw new RuntimeException("Not ==: " + u + " " + v);
1366 1373 }
1367 1374
1368 1375 static void ne0(Comparable u, Comparable v) throws URISyntaxException {
1369 1376 testCount++;
1370 1377 if (u.equals(v))
1371 1378 throw new RuntimeException("Equal: " + u + " " + v);
1372 1379 out.println();
1373 1380 out.println(u + " != " + v
1374 1381 + " [" + Integer.toHexString(u.hashCode())
1375 1382 + " " + Integer.toHexString(v.hashCode())
1376 1383 + "]");
1377 1384 }
1378 1385
1379 1386 static void ne(Comparable u, Comparable v) throws URISyntaxException {
1380 1387 ne0(u, v);
1381 1388 cmp0(u, v, false);
1382 1389 }
1383 1390
1384 1391 static void lt(Comparable u, Comparable v) throws URISyntaxException {
1385 1392 ne0(u, v);
1386 1393 int c = u.compareTo(v);
1387 1394 if (c >= 0) {
1388 1395 show((URI)u);
1389 1396 show((URI)v);
1390 1397 throw new RuntimeException("Not less than: " + u + " " + v
1391 1398 + " " + c);
1392 1399 }
1393 1400 out.println(u + " < " + v);
1394 1401 }
1395 1402
1396 1403 static void lt(String s, String t) throws URISyntaxException {
1397 1404 lt(new URI(s), new URI(t));
1398 1405 }
1399 1406
1400 1407 static void gt(Comparable u, Comparable v) throws URISyntaxException {
1401 1408 lt(v, u);
1402 1409 }
1403 1410
1404 1411 static void eqHashComp() throws URISyntaxException {
1405 1412
1406 1413 header("Equality, hashing, and comparison");
1407 1414
1408 1415 URI o = new URI("mailto:foo@bar.com");
1409 1416 URI r = new URI("reg://some%20registry/b/c/d?q#f");
1410 1417 URI s = new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#f");
1411 1418 eq(o, o);
1412 1419 lt(o, r);
1413 1420 lt(s, o);
1414 1421 lt(s, r);
1415 1422 eq(o, new URI("MaILto:foo@bar.com"));
1416 1423 gt(o, new URI("mailto:foo@bar.COM"));
1417 1424 eq(r, new URI("rEg://some%20registry/b/c/d?q#f"));
1418 1425 gt(r, new URI("reg://Some%20Registry/b/c/d?q#f"));
1419 1426 gt(r, new URI("reg://some%20registry/b/c/D?q#f"));
1420 1427 eq(s, new URI("hTtP://jag:cafebabe@Java.Sun.COM:94/b/c/d?q#f"));
1421 1428 gt(s, new URI("http://jag:CafeBabe@java.sun.com:94/b/c/d?q#f"));
1422 1429 lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?r#f"));
1423 1430 lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g"));
1424 1431
1425 1432 lt("p", "s:p");
1426 1433 lt("s:p", "T:p");
1427 1434 lt("S:p", "t:p");
1428 1435 lt("s:/p", "s:p");
1429 1436 lt("s:p", "s:q");
1430 1437 lt("s:p#f", "s:p#g");
1431 1438 lt("s://u@h:1", "s://v@h:1");
1432 1439 lt("s://u@h:1", "s://u@i:1");
1433 1440 lt("s://u@h:1", "s://v@h:2");
1434 1441 lt("s://a%20b", "s://a%20c");
1435 1442 lt("s://a%20b", "s://aab");
1436 1443 lt("s://AA", "s://A_");
1437 1444 lt("s:/p", "s:/q");
1438 1445 lt("s:/p?q", "s:/p?r");
1439 1446 lt("s:/p#f", "s:/p#g");
1440 1447
1441 1448 lt("s://h", "s://h/p");
1442 1449 lt("s://h/p", "s://h/p?q");
1443 1450
1444 1451 }
1445 1452
1446 1453
1447 1454 static void serial(URI u) throws IOException, URISyntaxException {
1448 1455
1449 1456 ByteArrayOutputStream bo = new ByteArrayOutputStream();
1450 1457 ObjectOutputStream oo = new ObjectOutputStream(bo);
1451 1458
1452 1459 oo.writeObject(u);
1453 1460 oo.close();
1454 1461
1455 1462 ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
1456 1463 ObjectInputStream oi = new ObjectInputStream(bi);
1457 1464 try {
1458 1465 Object o = oi.readObject();
1459 1466 eq(u, (Comparable)o);
1460 1467 } catch (ClassNotFoundException x) {
1461 1468 x.printStackTrace();
1462 1469 throw new RuntimeException(x.toString());
1463 1470 }
1464 1471
1465 1472 testCount++;
1466 1473 }
1467 1474
1468 1475 static void serial() throws IOException, URISyntaxException {
1469 1476 header("Serialization");
1470 1477
1471 1478 serial(URI.create("http://java.sun.com/jdk/1.4?release#beta"));
1472 1479 serial(URI.create("s://h/p").resolve("/long%20path/"));
1473 1480 }
1474 1481
1475 1482
1476 1483 static void urls() throws URISyntaxException {
1477 1484
1478 1485 header("URLs");
1479 1486
1480 1487 URI uri;
1481 1488 URL url;
1482 1489 boolean caught = false;
1483 1490
1484 1491 out.println();
1485 1492 uri = new URI("http://a/p?q#f");
1486 1493 try {
1487 1494 url = uri.toURL();
1488 1495 } catch (MalformedURLException x) {
1489 1496 throw new RuntimeException(x.toString());
1490 1497 }
1491 1498 if (!url.toString().equals("http://a/p?q#f"))
1492 1499 throw new RuntimeException("Incorrect URL: " + url);
1493 1500 out.println(uri + " url --> " + url);
1494 1501
1495 1502 out.println();
1496 1503 uri = new URI("a/b");
1497 1504 try {
1498 1505 out.println(uri + " url --> ");
1499 1506 url = uri.toURL();
1500 1507 } catch (IllegalArgumentException x) {
1501 1508 caught = true;
1502 1509 out.println("Correct exception: " + x);
1503 1510 } catch (MalformedURLException x) {
1504 1511 caught = true;
1505 1512 throw new RuntimeException("Incorrect exception: " + x);
1506 1513 }
1507 1514 if (!caught)
1508 1515 throw new RuntimeException("Incorrect URL: " + url);
1509 1516
1510 1517 out.println();
1511 1518 uri = new URI("foo://bar/baz");
1512 1519 caught = false;
1513 1520 try {
1514 1521 out.println(uri + " url --> ");
1515 1522 url = uri.toURL();
1516 1523 } catch (MalformedURLException x) {
1517 1524 caught = true;
1518 1525 out.println("Correct exception: " + x);
1519 1526 } catch (IllegalArgumentException x) {
1520 1527 caught = true;
1521 1528 throw new RuntimeException("Incorrect exception: " + x);
1522 1529 }
1523 1530 if (!caught)
1524 1531 throw new RuntimeException("Incorrect URL: " + url);
1525 1532
1526 1533 testCount += 3;
1527 1534 }
1528 1535
1529 1536
1530 1537 static void tests() throws IOException, URISyntaxException {
1531 1538 rfc2396();
1532 1539 ip();
1533 1540 misc();
1534 1541 chars();
1535 1542 eqHashComp();
1536 1543 serial();
1537 1544 urls();
1538 1545 npes();
1539 1546 bugs();
1540 1547 }
1541 1548
1542 1549
1543 1550 // -- Command-line invocation --
1544 1551
1545 1552 static void usage() {
1546 1553 out.println("Usage:");
1547 1554 out.println(" java Test -- Runs all tests in this file");
1548 1555 out.println(" java Test <uri> -- Parses uri, shows components");
1549 1556 out.println(" java Test <base> <uri> -- Parses uri and base, then resolves");
1550 1557 out.println(" uri against base");
1551 1558 }
1552 1559
1553 1560 static void clargs(String base, String uri) {
1554 1561 URI b = null, u;
1555 1562 try {
1556 1563 if (base != null) {
1557 1564 b = new URI(base);
1558 1565 out.println(base);
1559 1566 show(b);
1560 1567 }
1561 1568 u = new URI(uri);
1562 1569 out.println(uri);
1563 1570 show(u);
1564 1571 if (base != null) {
1565 1572 URI r = b.resolve(u);
1566 1573 out.println(r);
1567 1574 show(r);
1568 1575 }
1569 1576 } catch (URISyntaxException x) {
1570 1577 show("ERROR", x);
1571 1578 x.printStackTrace(out);
1572 1579 }
1573 1580 }
1574 1581
1575 1582
1576 1583 // miscellaneous bugs/rfes that don't fit in with the test framework
1577 1584
1578 1585 static void bugs() {
1579 1586 // 6339649 - include detail message from nested exception
1580 1587 try {
1581 1588 URI uri = URI.create("http://nowhere.net/should not be permitted");
1582 1589 } catch (IllegalArgumentException e) {
1583 1590 if ("".equals(e.getMessage()) || e.getMessage() == null) {
1584 1591 throw new RuntimeException ("No detail message");
1585 1592 }
1586 1593 }
1587 1594 }
1588 1595
1589 1596 public static void main(String[] args) throws Exception {
1590 1597 switch (args.length) {
1591 1598
1592 1599 case 0:
1593 1600 tests();
1594 1601 out.println();
1595 1602 out.println("Test cases: " + testCount);
1596 1603 break;
1597 1604
1598 1605 case 1:
1599 1606 if (args[0].equals("-help")) {
1600 1607 usage();
1601 1608 break;
1602 1609 }
1603 1610 clargs(null, args[0]);
1604 1611 break;
1605 1612
1606 1613 case 2:
1607 1614 clargs(args[0], args[1]);
1608 1615 break;
1609 1616
1610 1617 default:
1611 1618 usage();
1612 1619 break;
1613 1620
1614 1621 }
1615 1622 }
1616 1623
1617 1624 }
↓ open down ↓ |
555 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX