Path: blob/master/test/jdk/java/net/URI/Test.java
41149 views
/*1* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Unit test for java.net.URI25* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 704180026* 7171415 693387927* @author Mark Reinhold28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.io.ObjectInputStream;34import java.io.ObjectOutputStream;35import java.io.PrintStream;36import java.net.URI;37import java.net.URISyntaxException;38import java.net.URL;39import java.net.MalformedURLException;404142public class Test {4344static PrintStream out = System.out;45static int testCount = 0;4647// Properties that we check48static final int PARSEFAIL = 1 << 0;49static final int SCHEME = 1 << 1;50static final int SSP = 1 << 2;51static final int SSP_D = 1 << 3; // Decoded form52static final int OPAQUEPART = 1 << 4; // SSP, and URI is opaque53static final int USERINFO = 1 << 5;54static final int USERINFO_D = 1 << 6; // Decoded form55static final int HOST = 1 << 7;56static final int PORT = 1 << 8;57static final int REGISTRY = 1 << 9;58static final int REGISTRY_D = 1 << 10; // Decoded form59static final int PATH = 1 << 11;60static final int PATH_D = 1 << 12; // Decoded form61static final int QUERY = 1 << 13;62static final int QUERY_D = 1 << 14; // Decoded form63static final int FRAGMENT = 1 << 15;64static final int FRAGMENT_D = 1 << 16; // Decoded form65static final int TOASCII = 1 << 17;66static final int IDENT_STR = 1 << 18; // Identities67static final int IDENT_URI1 = 1 << 19;68static final int IDENT_URI3 = 1 << 20;69static final int IDENT_URI5 = 1 << 21;70static final int IDENT_URI7 = 1 << 22;71static final int TOSTRING = 1 << 23;7273String input;74URI uri = null;75URI originalURI;76URI base = null; // Base for resolution/relativization77String op = null; // Op performed if uri != originalURI78int checked = 0; // Mask for checked properties79int failed = 0; // Mask for failed properties80Exception exc = null;8182private Test(String s) {83testCount++;84input = s;85try {86uri = new URI(s);87} catch (URISyntaxException x) {88exc = x;89}90originalURI = uri;91}9293static Test test(String s) {94return new Test(s);95}9697private Test(String s, String u, String h, int n,98String p, String q, String f)99{100testCount++;101try {102uri = new URI(s, u, h, n, p, q, f);103} catch (URISyntaxException x) {104exc = x;105input = x.getInput();106}107if (uri != null)108input = uri.toString();109originalURI = uri;110}111112static Test test(String s, String u, String h, int n,113String p, String q, String f) {114return new Test(s, u, h, n, p, q, f);115}116117private Test(String s, String a,118String p, String q, String f)119{120testCount++;121try {122uri = new URI(s, a, p, q, f);123} catch (URISyntaxException x) {124exc = x;125input = x.getInput();126}127if (uri != null)128input = uri.toString();129originalURI = uri;130}131132static Test test(String s, String a,133String p, String q, String f) {134return new Test(s, a, p, q, f);135}136137private Test(String s, String h, String p, String f) {138testCount++;139try {140uri = new URI(s, h, p, f);141} catch (URISyntaxException x) {142exc = x;143input = x.getInput();144}145if (uri != null)146input = uri.toString();147originalURI = uri;148}149150static Test test(String s, String h, String p, String f) {151return new Test(s, h, p, f);152}153154private Test(String s, String ssp, String f) {155testCount++;156try {157uri = new URI(s, ssp, f);158} catch (URISyntaxException x) {159exc = x;160input = x.getInput();161}162if (uri != null)163input = uri.toString();164originalURI = uri;165}166167static Test test(String s, String ssp, String f) {168return new Test(s, ssp, f);169}170171private Test(String s, boolean xxx) {172testCount++;173try {174uri = URI.create(s);175} catch (IllegalArgumentException x) {176exc = x;177}178if (uri != null)179input = uri.toString();180originalURI = uri;181}182183static Test testCreate(String s) {184return new Test(s, false);185}186187boolean parsed() {188return uri != null;189}190191boolean resolved() {192return base != null;193}194195URI uri() {196return uri;197}198199200// Operations on Test instances201//202// These are short so as to make test cases compact.203//204// s Scheme205// sp Scheme-specific part206// spd Scheme-specific part, decoded207// o Opaque part (isOpaque() && ssp matches)208// g reGistry (authority matches, and host is not defined)209// gd reGistry, decoded210// u User info211// ud User info, decoded212// h Host213// n port Number214// p Path215// pd Path, decoded216// q Query217// qd Query, decoded218// f Fragment219// fd Fragment, decoded220//221// rslv Resolve against given base222// rtvz Relativize223// psa Parse server Authority224// norm Normalize225// ta ASCII form226//227// x Check that parse failed as expected228// z End -- ensure that unchecked components are null229230private boolean check1(int prop) {231checked |= prop;232if (!parsed()) {233failed |= prop;234return false;235}236return true;237}238239private void check2(String s, String ans, int prop) {240if ((s == null) || !s.equals(ans))241failed |= prop;242}243244Test s(String s) {245if (check1(SCHEME)) check2(uri.getScheme(), s, SCHEME);246return this;247}248249Test u(String s) {250if (check1(USERINFO)) check2(uri.getRawUserInfo(), s, USERINFO);251return this;252}253254Test ud(String s) {255if (check1(USERINFO_D)) {256check2(uri.getUserInfo(), s, USERINFO_D);257}258return this;259}260261Test h(String s) {262if (check1(HOST)) check2(uri.getHost(), s, HOST);263return this;264}265266Test g(String s) {267if (check1(REGISTRY)) {268if (uri.getHost() != null)269failed |= REGISTRY;270else271check2(uri.getRawAuthority(), s, REGISTRY);272}273return this;274}275276Test gd(String s) {277if (check1(REGISTRY_D)) {278if (uri.getHost() != null)279failed |= REGISTRY_D;280else281check2(uri.getAuthority(), s, REGISTRY_D);282}283return this;284}285286Test n(int n) {287checked |= PORT;288if (!parsed() || (uri.getPort() != n))289failed |= PORT;290return this;291}292293Test p(String s) {294if (check1(PATH)) check2(uri.getRawPath(), s, PATH);295return this;296}297298Test pd(String s) {299if (check1(PATH_D)) check2(uri.getPath(), s, PATH_D);300return this;301}302303Test o(String s) {304if (check1(OPAQUEPART)) {305if (!uri.isOpaque())306failed |= OPAQUEPART;307else308check2(uri.getSchemeSpecificPart(), s, OPAQUEPART);309}310return this;311}312313Test sp(String s) {314if (check1(SSP)) check2(uri.getRawSchemeSpecificPart(), s, SSP);315return this;316}317318Test spd(String s) {319if (check1(SSP_D)) check2(uri.getSchemeSpecificPart(), s, SSP_D);320return this;321}322323Test q(String s) {324if (check1(QUERY)) check2(uri.getRawQuery(), s, QUERY);325return this;326}327328Test qd(String s) {329if (check1(QUERY_D)) check2(uri.getQuery(), s, QUERY_D);330return this;331}332333Test f(String s) {334if (check1(FRAGMENT)) check2(uri.getRawFragment(), s, FRAGMENT);335return this;336}337338Test fd(String s) {339if (check1(FRAGMENT_D)) check2(uri.getFragment(), s, FRAGMENT_D);340return this;341}342343Test ta(String s) {344if (check1(TOASCII))345check2(uri.toASCIIString(), s, TOASCII);346return this;347}348349Test ts(String s) {350if (check1(TOSTRING))351check2(uri.toString(), s, TOSTRING);352return this;353}354355Test x() {356checked |= PARSEFAIL;357if (parsed())358failed |= PARSEFAIL;359return this;360}361362Test rslv(URI base) {363if (!parsed())364return this;365this.base = base;366op = "rslv";367URI u = uri;368uri = null;369try {370this.uri = base.resolve(u);371} catch (IllegalArgumentException x) {372exc = x;373}374checked = 0;375failed = 0;376return this;377}378379Test norm() {380if (!parsed())381return this;382op = "norm";383uri = uri.normalize();384return this;385}386387Test rtvz(URI base) {388if (!parsed())389return this;390this.base = base;391op = "rtvz";392uri = base.relativize(uri);393checked = 0;394failed = 0;395return this;396}397398Test psa() {399try {400uri.parseServerAuthority();401} catch (URISyntaxException x) {402exc = x;403uri = null;404}405checked = 0;406failed = 0;407return this;408}409410private void checkEmpty(String s, int prop) {411if (((checked & prop) == 0) && (s != null))412failed |= prop;413}414415// Check identity for the seven-argument URI constructor416//417void checkURI7() {418// Only works on hierarchical URIs419if (uri.isOpaque())420return;421// Only works with server-based authorities422if ((uri.getAuthority() == null)423!= ((uri.getUserInfo() == null) && (uri.getHost() == null)))424return;425// Not true if non-US-ASCII chars are encoded unnecessarily426if (uri.getPath().indexOf('\u20AC') >= 0)427return;428try {429URI u2 = new URI(uri.getScheme(), uri.getUserInfo(),430uri.getHost(), uri.getPort(), uri.getPath(),431uri.getQuery(), uri.getFragment());432if (!uri.equals(u2))433failed |= IDENT_URI7;434} catch (URISyntaxException x) {435failed |= IDENT_URI7;436}437}438439// Check identity for the five-argument URI constructor440//441void checkURI5() {442// Only works on hierarchical URIs443if (uri.isOpaque())444return;445try {446URI u2 = new URI(uri.getScheme(), uri.getAuthority(),447uri.getPath(), uri.getQuery(), uri.getFragment());448if (!uri.equals(u2))449failed |= IDENT_URI5;450} catch (URISyntaxException x) {451failed |= IDENT_URI5;452}453}454455// Check identity for the three-argument URI constructor456//457void checkURI3() {458try {459URI u2 = new URI(uri.getScheme(),460uri.getSchemeSpecificPart(),461uri.getFragment());462if (!uri.equals(u2))463failed |= IDENT_URI3;464} catch (URISyntaxException x) {465failed |= IDENT_URI3;466}467}468469// Check all identities mentioned in the URI class specification470//471void checkIdentities() {472if (input != null) {473if (!uri.toString().equals(input))474failed |= IDENT_STR;475}476try {477if (!(new URI(uri.toString())).equals(uri))478failed |= IDENT_URI1;479} catch (URISyntaxException x) {480failed |= IDENT_URI1;481}482483// Remaining identities fail if "//" given but authority is undefined484if ((uri.getAuthority() == null)485&& (uri.getSchemeSpecificPart() != null)486&& (uri.getSchemeSpecificPart().startsWith("///")487|| uri.getSchemeSpecificPart().startsWith("//?")488|| uri.getSchemeSpecificPart().equals("//")))489return;490491// Remaining identities fail if ":" given but port is undefined492if ((uri.getHost() != null)493&& (uri.getAuthority() != null)494&& (uri.getAuthority().equals(uri.getHost() + ":")))495return;496497// Remaining identities fail if non-US-ASCII chars are encoded498// unnecessarily499if ((uri.getPath() != null) && uri.getPath().indexOf('\u20AC') >= 0)500return;501502checkURI3();503checkURI5();504checkURI7();505}506507// Check identities, check that unchecked component properties are not508// defined, and report any failures509//510Test z() {511if (!parsed()) {512report();513return this;514}515516if (op == null)517checkIdentities();518519// Check that unchecked components are undefined520checkEmpty(uri.getScheme(), SCHEME);521checkEmpty(uri.getUserInfo(), USERINFO);522checkEmpty(uri.getHost(), HOST);523if (((checked & PORT) == 0) && (uri.getPort() != -1)) failed |= PORT;524checkEmpty(uri.getPath(), PATH);525checkEmpty(uri.getQuery(), QUERY);526checkEmpty(uri.getFragment(), FRAGMENT);527528// Report failures529report();530return this;531}532533534// Summarization and reporting535536static void header(String s) {537out.println();538out.println();539out.println("-- " + s + " --");540}541542static void show(String prefix, URISyntaxException x) {543out.println(uquote(x.getInput()));544if (x.getIndex() >= 0) {545for (int i = 0; i < x.getIndex(); i++) {546if (x.getInput().charAt(i) >= '\u0080')547out.print(" "); // Skip over \u1234548else549out.print(" ");550}551out.println("^");552}553out.println(prefix + ": " + x.getReason());554}555556private void summarize() {557out.println();558StringBuffer sb = new StringBuffer();559if (input.length() == 0)560sb.append("\"\"");561else562sb.append(input);563if (base != null) {564sb.append(" ");565sb.append(base);566}567if (!parsed()) {568String s = (((checked & PARSEFAIL) != 0)569? "Correct exception" : "UNEXPECTED EXCEPTION");570if (exc instanceof URISyntaxException)571show(s, (URISyntaxException)exc);572else {573out.println(uquote(sb.toString()));574out.print(s + ": ");575exc.printStackTrace(out);576}577} else {578if (uri != originalURI) {579sb.append(" ");580sb.append(op);581sb.append(" --> ");582sb.append(uri);583}584out.println(uquote(sb.toString()));585}586}587588public static String uquote(String str) {589if (str == null)590return str;591StringBuffer sb = new StringBuffer();592int n = str.length();593for (int i = 0; i < n; i++) {594char c = str.charAt(i);595if ((c >= ' ') && (c < 0x7f)) {596sb.append(c);597continue;598}599sb.append("\\u");600String s = Integer.toHexString(c).toUpperCase();601while (s.length() < 4)602s = "0" + s;603sb.append(s);604}605return sb.toString();606}607608static void show(String n, String v) {609out.println(" " + n610+ " = ".substring(n.length())611+ uquote(v));612}613614static void show(String n, String v, String vd) {615if ((v == null) || v.equals(vd))616show(n, v);617else {618out.println(" " + n619+ " = ".substring(n.length())620+ uquote(v)621+ " = " + uquote(vd));622}623}624625public static void show(URI u) {626show("opaque", "" + u.isOpaque());627show("scheme", u.getScheme());628show("ssp", u.getRawSchemeSpecificPart(), u.getSchemeSpecificPart());629show("authority", u.getRawAuthority(), u.getAuthority());630show("userinfo", u.getRawUserInfo(), u.getUserInfo());631show("host", u.getHost());632show("port", "" + u.getPort());633show("path", u.getRawPath(), u.getPath());634show("query", u.getRawQuery(), u.getQuery());635show("fragment", u.getRawFragment(), u.getFragment());636if (!u.toString().equals(u.toASCIIString()))637show("toascii", u.toASCIIString());638}639640private void report() {641summarize();642if (failed == 0) return;643StringBuffer sb = new StringBuffer();644sb.append("FAIL:");645if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");646if ((failed & SCHEME) != 0) sb.append(" scheme");647if ((failed & SSP) != 0) sb.append(" ssp");648if ((failed & OPAQUEPART) != 0) sb.append(" opaquepart");649if ((failed & USERINFO) != 0) sb.append(" userinfo");650if ((failed & USERINFO_D) != 0) sb.append(" userinfod");651if ((failed & HOST) != 0) sb.append(" host");652if ((failed & PORT) != 0) sb.append(" port");653if ((failed & REGISTRY) != 0) sb.append(" registry");654if ((failed & PATH) != 0) sb.append(" path");655if ((failed & PATH_D) != 0) sb.append(" pathd");656if ((failed & QUERY) != 0) sb.append(" query");657if ((failed & QUERY_D) != 0) sb.append(" queryd");658if ((failed & FRAGMENT) != 0) sb.append(" fragment");659if ((failed & FRAGMENT_D) != 0) sb.append(" fragmentd");660if ((failed & TOASCII) != 0) sb.append(" toascii");661if ((failed & IDENT_STR) != 0) sb.append(" ident-str");662if ((failed & IDENT_URI1) != 0) sb.append(" ident-uri1");663if ((failed & IDENT_URI3) != 0) sb.append(" ident-uri3");664if ((failed & IDENT_URI5) != 0) sb.append(" ident-uri5");665if ((failed & IDENT_URI7) != 0) sb.append(" ident-uri7");666if ((failed & TOSTRING) != 0) sb.append(" tostring");667out.println(sb.toString());668if (uri != null) show(uri);669throw new RuntimeException("Test failed");670}671672673674// -- Tests --675676static void rfc2396() {677678679header("RFC2396: Basic examples");680681test("ftp://ftp.is.co.za/rfc/rfc1808.txt")682.s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();683684test("http://www.math.uio.no/faq/compression-faq/part1.html")685.s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();686687test("mailto:[email protected]")688.s("mailto").o("[email protected]").z();689690test("news:comp.infosystems.www.servers.unix")691.s("news").o("comp.infosystems.www.servers.unix").z();692693test("telnet://melvyl.ucop.edu/")694.s("telnet").h("melvyl.ucop.edu").p("/").z();695696test("http://www.w3.org/Addressing/")697.s("http").h("www.w3.org").p("/Addressing/").z();698699test("ftp://ds.internic.net/rfc/")700.s("ftp").h("ds.internic.net").p("/rfc/").z();701702test("http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING")703.s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/historical.html")704.f("WARNING").z();705706test("http://www.ics.uci.edu/pub/ietf/uri/#Related")707.s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/")708.f("Related").z();709710711header("RFC2396: Normal relative-URI examples (appendix C)");712713URI base = (test("http://a/b/c/d;p?q")714.s("http").h("a").p("/b/c/d;p").q("q").z().uri());715716// g:h g:h717test("g:h")718.s("g").o("h").z()719.rslv(base).s("g").o("h").z();720721// g http://a/b/c/g722test("g")723.p("g").z()724.rslv(base).s("http").h("a").p("/b/c/g").z();725726// ./g http://a/b/c/g727test("./g")728.p("./g").z()729.rslv(base).s("http").h("a").p("/b/c/g").z();730731// g/ http://a/b/c/g/732test("g/")733.p("g/").z()734.rslv(base).s("http").h("a").p("/b/c/g/").z();735736// /g http://a/g737test("/g")738.p("/g").z()739.rslv(base).s("http").h("a").p("/g").z();740741// //g http://g742test("//g")743.h("g").p("").z()744.rslv(base).s("http").h("g").p("").z();745746// ?y http://a/b/c/?y747test("?y")748.p("").q("y").z()749.rslv(base).s("http").h("a").p("/b/c/").q("y").z();750751// g?y http://a/b/c/g?y752test("g?y")753.p("g").q("y").z()754.rslv(base).s("http").h("a").p("/b/c/g").q("y").z();755756// #s (current document)#s757// DEVIATION: Lone fragment parses as relative URI with empty path758test("#s")759.p("").f("s").z()760.rslv(base).s("http").h("a").p("/b/c/d;p").f("s").q("q").z();761762// g#s http://a/b/c/g#s763test("g#s")764.p("g").f("s").z()765.rslv(base).s("http").h("a").p("/b/c/g").f("s").z();766767// g?y#s http://a/b/c/g?y#s768test("g?y#s")769.p("g").q("y").f("s").z()770.rslv(base).s("http").h("a").p("/b/c/g").q("y").f("s").z();771772// ;x http://a/b/c/;x773test(";x")774.p(";x").z()775.rslv(base).s("http").h("a").p("/b/c/;x").z();776777// g;x http://a/b/c/g;x778test("g;x")779.p("g;x").z()780.rslv(base).s("http").h("a").p("/b/c/g;x").z();781782// g;x?y#s http://a/b/c/g;x?y#s783test("g;x?y#s")784.p("g;x").q("y").f("s").z()785.rslv(base).s("http").h("a").p("/b/c/g;x").q("y").f("s").z();786787// . http://a/b/c/788test(".")789.p(".").z()790.rslv(base).s("http").h("a").p("/b/c/").z();791792// ./ http://a/b/c/793test("./")794.p("./").z()795.rslv(base).s("http").h("a").p("/b/c/").z();796797// .. http://a/b/798test("..")799.p("..").z()800.rslv(base).s("http").h("a").p("/b/").z();801802// ../ http://a/b/803test("../")804.p("../").z()805.rslv(base).s("http").h("a").p("/b/").z();806807// ../g http://a/b/g808test("../g")809.p("../g").z()810.rslv(base).s("http").h("a").p("/b/g").z();811812// ../.. http://a/813test("../..")814.p("../..").z()815.rslv(base).s("http").h("a").p("/").z();816817// ../../ http://a/818test("../../")819.p("../../").z()820.rslv(base).s("http").h("a").p("/").z();821822// ../../g http://a/g823test("../../g")824.p("../../g").z()825.rslv(base).s("http").h("a").p("/g").z();826827828header("RFC2396: Abnormal relative-URI examples (appendix C)");829830// ../../../g = http://a/../g831test("../../../g")832.p("../../../g").z()833.rslv(base).s("http").h("a").p("/../g").z();834835// ../../../../g = http://a/../../g836test("../../../../g")837.p("../../../../g").z()838.rslv(base).s("http").h("a").p("/../../g").z();839840841// /./g = http://a/./g842test("/./g")843.p("/./g").z()844.rslv(base).s("http").h("a").p("/./g").z();845846// /../g = http://a/../g847test("/../g")848.p("/../g").z()849.rslv(base).s("http").h("a").p("/../g").z();850851// g. = http://a/b/c/g.852test("g.")853.p("g.").z()854.rslv(base).s("http").h("a").p("/b/c/g.").z();855856// .g = http://a/b/c/.g857test(".g")858.p(".g").z()859.rslv(base).s("http").h("a").p("/b/c/.g").z();860861// g.. = http://a/b/c/g..862test("g..")863.p("g..").z()864.rslv(base).s("http").h("a").p("/b/c/g..").z();865866// ..g = http://a/b/c/..g867test("..g")868.p("..g").z()869.rslv(base).s("http").h("a").p("/b/c/..g").z();870871// ./../g = http://a/b/g872test("./../g")873.p("./../g").z()874.rslv(base).s("http").h("a").p("/b/g").z();875876// ./g/. = http://a/b/c/g/877test("./g/.")878.p("./g/.").z()879.rslv(base).s("http").h("a").p("/b/c/g/").z();880881// g/./h = http://a/b/c/g/h882test("g/./h")883.p("g/./h").z()884.rslv(base).s("http").h("a").p("/b/c/g/h").z();885886// g/../h = http://a/b/c/h887test("g/../h")888.p("g/../h").z()889.rslv(base).s("http").h("a").p("/b/c/h").z();890891// g;x=1/./y = http://a/b/c/g;x=1/y892test("g;x=1/./y")893.p("g;x=1/./y").z()894.rslv(base).s("http").h("a").p("/b/c/g;x=1/y").z();895896// g;x=1/../y = http://a/b/c/y897test("g;x=1/../y")898.p("g;x=1/../y").z()899.rslv(base).s("http").h("a").p("/b/c/y").z();900901// g?y/./x = http://a/b/c/g?y/./x902test("g?y/./x")903.p("g").q("y/./x").z()904.rslv(base).s("http").h("a").p("/b/c/g").q("y/./x").z();905906// g?y/../x = http://a/b/c/g?y/../x907test("g?y/../x")908.p("g").q("y/../x").z()909.rslv(base).s("http").h("a").p("/b/c/g").q("y/../x").z();910911// g#s/./x = http://a/b/c/g#s/./x912test("g#s/./x")913.p("g").f("s/./x").z()914.rslv(base).s("http").h("a").p("/b/c/g").f("s/./x").z();915916// g#s/../x = http://a/b/c/g#s/../x917test("g#s/../x")918.p("g").f("s/../x").z()919.rslv(base).s("http").h("a").p("/b/c/g").f("s/../x").z();920921// http:g = http:g922test("http:g")923.s("http").o("g").z()924.rslv(base).s("http").o("g").z();925926}927928929static void ip() {930931header("IP addresses");932933test("http://1.2.3.4:5")934.s("http").h("1.2.3.4").n(5).p("").z();935936// From RFC2732937938test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")939.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")940.n(80).p("/index.html").z();941942test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]:80/index.html")943.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]")944.n(80).p("/index.html").z();945946test("http://[1080:0:0:0:8:800:200C:417A]/index.html")947.s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();948949test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")950.s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();951952test("http://[3ffe:2a00:100:7031::1]")953.s("http").h("[3ffe:2a00:100:7031::1]").p("").z();954955test("http://[1080::8:800:200C:417A]/foo")956.s("http").h("[1080::8:800:200C:417A]").p("/foo").z();957958test("http://[::192.9.5.5]/ipng")959.s("http").h("[::192.9.5.5]").p("/ipng").z();960961test("http://[::192.9.5.5%interface]/ipng")962.s("http").h("[::192.9.5.5%interface]").p("/ipng").z();963964test("http://[::FFFF:129.144.52.38]:80/index.html")965.s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();966967test("http://[2010:836B:4179::836B:4179]")968.s("http").h("[2010:836B:4179::836B:4179]").p("").z();969970// From RFC2373971972test("http://[FF01::101]")973.s("http").h("[FF01::101]").p("").z();974975test("http://[::1]")976.s("http").h("[::1]").p("").z();977978test("http://[::]")979.s("http").h("[::]").p("").z();980981test("http://[::%hme0]")982.s("http").h("[::%hme0]").p("").z();983984test("http://[0:0:0:0:0:0:13.1.68.3]")985.s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();986987test("http://[0:0:0:0:0:FFFF:129.144.52.38]")988.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();989990test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")991.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();992993test("http://[0:0:0:0:0:ffff:1.2.3.4]")994.s("http").h("[0:0:0:0:0:ffff:1.2.3.4]").p("").z();995996test("http://[::13.1.68.3]")997.s("http").h("[::13.1.68.3]").p("").z();998999// Optional IPv6 brackets in constructors10001001test("s", null, "1:2:3:4:5:6:7:8", -1, null, null, null)1002.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10031004test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)1005.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10061007test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)1008.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10091010test("s", "1:2:3:4:5:6:7:8", null, null)1011.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10121013test("s", "1:2:3:4:5:6:7:8%hme0", null, null)1014.s("s").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();10151016test("s", "1:2:3:4:5:6:7:8%1", null, null)1017.s("s").h("[1:2:3:4:5:6:7:8%1]").p("").z();10181019test("s", "[1:2:3:4:5:6:7:8]", null, null)1020.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10211022test("s", "[1:2:3:4:5:6:7:8]", null, null, null)1023.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();10241025test("s", "1:2:3:4:5:6:7:8", null, null, null)1026.s("s").g("1:2:3:4:5:6:7:8").p("").z();10271028// Error cases10291030test("http://[ff01:234/foo").x().z();1031test("http://[ff01:234:zzz]/foo").x().z();1032test("http://[foo]").x().z();1033test("http://[]").x().z();1034test("http://[129.33.44.55]").x().z();1035test("http://[ff:ee:dd:cc:bb::aa:9:8]").x().z();1036test("http://[fffff::1]").x().z();1037test("http://[ff::ee::8]").x().z();1038test("http://[1:2:3:4::5:6:7:8]").x().z();1039test("http://[1:2]").x().z();1040test("http://[1:2:3:4:5:6:7:8:9]").x().z();1041test("http://[1:2:3:4:5:6:7:8%]").x().z();1042test("http://[1:2:3:4:5:6:7:8%!/]").x().z();1043test("http://[::1.2.3.300]").x().z();1044test("http://1.2.3").psa().x().z();1045test("http://1.2.3.300").psa().x().z();1046test("http://1.2.3.4.5").psa().x().z();1047test("http://[1.2.3.4:5]").x().z();1048test("http://1:2:3:4:5:6:7:8").psa().x().z();1049test("http://[1.2.3.4]/").x().z();1050test("http://[1.2.3.4/").x().z();1051test("http://[foo]/").x().z();1052test("http://[foo/").x().z();1053test("s", "[foo]", "/", null, null).x().z();1054test("s", "[foo", "/", null, null).x().z();1055test("s", "[::foo", "/", null, null).x().z();10561057// Test hostnames that might initially look like IPv4 addresses10581059test("s://1.2.3.com").psa().s("s").h("1.2.3.com").p("").z();1060test("s://1.2.3.4me.com").psa().s("s").h("1.2.3.4me.com").p("").z();10611062test("s://7up.com").psa().s("s").h("7up.com").p("").z();1063test("s://7up.com/p").psa().s("s").h("7up.com").p("/p").z();1064test("s://7up").psa().s("s").h("7up").p("").z();1065test("s://7up/p").psa().s("s").h("7up").p("/p").z();1066test("s://7up.").psa().s("s").h("7up.").p("").z();1067test("s://7up./p").psa().s("s").h("7up.").p("/p").z();1068}106910701071static void misc() throws URISyntaxException {10721073URI base = new URI("s://h/a/b");1074URI rbase = new URI("a/b/c/d");107510761077header("Corner cases");10781079// The empty URI parses as a relative URI with an empty path1080test("").p("").z()1081.rslv(base).s("s").h("h").p("/a/").z();10821083// Resolving solo queries and fragments1084test("#f").p("").f("f").z()1085.rslv(base).s("s").h("h").p("/a/b").f("f").z();1086test("?q").p("").q("q").z()1087.rslv(base).s("s").h("h").p("/a/").q("q").z();10881089// Fragment is not part of ssp1090test("p#f").p("p").f("f").sp("p").z();1091test("s:p#f").s("s").o("p").f("f").z();1092test("p#f")1093.rslv(base).s("s").h("h").p("/a/p").f("f").sp("//h/a/p").z();1094test("").p("").sp("").z();1095109610971098header("Emptiness");10991100// Components that may be empty1101test("///p").p("/p").z(); // Authority (w/ path)1102test("//@h/p").u("").h("h").p("/p").z(); // User info1103test("//h:/p").h("h").p("/p").z(); // Port1104test("//h").h("h").p("").z(); // Path1105test("//h?q").h("h").p("").q("q").z(); // Path (w/query)1106test("//?q").p("").q("q").z(); // Authority (w/query)1107test("//#f").p("").f("f").z(); // Authority (w/fragment)1108test("p?#").p("p").q("").f("").z(); // Query & fragment11091110// Components that may not be empty1111test(":").x().z(); // Scheme1112test("x:").x().z(); // Hier/opaque1113test("//").x().z(); // Authority (w/o path)111411151116header("Resolution, normalization, and relativization");11171118// Resolving relative paths1119test("../e/f").p("../e/f").z()1120.rslv(rbase).p("a/b/e/f").z();1121test("../../../../d").p("../../../../d").z()1122.rslv(rbase).p("../d").z();1123test("../../../d:e").p("../../../d:e").z()1124.rslv(rbase).p("./d:e").z();1125test("../../../d:e/f").p("../../../d:e/f").z()1126.rslv(rbase).p("./d:e/f").z();11271128// Normalization1129test("a/./c/../d/f").p("a/./c/../d/f").z()1130.norm().p("a/d/f").z();1131test("http://a/./b/c/../d?q#f")1132.s("http").h("a").p("/./b/c/../d").q("q").f("f").z()1133.norm().s("http").h("a").p("/b/d").q("q").f("f").z();1134test("a/../b").p("a/../b").z().1135norm().p("b");1136test("a/../b:c").p("a/../b:c").z()1137.norm().p("./b:c").z();11381139// Normalization of already normalized URI should yield the1140// same URI1141URI u1 = URI.create("s://h/../p");1142URI u2 = u1.normalize();1143eq(u1, u2);1144eqeq(u1, u2);11451146// Relativization1147test("/a/b").p("/a/b").z()1148.rtvz(new URI("/a")).p("b").z();1149test("/a/b").p("/a/b").z()1150.rtvz(new URI("/a/")).p("b").z();1151test("a/b").p("a/b").z()1152.rtvz(new URI("a")).p("b").z();1153test("/a/b").p("/a/b").z()1154.rtvz(new URI("/a/b")).p("").z(); // Result is empty path1155test("a/../b:c/d").p("a/../b:c/d").z()1156.rtvz(new URI("./b:c/")).p("d").z();11571158test("http://a/b/d/e?q#f")1159.s("http").h("a").p("/b/d/e").q("q").f("f").z()1160.rtvz(new URI("http://a/b/?r#g"))1161.p("d/e").q("q").f("f").z();11621163// parseServerAuthority1164test("/a/b").psa().p("/a/b").z();1165test("s://u@h:1/p")1166.psa().s("s").u("u").h("h").n(1).p("/p").z();1167test("s://u@h:-foo/p").s("s").g("u@h:-foo").p("/p").z()1168.psa().x().z();1169test("s://h:999999999999999999999999").psa().x().z();1170test("s://:/b").psa().x().z();117111721173header("Constructors and factories");11741175test("s", null, null, -1, "p", null, null).x().z();1176test(null, null, null, -1, null, null, null).p("").z();1177test(null, null, null, -1, "p", null, null).p("p").z();1178test(null, null, "foo%20bar", -1, null, null, null).x().z();1179test(null, null, "foo", -100, null, null, null).x().z();1180test("s", null, null, -1, "", null, null).x().z();1181test("s", null, null, -1, "/p", null, null).s("s").p("/p").z();1182test("s", "u", "h", 10, "/p", "q", "f")1183.s("s").u("u").h("h").n(10).p("/p").q("q").f("f").z();1184test("s", "a:b", "/p", "q", "f")1185.s("s").g("a:b").p("/p").q("q").f("f").z();1186test("s", "h", "/p", "f")1187.s("s").h("h").p("/p").f("f").z();1188test("s", "p", "f").s("s").o("p").f("f").z();1189test("s", "/p", "f").s("s").p("/p").f("f").z();1190testCreate("s://u@h/p?q#f")1191.s("s").u("u").h("h").p("/p").q("q").f("f").z();1192}11931194static void npes() throws URISyntaxException {11951196header("NullPointerException");11971198URI base = URI.create("mailto:[email protected]");11991200out.println();12011202try {1203base.resolve((URI)null);1204throw new RuntimeException("NullPointerException not thrown");1205} catch (NullPointerException x) {1206out.println("resolve((URI)null) -->");1207out.println("Correct exception: " + x);1208}12091210out.println();12111212try {1213base.resolve((String)null);1214throw new RuntimeException("NullPointerException not thrown");1215} catch (NullPointerException x) {1216out.println("resolve((String)null) -->");1217out.println("Correct exception: " + x);1218}12191220out.println();12211222try {1223base.relativize((URI)null);1224throw new RuntimeException("NullPointerException not thrown");1225} catch (NullPointerException x) {1226out.println("relativize((String)null) -->");1227out.println("Correct exception: " + x);1228}12291230testCount += 3;1231}123212331234static void chars() throws URISyntaxException {12351236header("Escapes and non-US-ASCII characters");12371238URI uri;12391240// Escape pairs1241test("%0a%0A%0f%0F%01%09zz")1242.p("%0a%0A%0f%0F%01%09zz").z();1243test("foo%1").x().z();1244test("foo%z").x().z();1245test("foo%9z").x().z();12461247// Escapes not permitted in scheme, host1248test("s%20t://a").x().z();1249test("//a%20b").g("a%20b").p("").z(); // Parses as registry12501251// Escapes permitted in opaque part, userInfo, registry, path,1252// query, and fragment1253test("//u%20v@a").u("u%20v").h("a").p("").z();1254test("/p%20q").p("/p%20q").z();1255test("/p?q%20").p("/p").q("q%20").z();1256test("/p#%20f").p("/p").f("%20f").z();12571258// Non-US-ASCII chars1259test("s\u00a7t://a").x().z();1260test("//\u00a7/b").g("\u00a7").p("/b").z(); // Parses as registry1261test("//u\u00a7v@a").u("u\u00a7v").h("a").p("").z();1262test("/p\u00a7q").p("/p\u00a7q").z();1263test("/p?q\u00a7").p("/p").q("q\u00a7").z();1264test("/p#\u00a7f").p("/p").f("\u00a7f").z();12651266// 4648111 - Escapes quoted by toString after resolution1267uri = new URI("http://a/b/c/d;p?q");1268test("/p%20p")1269.rslv(uri).s("http").h("a").p("/p%20p").ts("http://a/p%20p").z();12701271// 4464135: Forbid unwise characters throughout opaque part1272test("foo:x{bar").x().z();1273test("foo:{bar").x().z();12741275// 4438319: Single-argument constructor requires quotation,1276// preserves escapes1277test("//u%01@h/a/b/%02/c?q%03#f%04")1278.u("u%01").ud("u\1")1279.h("h")1280.p("/a/b/%02/c").pd("/a/b/\2/c")1281.q("q%03").qd("q\3")1282.f("f%04").fd("f\4")1283.z();1284test("/a/b c").x().z();12851286// 4438319: Multi-argument constructors quote illegal chars and1287// preserve legal non-ASCII chars1288// \uA001-\uA009 are visible characters, \u2000 is a space character1289test(null, "u\uA001\1", "h", -1,1290"/p% \uA002\2\u2000",1291"q% \uA003\3\u2000",1292"f% \uA004\4\u2000")1293.u("u\uA001%01").h("h")1294.p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")1295.q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")1296.f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();1297test(null, "g\uA001\1",1298"/p% \uA002\2\u2000",1299"q% \uA003\3\u2000",1300"f% \uA004\4\u2000")1301.g("g\uA001%01")1302.p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")1303.q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")1304.f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();1305test(null, null, "/p% \uA002\2\u2000", "f% \uA004\4\u2000")1306.p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")1307.f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();1308test(null, "/sp% \uA001\1\u2000", "f% \uA004\4\u2000")1309.sp("/sp%25%20\uA001%01%E2%80%80").spd("/sp% \uA001\1\u2000")1310.p("/sp%25%20\uA001%01%E2%80%80").pd("/sp% \uA001\1\u2000")1311.f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();13121313// 4438319: Non-raw accessors decode all escaped octets1314test("/%25%20%E2%82%AC%E2%80%80")1315.p("/%25%20%E2%82%AC%E2%80%80").pd("/% \u20Ac\u2000").z();13161317// 4438319: toASCIIString1318test("/\uCAFE\uBABE")1319.p("/\uCAFE\uBABE").ta("/%EC%AB%BE%EB%AA%BE").z();13201321// 4991359 and 4866303: bad quoting by defineSchemeSpecificPart()1322URI base = new URI ("http://host/foo%20bar/a/b/c/d");1323test ("resolve")1324.rslv(base).spd("//host/foo bar/a/b/c/resolve")1325.sp("//host/foo%20bar/a/b/c/resolve").s("http")1326.pd("/foo bar/a/b/c/resolve").h("host")1327.p("/foo%20bar/a/b/c/resolve").z();13281329// 6773270: java.net.URI fails to escape u00001330test("s", "a", "/\u0000", null)1331.s("s").p("/%00").h("a")1332.ta("s://a/%00").z();1333}133413351336static void eq0(URI u, URI v) throws URISyntaxException {1337testCount++;1338if (!u.equals(v))1339throw new RuntimeException("Not equal: " + u + " " + v);1340int uh = u.hashCode();1341int vh = v.hashCode();1342if (uh != vh)1343throw new RuntimeException("Hash codes not equal: "1344+ u + " " + Integer.toHexString(uh) + " "1345+ v + " " + Integer.toHexString(vh));1346out.println();1347out.println(u + " == " + v1348+ " [" + Integer.toHexString(uh) + "]");1349}13501351static void cmp0(URI u, URI v, boolean same)1352throws URISyntaxException1353{1354int c = u.compareTo(v);1355if ((c == 0) != same)1356throw new RuntimeException("Comparison inconsistent: " + u + " " + v1357+ " " + c);1358}13591360static void eq(URI u, URI v) throws URISyntaxException {1361eq0(u, v);1362cmp0(u, v, true);1363}13641365static void eq(String expected, String actual) {1366if (expected == null && actual == null) {1367return;1368}1369if (expected != null && expected.equals(actual)) {1370return;1371}1372throw new AssertionError(String.format(1373"Strings are not equal: '%s', '%s'", expected, actual));1374}13751376static void eqeq(URI u, URI v) {1377testCount++;1378if (u != v)1379throw new RuntimeException("Not ==: " + u + " " + v);1380}13811382static void ne0(URI u, URI v) throws URISyntaxException {1383testCount++;1384if (u.equals(v))1385throw new RuntimeException("Equal: " + u + " " + v);1386out.println();1387out.println(u + " != " + v1388+ " [" + Integer.toHexString(u.hashCode())1389+ " " + Integer.toHexString(v.hashCode())1390+ "]");1391}13921393static void ne(URI u, URI v) throws URISyntaxException {1394ne0(u, v);1395cmp0(u, v, false);1396}13971398static void lt(URI u, URI v) throws URISyntaxException {1399ne0(u, v);1400int c = u.compareTo(v);1401if (c >= 0) {1402show(u);1403show(v);1404throw new RuntimeException("Not less than: " + u + " " + v1405+ " " + c);1406}1407out.println(u + " < " + v);1408}14091410static void lt(String s, String t) throws URISyntaxException {1411lt(new URI(s), new URI(t));1412}14131414static void gt0(URI u, URI v) throws URISyntaxException {1415ne0(u, v);1416int c = u.compareTo(v);1417if (c <= 0) {1418show(u);1419show(v);1420throw new RuntimeException("Not greater than: " + u + " " + v1421+ " " + c);1422}1423out.println(u + " < " + v);1424}14251426static void gt(URI u, URI v) throws URISyntaxException {1427lt(v, u);1428}14291430static void eqHashComp() throws URISyntaxException {14311432header("Equality, hashing, and comparison");14331434URI o = new URI("mailto:[email protected]");1435URI r = new URI("reg://some%20registry/b/c/d?q#f");1436URI s = new URI("http://jag:[email protected]:94/b/c/d?q#f");1437URI t = new URI("http://example.com/%5bsegment%5d");1438eq(o, o);1439lt(o, r);1440lt(s, o);1441lt(s, r);14421443eq(o, new URI("MaILto:[email protected]"));1444gt(o, new URI("mailto:[email protected]"));1445eq(r, new URI("rEg://some%20registry/b/c/d?q#f"));1446gt(r, new URI("reg://Some%20Registry/b/c/d?q#f"));1447gt(r, new URI("reg://some%20registry/b/c/D?q#f"));1448eq(s, new URI("hTtP://jag:[email protected]:94/b/c/d?q#f"));1449gt(s, new URI("http://jag:[email protected]:94/b/c/d?q#f"));1450lt(s, new URI("http://jag:[email protected]:94/b/c/d?r#f"));1451lt(s, new URI("http://jag:[email protected]:94/b/c/d?q#g"));1452cmp0(t, new URI("http://example.com/%5Bsegment%5D"), true);1453gt0(t, new URI("http://example.com/%5BSegment%5D"));1454lt(new URI("http://example.com/%5Asegment%5D"), new URI("http://example.com/%5Bsegment%5D"));1455eq(new URI("http://host/a%00bcd"), new URI("http://host/a%00bcd"));1456ne(new URI("http://host/a%00bcd"), new URI("http://host/aZ00bcd"));1457eq0(new URI("http://host/abc%e2def%C3ghi"),1458new URI("http://host/abc%E2def%c3ghi"));14591460lt("p", "s:p");1461lt("s:p", "T:p");1462lt("S:p", "t:p");1463lt("s:/p", "s:p");1464lt("s:p", "s:q");1465lt("s:p#f", "s:p#g");1466lt("s://u@h:1", "s://v@h:1");1467lt("s://u@h:1", "s://u@i:1");1468lt("s://u@h:1", "s://v@h:2");1469lt("s://a%20b", "s://a%20c");1470lt("s://a%20b", "s://aab");1471lt("s://AA", "s://A_");1472lt("s:/p", "s:/q");1473lt("s:/p?q", "s:/p?r");1474lt("s:/p#f", "s:/p#g");14751476lt("s://h", "s://h/p");1477lt("s://h/p", "s://h/p?q");14781479}148014811482static void serial(URI u) throws IOException, URISyntaxException {14831484ByteArrayOutputStream bo = new ByteArrayOutputStream();1485ObjectOutputStream oo = new ObjectOutputStream(bo);14861487oo.writeObject(u);1488oo.close();14891490ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());1491ObjectInputStream oi = new ObjectInputStream(bi);1492try {1493Object o = oi.readObject();1494eq(u, (URI)o);1495} catch (ClassNotFoundException x) {1496x.printStackTrace();1497throw new RuntimeException(x.toString());1498}14991500testCount++;1501}15021503static void serial() throws IOException, URISyntaxException {1504header("Serialization");15051506serial(URI.create("http://java.sun.com/jdk/1.4?release#beta"));1507serial(URI.create("s://h/p").resolve("/long%20path/"));1508}150915101511static void urls() throws URISyntaxException {15121513header("URLs");15141515URI uri;1516URL url;1517boolean caught = false;15181519out.println();1520uri = new URI("http://a/p?q#f");1521try {1522url = uri.toURL();1523} catch (MalformedURLException x) {1524throw new RuntimeException(x.toString());1525}1526if (!url.toString().equals("http://a/p?q#f"))1527throw new RuntimeException("Incorrect URL: " + url);1528out.println(uri + " url --> " + url);15291530out.println();1531uri = new URI("a/b");1532try {1533out.println(uri + " url --> ");1534url = uri.toURL();1535} catch (IllegalArgumentException x) {1536caught = true;1537out.println("Correct exception: " + x);1538} catch (MalformedURLException x) {1539caught = true;1540throw new RuntimeException("Incorrect exception: " + x);1541}1542if (!caught)1543throw new RuntimeException("Incorrect URL: " + url);15441545out.println();1546uri = new URI("foo://bar/baz");1547caught = false;1548try {1549out.println(uri + " url --> ");1550url = uri.toURL();1551} catch (MalformedURLException x) {1552caught = true;1553out.println("Correct exception: " + x);1554} catch (IllegalArgumentException x) {1555caught = true;1556throw new RuntimeException("Incorrect exception: " + x);1557}1558if (!caught)1559throw new RuntimeException("Incorrect URL: " + url);15601561testCount += 3;1562}156315641565static void tests() throws IOException, URISyntaxException {1566rfc2396();1567ip();1568misc();1569chars();1570eqHashComp();1571serial();1572urls();1573npes();1574bugs();1575}157615771578// -- Command-line invocation --15791580static void usage() {1581out.println("Usage:");1582out.println(" java Test -- Runs all tests in this file");1583out.println(" java Test <uri> -- Parses uri, shows components");1584out.println(" java Test <base> <uri> -- Parses uri and base, then resolves");1585out.println(" uri against base");1586}15871588static void clargs(String base, String uri) {1589URI b = null, u;1590try {1591if (base != null) {1592b = new URI(base);1593out.println(base);1594show(b);1595}1596u = new URI(uri);1597out.println(uri);1598show(u);1599if (base != null) {1600URI r = b.resolve(u);1601out.println(r);1602show(r);1603}1604} catch (URISyntaxException x) {1605show("ERROR", x);1606x.printStackTrace(out);1607}1608}160916101611// miscellaneous bugs/rfes that don't fit in with the test framework16121613static void bugs() {1614b6339649();1615b6933879();1616b8037396();1617}16181619// 6339649 - include detail message from nested exception1620private static void b6339649() {1621try {1622URI uri = URI.create("http://nowhere.net/should not be permitted");1623} catch (IllegalArgumentException e) {1624if ("".equals(e.getMessage()) || e.getMessage() == null) {1625throw new RuntimeException ("No detail message");1626}1627}1628}16291630// 6933879 - check that "." and "_" characters are allowed in IPv6 scope_id.1631private static void b6933879() {1632final String HOST = "fe80::c00:16fe:cebe:3214%eth1.12_55";1633URI uri;1634try {1635uri = new URI("http", null, HOST, 10, "/", null, null);1636} catch (URISyntaxException ex) {1637throw new AssertionError("Should not happen", ex);1638}1639eq("[" + HOST + "]", uri.getHost());1640}16411642private static void b8037396() {16431644// primary checks:16451646URI u;1647try {1648u = new URI("http", "example.org", "/[a b]", "[a b]", "[a b]");1649} catch (URISyntaxException e) {1650throw new AssertionError("shouldn't ever happen", e);1651}1652eq("/[a b]", u.getPath());1653eq("[a b]", u.getQuery());1654eq("[a b]", u.getFragment());16551656// additional checks:1657// * '%' symbols are still decoded outside square brackets1658// * the getRawXXX() functionality left intact16591660try {1661u = new URI("http", "example.org", "/a b[c d]", "a b[c d]", "a b[c d]");1662} catch (URISyntaxException e) {1663throw new AssertionError("shouldn't ever happen", e);1664}16651666eq("/a b[c d]", u.getPath());1667eq("a b[c d]", u.getQuery());1668eq("a b[c d]", u.getFragment());16691670eq("/a%20b%5Bc%20d%5D", u.getRawPath());1671eq("a%20b[c%20d]", u.getRawQuery());1672eq("a%20b[c%20d]", u.getRawFragment());1673}16741675public static void main(String[] args) throws Exception {1676switch (args.length) {16771678case 0:1679tests();1680out.println();1681out.println("Test cases: " + testCount);1682break;16831684case 1:1685if (args[0].equals("-help")) {1686usage();1687break;1688}1689clargs(null, args[0]);1690break;16911692case 2:1693clargs(args[0], args[1]);1694break;16951696default:1697usage();1698break;16991700}1701}17021703}170417051706