📄 uritest.java
字号:
URI base = new URI("s://h/a/b"); URI rbase = new URI("a/b/c/d"); header("Corner cases"); // The empty URI parses as a relative URI with an empty path test("").p("").z() .rslv(base).s("s").h("h").p("/a/").z(); // Resolving solo queries and fragments test("#f").p("").f("f").z() .rslv(base).s("s").h("h").p("/a/b").f("f").z(); test("?q").p("").q("q").z() .rslv(base).s("s").h("h").p("/a/").q("q").z(); // Fragment is not part of ssp test("p#f").p("p").f("f").sp("p").z(); test("s:p#f").s("s").o("p").f("f").z(); test("p#f") .rslv(base).s("s").h("h").p("/a/p").f("f").sp("//h/a/p").z(); test("").p("").sp("").z(); header("Emptiness"); // Components that may be empty test("///p").p("/p").z(); // Authority (w/ path) test("//@h/p").u("").h("h").p("/p").z(); // User info test("//h:/p").h("h").p("/p").z(); // Port test("//h").h("h").p("").z(); // Path test("//h?q").h("h").p("").q("q").z(); // Path (w/query) test("//?q").p("").q("q").z(); // Authority (w/query) test("//#f").p("").f("f").z(); // Authority (w/fragment) test("p?#").p("p").q("").f("").z(); // Query & fragment // Components that may not be empty test(":").x().z(); // Scheme test("x:").x().z(); // Hier/opaque test("//").x().z(); // Authority (w/o path) header("Resolution, normalization, and relativization"); // Resolving relative paths test("../e/f").p("../e/f").z() .rslv(rbase).p("a/b/e/f").z(); test("../../../../d").p("../../../../d").z() .rslv(rbase).p("../d").z(); test("../../../d:e").p("../../../d:e").z() .rslv(rbase).p("./d:e").z(); test("../../../d:e/f").p("../../../d:e/f").z() .rslv(rbase).p("./d:e/f").z(); // Normalization test("a/./c/../d/f").p("a/./c/../d/f").z() .norm().p("a/d/f").z(); test("http://a/./b/c/../d?q#f") .s("http").h("a").p("/./b/c/../d").q("q").f("f").z() .norm().s("http").h("a").p("/b/d").q("q").f("f").z(); test("a/../b").p("a/../b").z(). norm().p("b"); test("a/../b:c").p("a/../b:c").z() .norm().p("./b:c").z(); // Normalization of already normalized URI should yield the // same URI URI u1 = URI.create("s://h/../p"); URI u2 = u1.normalize(); eq(u1, u2); eqeq(u1, u2); // Relativization test("/a/b").p("/a/b").z() .rtvz(new URI("/a")).p("b").z(); test("/a/b").p("/a/b").z() .rtvz(new URI("/a/")).p("b").z(); test("a/b").p("a/b").z() .rtvz(new URI("a")).p("b").z(); test("/a/b").p("/a/b").z() .rtvz(new URI("/a/b")).p("").z(); // Result is empty path test("a/../b:c/d").p("a/../b:c/d").z() .rtvz(new URI("./b:c/")).p("d").z(); test("http://a/b/d/e?q#f") .s("http").h("a").p("/b/d/e").q("q").f("f").z() .rtvz(new URI("http://a/b/?r#g")) .p("d/e").q("q").f("f").z(); // parseServerAuthority test("/a/b").psa().p("/a/b").z(); test("s://u@h:1/p") .psa().s("s").u("u").h("h").n(1).p("/p").z(); test("s://u@h:-foo/p").s("s").g("u@h:-foo").p("/p").z() .psa().x().z(); test("s://h:999999999999999999999999").psa().x().z(); test("s://:/b").psa().x().z(); header("Constructors and factories"); test("s", null, null, -1, "p", null, null).x().z(); test(null, null, null, -1, null, null, null).p("").z(); test(null, null, null, -1, "p", null, null).p("p").z(); test(null, null, "foo%20bar", -1, null, null, null).x().z(); test(null, null, "foo", -100, null, null, null).x().z(); test("s", null, null, -1, "", null, null).x().z(); test("s", null, null, -1, "/p", null, null).s("s").p("/p").z(); test("s", "u", "h", 10, "/p", "q", "f") .s("s").u("u").h("h").n(10).p("/p").q("q").f("f").z(); test("s", "a:b", "/p", "q", "f") .s("s").g("a:b").p("/p").q("q").f("f").z(); test("s", "h", "/p", "f") .s("s").h("h").p("/p").f("f").z(); test("s", "p", "f").s("s").o("p").f("f").z(); test("s", "/p", "f").s("s").p("/p").f("f").z(); testCreate("s://u@h/p?q#f") .s("s").u("u").h("h").p("/p").q("q").f("f").z(); } static void npes() throws URISyntaxException { header("NullPointerException"); URI base = URI.create("mailto:root@foobar.com"); out.println(); try { base.resolve((URI)null); throw new RuntimeException("NullPointerException not thrown"); } catch (NullPointerException x) { out.println("resolve((URI)null) -->"); out.println("Correct exception: " + x); } out.println(); try { base.resolve((String)null); throw new RuntimeException("NullPointerException not thrown"); } catch (NullPointerException x) { out.println("resolve((String)null) -->"); out.println("Correct exception: " + x); } out.println(); try { base.relativize((URI)null); throw new RuntimeException("NullPointerException not thrown"); } catch (NullPointerException x) { out.println("relativize((String)null) -->"); out.println("Correct exception: " + x); } testCount += 3; } static void chars() throws URISyntaxException { header("Escapes and non-US-ASCII characters"); URI uri; // Escape pairs test("%0a%0A%0f%0F%01%09zz") .p("%0a%0A%0f%0F%01%09zz").z(); test("foo%1").x().z(); test("foo%z").x().z(); test("foo%9z").x().z(); // Escapes not permitted in scheme, host test("s%20t://a").x().z(); test("//a%20b").g("a%20b").p("").z(); // Parses as registry // Escapes permitted in opaque part, userInfo, registry, path, // query, and fragment test("//u%20v@a").u("u%20v").h("a").p("").z(); test("/p%20q").p("/p%20q").z(); test("/p?q%20").p("/p").q("q%20").z(); test("/p#%20f").p("/p").f("%20f").z(); // Non-US-ASCII chars test("s\u00a7t://a").x().z(); test("//\u00a7/b").g("\u00a7").p("/b").z(); // Parses as registry test("//u\u00a7v@a").u("u\u00a7v").h("a").p("").z(); test("/p\u00a7q").p("/p\u00a7q").z(); test("/p?q\u00a7").p("/p").q("q\u00a7").z(); test("/p#\u00a7f").p("/p").f("\u00a7f").z(); // 4648111 - Escapes quoted by toString after resolution uri = new URI("http://a/b/c/d;p?q"); test("/p%20p") .rslv(uri).s("http").h("a").p("/p%20p").ts("http://a/p%20p").z(); // 4464135: Forbid unwise characters throughout opaque part test("foo:x{bar").x().z(); test("foo:{bar").x().z(); // 4438319: Single-argument constructor requires quotation, // preserves escapes test("//u%01@h/a/b/%02/c?q%03#f%04") .u("u%01").ud("u\1") .h("h") .p("/a/b/%02/c").pd("/a/b/\2/c") .q("q%03").qd("q\3") .f("f%04").fd("f\4") .z(); test("/a/b c").x().z(); // 4438319: Multi-argument constructors quote illegal chars and // preserve legal non-ASCII chars // \uA001-\uA009 are visible characters, \u2000 is a space character test(null, "u\uA001\1", "h", -1, "/p% \uA002\2\u2000", "q% \uA003\3\u2000", "f% \uA004\4\u2000") .u("u\uA001%01").h("h") .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000") .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000") .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z(); test(null, "g\uA001\1", "/p% \uA002\2\u2000", "q% \uA003\3\u2000", "f% \uA004\4\u2000") .g("g\uA001%01") .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000") .q("q%25%20\uA003%03%E2%80%80").qd("q% \uA003\3\u2000") .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z(); test(null, null, "/p% \uA002\2\u2000", "f% \uA004\4\u2000") .p("/p%25%20\uA002%02%E2%80%80").pd("/p% \uA002\2\u2000") .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z(); test(null, "/sp% \uA001\1\u2000", "f% \uA004\4\u2000") .sp("/sp%25%20\uA001%01%E2%80%80").spd("/sp% \uA001\1\u2000") .p("/sp%25%20\uA001%01%E2%80%80").pd("/sp% \uA001\1\u2000") .f("f%25%20\uA004%04%E2%80%80").fd("f% \uA004\4\u2000").z(); // 4438319: Non-raw accessors decode all escaped octets test("/%25%20%E2%82%AC%E2%80%80") .p("/%25%20%E2%82%AC%E2%80%80").pd("/% \u20Ac\u2000").z(); // 4438319: toASCIIString test("/\uCAFE\uBABE") .p("/\uCAFE\uBABE").ta("/%EC%AB%BE%EB%AA%BE").z(); } static void eq0(Comparable u, Comparable v) throws URISyntaxException { testCount++; if (!u.equals(v)) throw new RuntimeException("Not equal: " + u + " " + v); int uh = u.hashCode(); int vh = v.hashCode(); if (uh != vh) throw new RuntimeException("Hash codes not equal: " + u + " " + Integer.toHexString(uh) + " " + v + " " + Integer.toHexString(vh)); out.println(); out.println(u + " == " + v + " [" + Integer.toHexString(uh) + "]"); } static void cmp0(Comparable u, Comparable v, boolean same) throws URISyntaxException { int c = u.compareTo(v); if ((c == 0) != same) throw new RuntimeException("Comparison inconsistent: " + u + " " + v + " " + c); } static void eq(Comparable u, Comparable v) throws URISyntaxException { eq0(u, v); cmp0(u, v, true); } static void eqeq(Comparable u, Comparable v) { testCount++; if (u != v) throw new RuntimeException("Not ==: " + u + " " + v); } static void ne0(Comparable u, Comparable v) throws URISyntaxException { testCount++; if (u.equals(v)) throw new RuntimeException("Equal: " + u + " " + v); out.println(); out.println(u + " != " + v + " [" + Integer.toHexString(u.hashCode()) + " " + Integer.toHexString(v.hashCode()) + "]"); } static void ne(Comparable u, Comparable v) throws URISyntaxException { ne0(u, v); cmp0(u, v, false); } static void lt(Comparable u, Comparable v) throws URISyntaxException { ne0(u, v); int c = u.compareTo(v); if (c >= 0) { show((URI)u); show((URI)v); throw new RuntimeException("Not less than: " + u + " " + v + " " + c); } out.println(u + " < " + v); } static void lt(String s, String t) throws URISyntaxException { lt(new URI(s), new URI(t)); } static void gt(Comparable u, Comparable v) throws URISyntaxException { lt(v, u); } static void eqHashComp() throws URISyntaxException { header("Equality, hashing, and comparison"); URI o = new URI("mailto:foo@bar.com"); URI r = new URI("reg://some%20registry/b/c/d?q#f"); URI s = new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#f"); eq(o, o); lt(o, r); lt(s, o); lt(s, r); eq(o, new URI("MaILto:foo@bar.com")); gt(o, new URI("mailto:foo@bar.COM")); eq(r, new URI("rEg://some%20registry/b/c/d?q#f")); gt(r, new URI("reg://Some%20Registry/b/c/d?q#f")); gt(r, new URI("reg://some%20registry/b/c/D?q#f")); eq(s, new URI("hTtP://jag:cafebabe@Java.Sun.COM:94/b/c/d?q#f")); gt(s, new URI("http://jag:CafeBabe@java.sun.com:94/b/c/d?q#f")); lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?r#f")); lt(s, new URI("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g")); lt("p", "s:p"); lt("s:p", "T:p"); lt("S:p", "t:p"); lt("s:/p", "s:p"); lt("s:p", "s:q"); lt("s:p#f", "s:p#g"); lt("s://u@h:1", "s://v@h:1"); lt("s://u@h:1", "s://u@i:1"); lt("s://u@h:1", "s://v@h:2"); lt("s://a%20b", "s://a%20c"); lt("s://a%20b", "s://aab"); lt("s://AA", "s://A_"); lt("s:/p", "s:/q"); lt("s:/p?q", "s:/p?r"); lt("s:/p#f", "s:/p#g"); lt("s://h", "s://h/p"); lt("s://h/p", "s://h/p?q"); } static void serial(URI u) throws IOException, URISyntaxException { ByteArrayOutputStream bo = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(bo); oo.writeObject(u); oo.close(); ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray()); ObjectInputStream oi = new ObjectInputStream(bi); try { Object o = oi.readObject(); eq(u, (Comparable)o); } catch (ClassNotFoundException x) { x.printStackTrace(); throw new RuntimeException(x.toString()); } testCount++; } static void serial() throws IOException, URISyntaxException { header("Serialization"); serial(URI.create("http://java.sun.com/jdk/1.4?release#beta")); serial(URI.create("s://h/p").resolve("/long%20path/")); } static void urls() throws URISyntaxException { header("URLs"); URI uri; URL url; boolean caught = false; out.println(); uri = new URI("http://a/p?q#f"); try { url = uri.toURL(); } catch (MalformedURLException x) { throw new RuntimeException(x.toString()); } if (!url.toString().equals("http://a/p?q#f")) throw new RuntimeException("Incorrect URL: " + url); out.println(uri + " url --> " + url); out.println(); uri = new URI("a/b"); try { out.println(uri + " url --> "); url = uri.toURL(); } catch (IllegalArgumentException x) { caught = true; out.println("Correct exception: " + x); } catch (MalformedURLException x) { caught = true; throw new RuntimeException("Incorrect exception: " + x); } if (!caught) throw new RuntimeException("Incorrect URL: " + url); out.println(); uri = new URI("foo://bar/baz"); caught = false; try { out.println(uri + " url --> "); url = uri.toURL(); } catch (MalformedURLException x) { caught = true; out.println("Correct exception: " + x); } catch (IllegalArgumentException x) { caught = true; throw new RuntimeException("Incorrect exception: " + x); } if (!caught) throw new RuntimeException("Incorrect URL: " + url); testCount += 3; } static void tests() throws IOException, URISyntaxException { rfc2396(); ip(); misc(); chars(); eqHashComp(); serial(); urls(); npes(); } // -- Command-line invocation -- static void usage() { out.println("Usage:"); out.println(" java URITest -- Runs all tests in this file"); out.println(" java URITest <uri> -- Parses uri, shows components"); out.println(" java URITest <base> <uri> -- Parses uri and base, then resolves"); out.println(" uri against base"); } static void clargs(String base, String uri) { URI b = null, u; try { if (base != null) { b = new URI(base); out.println(base); show(b); } u = new URI(uri); out.println(uri); show(u); if (base != null) { URI r = b.resolve(u); out.println(r); show(r); } } catch (URISyntaxException x) { show("ERROR", x); x.printStackTrace(out); } } public static void main(String[] args) throws Exception { switch (args.length) { case 0: tests(); out.println(); out.println("URITest cases: " + testCount); break; case 1: if (args[0].equals("-help")) { usage(); break; } clargs(null, args[0]); break; case 2: clargs(args[0], args[1]); break; default: usage(); break; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -