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