0001: /*
0002: * Licensed to the Apache Software Foundation (ASF) under one or more
0003: * contributor license agreements. See the NOTICE file distributed with
0004: * this work for additional information regarding copyright ownership.
0005: * The ASF licenses this file to You under the Apache License, Version 2.0
0006: * (the "License"); you may not use this file except in compliance with
0007: * the License. You may obtain a copy of the License at
0008: *
0009: * http://www.apache.org/licenses/LICENSE-2.0
0010: *
0011: * Unless required by applicable law or agreed to in writing, software
0012: * distributed under the License is distributed on an "AS IS" BASIS,
0013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014: * See the License for the specific language governing permissions and
0015: * limitations under the License.
0016: */
0017:
0018: package org.apache.harmony.luni.tests.java.net;
0019:
0020: import java.net.MalformedURLException;
0021: import java.net.URI;
0022: import java.net.URISyntaxException;
0023:
0024: import junit.framework.TestCase;
0025:
0026: public class URITest extends TestCase {
0027:
0028: private URI[] uris;
0029:
0030: private URI[] getUris() throws URISyntaxException {
0031: if (uris != null) {
0032: return uris;
0033: }
0034:
0035: uris = new URI[] {
0036: // single arg constructor
0037: new URI(
0038: "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag"),
0039: // escaped octets for illegal chars
0040: new URI(
0041: "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g"),
0042: // escaped octets for unicode chars
0043: new URI(
0044: "ascheme://user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g"),
0045: // unicode chars equivalent to = new
0046: // URI("ascheme://user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g"),
0047:
0048: // multiple arg constructors
0049: new URI(
0050: "http", "user%60%20info", "host", 80, "/a%20path", //$NON-NLS-4$
0051: "qu%60%20ery", "fr%5E%20ag"),
0052: // escaped octets for illegal
0053: new URI("http", "user%C3%9F%C2%A3info", "host", -1,
0054: "/a%E2%82%ACpath", "qu%C2%A9%C2%AEery",
0055: "fr%C3%A4%C3%A8g"),
0056: // escaped octets for unicode
0057: new URI("ascheme", "user\u00DF\u00A3info", "host", 80,
0058: "/a\u20ACpath", "qu\u00A9\u00AEery",
0059: "fr\u00E4\u00E8g"),
0060: // unicode chars equivalent to = new
0061: // URI("ascheme", "user\u00df\u00a3info", "host", 80,
0062: // "/a\u0080path", "qu\u00a9\u00aeery", "fr\u00e4\u00e8g"),
0063: new URI("http", "user` info", "host", 81, "/a path",
0064: "qu` ery", "fr^ ag"), // illegal chars
0065: new URI("http", "user%info", "host", 0, "/a%path",
0066: "que%ry", "f%rag"),
0067: // % as illegal char, not escaped octet
0068:
0069: // urls with undefined components
0070: new URI("mailto", "user@domain.com", null),
0071: // no host, path, query or fragment
0072: new URI("../adirectory/file.html#"),
0073: // relative path with empty fragment;
0074: new URI("news", "comp.infosystems.www.servers.unix",
0075: null), //
0076: new URI(null, null, null, "fragment"), // only fragment
0077: new URI("telnet://server.org"), // only host
0078: new URI("http://reg:istry?query"),
0079: // malformed hostname, therefore registry-based,
0080: // with query
0081: new URI("file:///c:/temp/calculate.pl?"),
0082: // empty authority, non empty path, empty query
0083: };
0084: return uris;
0085: }
0086:
0087: /**
0088: * @tests java.net.URI#URI(java.lang.String)
0089: */
0090: public void test_ConstructorLjava_lang_String()
0091: throws URISyntaxException {
0092: // tests for public URI(String uri) throws URISyntaxException
0093:
0094: String[] constructorTests = new String[] {
0095: "http://user@www.google.com:45/search?q=helpinfo#somefragment",
0096: // http with authority, query and fragment
0097: "ftp://ftp.is.co.za/rfc/rfc1808.txt", // ftp
0098: "gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles", // gopher
0099: "mailto:mduerst@ifi.unizh.ch", // mailto
0100: "news:comp.infosystems.www.servers.unix", // news
0101: "telnet://melvyl.ucop.edu/", // telnet
0102: "http://123.24.17.98/test", // IPv4 authority
0103: "http://www.google.com:80/test",// domain name authority
0104: "http://joe@[3ffe:2a00:100:7031::1]:80/test",
0105: // IPv6 authority, with userinfo and port
0106: "/relative", // relative starting with /
0107: "//relative", // relative starting with //
0108: "relative", // relative with no /
0109: "#fragment",// relative just with fragment
0110: "http://user@host:80", // UI, host,port
0111: "http://user@host", // ui, host
0112: "http://host", // host
0113: "http://host:80", // host,port
0114: "http://joe@:80", // ui, port (becomes registry-based)
0115: "file:///foo/bar", // empty authority, non empty path
0116: "ht?tp://hoe@host:80", // miscellaneous tests
0117: "mai/lto:hey?joe#man", "http://host/a%20path#frag",
0118: // path with an escaped octet for space char
0119: "http://host/a%E2%82%ACpath#frag",
0120: // path with escaped octet for unicode char, not USASCII
0121: "http://host/a\u20ACpath#frag",
0122: // path with unicode char, not USASCII equivalent to
0123: // = "http://host/a\u0080path#frag",
0124: "http://host%20name/", // escaped octets in host (becomes
0125: // registry based)
0126: "http://host\u00DFname/", // unicodechar in host (becomes
0127: // registry based)
0128: // equivalent to = "http://host\u00dfname/",
0129: "ht123-+tp://www.google.com:80/test", // legal chars in scheme
0130: };
0131:
0132: for (int i = 0; i < constructorTests.length; i++) {
0133: try {
0134: new URI(constructorTests[i]);
0135: } catch (URISyntaxException e) {
0136: fail("Failed to construct URI for: "
0137: + constructorTests[i] + " : " + e);
0138: }
0139: }
0140:
0141: String[] constructorTestsInvalid = new String[] {
0142: "http:///a path#frag", // space char in path, not in escaped
0143: // octet form, with no host
0144: "http://host/a[path#frag", // an illegal char, not in escaped
0145: // octet form, should throw an
0146: // exception
0147: "http://host/a%path#frag", // invalid escape sequence in path
0148: "http://host/a%#frag", // incomplete escape sequence in path
0149:
0150: "http://host#a frag", // space char in fragment, not in
0151: // escaped octet form, no path
0152: "http://host/a#fr#ag", // illegal char in fragment
0153: "http:///path#fr%ag", // invalid escape sequence in fragment,
0154: // with no host
0155: "http://host/path#frag%", // incomplete escape sequence in
0156: // fragment
0157:
0158: "http://host/path?a query#frag", // space char in query, not
0159: // in escaped octet form
0160: "http://host?query%ag", // invalid escape sequence in query, no
0161: // path
0162: "http:///path?query%", // incomplete escape sequence in query,
0163: // with no host
0164:
0165: "mailto:user^name@fklkf.com" // invalid char in scheme
0166: // specific part
0167: };
0168:
0169: int[] constructorTestsInvalidIndices = new int[] { 9, 13, 13,
0170: 13, 13, 16, 15, 21, 18, 17, 18, 11 };
0171:
0172: for (int i = 0; i < constructorTestsInvalid.length; i++) {
0173: try {
0174: new URI(constructorTestsInvalid[i]);
0175: fail("Failed to throw URISyntaxException for: "
0176: + constructorTestsInvalid[i]);
0177: } catch (URISyntaxException e) {
0178: assertTrue(
0179: "Wrong index in URISytaxException for: "
0180: + constructorTestsInvalid[i]
0181: + " expected: "
0182: + constructorTestsInvalidIndices[i]
0183: + ", received: " + e.getIndex(),
0184: e.getIndex() == constructorTestsInvalidIndices[i]);
0185: }
0186: }
0187:
0188: String invalid2[] = {
0189: // authority validation
0190: "http://user@[3ffe:2x00:100:7031::1]:80/test", // malformed
0191: // IPv6 authority
0192: "http://[ipv6address]/apath#frag", // malformed ipv6 address
0193: "http://[ipv6address/apath#frag", // malformed ipv6 address
0194: "http://ipv6address]/apath#frag", // illegal char in host name
0195: "http://ipv6[address/apath#frag",
0196: "http://ipv6addr]ess/apath#frag",
0197: "http://ipv6address[]/apath#frag",
0198: // illegal char in username...
0199: "http://us[]er@host/path?query#frag",
0200: "http://host name/path", // illegal
0201: // char
0202: // in
0203: // authority
0204: "http://host^name#fragment", // illegal char in authority
0205: "telnet://us er@hostname/", // illegal char in authority
0206: // missing components
0207: "//", // Authority expected
0208: "ascheme://", // Authority expected
0209: "ascheme:", // Scheme-specific part expected
0210: // scheme validation
0211: "a scheme://reg/", // illegal char
0212: "1scheme://reg/", // non alpha char as 1st char
0213: "asche\u00dfme:ssp", // unicode char , not USASCII
0214: "asc%20heme:ssp" // escape octets
0215: };
0216:
0217: for (int i = 0; i < invalid2.length; i++) {
0218: try {
0219: new URI(invalid2[i]);
0220: fail("Failed to throw URISyntaxException for: "
0221: + invalid2[i]);
0222: } catch (URISyntaxException e) {
0223: }
0224: }
0225:
0226: // Regression test for HARMONY-23
0227: try {
0228: new URI("%3");
0229: fail("Assert 0: URI constructor failed to throw exception on invalid input.");
0230: } catch (URISyntaxException e) {
0231: // Expected
0232: assertEquals(
0233: "Assert 1: Wrong index in URISyntaxException.", 0,
0234: e.getIndex());
0235: }
0236:
0237: // Regression test for HARMONY-25
0238: // if port value is negative, the authority should be considered
0239: // registry-based.
0240: URI uri = new URI("http://host:-8096/path/index.html");
0241: assertEquals("Assert 2: returned wrong port value,", -1, uri
0242: .getPort());
0243: assertNull("Assert 3: returned wrong host value,", uri
0244: .getHost());
0245: try {
0246: uri.parseServerAuthority();
0247: fail("Assert 4: Expected URISyntaxException");
0248: } catch (URISyntaxException e) {
0249: // Expected
0250: }
0251:
0252: uri = new URI("http", "//myhost:-8096", null);
0253: assertEquals("Assert 5: returned wrong port value,", -1, uri
0254: .getPort());
0255: assertNull("Assert 6: returned wrong host value,", uri
0256: .getHost());
0257: try {
0258: uri.parseServerAuthority();
0259: fail("Assert 7: Expected URISyntaxException");
0260: } catch (URISyntaxException e) {
0261: // Expected
0262: }
0263: }
0264:
0265: /**
0266: * @tests java.net.URI#URI(java.lang.String)
0267: */
0268: public void test_URI_String() {
0269: try {
0270: URI myUri = new URI(":abc@mymail.com");
0271: fail("TestA, URISyntaxException expected, but not received.");
0272: } catch (URISyntaxException e) {
0273: assertEquals("TestA, Wrong URISyntaxException index, ", 0,
0274: e.getIndex());
0275: }
0276:
0277: try {
0278: URI uri = new URI("path[one");
0279: fail("TestB, URISyntaxException expected, but not received.");
0280: } catch (URISyntaxException e1) {
0281: assertEquals("TestB, Wrong URISyntaxException index, ", 4,
0282: e1.getIndex());
0283: }
0284:
0285: try {
0286: URI uri = new URI(" ");
0287: fail("TestC, URISyntaxException expected, but not received.");
0288: } catch (URISyntaxException e2) {
0289: assertEquals("TestC, Wrong URISyntaxException index, ", 0,
0290: e2.getIndex());
0291: }
0292: }
0293:
0294: /**
0295: * @tests java.net.URI#URI(java.lang.String, java.lang.String,
0296: * java.lang.String)
0297: */
0298: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String()
0299: throws URISyntaxException {
0300: URI uri = new URI("mailto", "mduerst@ifi.unizh.ch", null);
0301: assertNull("wrong userinfo", uri.getUserInfo());
0302: assertNull("wrong hostname", uri.getHost());
0303: assertNull("wrong authority", uri.getAuthority());
0304: assertEquals("wrong port number", -1, uri.getPort());
0305: assertNull("wrong path", uri.getPath());
0306: assertNull("wrong query", uri.getQuery());
0307: assertNull("wrong fragment", uri.getFragment());
0308: assertEquals("wrong SchemeSpecificPart",
0309: "mduerst@ifi.unizh.ch", uri.getSchemeSpecificPart());
0310:
0311: // scheme specific part can not be null
0312: try {
0313: uri = new URI("mailto", null, null);
0314: fail("Expected URISyntaxException");
0315: } catch (URISyntaxException e) {
0316: // Expected
0317: }
0318:
0319: // scheme needs to start with an alpha char
0320: try {
0321: uri = new URI("3scheme", "//authority/path", "fragment");
0322: fail("Expected URISyntaxException");
0323: } catch (URISyntaxException e) {
0324: // Expected
0325: }
0326:
0327: // scheme can not be empty string
0328: try {
0329: uri = new URI("", "//authority/path", "fragment");
0330: fail("Expected URISyntaxException");
0331: } catch (URISyntaxException e) {
0332: // Expected
0333: }
0334: }
0335:
0336: /**
0337: * @tests java.net.URI#URI(java.lang.String, java.lang.String,
0338: * java.lang.String, int, java.lang.String, java.lang.String,
0339: * java.lang.String)
0340: */
0341: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringILjava_lang_StringLjava_lang_StringLjava_lang_String() {
0342: // check for URISyntaxException for invalid Server Authority
0343: construct1("http", "user", "host\u00DFname", -1, "/file",
0344: "query", "fragment"); // unicode chars in host name
0345: // equivalent to construct1("http", "user", "host\u00dfname", -1,
0346: // "/file", "query", "fragment");
0347: construct1("http", "user", "host%20name", -1, "/file", "query",
0348: "fragment"); // escaped octets in host name
0349: construct1("http", "user", "host name", -1, "/file", "query",
0350: "fragment"); // illegal char in host name
0351: construct1("http", "user", "host]name", -1, "/file", "query",
0352: "fragment"); // illegal char in host name
0353:
0354: // missing host name
0355: construct1("http", "user", "", 80, "/file", "query", "fragment");
0356:
0357: // missing host name
0358: construct1("http", "user", "", -1, "/file", "query", "fragment");
0359:
0360: // malformed ipv4 address
0361: construct1("telnet", null, "256.197.221.200", -1, null, null,
0362: null);
0363:
0364: // malformed ipv4 address
0365: construct1("ftp", null, "198.256.221.200", -1, null, null, null);
0366:
0367: // These tests fail on other implementations...
0368: // construct1("http", "user", null, 80, "/file", "query", "fragment");
0369: // //missing host name
0370: // construct1("http", "user", null, -1, "/file", "query", "fragment");
0371: // //missing host name
0372:
0373: // check for URISyntaxException for invalid scheme
0374: construct1("ht\u00DFtp", "user", "hostname", -1, "/file",
0375: "query", "fragment"); // unicode chars in scheme
0376: // equivalent to construct1("ht\u00dftp", "user", "hostname", -1,
0377: // "/file",
0378: // "query", "fragment");
0379:
0380: construct1("ht%20tp", "user", "hostname", -1, "/file", "query",
0381: "fragment"); // escaped octets in scheme
0382: construct1("ht tp", "user", "hostname", -1, "/file", "query",
0383: "fragment"); // illegal char in scheme
0384: construct1("ht]tp", "user", "hostname", -1, "/file", "query",
0385: "fragment"); // illegal char in scheme
0386:
0387: // relative path with scheme
0388: construct1("http", "user", "hostname", -1, "relative", "query",
0389: "fragment"); // unicode chars in scheme
0390:
0391: // functional test
0392: URI uri;
0393: try {
0394: uri = new URI("http", "us:e@r", "hostname", 85,
0395: "/file/dir#/qu?e/", "qu?er#y", "frag#me?nt");
0396: assertEquals("wrong userinfo", "us:e@r", uri.getUserInfo());
0397: assertEquals("wrong hostname", "hostname", uri.getHost());
0398: assertEquals("wrong port number", 85, uri.getPort());
0399: assertEquals("wrong path", "/file/dir#/qu?e/", uri
0400: .getPath());
0401: assertEquals("wrong query", "qu?er#y", uri.getQuery());
0402: assertEquals("wrong fragment", "frag#me?nt", uri
0403: .getFragment());
0404: assertEquals("wrong SchemeSpecificPart",
0405: "//us:e@r@hostname:85/file/dir#/qu?e/?qu?er#y", uri
0406: .getSchemeSpecificPart());
0407: } catch (URISyntaxException e) {
0408: fail("Unexpected Exception: " + e);
0409: }
0410: }
0411:
0412: /*
0413: * helper method checking if the 7 arg constructor throws URISyntaxException
0414: * for a given set of parameters
0415: */
0416: private void construct1(String scheme, String userinfo,
0417: String host, int port, String path, String query,
0418: String fragment) {
0419: try {
0420: URI uri = new URI(scheme, userinfo, host, port, path,
0421: query, fragment);
0422: fail("Expected URISyntaxException not thrown for URI: "
0423: + uri.toString());
0424: } catch (URISyntaxException e) {
0425: // this constructor throws URISyntaxException for malformed server
0426: // based authorities
0427: }
0428: }
0429:
0430: /**
0431: * @throws URISyntaxException
0432: * @tests java.net.URI#URI(java.lang.String, java.lang.String,
0433: * java.lang.String, java.lang.String)
0434: */
0435: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_String()
0436: throws URISyntaxException {
0437: // relative path
0438: try {
0439: URI myUri = new URI("http", "www.joe.com", "relative",
0440: "jimmy");
0441: fail("URISyntaxException expected but not received.");
0442: } catch (URISyntaxException e) {
0443: // Expected
0444: }
0445:
0446: // valid parameters for this constructor
0447: URI uri;
0448:
0449: uri = new URI("http", "www.joe.com", "/path", "jimmy");
0450:
0451: // illegal char in path
0452: uri = new URI("http", "www.host.com", "/path?q", "somefragment");
0453:
0454: // empty fragment
0455: uri = new URI("ftp", "ftp.is.co.za", "/rfc/rfc1808.txt", "");
0456:
0457: // path with escaped octet for unicode char, not USASCII
0458: uri = new URI("http", "host", "/a%E2%82%ACpath", "frag");
0459:
0460: // frag with unicode char, not USASCII
0461: // equivalent to = uri = new URI("http", "host", "/apath",
0462: // "\u0080frag");
0463: uri = new URI("http", "host", "/apath", "\u20ACfrag");
0464:
0465: // Regression test for Harmony-1693
0466: new URI(null, null, null, null);
0467:
0468: // regression for Harmony-1346
0469: try {
0470: uri = new URI("http", ":2:3:4:5:6:7:8", "/apath",
0471: "\u20ACfrag");
0472: fail("Should throw URISyntaxException");
0473: } catch (URISyntaxException e) {
0474: // Expected
0475: }
0476: }
0477:
0478: /**
0479: * @throws URISyntaxException
0480: * @tests java.net.URI#URI(java.lang.String, java.lang.String,
0481: * java.lang.String, java.lang.String, java.lang.String)
0482: */
0483: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_StringLjava_lang_String()
0484: throws URISyntaxException {
0485: // URISyntaxException on relative path
0486: try {
0487: URI myUri = new URI("http", "www.joe.com", "relative",
0488: "query", "jimmy");
0489: fail("URISyntaxException expected but not received.");
0490: } catch (URISyntaxException e) {
0491: // Expected
0492: }
0493:
0494: // test if empty authority is parsed into undefined host, userinfo and
0495: // port and if unicode chars and escaped octets in components are
0496: // preserved, illegal chars are quoted
0497: URI uri = new URI("ht12-3+tp", "", "/p#a%E2%82%ACth",
0498: "q^u%25ery", "f/r\u00DFag");
0499:
0500: assertEquals("wrong scheme", "ht12-3+tp", uri.getScheme());
0501: assertNull("wrong authority", uri.getUserInfo());
0502: assertNull("wrong userinfo", uri.getUserInfo());
0503: assertNull("wrong hostname", uri.getHost());
0504: assertEquals("wrong port number", -1, uri.getPort());
0505: assertEquals("wrong path", "/p#a%E2%82%ACth", uri.getPath());
0506: assertEquals("wrong query", "q^u%25ery", uri.getQuery());
0507: assertEquals("wrong fragment", "f/r\u00DFag", uri.getFragment());
0508: // equivalent to = assertTrue("wrong fragment",
0509: // uri.getFragment().equals("f/r\u00dfag"));
0510: assertEquals("wrong SchemeSpecificPart",
0511: "///p#a%E2%82%ACth?q^u%25ery", uri
0512: .getSchemeSpecificPart());
0513: assertEquals("wrong RawSchemeSpecificPart",
0514: "///p%23a%25E2%2582%25ACth?q%5Eu%2525ery", uri
0515: .getRawSchemeSpecificPart());
0516: assertEquals(
0517: "incorrect toString()",
0518: "ht12-3+tp:///p%23a%25E2%2582%25ACth?q%5Eu%2525ery#f/r\u00dfag",
0519: uri.toString());
0520: assertEquals(
0521: "incorrect toASCIIString()",
0522:
0523: "ht12-3+tp:///p%23a%25E2%2582%25ACth?q%5Eu%2525ery#f/r%C3%9Fag",
0524: uri.toASCIIString());
0525: }
0526:
0527: /**
0528: * @throws URISyntaxException
0529: * @tests java.net.URI#URI(java.lang.String, java.lang.String,
0530: * java.lang.String, java.lang.String, java.lang.String)
0531: */
0532: public void test_fiveArgConstructor() throws URISyntaxException {
0533: // accept [] as part of valid ipv6 host name
0534: URI uri = new URI("ftp", "[0001:1234::0001]", "/dir1/dir2",
0535: "query", "frag");
0536: assertEquals("Returned incorrect host", "[0001:1234::0001]",
0537: uri.getHost());
0538:
0539: // do not accept [] as part of invalid ipv6 address
0540: try {
0541: uri = new URI("ftp", "[www.abc.com]", "/dir1/dir2",
0542: "query", "frag");
0543: fail("Expected URISyntaxException for invalid ipv6 address");
0544: } catch (URISyntaxException e) {
0545: // Expected
0546: }
0547:
0548: // do not accept [] as part of user info
0549: try {
0550: uri = new URI("ftp", "[user]@host", "/dir1/dir2", "query",
0551: "frag");
0552: fail("Expected URISyntaxException invalid user info");
0553: } catch (URISyntaxException e) {
0554: // Expected
0555: }
0556: }
0557:
0558: /**
0559: * @tests java.net.URI#compareTo(java.lang.Object)
0560: */
0561: public void test_compareToLjava_lang_Object() throws Exception {
0562: // compareTo tests
0563:
0564: String[][] compareToData = new String[][] {
0565: // scheme tests
0566: { "http:test", "" }, // scheme null, scheme not null
0567: { "", "http:test" }, // reverse
0568: { "http:test", "ftp:test" }, // schemes different
0569: { "/test", "/test" }, // schemes null
0570: { "http://joe", "http://joe" }, // schemes same
0571: { "http://joe", "hTTp://joe" }, // schemes same ignoring case
0572:
0573: // opacity : one opaque, the other not
0574: { "http:opaque", "http://nonopaque" },
0575: { "http://nonopaque", "http:opaque" },
0576: { "mailto:abc", "mailto:abc" }, // same ssp
0577: { "mailto:abC", "mailto:Abc" }, // different, by case
0578: { "mailto:abc", "mailto:def" }, // different by letter
0579: { "mailto:abc#ABC", "mailto:abc#DEF" },
0580: { "mailto:abc#ABC", "mailto:abc#ABC" },
0581: { "mailto:abc#DEF", "mailto:abc#ABC" },
0582:
0583: // hierarchical tests..
0584:
0585: // different authorities
0586: { "//www.test.com/test", "//www.test2.com/test" },
0587:
0588: { "/nullauth", "//nonnullauth/test" }, // one null authority
0589: { "//nonnull", "/null" },
0590: { "/hello", "/hello" }, // both authorities null
0591: // different userinfo
0592: { "http://joe@test.com:80", "http://test.com" },
0593: { "http://jim@test.com", "http://james@test.com" },
0594: // different hostnames
0595: { "http://test.com", "http://toast.com" },
0596: { "http://test.com:80", "test.com:87" }, // different ports
0597: { "http://test.com", "http://test.com:80" },
0598: // different paths
0599: { "http://test.com:91/dir1", "http://test.com:91/dir2" },
0600: // one null host
0601: { "http:/hostless", "http://hostfilled.com/hostless" },
0602:
0603: // queries
0604: { "http://test.com/dir?query",
0605: "http://test.com/dir?koory" },
0606: { "/test?query", "/test" },
0607: { "/test", "/test?query" },
0608: { "/test", "/test" },
0609:
0610: // fragments
0611: { "ftp://test.com/path?query#frag",
0612: "ftp://test.com/path?query" },
0613: { "ftp://test.com/path?query",
0614: "ftp://test.com/path?query#frag" },
0615: { "#frag", "#frag" }, { "p", "" },
0616:
0617: { "http://www.google.com", "#test" } // miscellaneous
0618: };
0619:
0620: int[] compareToResults = { 1, -1, 2, 0, 0, 0, 1, -1, 0, 32, -3,
0621: -3, 0, 3, -4, -1, 1, 0, 1, 8, -10, -12, -81, -1, -1, 6,
0622: 1, -1, 0, 1, -1, 0, 1, 1, };
0623:
0624: // test compareTo functionality
0625: for (int i = 0; i < compareToResults.length; i++) {
0626: URI b = new URI(compareToData[i][0]);
0627: URI r = new URI(compareToData[i][1]);
0628: if (b.compareTo(r) != compareToResults[i]) {
0629: fail("Test " + i + ": " + compareToData[i][0]
0630: + " compared to " + compareToData[i][1]
0631: + " -> " + b.compareTo(r) + " rather than "
0632: + compareToResults[i]);
0633: }
0634: }
0635: }
0636:
0637: /**
0638: * @throws URISyntaxException
0639: * @tests java.net.URI#compareTo(java.lang.Object)
0640: */
0641: public void test_compareTo2() throws URISyntaxException {
0642: URI uri, uri2;
0643:
0644: // test URIs with host names with different casing
0645: uri = new URI("http://AbC.cOm/root/news");
0646: uri2 = new URI("http://aBc.CoM/root/news");
0647: assertEquals("TestA", 0, uri.compareTo(uri2));
0648: assertEquals("TestB", 0, uri.compareTo(uri2));
0649:
0650: // test URIs with one undefined component
0651: uri = new URI("http://abc.com:80/root/news");
0652: uri2 = new URI("http://abc.com/root/news");
0653: assertTrue("TestC", uri.compareTo(uri2) > 0);
0654: assertTrue("TestD", uri2.compareTo(uri) < 0);
0655:
0656: // test URIs with one undefined component
0657: uri = new URI("http://user@abc.com/root/news");
0658: uri2 = new URI("http://abc.com/root/news");
0659: assertTrue("TestE", uri.compareTo(uri2) > 0);
0660: assertTrue("TestF", uri2.compareTo(uri) < 0);
0661: }
0662:
0663: /**
0664: * @tests java.net.URI#create(java.lang.String)
0665: */
0666: public void test_createLjava_lang_String() {
0667: try {
0668: URI myUri = URI.create("a scheme://reg/");
0669: fail("IllegalArgumentException expected but not received.");
0670: } catch (IllegalArgumentException e) {
0671: // Expected
0672: }
0673: }
0674:
0675: /**
0676: * @tests java.net.URI#equals(java.lang.Object)
0677: */
0678: public void test_equalsLjava_lang_Object() throws Exception {
0679: String[][] equalsData = new String[][] {
0680: { "", "" }, // null frags
0681: { "/path", "/path#frag" },
0682: { "#frag", "#frag2" },
0683: { "#frag", "#FRag" },
0684:
0685: // case insensitive on hex escapes
0686: { "#fr%4F", "#fr%4f" },
0687:
0688: { "scheme:test", "scheme2:test" }, // scheme stuff
0689: { "test", "http:test" },
0690: { "http:test", "test" },
0691: { "SCheme:test", "schEMe:test" },
0692:
0693: // hierarchical/opaque mismatch
0694: { "mailto:jim", "mailto://jim" },
0695: { "mailto://test", "mailto:test" },
0696:
0697: // opaque
0698: { "mailto:name", "mailto:name" },
0699: { "mailtO:john", "mailto:jim" },
0700:
0701: // test hex case insensitivity on ssp
0702: { "mailto:te%4Fst", "mailto:te%4fst" },
0703:
0704: { "mailto:john#frag", "mailto:john#frag2" },
0705:
0706: // hierarchical
0707: { "/test", "/test" }, // paths
0708: { "/te%F4st", "/te%f4st" },
0709: { "/TEst", "/teSt" },
0710: { "", "/test" },
0711:
0712: // registry based because they don't resolve properly to
0713: // server-based add more tests here
0714: { "//host.com:80err", "//host.com:80e" },
0715: { "//host.com:81e%Abrr", "//host.com:81e%abrr" },
0716:
0717: { "/test", "//auth.com/test" },
0718: { "//test.com", "/test" },
0719:
0720: { "//test.com", "//test.com" }, // hosts
0721:
0722: // case insensitivity for hosts
0723: { "//HoSt.coM/", "//hOsT.cOm/" },
0724: { "//te%ae.com", "//te%aE.com" },
0725: { "//test.com:80", "//test.com:81" },
0726: { "//joe@test.com:80", "//test.com:80" },
0727: { "//jo%3E@test.com:82", "//jo%3E@test.com:82" },
0728: { "//test@test.com:85", "//test@test.com" }, };
0729:
0730: boolean[] equalsResults = new boolean[] { true, false, false,
0731: false, true, false, false, false, true, false, false,
0732: true, false, true, false, true, true, false, false,
0733: false, true, false, false, true, true, true, false,
0734: false, true, false, };
0735:
0736: // test equals functionality
0737: for (int i = 0; i < equalsResults.length; i++) {
0738: URI b = new URI(equalsData[i][0]);
0739: URI r = new URI(equalsData[i][1]);
0740: if (b.equals(r) != equalsResults[i]) {
0741: fail("Error: " + equalsData[i][0] + " == "
0742: + equalsData[i][1] + "? -> " + b.equals(r)
0743: + " expected " + equalsResults[i]);
0744: }
0745: }
0746:
0747: }
0748:
0749: /**
0750: * @throws URISyntaxException
0751: * @tests java.net.URI#equals(java.lang.Object)
0752: */
0753: public void test_equals2() throws URISyntaxException {
0754: // test URIs with empty string authority
0755: URI uri = new URI("http:///~/dictionary");
0756: URI uri2 = new URI(uri.getScheme(), uri.getAuthority(), uri
0757: .getPath(), uri.getQuery(), uri.getFragment());
0758: assertTrue(uri2.equals(uri));
0759:
0760: // test URIs with port number
0761: uri = new URI("http://abc.com%E2%82%AC:88/root/news");
0762: uri2 = new URI("http://abc.com%E2%82%AC/root/news");
0763: assertFalse(uri.equals(uri2));
0764: assertFalse(uri2.equals(uri));
0765:
0766: // test URIs with host names with different casing
0767: uri = new URI("http://AbC.cOm/root/news");
0768: uri2 = new URI("http://aBc.CoM/root/news");
0769: assertTrue(uri.equals(uri2));
0770: assertTrue(uri2.equals(uri));
0771: }
0772:
0773: /**
0774: * @tests java.net.URI#getAuthority()
0775: */
0776: public void test_getAuthority() throws Exception {
0777: URI[] uris = getUris();
0778:
0779: String[] getAuthorityResults = {
0780: "user` info@host",
0781: "user\u00DF\u00A3info@host:80", // =
0782: // "user\u00df\u00a3info@host:80",
0783: "user\u00DF\u00A3info@host:0", // =
0784: // "user\u00df\u00a3info@host:0",
0785: "user%60%20info@host:80",
0786: "user%C3%9F%C2%A3info@host",
0787: "user\u00DF\u00A3info@host:80", // =
0788: // "user\u00df\u00a3info@host:80",
0789: "user` info@host:81", "user%info@host:0", null, null,
0790: null, null, "server.org", "reg:istry", null, };
0791:
0792: for (int i = 0; i < uris.length; i++) {
0793: String result = uris[i].getAuthority();
0794: if (getAuthorityResults[i] != result
0795: && !getAuthorityResults[i].equals(result)) {
0796: fail("Error: For URI \"" + uris[i].toString()
0797: + "\", getAuthority() returned: " + result
0798: + ", expected: " + getAuthorityResults[i]);
0799: }
0800: }
0801: // regression test for HARMONY-1119
0802: assertNull(new URI(null, null, null, 127, null, null, null)
0803: .getAuthority());
0804: }
0805:
0806: /**
0807: * @tests java.net.URI#getAuthority()
0808: */
0809: public void test_getAuthority2() throws Exception {
0810: // tests for URIs with empty string authority component
0811:
0812: URI uri = new URI("file:///tmp/");
0813: assertNull("Authority not null for URI: " + uri, uri
0814: .getAuthority());
0815: assertNull("Host not null for URI " + uri, uri.getHost());
0816: assertEquals("testA, toString() returned incorrect value",
0817: "file:///tmp/", uri.toString());
0818:
0819: uri = new URI("file", "", "/tmp", "frag");
0820: assertNull("Authority not null for URI: " + uri, uri
0821: .getAuthority());
0822: assertNull("Host not null for URI " + uri, uri.getHost());
0823: assertEquals("testB, toString() returned incorrect value",
0824: "file:///tmp#frag", uri.toString());
0825:
0826: uri = new URI("file", "", "/tmp", "query", "frag");
0827: assertNull("Authority not null for URI: " + uri, uri
0828: .getAuthority());
0829: assertNull("Host not null for URI " + uri, uri.getHost());
0830: assertEquals("test C, toString() returned incorrect value",
0831: "file:///tmp?query#frag", uri.toString());
0832:
0833: // after normalization the host string info may be lost since the
0834: // uri string is reconstructed
0835: uri = new URI("file", "", "/tmp/a/../b/c", "query", "frag");
0836: URI uri2 = uri.normalize();
0837: assertNull("Authority not null for URI: " + uri2, uri
0838: .getAuthority());
0839: assertNull("Host not null for URI " + uri2, uri.getHost());
0840: assertEquals("test D, toString() returned incorrect value",
0841: "file:///tmp/a/../b/c?query#frag", uri.toString());
0842: assertEquals("test E, toString() returned incorrect value",
0843: "file:/tmp/b/c?query#frag", uri2.toString());
0844:
0845: // the empty string host will give URISyntaxException
0846: // for the 7 arg constructor
0847: try {
0848: uri = new URI("file", "user", "", 80, "/path", "query",
0849: "frag");
0850: fail("Expected URISyntaxException");
0851: } catch (URISyntaxException e) {
0852: // Expected
0853: }
0854: }
0855:
0856: /**
0857: * @tests java.net.URI#getFragment()
0858: */
0859: public void test_getFragment() throws Exception {
0860: URI[] uris = getUris();
0861:
0862: String[] getFragmentResults = {
0863: "fr^ ag",
0864: "fr\u00E4\u00E8g", // =
0865: // "fr\u00e4\u00e8g",
0866: "fr\u00E4\u00E8g", // = "fr\u00e4\u00e8g",
0867: "fr%5E%20ag", "fr%C3%A4%C3%A8g",
0868: "fr\u00E4\u00E8g", // =
0869: // "fr\u00e4\u00e8g",
0870: "fr^ ag", "f%rag", null, "", null, "fragment", null,
0871: null, null };
0872:
0873: for (int i = 0; i < uris.length; i++) {
0874: String result = uris[i].getFragment();
0875: if (getFragmentResults[i] != result
0876: && !getFragmentResults[i].equals(result)) {
0877: fail("Error: For URI \"" + uris[i].toString()
0878: + "\", getFragment() returned: " + result
0879: + ", expected: " + getFragmentResults[i]);
0880: }
0881: }
0882: }
0883:
0884: /**
0885: * @tests java.net.URI#getHost()
0886: */
0887: public void test_getHost() throws Exception {
0888: URI[] uris = getUris();
0889:
0890: String[] getHostResults = { "host", "host", "host", "host",
0891: "host", "host", "host", "host", null, null, null, null,
0892: "server.org", null, null };
0893:
0894: for (int i = 0; i < uris.length; i++) {
0895: String result = uris[i].getHost();
0896: if (getHostResults[i] != result
0897: && !getHostResults[i].equals(result)) {
0898: fail("Error: For URI \"" + uris[i].toString()
0899: + "\", getHost() returned: " + result
0900: + ", expected: " + getHostResults[i]);
0901: }
0902: }
0903: }
0904:
0905: /**
0906: * @tests java.net.URI#getPath()
0907: */
0908: public void test_getPath() throws Exception {
0909: URI[] uris = getUris();
0910:
0911: String[] getPathResults = { "/a path",
0912: "/a\u20ACpath", // = "/a\u0080path",
0913: "/a\u20ACpath", // = "/a\u0080path",
0914: "/a%20path", "/a%E2%82%ACpath",
0915: "/a\u20ACpath", // = "/a\u0080path",
0916: "/a path", "/a%path", null, "../adirectory/file.html",
0917: null, "", "", "", "/c:/temp/calculate.pl" };
0918:
0919: for (int i = 0; i < uris.length; i++) {
0920: String result = uris[i].getPath();
0921: if (getPathResults[i] != result
0922: && !getPathResults[i].equals(result)) {
0923: fail("Error: For URI \"" + uris[i].toString()
0924: + "\", getPath() returned: " + result
0925: + ", expected: " + getPathResults[i]);
0926: }
0927: }
0928: }
0929:
0930: /**
0931: * @tests java.net.URI#getPort()
0932: */
0933: public void test_getPort() throws Exception {
0934: URI[] uris = getUris();
0935:
0936: int[] getPortResults = { -1, 80, 0, 80, -1, 80, 81, 0, -1, -1,
0937: -1, -1, -1, -1, -1 };
0938:
0939: for (int i = 0; i < uris.length; i++) {
0940: int result = uris[i].getPort();
0941: assertTrue("Error: For URI \"" + uris[i].toString()
0942: + "\", getPort() returned: " + result
0943: + ", expected: " + getPortResults[i],
0944: result == getPortResults[i]);
0945: }
0946: }
0947:
0948: /**
0949: * @tests java.net.URI#getPort()
0950: */
0951: public void test_getPort2() throws Exception {
0952: // if port value is negative, the authority should be
0953: // consider registry based.
0954:
0955: URI uri = new URI("http://myhost:-8096/site/index.html");
0956: assertEquals("TestA, returned wrong port value,", -1, uri
0957: .getPort());
0958: assertNull("TestA, returned wrong host value,", uri.getHost());
0959: try {
0960: uri.parseServerAuthority();
0961: fail("TestA, Expected URISyntaxException");
0962: } catch (URISyntaxException e) {
0963: // Expected
0964: }
0965:
0966: uri = new URI("http", "//myhost:-8096", null);
0967: assertEquals("TestB returned wrong port value,", -1, uri
0968: .getPort());
0969: assertNull("TestB returned wrong host value,", uri.getHost());
0970: try {
0971: uri.parseServerAuthority();
0972: fail("TestB, Expected URISyntaxException");
0973: } catch (URISyntaxException e) {
0974: // Expected
0975: }
0976: }
0977:
0978: /**
0979: * @tests java.net.URI#getQuery()
0980: */
0981: public void test_getQuery() throws Exception {
0982: URI[] uris = getUris();
0983:
0984: String[] getQueryResults = {
0985: "qu` ery",
0986: "qu\u00A9\u00AEery", // =
0987: // "qu\u00a9\u00aeery",
0988: "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery",
0989: "qu%60%20ery",
0990: "qu%C2%A9%C2%AEery",
0991: "qu\u00A9\u00AEery", // =
0992: // "qu\u00a9\u00aeery",
0993: "qu` ery", "que%ry", null, null, null, null, null,
0994: "query", "" };
0995:
0996: for (int i = 0; i < uris.length; i++) {
0997: String result = uris[i].getQuery();
0998: if (getQueryResults[i] != result
0999: && !getQueryResults[i].equals(result)) {
1000: fail("Error: For URI \"" + uris[i].toString()
1001: + "\", getQuery() returned: " + result
1002: + ", expected: " + getQueryResults[i]);
1003: }
1004: }
1005: }
1006:
1007: /**
1008: * @tests java.net.URI#getRawAuthority()
1009: */
1010: public void test_getRawAuthority() throws Exception {
1011: URI[] uris = getUris();
1012:
1013: String[] getRawAuthorityResults = {
1014: "user%60%20info@host",
1015: "user%C3%9F%C2%A3info@host:80",
1016: "user\u00DF\u00A3info@host:0", // =
1017: // "user\u00df\u00a3info@host:0",
1018: "user%2560%2520info@host:80",
1019: "user%25C3%259F%25C2%25A3info@host",
1020: "user\u00DF\u00A3info@host:80", // =
1021: // "user\u00df\u00a3info@host:80",
1022: "user%60%20info@host:81", "user%25info@host:0", null,
1023: null, null, null, "server.org", "reg:istry", null };
1024:
1025: for (int i = 0; i < uris.length; i++) {
1026: String result = uris[i].getRawAuthority();
1027: if (getRawAuthorityResults[i] != result
1028: && !getRawAuthorityResults[i].equals(result)) {
1029: fail("Error: For URI \"" + uris[i].toString()
1030: + "\", getRawAuthority() returned: " + result
1031: + ", expected: " + getRawAuthorityResults[i]);
1032: }
1033: }
1034: }
1035:
1036: /**
1037: * @tests java.net.URI#getRawFragment()
1038: */
1039: public void test_getRawFragment() throws Exception {
1040: URI[] uris = getUris();
1041:
1042: String[] getRawFragmentResults = {
1043: "fr%5E%20ag",
1044: "fr%C3%A4%C3%A8g",
1045: "fr\u00E4\u00E8g", // = "fr\u00e4\u00e8g",
1046: "fr%255E%2520ag",
1047: "fr%25C3%25A4%25C3%25A8g",
1048: "fr\u00E4\u00E8g", // =
1049: // "fr\u00e4\u00e8g",
1050: "fr%5E%20ag", "f%25rag", null, "", null, "fragment",
1051: null, null, null };
1052:
1053: for (int i = 0; i < uris.length; i++) {
1054: String result = uris[i].getRawFragment();
1055: if (getRawFragmentResults[i] != result
1056: && !getRawFragmentResults[i].equals(result)) {
1057: fail("Error: For URI \"" + uris[i].toString()
1058: + "\", getRawFragment() returned: " + result
1059: + ", expected: " + getRawFragmentResults[i]);
1060: }
1061: }
1062: }
1063:
1064: /**
1065: * @tests java.net.URI#getRawPath()
1066: */
1067: public void test_getRawPath() throws Exception {
1068: URI[] uris = getUris();
1069:
1070: String[] getRawPathResults = {
1071: "/a%20path",
1072: "/a%E2%82%ACpath",
1073: "/a\u20ACpath", // = "/a\u0080path",
1074: "/a%2520path",
1075: "/a%25E2%2582%25ACpath",
1076: "/a\u20ACpath", // =
1077: // "/a\u0080path",
1078: "/a%20path", "/a%25path", null,
1079: "../adirectory/file.html", null, "", "", "",
1080: "/c:/temp/calculate.pl" };
1081:
1082: for (int i = 0; i < uris.length; i++) {
1083: String result = uris[i].getRawPath();
1084: if (getRawPathResults[i] != result
1085: && !getRawPathResults[i].equals(result)) {
1086: fail("Error: For URI \"" + uris[i].toString()
1087: + "\", getRawPath() returned: " + result
1088: + ", expected: " + getRawPathResults[i]);
1089: }
1090: }
1091: }
1092:
1093: /**
1094: * @tests java.net.URI#getRawQuery()
1095: */
1096: public void test_getRawQuery() throws Exception {
1097: URI[] uris = getUris();
1098:
1099: String[] getRawQueryResults = {
1100: "qu%60%20ery",
1101: "qu%C2%A9%C2%AEery",
1102: "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery",
1103: "qu%2560%2520ery",
1104: "qu%25C2%25A9%25C2%25AEery",
1105: "qu\u00A9\u00AEery", // = "qu\u00a9\u00aeery",
1106: "qu%60%20ery", "que%25ry", null, null, null, null,
1107: null, "query", "" };
1108:
1109: for (int i = 0; i < uris.length; i++) {
1110: String result = uris[i].getRawQuery();
1111: if (getRawQueryResults[i] != result
1112: && !getRawQueryResults[i].equals(result)) {
1113: fail("Error: For URI \"" + uris[i].toString()
1114: + "\", getRawQuery() returned: " + result
1115: + ", expected: " + getRawQueryResults[i]);
1116: }
1117: }
1118:
1119: }
1120:
1121: /**
1122: * @tests java.net.URI#getRawSchemeSpecificPart()
1123: */
1124: public void test_getRawSchemeSpecificPart() throws Exception {
1125: URI[] uris = getUris();
1126:
1127: String[] getRawSspResults = {
1128: "//user%60%20info@host/a%20path?qu%60%20ery",
1129: "//user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery",
1130: "//user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery", // =
1131: // "//user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery"
1132: "//user%2560%2520info@host:80/a%2520path?qu%2560%2520ery",
1133: "//user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery",
1134: "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // =
1135: // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery"
1136: "//user%60%20info@host:81/a%20path?qu%60%20ery",
1137: "//user%25info@host:0/a%25path?que%25ry",
1138: "user@domain.com", "../adirectory/file.html",
1139: "comp.infosystems.www.servers.unix", "",
1140: "//server.org", "//reg:istry?query",
1141: "///c:/temp/calculate.pl?" };
1142:
1143: for (int i = 0; i < uris.length; i++) {
1144: String result = uris[i].getRawSchemeSpecificPart();
1145: if (!getRawSspResults[i].equals(result)) {
1146: fail("Error: For URI \"" + uris[i].toString()
1147: + "\", getRawSchemeSpecificPart() returned: "
1148: + result + ", expected: " + getRawSspResults[i]);
1149: }
1150: }
1151: }
1152:
1153: /**
1154: * @tests java.net.URI#getRawUserInfo()
1155: */
1156: public void test_getRawUserInfo() throws URISyntaxException {
1157: URI[] uris = getUris();
1158:
1159: String[] getRawUserInfoResults = {
1160: "user%60%20info",
1161: "user%C3%9F%C2%A3info",
1162: "user\u00DF\u00A3info", // = "user\u00df\u00a3info",
1163: "user%2560%2520info",
1164: "user%25C3%259F%25C2%25A3info",
1165: "user\u00DF\u00A3info", // = "user\u00df\u00a3info",
1166: "user%60%20info", "user%25info", null, null, null,
1167: null, null, null, null };
1168:
1169: for (int i = 0; i < uris.length; i++) {
1170: String result = uris[i].getRawUserInfo();
1171: if (getRawUserInfoResults[i] != result
1172: && !getRawUserInfoResults[i].equals(result)) {
1173: fail("Error: For URI \"" + uris[i].toString()
1174: + "\", getRawUserInfo() returned: " + result
1175: + ", expected: " + getRawUserInfoResults[i]);
1176: }
1177: }
1178: }
1179:
1180: /**
1181: * @tests java.net.URI#getScheme()
1182: */
1183: public void test_getScheme() throws Exception {
1184: URI[] uris = getUris();
1185:
1186: String[] getSchemeResults = { "http", "http", "ascheme",
1187: "http", "http", "ascheme", "http", "http", "mailto",
1188: null, "news", null, "telnet", "http", "file" };
1189:
1190: for (int i = 0; i < uris.length; i++) {
1191: String result = uris[i].getScheme();
1192: if (getSchemeResults[i] != result
1193: && !getSchemeResults[i].equals(result)) {
1194: fail("Error: For URI \"" + uris[i].toString()
1195: + "\", getScheme() returned: " + result
1196: + ", expected: " + getSchemeResults[i]);
1197: }
1198: }
1199: }
1200:
1201: /**
1202: * @tests java.net.URI#getSchemeSpecificPart()
1203: */
1204: public void test_getSchemeSpecificPart() throws Exception {
1205: URI[] uris = getUris();
1206:
1207: String[] getSspResults = {
1208: "//user` info@host/a path?qu` ery",
1209: "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // =
1210: // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery",
1211: "//user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery", // =
1212: // "//user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery",
1213: "//user%60%20info@host:80/a%20path?qu%60%20ery",
1214: "//user%C3%9F%C2%A3info@host/a%E2%82%ACpath?qu%C2%A9%C2%AEery",
1215: "//user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery", // =
1216: // "//user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery",
1217: "//user` info@host:81/a path?qu` ery",
1218: "//user%info@host:0/a%path?que%ry", "user@domain.com",
1219: "../adirectory/file.html",
1220: "comp.infosystems.www.servers.unix", "",
1221: "//server.org", "//reg:istry?query",
1222: "///c:/temp/calculate.pl?" };
1223:
1224: for (int i = 0; i < uris.length; i++) {
1225: String result = uris[i].getSchemeSpecificPart();
1226: if (!getSspResults[i].equals(result)) {
1227: fail("Error: For URI \"" + uris[i].toString()
1228: + "\", getSchemeSpecificPart() returned: "
1229: + result + ", expected: " + getSspResults[i]);
1230: }
1231: }
1232:
1233: }
1234:
1235: /**
1236: * @tests java.net.URI#getUserInfo()
1237: */
1238: public void test_getUserInfo() throws Exception {
1239: URI[] uris = getUris();
1240:
1241: String[] getUserInfoResults = {
1242: "user` info",
1243: "user\u00DF\u00A3info", // =
1244: // "user\u00df\u00a3info",
1245: "user\u00DF\u00A3info", // = "user\u00df\u00a3info",
1246: "user%60%20info",
1247: "user%C3%9F%C2%A3info",
1248: "user\u00DF\u00A3info", // = "user\u00df\u00a3info",
1249: "user` info", "user%info", null, null, null, null,
1250: null, null, null };
1251:
1252: for (int i = 0; i < uris.length; i++) {
1253: String result = uris[i].getUserInfo();
1254: if (getUserInfoResults[i] != result
1255: && !getUserInfoResults[i].equals(result)) {
1256: fail("Error: For URI \"" + uris[i].toString()
1257: + "\", getUserInfo() returned: " + result
1258: + ", expected: " + getUserInfoResults[i]);
1259: }
1260: }
1261: }
1262:
1263: /**
1264: * @tests java.net.URI#hashCode()
1265: */
1266: public void test_hashCode() throws Exception {
1267: String[][] hashCodeData = new String[][] {
1268: { "", "" }, // null frags
1269: { "/path", "/path#frag" },
1270: { "#frag", "#frag2" },
1271: { "#frag", "#FRag" },
1272:
1273: { "#fr%4F", "#fr%4F" }, // case insensitive on hex escapes
1274:
1275: { "scheme:test", "scheme2:test" }, // scheme
1276: { "test", "http:test" },
1277: { "http:test", "test" },
1278:
1279: // case insensitivity for scheme
1280: { "SCheme:test", "schEMe:test" },
1281:
1282: // hierarchical/opaque mismatch
1283: { "mailto:jim", "mailto://jim" },
1284: { "mailto://test", "mailto:test" },
1285:
1286: // opaque
1287: { "mailto:name", "mailto:name" },
1288: { "mailtO:john", "mailto:jim" },
1289: { "mailto:te%4Fst", "mailto:te%4Fst" },
1290: { "mailto:john#frag", "mailto:john#frag2" },
1291:
1292: // hierarchical
1293: { "/test/", "/test/" }, // paths
1294: { "/te%F4st", "/te%F4st" },
1295: { "/TEst", "/teSt" },
1296: { "", "/test" },
1297:
1298: // registry based because they don't resolve properly to
1299: // server-based
1300: // add more tests here
1301: { "//host.com:80err", "//host.com:80e" },
1302: { "//host.com:81e%Abrr", "//host.com:81e%Abrr" },
1303: { "//Host.com:80e", "//hoSt.com:80e" },
1304:
1305: { "/test", "//auth.com/test" },
1306: { "//test.com", "/test" },
1307:
1308: { "//test.com", "//test.com" }, // server based
1309:
1310: // case insensitivity for host
1311: { "//HoSt.coM/", "//hOsT.cOm/" },
1312: { "//te%aE.com", "//te%aE.com" },
1313: { "//test.com:80", "//test.com:81" },
1314: { "//joe@test.com:80", "//test.com:80" },
1315: { "//jo%3E@test.com:82", "//jo%3E@test.com:82" },
1316: { "//test@test.com:85", "//test@test.com" }, };
1317:
1318: boolean[] hashCodeResults = new boolean[] { true, false, false,
1319: false, true, false, false, false, true, false, false,
1320: true, false, true, false, true, true, false, false,
1321: false, true, false, false, false, true, true, true,
1322: false, false, true, false, };
1323:
1324: for (int i = 0; i < hashCodeResults.length; i++) {
1325: URI b = new URI(hashCodeData[i][0]);
1326: URI r = new URI(hashCodeData[i][1]);
1327: assertEquals("Error in hashcode equals results for"
1328: + b.toString() + " " + r.toString(),
1329: hashCodeResults[i], b.hashCode() == r.hashCode());
1330: }
1331:
1332: }
1333:
1334: /**
1335: * @tests java.net.URI#isAbsolute()
1336: */
1337: public void test_isAbsolute() throws URISyntaxException {
1338: String[] isAbsoluteData = new String[] {
1339: "mailto:user@ca.ibm.com", "urn:isbn:123498989h",
1340: "news:software.ibm.com", "http://www.amazon.ca",
1341: "file:///d:/temp/results.txt", "scheme:ssp",
1342: "calculate.pl?isbn=123498989h", "?isbn=123498989h",
1343: "//www.amazon.ca", "a.html", "#top", "//pc1/",
1344: "//user@host/path/file" };
1345:
1346: boolean results[] = new boolean[] { true, true, true, true,
1347: true, true, false, false, false, false, false, false,
1348: false };
1349:
1350: for (int i = 0; i < isAbsoluteData.length; i++) {
1351: boolean result = new URI(isAbsoluteData[i]).isAbsolute();
1352: assertEquals("new URI(" + isAbsoluteData[i]
1353: + ").isAbsolute()", results[i], result);
1354: }
1355: }
1356:
1357: /**
1358: * @tests java.net.URI#isOpaque()
1359: */
1360: public void test_isOpaque() throws URISyntaxException {
1361: String[] isOpaqueData = new String[] {
1362: "mailto:user@ca.ibm.com", "urn:isbn:123498989h",
1363: "news:software.ibm.com", "http://www.amazon.ca",
1364: "file:///d:/temp/results.txt", "scheme:ssp",
1365: "calculate.pl?isbn=123498989h", "?isbn=123498989h",
1366: "//www.amazon.ca", "a.html", "#top", "//pc1/",
1367: "//user@host/path/file" };
1368:
1369: boolean results[] = new boolean[] { true, true, true, false,
1370: false, true, false, false, false, false, false, false,
1371: false };
1372:
1373: for (int i = 0; i < isOpaqueData.length; i++) {
1374: boolean result = new URI(isOpaqueData[i]).isOpaque();
1375: assertEquals("new URI(" + isOpaqueData[i] + ").isOpaque()",
1376: results[i], result);
1377: }
1378: }
1379:
1380: /**
1381: * @tests java.net.URI#normalize()
1382: */
1383: public void test_normalize() throws Exception {
1384:
1385: String[] normalizeData = new String[] {
1386: // normal
1387: "/",
1388: "/a",
1389: "/a/b",
1390: "/a/b/c",
1391: // single, '.'
1392: "/.", "/./",
1393: "/./.",
1394: "/././",
1395: "/./a",
1396: "/./a/",
1397: "/././a",
1398: "/././a/",
1399: "/a/.",
1400: "/a/./",
1401: "/a/./.",
1402: "/a/./b",
1403: // double, '..'
1404: "/a/..", "/a/../", "/a/../b", "/a/../b/..",
1405: "/a/../b/../", "/a/../b/../c", "/..", "/../", "/../..",
1406: "/../../", "/../a", "/../a/", "/../../a", "/../../a/",
1407: "/a/b/../../c",
1408: "/a/b/../..",
1409: "/a/b/../../",
1410: "/a/b/../../c",
1411: "/a/b/c/../../../d",
1412: "/a/b/..",
1413: "/a/b/../",
1414: "/a/b/../c",
1415: // miscellaneous
1416: "/a/b/.././../../c/./d/../e",
1417: "/a/../../.c././../././c/d/../g/..",
1418: // '.' in the middle of segments
1419: "/a./b", "/.a/b", "/a.b/c", "/a/b../c",
1420: "/a/..b/c",
1421: "/a/b..c/d",
1422: // no leading slash, miscellaneous
1423: "", "a", "a/b", "a/b/c", "../", ".", "..", "../g",
1424: "g/a/../../b/c/./g", "a/b/.././../../c/./d/../e",
1425: "a/../../.c././../././c/d/../g/..", };
1426:
1427: String[] normalizeResults = new String[] { "/", "/a", "/a/b",
1428: "/a/b/c", "/", "/", "/", "/", "/a", "/a/", "/a", "/a/",
1429: "/a/", "/a/", "/a/", "/a/b", "/", "/", "/b", "/", "/",
1430: "/c", "/..", "/../", "/../..", "/../../", "/../a",
1431: "/../a/", "/../../a", "/../../a/", "/c", "/", "/",
1432: "/c", "/d", "/a/", "/a/", "/a/c", "/../c/e", "/../c/",
1433: "/a./b", "/.a/b", "/a.b/c", "/a/b../c", "/a/..b/c",
1434: "/a/b..c/d", "", "a", "a/b", "a/b/c", "../", "", "..",
1435: "../g", "b/c/g", "../c/e", "../c/", };
1436:
1437: for (int i = 0; i < normalizeData.length; i++) {
1438: URI test = new URI(normalizeData[i]);
1439: String result = test.normalize().toString();
1440: assertEquals("Normalized incorrectly, ",
1441: normalizeResults[i], result.toString());
1442: }
1443: }
1444:
1445: /**
1446: * @tests java.net.URI#normalize()
1447: */
1448: public void test_normalize2() throws URISyntaxException {
1449: URI uri1 = null, uri2 = null;
1450: uri1 = new URI("file:/D:/one/two/../../three");
1451: uri2 = uri1.normalize();
1452:
1453: assertEquals("Normalized to incorrect URI", "file:/D:/three",
1454: uri2.toString());
1455: assertTrue("Resolved URI is not absolute", uri2.isAbsolute());
1456: assertFalse("Resolved URI is opaque", uri2.isOpaque());
1457: assertEquals(
1458: "Resolved URI has incorrect scheme specific part",
1459: "/D:/three", uri2.getRawSchemeSpecificPart());
1460: }
1461:
1462: /**
1463: * @tests java.net.URI#normalize()
1464: */
1465: public void test_normalize3() throws URISyntaxException {
1466: // return same URI if it has a normalized path already
1467: URI uri1 = null, uri2 = null;
1468: uri1 = new URI("http://host/D:/one/two/three");
1469: uri2 = uri1.normalize();
1470: assertSame("Failed to return same URI after normalization",
1471: uri1, uri2);
1472:
1473: // try with empty path
1474: uri1 = new URI("http", "host", null, "fragment");
1475: uri2 = uri1.normalize();
1476: assertSame("Failed to return same URI after normalization",
1477: uri1, uri2);
1478: }
1479:
1480: /**
1481: * @tests java.net.URI#parseServerAuthority()
1482: */
1483: public void test_parseServerAuthority() throws URISyntaxException {
1484: // registry based uris
1485: URI[] uris = null;
1486: uris = new URI[] {
1487: // port number not digits
1488: new URI("http://foo:bar/file#fragment"),
1489: new URI("http", "//foo:bar/file", "fragment"),
1490:
1491: // unicode char in the hostname = new
1492: // URI("http://host\u00dfname/")
1493: new URI("http://host\u00DFname/"),
1494:
1495: new URI("http", "//host\u00DFname/", null),
1496: // = new URI("http://host\u00dfname/", null),
1497:
1498: // escaped octets in host name
1499: new URI("http://host%20name/"),
1500: new URI("http", "//host%20name/", null),
1501:
1502: // missing host name, port number
1503: new URI("http://joe@:80"),
1504:
1505: // missing host name, no port number
1506: new URI("http://user@/file?query#fragment"),
1507:
1508: new URI("//host.com:80err"), // malformed port number
1509: new URI("//host.com:81e%Abrr"),
1510:
1511: // malformed ipv4 address
1512: new URI("telnet", "//256.197.221.200", null),
1513:
1514: new URI("telnet://198.256.221.200"),
1515: new URI("//te%ae.com"), // misc ..
1516: new URI("//:port"),
1517: new URI("//:80"),
1518:
1519: // last label has to start with alpha char
1520: new URI("//fgj234fkgj.jhj.123."),
1521:
1522: new URI("//fgj234fkgj.jhj.123"),
1523:
1524: // '-' cannot be first or last character in a label
1525: new URI("//-domain.name"), new URI("//domain.name-"),
1526: new URI("//domain-"),
1527:
1528: // illegal char in host name
1529: new URI("//doma*in"),
1530:
1531: // host expected
1532: new URI("http://:80/"),
1533: new URI("http://user@/"),
1534:
1535: // ipv6 address not enclosed in "[]"
1536: new URI("http://3ffe:2a00:100:7031:22:1:80:89/"),
1537:
1538: // expected ipv6 addresses to be enclosed in "[]"
1539: new URI("http", "34::56:78", "/path", "query",
1540: "fragment"),
1541:
1542: // expected host
1543: new URI("http", "user@", "/path", "query", "fragment") };
1544: // these URIs do not have valid server based authorities,
1545: // but single arg, 3 and 5 arg constructors
1546: // parse them as valid registry based authorities
1547:
1548: // exception should occur when parseServerAuthority is
1549: // requested on these uris
1550: for (int i = 0; i < uris.length; i++) {
1551: try {
1552: URI uri = uris[i].parseServerAuthority();
1553: fail("URISyntaxException expected but not received for URI: "
1554: + uris[i].toString());
1555: } catch (URISyntaxException e) {
1556: // Expected
1557: }
1558: }
1559:
1560: // valid Server based authorities
1561: new URI("http", "3ffe:2a00:100:7031:2e:1:80:80", "/path",
1562: "fragment").parseServerAuthority();
1563: new URI("http", "host:80", "/path", "query", "fragment")
1564: .parseServerAuthority();
1565: new URI("http://[::3abc:4abc]:80/").parseServerAuthority();
1566: new URI("http", "34::56:78", "/path", "fragment")
1567: .parseServerAuthority();
1568: new URI("http", "[34:56::78]:80", "/path", "fragment")
1569: .parseServerAuthority();
1570:
1571: // invalid authorities (neither server nor registry)
1572: try {
1573: URI uri = new URI("http://us[er@host:80/");
1574: fail("Expected URISyntaxException for URI "
1575: + uri.toString());
1576: } catch (URISyntaxException e) {
1577: // Expected
1578: }
1579:
1580: try {
1581: URI uri = new URI("http://[ddd::hgghh]/");
1582: fail("Expected URISyntaxException for URI "
1583: + uri.toString());
1584: } catch (URISyntaxException e) {
1585: // Expected
1586: }
1587:
1588: try {
1589: URI uri = new URI("http",
1590: "[3ffe:2a00:100:7031:2e:1:80:80]a:80", "/path",
1591: "fragment");
1592: fail("Expected URISyntaxException for URI "
1593: + uri.toString());
1594: } catch (URISyntaxException e) {
1595: // Expected
1596: }
1597:
1598: try {
1599: URI uri = new URI("http", "host:80", "/path", "fragment");
1600: fail("Expected URISyntaxException for URI "
1601: + uri.toString());
1602: } catch (URISyntaxException e) {
1603: // Expected
1604: }
1605:
1606: // regression test for HARMONY-1126
1607: assertNotNull(URI.create("file://C:/1.txt")
1608: .parseServerAuthority());
1609: }
1610:
1611: /**
1612: * @tests java.net.URI#relativize(java.net.URI)
1613: */
1614: public void test_relativizeLjava_net_URI()
1615: throws URISyntaxException {
1616: // relativization tests
1617: String[][] relativizeData = new String[][] {
1618: // first is base, second is the one to relativize
1619: { "http://www.google.com/dir1/dir2", "mailto:test" }, // rel =
1620: // opaque
1621: { "mailto:test", "http://www.google.com" }, // base = opaque
1622:
1623: // different authority
1624: { "http://www.eclipse.org/dir1",
1625: "http://www.google.com/dir1/dir2" },
1626:
1627: // different scheme
1628: { "http://www.google.com", "ftp://www.google.com" },
1629:
1630: { "http://www.google.com/dir1/dir2/",
1631: "http://www.google.com/dir3/dir4/file.txt" },
1632: { "http://www.google.com/dir1/",
1633: "http://www.google.com/dir1/dir2/file.txt" },
1634: { "./dir1/", "./dir1/hi" },
1635: { "/dir1/./dir2", "/dir1/./dir2/hi" },
1636: { "/dir1/dir2/..", "/dir1/dir2/../hi" },
1637: { "/dir1/dir2/..", "/dir1/dir2/hi" },
1638: { "/dir1/dir2/", "/dir1/dir3/../dir2/text" },
1639: { "//www.google.com", "//www.google.com/dir1/file" },
1640: { "/dir1", "/dir1/hi" }, { "/dir1/", "/dir1/hi" }, };
1641:
1642: // expected results
1643: String[] relativizeResults = new String[] { "mailto:test",
1644: "http://www.google.com",
1645: "http://www.google.com/dir1/dir2",
1646: "ftp://www.google.com",
1647: "http://www.google.com/dir3/dir4/file.txt",
1648: "dir2/file.txt", "hi", "hi", "hi", "dir2/hi", "text",
1649: "dir1/file", "hi", "hi", };
1650:
1651: for (int i = 0; i < relativizeData.length; i++) {
1652: try {
1653: URI b = new URI(relativizeData[i][0]);
1654: URI r = new URI(relativizeData[i][1]);
1655: if (!b.relativize(r).toString().equals(
1656: relativizeResults[i])) {
1657: fail("Error: relativize, " + relativizeData[i][0]
1658: + ", " + relativizeData[i][1]
1659: + " returned: " + b.relativize(r)
1660: + ", expected:" + relativizeResults[i]);
1661: }
1662: } catch (URISyntaxException e) {
1663: fail("Exception on relativize test on data "
1664: + relativizeData[i][0] + ", "
1665: + relativizeData[i][1] + ": " + e);
1666: }
1667: }
1668:
1669: URI a = new URI("http://host/dir");
1670: URI b = new URI("http://host/dir/file?query");
1671: assertEquals("Assert 0: URI relativized incorrectly,", new URI(
1672: "file?query"), a.relativize(b));
1673:
1674: // One URI with empty host
1675: a = new URI("file:///~/first");
1676: b = new URI("file://tools/~/first");
1677: assertEquals("Assert 1: URI relativized incorrectly,", new URI(
1678: "file://tools/~/first"), a.relativize(b));
1679: assertEquals("Assert 2: URI relativized incorrectly,", new URI(
1680: "file:///~/first"), b.relativize(a));
1681:
1682: // Both URIs with empty hosts
1683: b = new URI("file:///~/second");
1684: assertEquals("Assert 3: URI relativized incorrectly,", new URI(
1685: "file:///~/second"), a.relativize(b));
1686: assertEquals("Assert 4: URI relativized incorrectly,", new URI(
1687: "file:///~/first"), b.relativize(a));
1688: }
1689:
1690: /**
1691: * @tests java.net.URI#relativize(java.net.URI)
1692: */
1693: public void test_relativize2() throws URISyntaxException {
1694: URI a = new URI("http://host/dir");
1695: URI b = new URI("http://host/dir/file?query");
1696: assertEquals("relativized incorrectly,", new URI("file?query"),
1697: a.relativize(b));
1698:
1699: // one URI with empty host
1700: a = new URI("file:///~/dictionary");
1701: b = new URI("file://tools/~/dictionary");
1702: assertEquals("relativized incorrectly,", new URI(
1703: "file://tools/~/dictionary"), a.relativize(b));
1704: assertEquals("relativized incorrectly,", new URI(
1705: "file:///~/dictionary"), b.relativize(a));
1706:
1707: // two URIs with empty hosts
1708: b = new URI("file:///~/therasus");
1709: assertEquals("relativized incorrectly,", new URI(
1710: "file:///~/therasus"), a.relativize(b));
1711: assertEquals("relativized incorrectly,", new URI(
1712: "file:///~/dictionary"), b.relativize(a));
1713:
1714: URI one = new URI("file:/C:/test/ws");
1715: URI two = new URI("file:/C:/test/ws");
1716:
1717: URI empty = new URI("");
1718: assertEquals(empty, one.relativize(two));
1719:
1720: one = new URI("file:/C:/test/ws");
1721: two = new URI("file:/C:/test/ws/p1");
1722: URI result = new URI("p1");
1723: assertEquals(result, one.relativize(two));
1724:
1725: one = new URI("file:/C:/test/ws/");
1726: assertEquals(result, one.relativize(two));
1727: }
1728:
1729: /**
1730: * @tests java.net.URI#resolve(java.net.URI)
1731: */
1732: public void test_resolve() throws URISyntaxException {
1733: URI uri1 = null, uri2 = null;
1734: uri1 = new URI("file:/D:/one/two/three");
1735: uri2 = uri1.resolve(new URI(".."));
1736:
1737: assertEquals("Resolved to incorrect URI", "file:/D:/one/", uri2
1738: .toString());
1739: assertTrue("Resolved URI is not absolute", uri2.isAbsolute());
1740: assertFalse("Resolved URI is opaque", uri2.isOpaque());
1741: assertEquals(
1742: "Resolved URI has incorrect scheme specific part",
1743: "/D:/one/", uri2.getRawSchemeSpecificPart());
1744: }
1745:
1746: /**
1747: * @tests java.net.URI#resolve(java.net.URI)
1748: */
1749: public void test_resolveLjava_net_URI() {
1750: // resolution tests
1751: String[][] resolveData = new String[][] {
1752: // authority in given URI
1753: { "http://www.test.com/dir",
1754: "//www.test.com/hello?query#fragment" },
1755: // no authority, absolute path
1756: { "http://www.test.com/dir", "/abspath/file.txt" },
1757: // no authority, relative paths
1758: { "/", "dir1/file.txt" }, { "/dir1", "dir2/file.txt" },
1759: { "/dir1/", "dir2/file.txt" },
1760: { "", "dir1/file.txt" },
1761: { "dir1", "dir2/file.txt" },
1762: { "dir1/", "dir2/file.txt" },
1763: // normalization required
1764: { "/dir1/dir2/../dir3/./", "dir4/./file.txt" },
1765: // allow a standalone fragment to be resolved
1766: { "http://www.google.com/hey/joe?query#fragment",
1767: "#frag2" },
1768: // return given when base is opaque
1769: { "mailto:idontexist@uk.ibm.com", "dir1/dir2" },
1770: // return given when given is absolute
1771: { "http://www.google.com/hi/joe",
1772: "http://www.oogle.com" }, };
1773:
1774: // expected results
1775: String[] resolveResults = new String[] {
1776: "http://www.test.com/hello?query#fragment",
1777: "http://www.test.com/abspath/file.txt",
1778: "/dir1/file.txt", "/dir2/file.txt",
1779: "/dir1/dir2/file.txt", "dir1/file.txt",
1780: "dir2/file.txt", "dir1/dir2/file.txt",
1781: "/dir1/dir3/dir4/file.txt",
1782: "http://www.google.com/hey/joe?query#frag2",
1783: "dir1/dir2", "http://www.oogle.com", };
1784:
1785: for (int i = 0; i < resolveResults.length; i++) {
1786: try {
1787: URI b = new URI(resolveData[i][0]);
1788: URI r = new URI(resolveData[i][1]);
1789: URI result = b.resolve(r);
1790: if (!result.toString().equals(resolveResults[i])) {
1791: fail("Error: resolve, " + resolveData[i][0] + ", "
1792: + resolveData[i][1] + " returned: "
1793: + b.resolve(r) + ", expected:"
1794: + resolveResults[i]);
1795: }
1796: if (!b.isOpaque()) {
1797: assertEquals(b + " and " + result
1798: + " incorrectly differ in absoluteness", b
1799: .isAbsolute(), result.isAbsolute());
1800: }
1801: } catch (URISyntaxException e) {
1802: fail("Exception on resolve test on data "
1803: + resolveData[i][0] + ", " + resolveData[i][1]
1804: + ": " + e);
1805: }
1806: }
1807: }
1808:
1809: /**
1810: * @tests java.net.URI#toASCIIString()
1811: */
1812: public void test_toASCIIString() throws Exception {
1813: URI[] uris = getUris();
1814:
1815: String[] toASCIIStringResults0 = new String[] {
1816: "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag",
1817: "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g",
1818: "ascheme://user%C3%9F%C2%A3info@host:0/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g",
1819: "http://user%2560%2520info@host:80/a%2520path?qu%2560%2520ery#fr%255E%2520ag",
1820: "http://user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery#fr%25C3%25A4%25C3%25A8g",
1821: "ascheme://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g",
1822: "http://user%60%20info@host:81/a%20path?qu%60%20ery#fr%5E%20ag",
1823: "http://user%25info@host:0/a%25path?que%25ry#f%25rag",
1824: "mailto:user@domain.com", "../adirectory/file.html#",
1825: "news:comp.infosystems.www.servers.unix", "#fragment",
1826: "telnet://server.org", "http://reg:istry?query",
1827: "file:///c:/temp/calculate.pl?" };
1828:
1829: for (int i = 0; i < uris.length; i++) {
1830: String result = uris[i].toASCIIString();
1831: assertTrue("Error: For URI \"" + uris[i].toString()
1832: + "\", toASCIIString() returned: " + result
1833: + ", expected: " + toASCIIStringResults0[i], result
1834: .equals(toASCIIStringResults0[i]));
1835: }
1836:
1837: String[] toASCIIStringData = new String[] {
1838: "http://www.test.com/\u00DF/dir/",
1839: "http://www.test.com/\u20AC/dir",
1840: "http://www.\u20AC.com/dir",
1841: "http://www.test.com/\u20AC/dir/file#fragment",
1842: "mailto://user@domain.com",
1843: "mailto://user\u00DF@domain.com", };
1844:
1845: String[] toASCIIStringResults = new String[] {
1846: "http://www.test.com/%C3%9F/dir/",
1847: "http://www.test.com/%E2%82%AC/dir",
1848: "http://www.%E2%82%AC.com/dir",
1849: "http://www.test.com/%E2%82%AC/dir/file#fragment",
1850: "mailto://user@domain.com",
1851: "mailto://user%C3%9F@domain.com", };
1852:
1853: for (int i = 0; i < toASCIIStringData.length; i++) {
1854: URI test = new URI(toASCIIStringData[i]);
1855: String result = test.toASCIIString();
1856: assertTrue("Error: new URI(\"" + toASCIIStringData[i]
1857: + "\").toASCIIString() returned: " + result
1858: + ", expected: " + toASCIIStringResults[i], result
1859: .equals(toASCIIStringResults[i]));
1860: }
1861: }
1862:
1863: /**
1864: * @tests java.net.URI#toString()
1865: */
1866: public void test_toString() throws Exception {
1867: URI[] uris = getUris();
1868:
1869: String[] toStringResults = {
1870: "http://user%60%20info@host/a%20path?qu%60%20ery#fr%5E%20ag",
1871: "http://user%C3%9F%C2%A3info@host:80/a%E2%82%ACpath?qu%C2%A9%C2%AEery#fr%C3%A4%C3%A8g",
1872: "ascheme://user\u00DF\u00A3info@host:0/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g",
1873: // =
1874: // "ascheme://user\u00df\u00a3info@host:0/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g",
1875: "http://user%2560%2520info@host:80/a%2520path?qu%2560%2520ery#fr%255E%2520ag",
1876: "http://user%25C3%259F%25C2%25A3info@host/a%25E2%2582%25ACpath?qu%25C2%25A9%25C2%25AEery#fr%25C3%25A4%25C3%25A8g",
1877: "ascheme://user\u00DF\u00A3info@host:80/a\u20ACpath?qu\u00A9\u00AEery#fr\u00E4\u00E8g",
1878: // =
1879: // "ascheme://user\u00df\u00a3info@host:80/a\u0080path?qu\u00a9\u00aeery#fr\u00e4\u00e8g",
1880: "http://user%60%20info@host:81/a%20path?qu%60%20ery#fr%5E%20ag",
1881: "http://user%25info@host:0/a%25path?que%25ry#f%25rag",
1882: "mailto:user@domain.com", "../adirectory/file.html#",
1883: "news:comp.infosystems.www.servers.unix", "#fragment",
1884: "telnet://server.org", "http://reg:istry?query",
1885: "file:///c:/temp/calculate.pl?" };
1886:
1887: for (int i = 0; i < uris.length; i++) {
1888: String result = uris[i].toString();
1889: assertTrue("Error: For URI \"" + uris[i].toString()
1890: + "\", toString() returned: " + result
1891: + ", expected: " + toStringResults[i], result
1892: .equals(toStringResults[i]));
1893: }
1894: }
1895:
1896: /**
1897: * @tests java.net.URI#toURL()
1898: */
1899: public void test_toURL() throws Exception {
1900: String absoluteuris[] = new String[] {
1901: "mailto:noreply@apache.org", "urn:isbn:123498989h",
1902: "news:software.ibm.com", "http://www.apache.org",
1903: "file:///d:/temp/results.txt", "scheme:ssp", };
1904:
1905: String relativeuris[] = new String[] {
1906: "calculate.pl?isbn=123498989h", "?isbn=123498989h",
1907: "//www.apache.org", "a.html", "#top", "//pc1/",
1908: "//user@host/path/file" };
1909:
1910: for (int i = 0; i < absoluteuris.length; i++) {
1911: try {
1912: new URI(absoluteuris[i]).toURL();
1913: } catch (MalformedURLException e) {
1914: // not all the URIs can be translated into valid URLs
1915: }
1916: }
1917:
1918: for (int i = 0; i < relativeuris.length; i++) {
1919: try {
1920: new URI(relativeuris[i]).toURL();
1921: fail("Expected IllegalArgumentException not thrown");
1922: } catch (IllegalArgumentException e) {
1923: // Expected
1924: }
1925: }
1926: }
1927: }
|