📄 uritest.java
字号:
/* * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//* @test @(#)URITest.java 1.23 06/10/10 * @summary Unit test for java.net.URI * @bug 4464135 4505046 4503239 4438319 * @author Mark Reinhold */import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.PrintStream;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.net.MalformedURLException;public class URITest { static PrintStream out = System.out; static int testCount = 0; // Properties that we check static final int PARSEFAIL = 1 << 0; static final int SCHEME = 1 << 1; static final int SSP = 1 << 2; static final int SSP_D = 1 << 3; // Decoded form static final int OPAQUEPART = 1 << 4; // SSP, and URI is opaque static final int USERINFO = 1 << 5; static final int USERINFO_D = 1 << 6; // Decoded form static final int HOST = 1 << 7; static final int PORT = 1 << 8; static final int REGISTRY = 1 << 9; static final int REGISTRY_D = 1 << 10; // Decoded form static final int PATH = 1 << 11; static final int PATH_D = 1 << 12; // Decoded form static final int QUERY = 1 << 13; static final int QUERY_D = 1 << 14; // Decoded form static final int FRAGMENT = 1 << 15; static final int FRAGMENT_D = 1 << 16; // Decoded form static final int TOASCII = 1 << 17; static final int IDENT_STR = 1 << 18; // Identities static final int IDENT_URI1 = 1 << 19; static final int IDENT_URI3 = 1 << 20; static final int IDENT_URI5 = 1 << 21; static final int IDENT_URI7 = 1 << 22; static final int TOSTRING = 1 << 23; String input; URI uri = null; URI originalURI; URI base = null; // Base for resolution/relativization String op = null; // Op performed if uri != originalURI int checked = 0; // Mask for checked properties int failed = 0; // Mask for failed properties Exception exc = null; private URITest(String s) { testCount++; input = s; try { uri = new URI(s); } catch (URISyntaxException x) { exc = x; } originalURI = uri; } static URITest test(String s) { return new URITest(s); } private URITest(String s, String u, String h, int n, String p, String q, String f) { testCount++; try { uri = new URI(s, u, h, n, p, q, f); } catch (URISyntaxException x) { exc = x; input = x.getInput(); } if (uri != null) input = uri.toString(); originalURI = uri; } static URITest test(String s, String u, String h, int n, String p, String q, String f) { return new URITest(s, u, h, n, p, q, f); } private URITest(String s, String a, String p, String q, String f) { testCount++; try { uri = new URI(s, a, p, q, f); } catch (URISyntaxException x) { exc = x; input = x.getInput(); } if (uri != null) input = uri.toString(); originalURI = uri; } static URITest test(String s, String a, String p, String q, String f) { return new URITest(s, a, p, q, f); } private URITest(String s, String h, String p, String f) { testCount++; try { uri = new URI(s, h, p, f); } catch (URISyntaxException x) { exc = x; input = x.getInput(); } if (uri != null) input = uri.toString(); originalURI = uri; } static URITest test(String s, String h, String p, String f) { return new URITest(s, h, p, f); } private URITest(String s, String ssp, String f) { testCount++; try { uri = new URI(s, ssp, f); } catch (URISyntaxException x) { exc = x; input = x.getInput(); } if (uri != null) input = uri.toString(); originalURI = uri; } static URITest test(String s, String ssp, String f) { return new URITest(s, ssp, f); } private URITest(String s, boolean xxx) { testCount++; try { uri = URI.create(s); } catch (IllegalArgumentException x) { exc = x; } if (uri != null) input = uri.toString(); originalURI = uri; } static URITest testCreate(String s) { return new URITest(s, false); } boolean parsed() { return uri != null; } boolean resolved() { return base != null; } URI uri() { return uri; } // Operations on URITest instances // // These are short so as to make test cases compact. // // s Scheme // sp Scheme-specific part // spd Scheme-specific part, decoded // o Opaque part (isOpaque() && ssp matches) // g reGistry (authority matches, and host is not defined) // gd reGistry, decoded // u User info // ud User info, decoded // h Host // n port Number // p Path // pd Path, decoded // q Query // qd Query, decoded // f Fragment // fd Fragment, decoded // // rslv Resolve against given base // rtvz Relativize // psa Parse server Authority // norm Normalize // ta ASCII form // // x Check that parse failed as expected // z End -- ensure that unchecked components are null private boolean check1(int prop) { checked |= prop; if (!parsed()) { failed |= prop; return false; } return true; } private void check2(String s, String ans, int prop) { if ((s == null) || !s.equals(ans)) failed |= prop; } URITest s(String s) { if (check1(SCHEME)) check2(uri.getScheme(), s, SCHEME); return this; } URITest u(String s) { if (check1(USERINFO)) check2(uri.getRawUserInfo(), s, USERINFO); return this; } URITest ud(String s) { if (check1(USERINFO_D)) { check2(uri.getUserInfo(), s, USERINFO_D); } return this; } URITest h(String s) { if (check1(HOST)) check2(uri.getHost(), s, HOST); return this; } URITest g(String s) { if (check1(REGISTRY)) { if (uri.getHost() != null) failed |= REGISTRY; else check2(uri.getRawAuthority(), s, REGISTRY); } return this; } URITest gd(String s) { if (check1(REGISTRY_D)) { if (uri.getHost() != null) failed |= REGISTRY_D; else check2(uri.getAuthority(), s, REGISTRY_D); } return this; } URITest n(int n) { checked |= PORT; if (!parsed() || (uri.getPort() != n)) failed |= PORT; return this; } URITest p(String s) { if (check1(PATH)) check2(uri.getRawPath(), s, PATH); return this; } URITest pd(String s) { if (check1(PATH_D)) check2(uri.getPath(), s, PATH_D); return this; } URITest o(String s) { if (check1(OPAQUEPART)) { if (!uri.isOpaque()) failed |= OPAQUEPART; else check2(uri.getSchemeSpecificPart(), s, OPAQUEPART); } return this; } URITest sp(String s) { if (check1(SSP)) check2(uri.getRawSchemeSpecificPart(), s, SSP); return this; } URITest spd(String s) { if (check1(SSP_D)) check2(uri.getSchemeSpecificPart(), s, SSP_D); return this; } URITest q(String s) { if (check1(QUERY)) check2(uri.getRawQuery(), s, QUERY); return this; } URITest qd(String s) { if (check1(QUERY_D)) check2(uri.getQuery(), s, QUERY_D); return this; } URITest f(String s) { if (check1(FRAGMENT)) check2(uri.getRawFragment(), s, FRAGMENT); return this; } URITest fd(String s) { if (check1(FRAGMENT_D)) check2(uri.getFragment(), s, FRAGMENT_D); return this; } URITest ta(String s) { if (check1(TOASCII)) check2(uri.toASCIIString(), s, TOASCII); return this; } URITest ts(String s) { if (check1(TOSTRING)) check2(uri.toString(), s, TOSTRING); return this; } URITest x() { checked |= PARSEFAIL; if (parsed()) failed |= PARSEFAIL; return this; } URITest rslv(URI base) { if (!parsed()) return this; this.base = base; op = "rslv"; URI u = uri; uri = null; try { this.uri = base.resolve(u); } catch (IllegalArgumentException x) { exc = x; } checked = 0; failed = 0; return this; } URITest norm() { if (!parsed()) return this; op = "norm"; uri = uri.normalize(); return this; } URITest rtvz(URI base) { if (!parsed()) return this; this.base = base; op = "rtvz"; uri = base.relativize(uri); checked = 0; failed = 0; return this; } URITest psa() { try { uri.parseServerAuthority(); } catch (URISyntaxException x) { exc = x; uri = null; } checked = 0; failed = 0; return this; } private void checkEmpty(String s, int prop) { if (((checked & prop) == 0) && (s != null)) failed |= prop; } // Check identity for the seven-argument URI constructor // void checkURI7() { // Only works on hierarchical URIs if (uri.isOpaque()) return; // Only works with server-based authorities if ((uri.getAuthority() == null) != ((uri.getUserInfo() == null) && (uri.getHost() == null))) return; // Not true if non-US-ASCII chars are encoded unnecessarily if (uri.getPath().indexOf('\u20AC') >= 0) return; try { URI u2 = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); if (!uri.equals(u2)) failed |= IDENT_URI7; } catch (URISyntaxException x) { failed |= IDENT_URI7; } } // Check identity for the five-argument URI constructor // void checkURI5() { // Only works on hierarchical URIs if (uri.isOpaque()) return; try { URI u2 = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), uri.getFragment()); if (!uri.equals(u2)) failed |= IDENT_URI5; } catch (URISyntaxException x) { failed |= IDENT_URI5; } } // Check identity for the three-argument URI constructor // void checkURI3() { try { URI u2 = new URI(uri.getScheme(), uri.getSchemeSpecificPart(), uri.getFragment()); if (!uri.equals(u2)) failed |= IDENT_URI3; } catch (URISyntaxException x) { failed |= IDENT_URI3; } } // Check all identities mentioned in the URI class specification // void checkIdentities() { if (input != null) { if (!uri.toString().equals(input)) failed |= IDENT_STR; } try { if (!(new URI(uri.toString())).equals(uri)) failed |= IDENT_URI1; } catch (URISyntaxException x) { failed |= IDENT_URI1; } // Remaining identities fail if "//" given but authority is undefined if ((uri.getAuthority() == null) && (uri.getSchemeSpecificPart() != null) && (uri.getSchemeSpecificPart().startsWith("///") || uri.getSchemeSpecificPart().startsWith("//?") || uri.getSchemeSpecificPart().equals("//"))) return; // Remaining identities fail if ":" given but port is undefined if ((uri.getHost() != null) && (uri.getAuthority() != null) && (uri.getAuthority().equals(uri.getHost() + ":"))) return; // Remaining identities fail if non-US-ASCII chars are encoded // unnecessarily if ((uri.getPath() != null) && uri.getPath().indexOf('\u20AC') >= 0) return; checkURI3(); checkURI5(); checkURI7(); } // Check identities, check that unchecked component properties are not // defined, and report any failures // URITest z() { if (!parsed()) { report(); return this; } if (op == null) checkIdentities(); // Check that unchecked components are undefined checkEmpty(uri.getScheme(), SCHEME); checkEmpty(uri.getUserInfo(), USERINFO);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -