Test.java revision 3749
0N/A/*
3909N/A * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
2362N/A */
2362N/A
0N/A/* @test
0N/A * @summary Unit test for java.net.URI
0N/A * @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363
0N/A * @author Mark Reinhold
0N/A */
0N/A
0N/Aimport java.io.ByteArrayInputStream;
0N/Aimport java.io.ByteArrayOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.PrintStream;
0N/Aimport java.net.URI;
0N/Aimport java.net.URISyntaxException;
0N/Aimport java.net.URL;
0N/Aimport java.net.MalformedURLException;
0N/A
0N/A
0N/Apublic class Test {
1338N/A
0N/A static PrintStream out = System.out;
3787N/A static int testCount = 0;
3787N/A
3787N/A // Properties that we check
3787N/A static final int PARSEFAIL = 1 << 0;
3787N/A static final int SCHEME = 1 << 1;
3787N/A static final int SSP = 1 << 2;
3787N/A static final int SSP_D = 1 << 3; // Decoded form
3787N/A static final int OPAQUEPART = 1 << 4; // SSP, and URI is opaque
3787N/A static final int USERINFO = 1 << 5;
3787N/A static final int USERINFO_D = 1 << 6; // Decoded form
0N/A static final int HOST = 1 << 7;
0N/A static final int PORT = 1 << 8;
0N/A static final int REGISTRY = 1 << 9;
0N/A static final int REGISTRY_D = 1 << 10; // Decoded form
0N/A static final int PATH = 1 << 11;
0N/A static final int PATH_D = 1 << 12; // Decoded form
0N/A static final int QUERY = 1 << 13;
0N/A static final int QUERY_D = 1 << 14; // Decoded form
0N/A static final int FRAGMENT = 1 << 15;
0N/A static final int FRAGMENT_D = 1 << 16; // Decoded form
0N/A static final int TOASCII = 1 << 17;
0N/A static final int IDENT_STR = 1 << 18; // Identities
0N/A static final int IDENT_URI1 = 1 << 19;
0N/A static final int IDENT_URI3 = 1 << 20;
0N/A static final int IDENT_URI5 = 1 << 21;
0N/A static final int IDENT_URI7 = 1 << 22;
0N/A static final int TOSTRING = 1 << 23;
0N/A
0N/A String input;
0N/A URI uri = null;
0N/A URI originalURI;
0N/A URI base = null; // Base for resolution/relativization
0N/A String op = null; // Op performed if uri != originalURI
0N/A int checked = 0; // Mask for checked properties
0N/A int failed = 0; // Mask for failed properties
0N/A Exception exc = null;
0N/A
0N/A private Test(String s) {
0N/A testCount++;
0N/A input = s;
0N/A try {
0N/A uri = new URI(s);
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A }
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test test(String s) {
0N/A return new Test(s);
0N/A }
0N/A
0N/A private Test(String s, String u, String h, int n,
0N/A String p, String q, String f)
0N/A {
0N/A testCount++;
0N/A try {
0N/A uri = new URI(s, u, h, n, p, q, f);
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A input = x.getInput();
0N/A }
0N/A if (uri != null)
0N/A input = uri.toString();
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test test(String s, String u, String h, int n,
0N/A String p, String q, String f) {
0N/A return new Test(s, u, h, n, p, q, f);
0N/A }
0N/A
0N/A private Test(String s, String a,
0N/A String p, String q, String f)
0N/A {
0N/A testCount++;
0N/A try {
0N/A uri = new URI(s, a, p, q, f);
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A input = x.getInput();
0N/A }
0N/A if (uri != null)
0N/A input = uri.toString();
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test test(String s, String a,
0N/A String p, String q, String f) {
0N/A return new Test(s, a, p, q, f);
0N/A }
0N/A
0N/A private Test(String s, String h, String p, String f) {
0N/A testCount++;
0N/A try {
0N/A uri = new URI(s, h, p, f);
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A input = x.getInput();
0N/A }
0N/A if (uri != null)
0N/A input = uri.toString();
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test test(String s, String h, String p, String f) {
0N/A return new Test(s, h, p, f);
0N/A }
0N/A
0N/A private Test(String s, String ssp, String f) {
0N/A testCount++;
0N/A try {
0N/A uri = new URI(s, ssp, f);
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A input = x.getInput();
0N/A }
0N/A if (uri != null)
0N/A input = uri.toString();
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test test(String s, String ssp, String f) {
0N/A return new Test(s, ssp, f);
0N/A }
0N/A
0N/A private Test(String s, boolean xxx) {
0N/A testCount++;
0N/A try {
0N/A uri = URI.create(s);
0N/A } catch (IllegalArgumentException x) {
0N/A exc = x;
0N/A }
0N/A if (uri != null)
0N/A input = uri.toString();
0N/A originalURI = uri;
0N/A }
0N/A
0N/A static Test testCreate(String s) {
0N/A return new Test(s, false);
0N/A }
0N/A
0N/A boolean parsed() {
0N/A return uri != null;
0N/A }
0N/A
0N/A boolean resolved() {
0N/A return base != null;
0N/A }
0N/A
0N/A URI uri() {
0N/A return uri;
0N/A }
0N/A
0N/A
0N/A // Operations on Test instances
0N/A //
0N/A // These are short so as to make test cases compact.
0N/A //
0N/A // s Scheme
0N/A // sp Scheme-specific part
0N/A // spd Scheme-specific part, decoded
0N/A // o Opaque part (isOpaque() && ssp matches)
0N/A // g reGistry (authority matches, and host is not defined)
0N/A // gd reGistry, decoded
0N/A // u User info
0N/A // ud User info, decoded
0N/A // h Host
0N/A // n port Number
0N/A // p Path
0N/A // pd Path, decoded
0N/A // q Query
0N/A // qd Query, decoded
0N/A // f Fragment
0N/A // fd Fragment, decoded
0N/A //
0N/A // rslv Resolve against given base
0N/A // rtvz Relativize
0N/A // psa Parse server Authority
0N/A // norm Normalize
0N/A // ta ASCII form
0N/A //
0N/A // x Check that parse failed as expected
0N/A // z End -- ensure that unchecked components are null
0N/A
0N/A private boolean check1(int prop) {
0N/A checked |= prop;
0N/A if (!parsed()) {
0N/A failed |= prop;
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A private void check2(String s, String ans, int prop) {
0N/A if ((s == null) || !s.equals(ans))
0N/A failed |= prop;
0N/A }
0N/A
0N/A Test s(String s) {
0N/A if (check1(SCHEME)) check2(uri.getScheme(), s, SCHEME);
0N/A return this;
0N/A }
0N/A
0N/A Test u(String s) {
0N/A if (check1(USERINFO)) check2(uri.getRawUserInfo(), s, USERINFO);
0N/A return this;
0N/A }
0N/A
0N/A Test ud(String s) {
0N/A if (check1(USERINFO_D)) {
0N/A check2(uri.getUserInfo(), s, USERINFO_D);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A Test h(String s) {
0N/A if (check1(HOST)) check2(uri.getHost(), s, HOST);
0N/A return this;
0N/A }
0N/A
0N/A Test g(String s) {
0N/A if (check1(REGISTRY)) {
0N/A if (uri.getHost() != null)
0N/A failed |= REGISTRY;
0N/A else
0N/A check2(uri.getRawAuthority(), s, REGISTRY);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A Test gd(String s) {
0N/A if (check1(REGISTRY_D)) {
0N/A if (uri.getHost() != null)
0N/A failed |= REGISTRY_D;
0N/A else
0N/A check2(uri.getAuthority(), s, REGISTRY_D);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A Test n(int n) {
0N/A checked |= PORT;
0N/A if (!parsed() || (uri.getPort() != n))
0N/A failed |= PORT;
0N/A return this;
0N/A }
0N/A
0N/A Test p(String s) {
0N/A if (check1(PATH)) check2(uri.getRawPath(), s, PATH);
0N/A return this;
0N/A }
1338N/A
1338N/A Test pd(String s) {
1338N/A if (check1(PATH_D)) check2(uri.getPath(), s, PATH_D);
0N/A return this;
0N/A }
0N/A
0N/A Test o(String s) {
0N/A if (check1(OPAQUEPART)) {
0N/A if (!uri.isOpaque())
0N/A failed |= OPAQUEPART;
0N/A else
0N/A check2(uri.getSchemeSpecificPart(), s, OPAQUEPART);
0N/A }
0N/A return this;
0N/A }
0N/A
0N/A Test sp(String s) {
0N/A if (check1(SSP)) check2(uri.getRawSchemeSpecificPart(), s, SSP);
0N/A return this;
0N/A }
0N/A
0N/A Test spd(String s) {
0N/A if (check1(SSP_D)) check2(uri.getSchemeSpecificPart(), s, SSP_D);
0N/A return this;
0N/A }
0N/A
0N/A Test q(String s) {
0N/A if (check1(QUERY)) check2(uri.getRawQuery(), s, QUERY);
0N/A return this;
0N/A }
0N/A
0N/A Test qd(String s) {
0N/A if (check1(QUERY_D)) check2(uri.getQuery(), s, QUERY_D);
0N/A return this;
0N/A }
0N/A
0N/A Test f(String s) {
0N/A if (check1(FRAGMENT)) check2(uri.getRawFragment(), s, FRAGMENT);
0N/A return this;
0N/A }
0N/A
0N/A Test fd(String s) {
0N/A if (check1(FRAGMENT_D)) check2(uri.getFragment(), s, FRAGMENT_D);
0N/A return this;
0N/A }
0N/A
0N/A Test ta(String s) {
0N/A if (check1(TOASCII))
0N/A check2(uri.toASCIIString(), s, TOASCII);
0N/A return this;
0N/A }
0N/A
0N/A Test ts(String s) {
0N/A if (check1(TOSTRING))
0N/A check2(uri.toString(), s, TOSTRING);
0N/A return this;
0N/A }
0N/A
0N/A Test x() {
0N/A checked |= PARSEFAIL;
3370N/A if (parsed())
0N/A failed |= PARSEFAIL;
0N/A return this;
0N/A }
0N/A
0N/A Test rslv(URI base) {
0N/A if (!parsed())
0N/A return this;
0N/A this.base = base;
0N/A op = "rslv";
0N/A URI u = uri;
0N/A uri = null;
0N/A try {
0N/A this.uri = base.resolve(u);
0N/A } catch (IllegalArgumentException x) {
0N/A exc = x;
0N/A }
0N/A checked = 0;
0N/A failed = 0;
0N/A return this;
0N/A }
0N/A
0N/A Test norm() {
0N/A if (!parsed())
0N/A return this;
0N/A op = "norm";
0N/A uri = uri.normalize();
0N/A return this;
0N/A }
0N/A
0N/A Test rtvz(URI base) {
0N/A if (!parsed())
0N/A return this;
0N/A this.base = base;
0N/A op = "rtvz";
0N/A uri = base.relativize(uri);
3370N/A checked = 0;
0N/A failed = 0;
0N/A return this;
0N/A }
0N/A
0N/A Test psa() {
0N/A try {
0N/A uri.parseServerAuthority();
0N/A } catch (URISyntaxException x) {
0N/A exc = x;
0N/A uri = null;
0N/A }
0N/A checked = 0;
0N/A failed = 0;
0N/A return this;
0N/A }
0N/A
0N/A private void checkEmpty(String s, int prop) {
0N/A if (((checked & prop) == 0) && (s != null))
0N/A failed |= prop;
0N/A }
0N/A
0N/A // Check identity for the seven-argument URI constructor
0N/A //
0N/A void checkURI7() {
0N/A // Only works on hierarchical URIs
3370N/A if (uri.isOpaque())
0N/A return;
0N/A // Only works with server-based authorities
0N/A if ((uri.getAuthority() == null)
0N/A != ((uri.getUserInfo() == null) && (uri.getHost() == null)))
0N/A return;
0N/A // Not true if non-US-ASCII chars are encoded unnecessarily
0N/A if (uri.getPath().indexOf('\u20AC') >= 0)
0N/A return;
0N/A try {
0N/A URI u2 = new URI(uri.getScheme(), uri.getUserInfo(),
0N/A uri.getHost(), uri.getPort(), uri.getPath(),
0N/A uri.getQuery(), uri.getFragment());
0N/A if (!uri.equals(u2))
0N/A failed |= IDENT_URI7;
0N/A } catch (URISyntaxException x) {
0N/A failed |= IDENT_URI7;
0N/A }
0N/A }
0N/A
0N/A // Check identity for the five-argument URI constructor
0N/A //
0N/A void checkURI5() {
0N/A // Only works on hierarchical URIs
0N/A if (uri.isOpaque())
0N/A return;
0N/A try {
0N/A URI u2 = new URI(uri.getScheme(), uri.getAuthority(),
0N/A uri.getPath(), uri.getQuery(), uri.getFragment());
0N/A if (!uri.equals(u2))
0N/A failed |= IDENT_URI5;
0N/A } catch (URISyntaxException x) {
0N/A failed |= IDENT_URI5;
0N/A }
3370N/A }
0N/A
0N/A // Check identity for the three-argument URI constructor
0N/A //
0N/A void checkURI3() {
0N/A try {
0N/A URI u2 = new URI(uri.getScheme(),
0N/A uri.getSchemeSpecificPart(),
0N/A uri.getFragment());
0N/A if (!uri.equals(u2))
0N/A failed |= IDENT_URI3;
0N/A } catch (URISyntaxException x) {
0N/A failed |= IDENT_URI3;
0N/A }
0N/A }
0N/A
0N/A // Check all identities mentioned in the URI class specification
0N/A //
0N/A void checkIdentities() {
0N/A if (input != null) {
0N/A if (!uri.toString().equals(input))
0N/A failed |= IDENT_STR;
0N/A }
0N/A try {
0N/A if (!(new URI(uri.toString())).equals(uri))
0N/A failed |= IDENT_URI1;
0N/A } catch (URISyntaxException x) {
0N/A failed |= IDENT_URI1;
3370N/A }
0N/A
0N/A // Remaining identities fail if "//" given but authority is undefined
0N/A if ((uri.getAuthority() == null)
0N/A && (uri.getSchemeSpecificPart() != null)
0N/A && (uri.getSchemeSpecificPart().startsWith("///")
0N/A || uri.getSchemeSpecificPart().startsWith("//?")
0N/A || uri.getSchemeSpecificPart().equals("//")))
0N/A return;
0N/A
0N/A // Remaining identities fail if ":" given but port is undefined
0N/A if ((uri.getHost() != null)
0N/A && (uri.getAuthority() != null)
0N/A && (uri.getAuthority().equals(uri.getHost() + ":")))
0N/A return;
0N/A
0N/A // Remaining identities fail if non-US-ASCII chars are encoded
0N/A // unnecessarily
0N/A if ((uri.getPath() != null) && uri.getPath().indexOf('\u20AC') >= 0)
0N/A return;
0N/A
0N/A checkURI3();
0N/A checkURI5();
0N/A checkURI7();
0N/A }
0N/A
0N/A // Check identities, check that unchecked component properties are not
0N/A // defined, and report any failures
0N/A //
0N/A Test z() {
0N/A if (!parsed()) {
0N/A report();
0N/A return this;
3370N/A }
0N/A
0N/A if (op == null)
0N/A checkIdentities();
0N/A
0N/A // Check that unchecked components are undefined
0N/A checkEmpty(uri.getScheme(), SCHEME);
0N/A checkEmpty(uri.getUserInfo(), USERINFO);
0N/A checkEmpty(uri.getHost(), HOST);
0N/A if (((checked & PORT) == 0) && (uri.getPort() != -1)) failed |= PORT;
0N/A checkEmpty(uri.getPath(), PATH);
0N/A checkEmpty(uri.getQuery(), QUERY);
0N/A checkEmpty(uri.getFragment(), FRAGMENT);
0N/A
0N/A // Report failures
0N/A report();
0N/A return this;
0N/A }
0N/A
0N/A
0N/A // Summarization and reporting
0N/A
0N/A static void header(String s) {
0N/A out.println();
0N/A out.println();
0N/A out.println("-- " + s + " --");
0N/A }
0N/A
0N/A static void show(String prefix, URISyntaxException x) {
0N/A out.println(uquote(x.getInput()));
0N/A if (x.getIndex() >= 0) {
0N/A for (int i = 0; i < x.getIndex(); i++) {
0N/A if (x.getInput().charAt(i) >= '\u0080')
0N/A out.print(" "); // Skip over \u1234
0N/A else
0N/A out.print(" ");
0N/A }
0N/A out.println("^");
0N/A }
0N/A out.println(prefix + ": " + x.getReason());
0N/A }
0N/A
0N/A private void summarize() {
0N/A out.println();
0N/A StringBuffer sb = new StringBuffer();
0N/A if (input.length() == 0)
0N/A sb.append("\"\"");
0N/A else
0N/A sb.append(input);
0N/A if (base != null) {
0N/A sb.append(" ");
0N/A sb.append(base);
0N/A }
0N/A if (!parsed()) {
0N/A String s = (((checked & PARSEFAIL) != 0)
0N/A ? "Correct exception" : "UNEXPECTED EXCEPTION");
0N/A if (exc instanceof URISyntaxException)
0N/A show(s, (URISyntaxException)exc);
0N/A else {
0N/A out.println(uquote(sb.toString()));
0N/A out.print(s + ": ");
0N/A exc.printStackTrace(out);
0N/A }
0N/A } else {
0N/A if (uri != originalURI) {
0N/A sb.append(" ");
0N/A sb.append(op);
0N/A sb.append(" --> ");
0N/A sb.append(uri);
0N/A }
0N/A out.println(uquote(sb.toString()));
0N/A }
0N/A }
0N/A
0N/A public static String uquote(String str) {
0N/A if (str == null)
0N/A return str;
0N/A StringBuffer sb = new StringBuffer();
0N/A int n = str.length();
0N/A for (int i = 0; i < n; i++) {
0N/A char c = str.charAt(i);
0N/A if ((c >= ' ') && (c < 0x7f)) {
0N/A sb.append(c);
0N/A continue;
0N/A }
0N/A sb.append("\\u");
0N/A String s = Integer.toHexString(c).toUpperCase();
0N/A while (s.length() < 4)
0N/A s = "0" + s;
0N/A sb.append(s);
0N/A }
0N/A return sb.toString();
0N/A }
0N/A
0N/A static void show(String n, String v) {
0N/A out.println(" " + n
0N/A + " = ".substring(n.length())
0N/A + uquote(v));
0N/A }
0N/A
0N/A static void show(String n, String v, String vd) {
0N/A if ((v == null) || v.equals(vd))
0N/A show(n, v);
0N/A else {
0N/A out.println(" " + n
0N/A + " = ".substring(n.length())
0N/A + uquote(v)
0N/A + " = " + uquote(vd));
0N/A }
0N/A }
0N/A
0N/A public static void show(URI u) {
0N/A show("opaque", "" + u.isOpaque());
0N/A show("scheme", u.getScheme());
0N/A show("ssp", u.getRawSchemeSpecificPart(), u.getSchemeSpecificPart());
0N/A show("authority", u.getRawAuthority(), u.getAuthority());
0N/A show("userinfo", u.getRawUserInfo(), u.getUserInfo());
0N/A show("host", u.getHost());
0N/A show("port", "" + u.getPort());
0N/A show("path", u.getRawPath(), u.getPath());
0N/A show("query", u.getRawQuery(), u.getQuery());
0N/A show("fragment", u.getRawFragment(), u.getFragment());
0N/A if (!u.toString().equals(u.toASCIIString()))
0N/A show("toascii", u.toASCIIString());
0N/A }
0N/A
2129N/A private void report() {
2129N/A summarize();
2129N/A if (failed == 0) return;
2129N/A StringBuffer sb = new StringBuffer();
2129N/A sb.append("FAIL:");
2129N/A if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");
2129N/A if ((failed & SCHEME) != 0) sb.append(" scheme");
2129N/A if ((failed & SSP) != 0) sb.append(" ssp");
2129N/A if ((failed & OPAQUEPART) != 0) sb.append(" opaquepart");
2129N/A if ((failed & USERINFO) != 0) sb.append(" userinfo");
2129N/A if ((failed & USERINFO_D) != 0) sb.append(" userinfod");
2129N/A if ((failed & HOST) != 0) sb.append(" host");
2129N/A if ((failed & PORT) != 0) sb.append(" port");
2129N/A if ((failed & REGISTRY) != 0) sb.append(" registry");
2129N/A if ((failed & PATH) != 0) sb.append(" path");
2129N/A if ((failed & PATH_D) != 0) sb.append(" pathd");
2129N/A if ((failed & QUERY) != 0) sb.append(" query");
2129N/A if ((failed & QUERY_D) != 0) sb.append(" queryd");
2129N/A if ((failed & FRAGMENT) != 0) sb.append(" fragment");
2129N/A if ((failed & FRAGMENT_D) != 0) sb.append(" fragmentd");
2129N/A if ((failed & TOASCII) != 0) sb.append(" toascii");
2129N/A if ((failed & IDENT_STR) != 0) sb.append(" ident-str");
2129N/A if ((failed & IDENT_URI1) != 0) sb.append(" ident-uri1");
2129N/A if ((failed & IDENT_URI3) != 0) sb.append(" ident-uri3");
2129N/A if ((failed & IDENT_URI5) != 0) sb.append(" ident-uri5");
2129N/A if ((failed & IDENT_URI7) != 0) sb.append(" ident-uri7");
2129N/A if ((failed & TOSTRING) != 0) sb.append(" tostring");
2129N/A out.println(sb.toString());
2129N/A if (uri != null) show(uri);
2129N/A throw new RuntimeException("Test failed");
2129N/A }
2129N/A
2129N/A
2129N/A
2129N/A // -- Tests --
2129N/A
2129N/A static void rfc2396() {
2129N/A
2129N/A
2129N/A header("RFC2396: Basic examples");
2129N/A
2129N/A test("ftp://ftp.is.co.za/rfc/rfc1808.txt")
2129N/A .s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();
2129N/A
2129N/A test("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")
2129N/A .s("gopher").h("spinaltap.micro.umn.edu")
2129N/A .p("/00/Weather/California/Los%20Angeles").z();
2129N/A
2129N/A test("http://www.math.uio.no/faq/compression-faq/part1.html")
2129N/A .s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();
2129N/A
2129N/A test("mailto:mduerst@ifi.unizh.ch")
2129N/A .s("mailto").o("mduerst@ifi.unizh.ch").z();
2129N/A
2129N/A test("news:comp.infosystems.www.servers.unix")
2129N/A .s("news").o("comp.infosystems.www.servers.unix").z();
2129N/A
2129N/A test("telnet://melvyl.ucop.edu/")
2129N/A .s("telnet").h("melvyl.ucop.edu").p("/").z();
2129N/A
2129N/A test("http://www.w3.org/Addressing/")
2129N/A .s("http").h("www.w3.org").p("/Addressing/").z();
2129N/A
0N/A test("ftp://ds.internic.net/rfc/")
0N/A .s("ftp").h("ds.internic.net").p("/rfc/").z();
0N/A
0N/A test("http://www.ics.uci.edu/pub/ietf/uri/historical.html#WARNING")
0N/A .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/historical.html")
0N/A .f("WARNING").z();
0N/A
0N/A test("http://www.ics.uci.edu/pub/ietf/uri/#Related")
0N/A .s("http").h("www.ics.uci.edu").p("/pub/ietf/uri/")
0N/A .f("Related").z();
0N/A
0N/A
0N/A header("RFC2396: Normal relative-URI examples (appendix C)");
0N/A
0N/A URI base = (test("http://a/b/c/d;p?q")
0N/A .s("http").h("a").p("/b/c/d;p").q("q").z().uri());
0N/A
0N/A // g:h g:h
0N/A test("g:h")
0N/A .s("g").o("h").z()
0N/A .rslv(base).s("g").o("h").z();
0N/A
0N/A // g http://a/b/c/g
0N/A test("g")
0N/A .p("g").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").z();
0N/A
0N/A // ./g http://a/b/c/g
0N/A test("./g")
0N/A .p("./g").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").z();
0N/A
0N/A // g/ http://a/b/c/g/
0N/A test("g/")
0N/A .p("g/").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g/").z();
0N/A
0N/A // /g http://a/g
0N/A test("/g")
0N/A .p("/g").z()
0N/A .rslv(base).s("http").h("a").p("/g").z();
0N/A
0N/A // //g http://g
0N/A test("//g")
0N/A .h("g").p("").z()
0N/A .rslv(base).s("http").h("g").p("").z();
0N/A
0N/A // ?y http://a/b/c/?y
0N/A test("?y")
0N/A .p("").q("y").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/").q("y").z();
0N/A
0N/A // g?y http://a/b/c/g?y
0N/A test("g?y")
0N/A .p("g").q("y").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").q("y").z();
0N/A
0N/A // #s (current document)#s
0N/A // DEVIATION: Lone fragment parses as relative URI with empty path
0N/A test("#s")
0N/A .p("").f("s").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/d;p").f("s").q("q").z();
0N/A
0N/A // g#s http://a/b/c/g#s
0N/A test("g#s")
0N/A .p("g").f("s").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").f("s").z();
0N/A
0N/A // g?y#s http://a/b/c/g?y#s
0N/A test("g?y#s")
0N/A .p("g").q("y").f("s").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").q("y").f("s").z();
0N/A
0N/A // ;x http://a/b/c/;x
0N/A test(";x")
0N/A .p(";x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/;x").z();
0N/A
0N/A // g;x http://a/b/c/g;x
0N/A test("g;x")
0N/A .p("g;x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g;x").z();
0N/A
0N/A // g;x?y#s http://a/b/c/g;x?y#s
0N/A test("g;x?y#s")
0N/A .p("g;x").q("y").f("s").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g;x").q("y").f("s").z();
0N/A
0N/A // . http://a/b/c/
0N/A test(".")
0N/A .p(".").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/").z();
0N/A
0N/A // ./ http://a/b/c/
0N/A test("./")
0N/A .p("./").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/").z();
0N/A
0N/A // .. http://a/b/
0N/A test("..")
0N/A .p("..").z()
0N/A .rslv(base).s("http").h("a").p("/b/").z();
0N/A
0N/A // ../ http://a/b/
0N/A test("../")
0N/A .p("../").z()
0N/A .rslv(base).s("http").h("a").p("/b/").z();
0N/A
0N/A // ../g http://a/b/g
0N/A test("../g")
0N/A .p("../g").z()
0N/A .rslv(base).s("http").h("a").p("/b/g").z();
0N/A
0N/A // ../.. http://a/
0N/A test("../..")
0N/A .p("../..").z()
0N/A .rslv(base).s("http").h("a").p("/").z();
0N/A
0N/A // ../../ http://a/
0N/A test("../../")
0N/A .p("../../").z()
0N/A .rslv(base).s("http").h("a").p("/").z();
0N/A
0N/A // ../../g http://a/g
0N/A test("../../g")
0N/A .p("../../g").z()
0N/A .rslv(base).s("http").h("a").p("/g").z();
0N/A
0N/A
0N/A header("RFC2396: Abnormal relative-URI examples (appendix C)");
0N/A
0N/A // ../../../g = http://a/../g
0N/A test("../../../g")
0N/A .p("../../../g").z()
0N/A .rslv(base).s("http").h("a").p("/../g").z();
0N/A
0N/A // ../../../../g = http://a/../../g
0N/A test("../../../../g")
0N/A .p("../../../../g").z()
0N/A .rslv(base).s("http").h("a").p("/../../g").z();
0N/A
0N/A
0N/A // /./g = http://a/./g
0N/A test("/./g")
0N/A .p("/./g").z()
0N/A .rslv(base).s("http").h("a").p("/./g").z();
0N/A
0N/A // /../g = http://a/../g
0N/A test("/../g")
0N/A .p("/../g").z()
0N/A .rslv(base).s("http").h("a").p("/../g").z();
0N/A
0N/A // g. = http://a/b/c/g.
0N/A test("g.")
0N/A .p("g.").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g.").z();
0N/A
0N/A // .g = http://a/b/c/.g
0N/A test(".g")
0N/A .p(".g").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/.g").z();
0N/A
0N/A // g.. = http://a/b/c/g..
0N/A test("g..")
0N/A .p("g..").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g..").z();
0N/A
0N/A // ..g = http://a/b/c/..g
0N/A test("..g")
0N/A .p("..g").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/..g").z();
0N/A
0N/A // ./../g = http://a/b/g
0N/A test("./../g")
0N/A .p("./../g").z()
0N/A .rslv(base).s("http").h("a").p("/b/g").z();
0N/A
0N/A // ./g/. = http://a/b/c/g/
0N/A test("./g/.")
0N/A .p("./g/.").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g/").z();
0N/A
0N/A // g/./h = http://a/b/c/g/h
0N/A test("g/./h")
0N/A .p("g/./h").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g/h").z();
0N/A
0N/A // g/../h = http://a/b/c/h
0N/A test("g/../h")
0N/A .p("g/../h").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/h").z();
0N/A
0N/A // g;x=1/./y = http://a/b/c/g;x=1/y
0N/A test("g;x=1/./y")
0N/A .p("g;x=1/./y").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g;x=1/y").z();
0N/A
0N/A // g;x=1/../y = http://a/b/c/y
0N/A test("g;x=1/../y")
0N/A .p("g;x=1/../y").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/y").z();
0N/A
0N/A // g?y/./x = http://a/b/c/g?y/./x
0N/A test("g?y/./x")
0N/A .p("g").q("y/./x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").q("y/./x").z();
0N/A
0N/A // g?y/../x = http://a/b/c/g?y/../x
0N/A test("g?y/../x")
0N/A .p("g").q("y/../x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").q("y/../x").z();
0N/A
0N/A // g#s/./x = http://a/b/c/g#s/./x
0N/A test("g#s/./x")
0N/A .p("g").f("s/./x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").f("s/./x").z();
0N/A
0N/A // g#s/../x = http://a/b/c/g#s/../x
0N/A test("g#s/../x")
0N/A .p("g").f("s/../x").z()
0N/A .rslv(base).s("http").h("a").p("/b/c/g").f("s/../x").z();
0N/A
0N/A // http:g = http:g
0N/A test("http:g")
0N/A .s("http").o("g").z()
0N/A .rslv(base).s("http").o("g").z();
0N/A
0N/A }
0N/A
0N/A
0N/A static void ip() {
0N/A
0N/A header("IP addresses");
0N/A
0N/A test("http://1.2.3.4:5")
0N/A .s("http").h("1.2.3.4").n(5).p("").z();
0N/A
0N/A // From RFC2732
0N/A
0N/A test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")
0N/A .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")
0N/A .n(80).p("/index.html").z();
0N/A
0N/A test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]:80/index.html")
0N/A .s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]")
0N/A .n(80).p("/index.html").z();
0N/A
0N/A test("http://[1080:0:0:0:8:800:200C:417A]/index.html")
0N/A .s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();
0N/A
0N/A test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")
0N/A .s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();
0N/A
0N/A test("http://[3ffe:2a00:100:7031::1]")
0N/A .s("http").h("[3ffe:2a00:100:7031::1]").p("").z();
0N/A
0N/A test("http://[1080::8:800:200C:417A]/foo")
0N/A .s("http").h("[1080::8:800:200C:417A]").p("/foo").z();
0N/A
0N/A test("http://[::192.9.5.5]/ipng")
0N/A .s("http").h("[::192.9.5.5]").p("/ipng").z();
0N/A
0N/A test("http://[::192.9.5.5%interface]/ipng")
0N/A .s("http").h("[::192.9.5.5%interface]").p("/ipng").z();
0N/A
0N/A test("http://[::FFFF:129.144.52.38]:80/index.html")
0N/A .s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();
0N/A
0N/A test("http://[2010:836B:4179::836B:4179]")
0N/A .s("http").h("[2010:836B:4179::836B:4179]").p("").z();
0N/A
0N/A // From RFC2373
0N/A
0N/A test("http://[FF01::101]")
0N/A .s("http").h("[FF01::101]").p("").z();
0N/A
0N/A test("http://[::1]")
0N/A .s("http").h("[::1]").p("").z();
0N/A
0N/A test("http://[::]")
0N/A .s("http").h("[::]").p("").z();
0N/A
0N/A test("http://[::%hme0]")
0N/A .s("http").h("[::%hme0]").p("").z();
0N/A
0N/A test("http://[0:0:0:0:0:0:13.1.68.3]")
0N/A .s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();
0N/A
0N/A test("http://[0:0:0:0:0:FFFF:129.144.52.38]")
0N/A .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();
0N/A
0N/A test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")
0N/A .s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();
0N/A
0N/A test("http://[0:0:0:0:0:ffff:1.2.3.4]")
0N/A .s("http").h("[0:0:0:0:0:ffff:1.2.3.4]").p("").z();
0N/A
0N/A test("http://[::13.1.68.3]")
0N/A .s("http").h("[::13.1.68.3]").p("").z();
0N/A
0N/A // Optional IPv6 brackets in constructors
0N/A
0N/A test("s", null, "1:2:3:4:5:6:7:8", -1, null, null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", null, "[1:2:3:4:5:6:7:8]", -1, null, null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", "1:2:3:4:5:6:7:8", null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", "1:2:3:4:5:6:7:8%hme0", null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();
0N/A
0N/A test("s", "1:2:3:4:5:6:7:8%1", null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8%1]").p("").z();
0N/A
0N/A test("s", "[1:2:3:4:5:6:7:8]", null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", "[1:2:3:4:5:6:7:8]", null, null, null)
0N/A .s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
0N/A
0N/A test("s", "1:2:3:4:5:6:7:8", null, null, null)
0N/A .s("s").g("1:2:3:4:5:6:7:8").p("").z();
0N/A
0N/A // Error cases
0N/A
0N/A test("http://[ff01:234/foo").x().z();
0N/A test("http://[ff01:234:zzz]/foo").x().z();
0N/A test("http://[foo]").x().z();
0N/A test("http://[]").x().z();
0N/A test("http://[129.33.44.55]").x().z();
0N/A test("http://[ff:ee:dd:cc:bb::aa:9:8]").x().z();
0N/A test("http://[fffff::1]").x().z();
0N/A test("http://[ff::ee::8]").x().z();
0N/A test("http://[1:2:3:4::5:6:7:8]").x().z();
0N/A test("http://[1:2]").x().z();
0N/A test("http://[1:2:3:4:5:6:7:8:9]").x().z();
0N/A test("http://[1:2:3:4:5:6:7:8%]").x().z();
0N/A test("http://[1:2:3:4:5:6:7:8%!/]").x().z();
0N/A test("http://[::1.2.3.300]").x().z();
0N/A test("http://1.2.3").psa().x().z();
0N/A test("http://1.2.3.300").psa().x().z();
0N/A test("http://1.2.3.4.5").psa().x().z();
0N/A test("http://[1.2.3.4:5]").x().z();
0N/A test("http://1:2:3:4:5:6:7:8").psa().x().z();
0N/A test("http://[1.2.3.4]/").x().z();
0N/A test("http://[1.2.3.4/").x().z();
0N/A test("http://[foo]/").x().z();
0N/A test("http://[foo/").x().z();
0N/A test("s", "[foo]", "/", null, null).x().z();
0N/A test("s", "[foo", "/", null, null).x().z();
0N/A test("s", "[::foo", "/", null, null).x().z();
0N/A
0N/A // Test hostnames that might initially look like IPv4 addresses
0N/A
0N/A test("s://1.2.3.com").psa().s("s").h("1.2.3.com").p("").z();
0N/A test("s://1.2.3.4me.com").psa().s("s").h("1.2.3.4me.com").p("").z();
0N/A
0N/A test("s://7up.com").psa().s("s").h("7up.com").p("").z();
0N/A test("s://7up.com/p").psa().s("s").h("7up.com").p("/p").z();
0N/A test("s://7up").psa().s("s").h("7up").p("").z();
0N/A test("s://7up/p").psa().s("s").h("7up").p("/p").z();
0N/A test("s://7up.").psa().s("s").h("7up.").p("").z();
0N/A test("s://7up./p").psa().s("s").h("7up.").p("/p").z();
0N/A }
0N/A
0N/A
0N/A static void misc() throws URISyntaxException {
0N/A
0N/A URI base = new URI("s://h/a/b");
0N/A URI rbase = new URI("a/b/c/d");
0N/A
0N/A
0N/A header("Corner cases");
0N/A
0N/A // The empty URI parses as a relative URI with an empty path
0N/A test("").p("").z()
0N/A .rslv(base).s("s").h("h").p("/a/").z();
0N/A
0N/A // Resolving solo queries and fragments
0N/A test("#f").p("").f("f").z()
0N/A .rslv(base).s("s").h("h").p("/a/b").f("f").z();
0N/A test("?q").p("").q("q").z()
0N/A .rslv(base).s("s").h("h").p("/a/").q("q").z();
0N/A
0N/A // Fragment is not part of ssp
0N/A test("p#f").p("p").f("f").sp("p").z();
0N/A test("s:p#f").s("s").o("p").f("f").z();
0N/A test("p#f")
0N/A .rslv(base).s("s").h("h").p("/a/p").f("f").sp("//h/a/p").z();
0N/A test("").p("").sp("").z();
0N/A
0N/A
0N/A
0N/A header("Emptiness");
0N/A
0N/A // Components that may be empty
0N/A test("///p").p("/p").z(); // Authority (w/ path)
0N/A test("//@h/p").u("").h("h").p("/p").z(); // User info
0N/A test("//h:/p").h("h").p("/p").z(); // Port
0N/A test("//h").h("h").p("").z(); // Path
0N/A test("//h?q").h("h").p("").q("q").z(); // Path (w/query)
0N/A test("//?q").p("").q("q").z(); // Authority (w/query)
0N/A test("//#f").p("").f("f").z(); // Authority (w/fragment)
0N/A test("p?#").p("p").q("").f("").z(); // Query & fragment
0N/A
0N/A // Components that may not be empty
0N/A test(":").x().z(); // Scheme
0N/A test("x:").x().z(); // Hier/opaque
0N/A test("//").x().z(); // Authority (w/o path)
0N/A
0N/A
0N/A header("Resolution, normalization, and relativization");
0N/A
0N/A // Resolving relative paths
0N/A test("../e/f").p("../e/f").z()
0N/A .rslv(rbase).p("a/b/e/f").z();
0N/A test("../../../../d").p("../../../../d").z()
0N/A .rslv(rbase).p("../d").z();
0N/A test("../../../d:e").p("../../../d:e").z()
0N/A .rslv(rbase).p("./d:e").z();
0N/A test("../../../d:e/f").p("../../../d:e/f").z()
0N/A .rslv(rbase).p("./d:e/f").z();
0N/A
0N/A // Normalization
0N/A test("a/./c/../d/f").p("a/./c/../d/f").z()
0N/A .norm().p("a/d/f").z();
0N/A test("http://a/./b/c/../d?q#f")
0N/A .s("http").h("a").p("/./b/c/../d").q("q").f("f").z()
0N/A .norm().s("http").h("a").p("/b/d").q("q").f("f").z();
0N/A test("a/../b").p("a/../b").z().
0N/A norm().p("b");
0N/A test("a/../b:c").p("a/../b:c").z()
0N/A .norm().p("./b:c").z();
0N/A
0N/A // Normalization of already normalized URI should yield the
0N/A // same URI
0N/A URI u1 = URI.create("s://h/../p");
0N/A URI u2 = u1.normalize();
0N/A eq(u1, u2);
0N/A eqeq(u1, u2);
0N/A
0N/A // Relativization
0N/A test("/a/b").p("/a/b").z()
0N/A .rtvz(new URI("/a")).p("b").z();
0N/A test("/a/b").p("/a/b").z()
0N/A .rtvz(new URI("/a/")).p("b").z();
0N/A test("a/b").p("a/b").z()
0N/A .rtvz(new URI("a")).p("b").z();
0N/A test("/a/b").p("/a/b").z()
0N/A .rtvz(new URI("/a/b")).p("").z(); // Result is empty path
0N/A test("a/../b:c/d").p("a/../b:c/d").z()
0N/A .rtvz(new URI("./b:c/")).p("d").z();
0N/A
0N/A test("http://a/b/d/e?q#f")
0N/A .s("http").h("a").p("/b/d/e").q("q").f("f").z()
0N/A .rtvz(new URI("http://a/b/?r#g"))
0N/A .p("d/e").q("q").f("f").z();
0N/A
0N/A // parseServerAuthority
0N/A test("/a/b").psa().p("/a/b").z();
0N/A test("s://u@h:1/p")
0N/A .psa().s("s").u("u").h("h").n(1).p("/p").z();
0N/A test("s://u@h:-foo/p").s("s").g("u@h:-foo").p("/p").z()
0N/A .psa().x().z();
0N/A test("s://h:999999999999999999999999").psa().x().z();
0N/A test("s://:/b").psa().x().z();
0N/A
0N/A
0N/A header("Constructors and factories");
0N/A
0N/A test("s", null, null, -1, "p", null, null).x().z();
0N/A test(null, null, null, -1, null, null, null).p("").z();
0N/A test(null, null, null, -1, "p", null, null).p("p").z();
0N/A test(null, null, "foo%20bar", -1, null, null, null).x().z();
0N/A test(null, null, "foo", -100, null, null, null).x().z();
0N/A test("s", null, null, -1, "", null, null).x().z();
0N/A test("s", null, null, -1, "/p", null, null).s("s").p("/p").z();
0N/A test("s", "u", "h", 10, "/p", "q", "f")
0N/A .s("s").u("u").h("h").n(10).p("/p").q("q").f("f").z();
0N/A test("s", "a:b", "/p", "q", "f")
0N/A .s("s").g("a:b").p("/p").q("q").f("f").z();
0N/A test("s", "h", "/p", "f")
0N/A .s("s").h("h").p("/p").f("f").z();
0N/A test("s", "p", "f").s("s").o("p").f("f").z();
0N/A test("s", "/p", "f").s("s").p("/p").f("f").z();
0N/A testCreate("s://u@h/p?q#f")
0N/A .s("s").u("u").h("h").p("/p").q("q").f("f").z();
0N/A }
0N/A
0N/A static void npes() throws URISyntaxException {
0N/A
0N/A header("NullPointerException");
0N/A
0N/A URI base = URI.create("mailto:root@foobar.com");
0N/A
0N/A out.println();
0N/A
0N/A try {
0N/A base.resolve((URI)null);
0N/A throw new RuntimeException("NullPointerException not thrown");
0N/A } catch (NullPointerException x) {
0N/A out.println("resolve((URI)null) -->");
0N/A out.println("Correct exception: " + x);
0N/A }
0N/A
0N/A out.println();
0N/A
0N/A try {
0N/A base.resolve((String)null);
0N/A throw new RuntimeException("NullPointerException not thrown");
0N/A } catch (NullPointerException x) {
0N/A out.println("resolve((String)null) -->");
0N/A out.println("Correct exception: " + x);
0N/A }
0N/A
0N/A out.println();
0N/A
0N/A try {
0N/A base.relativize((URI)null);
0N/A throw new RuntimeException("NullPointerException not thrown");
0N/A } catch (NullPointerException x) {
0N/A out.println("relativize((String)null) -->");
0N/A out.println("Correct exception: " + x);
0N/A }
0N/A
0N/A testCount += 3;
0N/A }
0N/A
0N/A
0N/A static void chars() throws URISyntaxException {
0N/A
0N/A header("Escapes and non-US-ASCII characters");
0N/A
0N/A URI uri;
0N/A
0N/A // Escape pairs
0N/A test("%0a%0A%0f%0F%01%09zz")
0N/A .p("%0a%0A%0f%0F%01%09zz").z();
0N/A test("foo%1").x().z();
0N/A test("foo%z").x().z();
0N/A test("foo%9z").x().z();
0N/A
0N/A // Escapes not permitted in scheme, host
0N/A test("s%20t://a").x().z();
0N/A test("//a%20b").g("a%20b").p("").z(); // Parses as registry
0N/A
0N/A // Escapes permitted in opaque part, userInfo, registry, path,
0N/A // query, and fragment
0N/A test("//u%20v@a").u("u%20v").h("a").p("").z();
0N/A test("/p%20q").p("/p%20q").z();
0N/A test("/p?q%20").p("/p").q("q%20").z();
0N/A test("/p#%20f").p("/p").f("%20f").z();
0N/A
0N/A // Non-US-ASCII chars
0N/A test("s\u00a7t://a").x().z();
0N/A test("//\u00a7/b").g("\u00a7").p("/b").z(); // Parses as registry
0N/A test("//u\u00a7v@a").u("u\u00a7v").h("a").p("").z();
0N/A test("/p\u00a7q").p("/p\u00a7q").z();
0N/A test("/p?q\u00a7").p("/p").q("q\u00a7").z();
0N/A test("/p#\u00a7f").p("/p").f("\u00a7f").z();
0N/A
0N/A // 4648111 - Escapes quoted by toString after resolution
0N/A uri = new URI("http://a/b/c/d;p?q");
0N/A test("/p%20p")
0N/A .rslv(uri).s("http").h("a").p("/p%20p").ts("http://a/p%20p").z();
0N/A
0N/A // 4464135: Forbid unwise characters throughout opaque part
0N/A test("foo:x{bar").x().z();
0N/A test("foo:{bar").x().z();
0N/A
0N/A // 4438319: Single-argument constructor requires quotation,
0N/A // preserves escapes
0N/A test("//u%01@h/a/b/%02/c?q%03#f%04")
0N/A .u("u%01").ud("u\1")
0N/A .h("h")
0N/A .p("/a/b/%02/c").pd("/a/b/\2/c")
0N/A .q("q%03").qd("q\3")
0N/A .f("f%04").fd("f\4")
0N/A .z();
0N/A test("/a/b c").x().z();
0N/A
0N/A // 4438319: Multi-argument constructors quote illegal chars and
0N/A // preserve legal non-ASCII chars
0N/A // \uA001-\uA009 are visible characters, \u2000 is a space character
0N/A test(null, "u\uA001\1", "h", -1,
0N/A "/p% \uA002\2\u2000",
0N/A "q% \uA003\3\u2000",
0N/A "f% \uA004\4\u2000")
0N/A .u("u\uA001%01").h("h")
0N/A .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
0N/A .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")
0N/A .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
0N/A test(null, "g\uA001\1",
0N/A "/p% \uA002\2\u2000",
0N/A "q% \uA003\3\u2000",
0N/A "f% \uA004\4\u2000")
0N/A .g("g\uA001%01")
0N/A .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
0N/A .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000")
0N/A .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
0N/A test(null, null, "/p% \uA002\2\u2000", "f% \uA004\4\u2000")
0N/A .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000")
0N/A .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
0N/A test(null, "/sp% \uA001\1\u2000", "f% \uA004\4\u2000")
0N/A .sp("/sp%25%20\uA001%01%E2%80%80").spd("/sp% \uA001\1\u2000")
0N/A .p("/sp%25%20\uA001%01%E2%80%80").pd("/sp% \uA001\1\u2000")
0N/A .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z();
0N/A
0N/A // 4438319: Non-raw accessors decode all escaped octets
0N/A test("/%25%20%E2%82%AC%E2%80%80")
0N/A .p("/%25%20%E2%82%AC%E2%80%80").pd("/% \u20Ac\u2000").z();
0N/A
0N/A // 4438319: toASCIIString
0N/A test("/\uCAFE\uBABE")
0N/A .p("/\uCAFE\uBABE").ta("/%EC%AB%BE%EB%AA%BE").z();
0N/A
0N/A // 4991359 and 4866303: bad quoting by defineSchemeSpecificPart()
0N/A URI base = new URI ("http://host/foo%20bar/a/b/c/d");
0N/A test ("resolve")
0N/A .rslv(base).spd("//host/foo bar/a/b/c/resolve")
0N/A .sp("//host/foo%20bar/a/b/c/resolve").s("http")
0N/A .pd("/foo bar/a/b/c/resolve").h("host")
0N/A .p("/foo%20bar/a/b/c/resolve").z();
0N/A
0N/A // 6773270: java.net.URI fails to escape u0000
0N/A test("s", "a", "/\u0000", null)
0N/A .s("s").p("/%00").h("a")
0N/A .ta("s://a/%00").z();
0N/A }
0N/A
0N/A
0N/A static void eq0(Comparable u, Comparable v) throws URISyntaxException {
0N/A testCount++;
0N/A if (!u.equals(v))
0N/A throw new RuntimeException("Not equal: " + u + " " + v);
0N/A int uh = u.hashCode();
0N/A int vh = v.hashCode();
0N/A if (uh != vh)
0N/A throw new RuntimeException("Hash codes not equal: "
0N/A + u + " " + Integer.toHexString(uh) + " "
0N/A + v + " " + Integer.toHexString(vh));
0N/A out.println();
0N/A out.println(u + " == " + v
0N/A + " [" + Integer.toHexString(uh) + "]");
0N/A }
0N/A
0N/A static void cmp0(Comparable u, Comparable v, boolean same)
0N/A throws URISyntaxException
0N/A {
0N/A int c = u.compareTo(v);
0N/A if ((c == 0) != same)
0N/A throw new RuntimeException("Comparison inconsistent: " + u + " " + v
0N/A + " " + c);
0N/A }
0N/A
0N/A static void eq(Comparable u, Comparable v) throws URISyntaxException {
0N/A eq0(u, v);
0N/A cmp0(u, v, true);
0N/A }
0N/A
0N/A static void eqeq(Comparable u, Comparable v) {
0N/A testCount++;
0N/A if (u != v)
0N/A throw new RuntimeException("Not ==: " + u + " " + v);
0N/A }
0N/A
0N/A static void ne0(Comparable u, Comparable v) throws URISyntaxException {
0N/A testCount++;
0N/A if (u.equals(v))
0N/A throw new RuntimeException("Equal: " + u + " " + v);
0N/A out.println();
0N/A out.println(u + " != " + v
0N/A + " [" + Integer.toHexString(u.hashCode())
0N/A + " " + Integer.toHexString(v.hashCode())
0N/A + "]");
0N/A }
0N/A
0N/A static void ne(Comparable u, Comparable v) throws URISyntaxException {
0N/A ne0(u, v);
0N/A cmp0(u, v, false);
0N/A }
0N/A
0N/A static void lt(Comparable u, Comparable v) throws URISyntaxException {
0N/A ne0(u, v);
0N/A int c = u.compareTo(v);
0N/A if (c >= 0) {
0N/A show((URI)u);
0N/A show((URI)v);
0N/A throw new RuntimeException("Not less than: " + u + " " + v
0N/A + " " + c);
0N/A }
0N/A out.println(u + " < " + v);
0N/A }
0N/A
0N/A static void lt(String s, String t) throws URISyntaxException {
0N/A lt(new URI(s), new URI(t));
0N/A }
0N/A
0N/A static void gt(Comparable u, Comparable v) throws URISyntaxException {
0N/A lt(v, u);
0N/A }
0N/A
0N/A static void eqHashComp() throws URISyntaxException {
0N/A
0N/A header("Equality, hashing, and comparison");
0N/A
0N/A URI o = new URI("mailto:foo@bar.com");
0N/A URI r = new URI("reg://some%20registry/b/c/d?q#f");
0N/A URI s = new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#f");
0N/A eq(o, o);
0N/A lt(o, r);
0N/A lt(s, o);
0N/A lt(s, r);
0N/A eq(o, new URI("MaILto:foo@bar.com"));
0N/A gt(o, new URI("mailto:foo@bar.COM"));
0N/A eq(r, new URI("rEg://some%20registry/b/c/d?q#f"));
0N/A gt(r, new URI("reg://Some%20Registry/b/c/d?q#f"));
0N/A gt(r, new URI("reg://some%20registry/b/c/D?q#f"));
0N/A eq(s, new URI("hTtP://jag:cafebabe@Java.Sun.COM:94/b/c/d?q#f"));
0N/A gt(s, new URI("http://jag:CafeBabe@java.sun.com:94/b/c/d?q#f"));
0N/A lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?r#f"));
0N/A lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g"));
0N/A
0N/A lt("p", "s:p");
0N/A lt("s:p", "T:p");
0N/A lt("S:p", "t:p");
0N/A lt("s:/p", "s:p");
0N/A lt("s:p", "s:q");
0N/A lt("s:p#f", "s:p#g");
0N/A lt("s://u@h:1", "s://v@h:1");
0N/A lt("s://u@h:1", "s://u@i:1");
1338N/A lt("s://u@h:1", "s://v@h:2");
1338N/A lt("s://a%20b", "s://a%20c");
1338N/A lt("s://a%20b", "s://aab");
0N/A lt("s://AA", "s://A_");
0N/A lt("s:/p", "s:/q");
0N/A lt("s:/p?q", "s:/p?r");
0N/A lt("s:/p#f", "s:/p#g");
0N/A
0N/A lt("s://h", "s://h/p");
0N/A lt("s://h/p", "s://h/p?q");
0N/A
0N/A }
0N/A
0N/A
0N/A static void serial(URI u) throws IOException, URISyntaxException {
0N/A
0N/A ByteArrayOutputStream bo = new ByteArrayOutputStream();
0N/A ObjectOutputStream oo = new ObjectOutputStream(bo);
0N/A
0N/A oo.writeObject(u);
0N/A oo.close();
0N/A
0N/A ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
0N/A ObjectInputStream oi = new ObjectInputStream(bi);
0N/A try {
0N/A Object o = oi.readObject();
0N/A eq(u, (Comparable)o);
0N/A } catch (ClassNotFoundException x) {
0N/A x.printStackTrace();
0N/A throw new RuntimeException(x.toString());
0N/A }
0N/A
0N/A testCount++;
0N/A }
0N/A
0N/A static void serial() throws IOException, URISyntaxException {
0N/A header("Serialization");
0N/A
0N/A serial(URI.create("http://java.sun.com/jdk/1.4?release#beta"));
0N/A serial(URI.create("s://h/p").resolve("/long%20path/"));
0N/A }
0N/A
0N/A
0N/A static void urls() throws URISyntaxException {
0N/A
0N/A header("URLs");
0N/A
0N/A URI uri;
0N/A URL url;
0N/A boolean caught = false;
0N/A
0N/A out.println();
0N/A uri = new URI("http://a/p?q#f");
0N/A try {
0N/A url = uri.toURL();
0N/A } catch (MalformedURLException x) {
0N/A throw new RuntimeException(x.toString());
0N/A }
0N/A if (!url.toString().equals("http://a/p?q#f"))
0N/A throw new RuntimeException("Incorrect URL: " + url);
0N/A out.println(uri + " url --> " + url);
0N/A
0N/A out.println();
0N/A uri = new URI("a/b");
0N/A try {
0N/A out.println(uri + " url --> ");
0N/A url = uri.toURL();
0N/A } catch (IllegalArgumentException x) {
0N/A caught = true;
0N/A out.println("Correct exception: " + x);
0N/A } catch (MalformedURLException x) {
0N/A caught = true;
0N/A throw new RuntimeException("Incorrect exception: " + x);
0N/A }
0N/A if (!caught)
0N/A throw new RuntimeException("Incorrect URL: " + url);
0N/A
0N/A out.println();
0N/A uri = new URI("foo://bar/baz");
0N/A caught = false;
0N/A try {
0N/A out.println(uri + " url --> ");
0N/A url = uri.toURL();
0N/A } catch (MalformedURLException x) {
0N/A caught = true;
0N/A out.println("Correct exception: " + x);
0N/A } catch (IllegalArgumentException x) {
0N/A caught = true;
0N/A throw new RuntimeException("Incorrect exception: " + x);
0N/A }
0N/A if (!caught)
0N/A throw new RuntimeException("Incorrect URL: " + url);
0N/A
0N/A testCount += 3;
0N/A }
0N/A
0N/A
0N/A static void tests() throws IOException, URISyntaxException {
0N/A rfc2396();
0N/A ip();
0N/A misc();
0N/A chars();
0N/A eqHashComp();
0N/A serial();
0N/A urls();
0N/A npes();
0N/A bugs();
0N/A }
0N/A
0N/A
0N/A // -- Command-line invocation --
0N/A
0N/A static void usage() {
0N/A out.println("Usage:");
0N/A out.println(" java Test -- Runs all tests in this file");
0N/A out.println(" java Test <uri> -- Parses uri, shows components");
0N/A out.println(" java Test <base> <uri> -- Parses uri and base, then resolves");
0N/A out.println(" uri against base");
0N/A }
0N/A
0N/A static void clargs(String base, String uri) {
0N/A URI b = null, u;
0N/A try {
0N/A if (base != null) {
0N/A b = new URI(base);
0N/A out.println(base);
0N/A show(b);
0N/A }
0N/A u = new URI(uri);
0N/A out.println(uri);
0N/A show(u);
0N/A if (base != null) {
0N/A URI r = b.resolve(u);
0N/A out.println(r);
0N/A show(r);
0N/A }
0N/A } catch (URISyntaxException x) {
0N/A show("ERROR", x);
0N/A x.printStackTrace(out);
0N/A }
0N/A }
0N/A
0N/A
0N/A // miscellaneous bugs/rfes that don't fit in with the test framework
0N/A
0N/A static void bugs() {
0N/A // 6339649 - include detail message from nested exception
0N/A try {
0N/A URI uri = URI.create("http://nowhere.net/should not be permitted");
0N/A } catch (IllegalArgumentException e) {
0N/A if ("".equals(e.getMessage()) || e.getMessage() == null) {
0N/A throw new RuntimeException ("No detail message");
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A switch (args.length) {
2129N/A
2129N/A case 0:
2129N/A tests();
2129N/A out.println();
2129N/A out.println("Test cases: " + testCount);
2129N/A break;
0N/A
0N/A case 1:
0N/A if (args[0].equals("-help")) {
0N/A usage();
0N/A break;
0N/A }
0N/A clargs(null, args[0]);
0N/A break;
0N/A
0N/A case 2:
0N/A clargs(args[0], args[1]);
0N/A break;
0N/A
0N/A default:
0N/A usage();
0N/A break;
0N/A
0N/A }
0N/A }
0N/A
0N/A}
0N/A