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.awt.image.ImageProducer;
0021: import java.io.File;
0022: import java.io.FileOutputStream;
0023: import java.io.IOException;
0024: import java.io.InputStream;
0025: import java.net.InetAddress;
0026: import java.net.InetSocketAddress;
0027: import java.net.MalformedURLException;
0028: import java.net.Proxy;
0029: import java.net.ProxySelector;
0030: import java.net.SocketAddress;
0031: import java.net.URI;
0032: import java.net.URL;
0033: import java.net.URLConnection;
0034: import java.net.URLStreamHandler;
0035: import java.net.URLStreamHandlerFactory;
0036: import java.net.Proxy.Type;
0037: import java.security.Permission;
0038: import java.util.ArrayList;
0039: import java.util.List;
0040:
0041: import junit.framework.TestCase;
0042: import tests.support.Support_Configuration;
0043: import tests.support.Support_Jetty;
0044: import tests.support.resource.Support_Resources;
0045:
0046: public class URLTest extends TestCase {
0047:
0048: public static class MyHandler extends URLStreamHandler {
0049: protected URLConnection openConnection(URL u)
0050: throws IOException {
0051: return null;
0052: }
0053: }
0054:
0055: URL u;
0056:
0057: URL u1;
0058:
0059: URL u2;
0060:
0061: URL u3;
0062:
0063: URL u4;
0064:
0065: URL u5;
0066:
0067: URL u6;
0068:
0069: boolean caught = false;
0070:
0071: static boolean isSelectCalled;
0072:
0073: /**
0074: * @tests java.net.URL#URL(java.lang.String)
0075: */
0076: public void test_ConstructorLjava_lang_String() throws IOException {
0077: // Tests for multiple URL instantiation basic parsing test
0078: u = new URL(
0079: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
0080: assertEquals("u returns a wrong protocol", "http", u
0081: .getProtocol());
0082: assertEquals("u returns a wrong host", "www.yahoo1.com", u
0083: .getHost());
0084: assertEquals("u returns a wrong port", 8080, u.getPort());
0085: assertEquals("u returns a wrong file",
0086: "/dir1/dir2/test.cgi?point1.html", u.getFile());
0087: assertEquals("u returns a wrong anchor", "anchor1", u.getRef());
0088:
0089: // test for no file
0090: u1 = new URL("http://www.yahoo2.com:9999");
0091: assertEquals("u1 returns a wrong protocol", "http", u1
0092: .getProtocol());
0093: assertEquals("u1 returns a wrong host", "www.yahoo2.com", u1
0094: .getHost());
0095: assertEquals("u1 returns a wrong port", 9999, u1.getPort());
0096: assertTrue("u1 returns a wrong file", u1.getFile().equals(""));
0097: assertNull("u1 returns a wrong anchor", u1.getRef());
0098:
0099: // test for no port
0100: u2 = new URL(
0101: "http://www.yahoo3.com/dir1/dir2/test.cgi?point1.html#anchor1");
0102: assertEquals("u2 returns a wrong protocol", "http", u2
0103: .getProtocol());
0104: assertEquals("u2 returns a wrong host", "www.yahoo3.com", u2
0105: .getHost());
0106: assertEquals("u2 returns a wrong port", -1, u2.getPort());
0107: assertEquals("u2 returns a wrong file",
0108: "/dir1/dir2/test.cgi?point1.html", u2.getFile());
0109: assertEquals("u2 returns a wrong anchor", "anchor1", u2
0110: .getRef());
0111:
0112: // test for no port
0113: URL u2a = new URL(
0114: "file://www.yahoo3.com/dir1/dir2/test.cgi#anchor1");
0115: assertEquals("u2a returns a wrong protocol", "file", u2a
0116: .getProtocol());
0117: assertEquals("u2a returns a wrong host", "www.yahoo3.com", u2a
0118: .getHost());
0119: assertEquals("u2a returns a wrong port", -1, u2a.getPort());
0120: assertEquals("u2a returns a wrong file", "/dir1/dir2/test.cgi",
0121: u2a.getFile());
0122: assertEquals("u2a returns a wrong anchor", "anchor1", u2a
0123: .getRef());
0124:
0125: // test for no file, no port
0126: u3 = new URL("http://www.yahoo4.com/");
0127: assertEquals("u3 returns a wrong protocol", "http", u3
0128: .getProtocol());
0129: assertEquals("u3 returns a wrong host", "www.yahoo4.com", u3
0130: .getHost());
0131: assertEquals("u3 returns a wrong port", -1, u3.getPort());
0132: assertEquals("u3 returns a wrong file", "/", u3.getFile());
0133: assertNull("u3 returns a wrong anchor", u3.getRef());
0134:
0135: // test for no file, no port
0136: URL u3a = new URL("file://www.yahoo4.com/");
0137: assertEquals("u3a returns a wrong protocol", "file", u3a
0138: .getProtocol());
0139: assertEquals("u3a returns a wrong host", "www.yahoo4.com", u3a
0140: .getHost());
0141: assertEquals("u3a returns a wrong port", -1, u3a.getPort());
0142: assertEquals("u3a returns a wrong file", "/", u3a.getFile());
0143: assertNull("u3a returns a wrong anchor", u3a.getRef());
0144:
0145: // test for no file, no port
0146: URL u3b = new URL("file://www.yahoo4.com");
0147: assertEquals("u3b returns a wrong protocol", "file", u3b
0148: .getProtocol());
0149: assertEquals("u3b returns a wrong host", "www.yahoo4.com", u3b
0150: .getHost());
0151: assertEquals("u3b returns a wrong port", -1, u3b.getPort());
0152: assertTrue("u3b returns a wrong file", u3b.getFile().equals(""));
0153: assertNull("u3b returns a wrong anchor", u3b.getRef());
0154:
0155: // test for non-port ":" and wierd characters occurrences
0156: u4 = new URL(
0157: "http://www.yahoo5.com/di!@$%^&*()_+r1/di:::r2/test.cgi?point1.html#anchor1");
0158: assertEquals("u4 returns a wrong protocol", "http", u4
0159: .getProtocol());
0160: assertEquals("u4 returns a wrong host", "www.yahoo5.com", u4
0161: .getHost());
0162: assertEquals("u4 returns a wrong port", -1, u4.getPort());
0163: assertEquals("u4 returns a wrong file",
0164: "/di!@$%^&*()_+r1/di:::r2/test.cgi?point1.html", u4
0165: .getFile());
0166: assertEquals("u4 returns a wrong anchor", "anchor1", u4
0167: .getRef());
0168:
0169: u5 = new URL("file:/testing.tst");
0170: assertEquals("u5 returns a wrong protocol", "file", u5
0171: .getProtocol());
0172: assertTrue("u5 returns a wrong host", u5.getHost().equals(""));
0173: assertEquals("u5 returns a wrong port", -1, u5.getPort());
0174: assertEquals("u5 returns a wrong file", "/testing.tst", u5
0175: .getFile());
0176: assertNull("u5 returns a wrong anchor", u5.getRef());
0177:
0178: URL u5a = new URL("file:testing.tst");
0179: assertEquals("u5a returns a wrong protocol", "file", u5a
0180: .getProtocol());
0181: assertTrue("u5a returns a wrong host", u5a.getHost().equals(""));
0182: assertEquals("u5a returns a wrong port", -1, u5a.getPort());
0183: assertEquals("u5a returns a wrong file", "testing.tst", u5a
0184: .getFile());
0185: assertNull("u5a returns a wrong anchor", u5a.getRef());
0186:
0187: URL u6 = new URL("http://host:/file");
0188: assertEquals("u6 return a wrong port", -1, u6.getPort());
0189:
0190: URL u7 = new URL("file:../../file.txt");
0191: assertTrue("u7 returns a wrong file: " + u7.getFile(), u7
0192: .getFile().equals("../../file.txt"));
0193:
0194: URL u8 = new URL(
0195: "http://[fec0::1:20d:60ff:fe24:7410]:35/file.txt");
0196: assertTrue("u8 returns a wrong protocol " + u8.getProtocol(),
0197: u8.getProtocol().equals("http"));
0198: assertTrue("u8 returns a wrong host " + u8.getHost(), u8
0199: .getHost().equals("[fec0::1:20d:60ff:fe24:7410]"));
0200: assertTrue("u8 returns a wrong port " + u8.getPort(), u8
0201: .getPort() == 35);
0202: assertTrue("u8 returns a wrong file " + u8.getFile(), u8
0203: .getFile().equals("/file.txt"));
0204: assertNull("u8 returns a wrong anchor " + u8.getRef(), u8
0205: .getRef());
0206:
0207: URL u9 = new URL(
0208: "file://[fec0::1:20d:60ff:fe24:7410]/file.txt#sogood");
0209: assertTrue("u9 returns a wrong protocol " + u9.getProtocol(),
0210: u9.getProtocol().equals("file"));
0211: assertTrue("u9 returns a wrong host " + u9.getHost(), u9
0212: .getHost().equals("[fec0::1:20d:60ff:fe24:7410]"));
0213: assertTrue("u9 returns a wrong port " + u9.getPort(), u9
0214: .getPort() == -1);
0215: assertTrue("u9 returns a wrong file " + u9.getFile(), u9
0216: .getFile().equals("/file.txt"));
0217: assertTrue("u9 returns a wrong anchor " + u9.getRef(), u9
0218: .getRef().equals("sogood"));
0219:
0220: URL u10 = new URL("file://[fec0::1:20d:60ff:fe24:7410]");
0221: assertTrue("u10 returns a wrong protocol " + u10.getProtocol(),
0222: u10.getProtocol().equals("file"));
0223: assertTrue("u10 returns a wrong host " + u10.getHost(), u10
0224: .getHost().equals("[fec0::1:20d:60ff:fe24:7410]"));
0225: assertTrue("u10 returns a wrong port " + u10.getPort(), u10
0226: .getPort() == -1);
0227:
0228: // test for error catching
0229:
0230: // Bad HTTP format - no "//"
0231: u = new URL(
0232: "http:www.yahoo5.com::22/dir1/di:::r2/test.cgi?point1.html#anchor1");
0233:
0234: caught = false;
0235: try {
0236: u = new URL(
0237: "http://www.yahoo5.com::22/dir1/di:::r2/test.cgi?point1.html#anchor1");
0238: } catch (MalformedURLException e) {
0239: caught = true;
0240: }
0241: assertTrue("Should have throw MalformedURLException", caught);
0242:
0243: // unknown protocol
0244: try {
0245: u = new URL("myProtocol://www.yahoo.com:22");
0246: } catch (MalformedURLException e) {
0247: caught = true;
0248: }
0249: assertTrue("3 Failed to throw MalformedURLException", caught);
0250:
0251: caught = false;
0252: // no protocol
0253: try {
0254: u = new URL("www.yahoo.com");
0255: } catch (MalformedURLException e) {
0256: caught = true;
0257: }
0258: assertTrue("4 Failed to throw MalformedURLException", caught);
0259:
0260: caught = false;
0261:
0262: URL u1 = null;
0263: try {
0264: // No leading or trailing spaces.
0265: u1 = new URL("file:/some/path");
0266: assertEquals("5 got wrong file length1", 10, u1.getFile()
0267: .length());
0268:
0269: // Leading spaces.
0270: u1 = new URL(" file:/some/path");
0271: assertEquals("5 got wrong file length2", 10, u1.getFile()
0272: .length());
0273:
0274: // Trailing spaces.
0275: u1 = new URL("file:/some/path ");
0276: assertEquals("5 got wrong file length3", 10, u1.getFile()
0277: .length());
0278:
0279: // Leading and trailing.
0280: u1 = new URL(" file:/some/path ");
0281: assertEquals("5 got wrong file length4", 10, u1.getFile()
0282: .length());
0283:
0284: // in-place spaces.
0285: u1 = new URL(" file: /some/path ");
0286: assertEquals("5 got wrong file length5", 12, u1.getFile()
0287: .length());
0288:
0289: } catch (MalformedURLException e) {
0290: fail("5 Did not expect the exception " + e);
0291: }
0292:
0293: // testing jar protocol with relative path
0294: // to make sure it's not canonicalized
0295: try {
0296: String file = "file:/a!/b/../d";
0297:
0298: u = new URL("jar:" + file);
0299: assertEquals("Wrong file (jar protocol, relative path)",
0300: file, u.getFile());
0301: } catch (MalformedURLException e) {
0302: fail("Unexpected exception (jar protocol, relative path)"
0303: + e);
0304: }
0305: }
0306:
0307: /**
0308: * @tests java.net.URL#URL(java.net.URL, java.lang.String)
0309: */
0310: public void test_ConstructorLjava_net_URLLjava_lang_String()
0311: throws Exception {
0312: // Test for method java.net.URL(java.net.URL, java.lang.String)
0313: u = new URL("http://www.yahoo.com");
0314: URL uf = new URL("file://www.yahoo.com");
0315: // basic ones
0316: u1 = new URL(u, "file.java");
0317: assertEquals("1 returns a wrong protocol", "http", u1
0318: .getProtocol());
0319: assertEquals("1 returns a wrong host", "www.yahoo.com", u1
0320: .getHost());
0321: assertEquals("1 returns a wrong port", -1, u1.getPort());
0322: assertEquals("1 returns a wrong file", "/file.java", u1
0323: .getFile());
0324: assertNull("1 returns a wrong anchor", u1.getRef());
0325:
0326: URL u1f = new URL(uf, "file.java");
0327: assertEquals("1f returns a wrong protocol", "file", u1f
0328: .getProtocol());
0329: assertEquals("1f returns a wrong host", "www.yahoo.com", u1f
0330: .getHost());
0331: assertEquals("1f returns a wrong port", -1, u1f.getPort());
0332: assertEquals("1f returns a wrong file", "/file.java", u1f
0333: .getFile());
0334: assertNull("1f returns a wrong anchor", u1f.getRef());
0335:
0336: u1 = new URL(u, "dir1/dir2/../file.java");
0337: assertEquals("3 returns a wrong protocol", "http", u1
0338: .getProtocol());
0339: assertTrue("3 returns a wrong host: " + u1.getHost(), u1
0340: .getHost().equals("www.yahoo.com"));
0341: assertEquals("3 returns a wrong port", -1, u1.getPort());
0342: assertEquals("3 returns a wrong file",
0343: "/dir1/dir2/../file.java", u1.getFile());
0344: assertNull("3 returns a wrong anchor", u1.getRef());
0345:
0346: u1 = new URL(u, "http:dir1/dir2/../file.java");
0347: assertEquals("3a returns a wrong protocol", "http", u1
0348: .getProtocol());
0349: assertTrue("3a returns a wrong host: " + u1.getHost(), u1
0350: .getHost().equals(""));
0351: assertEquals("3a returns a wrong port", -1, u1.getPort());
0352: assertEquals("3a returns a wrong file",
0353: "dir1/dir2/../file.java", u1.getFile());
0354: assertNull("3a returns a wrong anchor", u1.getRef());
0355:
0356: u = new URL("http://www.apache.org/testing/");
0357: u1 = new URL(u, "file.java");
0358: assertEquals("4 returns a wrong protocol", "http", u1
0359: .getProtocol());
0360: assertEquals("4 returns a wrong host", "www.apache.org", u1
0361: .getHost());
0362: assertEquals("4 returns a wrong port", -1, u1.getPort());
0363: assertEquals("4 returns a wrong file", "/testing/file.java", u1
0364: .getFile());
0365: assertNull("4 returns a wrong anchor", u1.getRef());
0366:
0367: uf = new URL("file://www.apache.org/testing/");
0368: u1f = new URL(uf, "file.java");
0369: assertEquals("4f returns a wrong protocol", "file", u1f
0370: .getProtocol());
0371: assertEquals("4f returns a wrong host", "www.apache.org", u1f
0372: .getHost());
0373: assertEquals("4f returns a wrong port", -1, u1f.getPort());
0374: assertEquals("4f returns a wrong file", "/testing/file.java",
0375: u1f.getFile());
0376: assertNull("4f returns a wrong anchor", u1f.getRef());
0377:
0378: uf = new URL("file:/testing/");
0379: u1f = new URL(uf, "file.java");
0380: assertEquals("4fa returns a wrong protocol", "file", u1f
0381: .getProtocol());
0382: assertTrue("4fa returns a wrong host", u1f.getHost().equals(""));
0383: assertEquals("4fa returns a wrong port", -1, u1f.getPort());
0384: assertEquals("4fa returns a wrong file", "/testing/file.java",
0385: u1f.getFile());
0386: assertNull("4fa returns a wrong anchor", u1f.getRef());
0387:
0388: uf = new URL("file:testing/");
0389: u1f = new URL(uf, "file.java");
0390: assertEquals("4fb returns a wrong protocol", "file", u1f
0391: .getProtocol());
0392: assertTrue("4fb returns a wrong host", u1f.getHost().equals(""));
0393: assertEquals("4fb returns a wrong port", -1, u1f.getPort());
0394: assertEquals("4fb returns a wrong file", "testing/file.java",
0395: u1f.getFile());
0396: assertNull("4fb returns a wrong anchor", u1f.getRef());
0397:
0398: u1f = new URL(uf, "file:file.java");
0399: assertEquals("4fc returns a wrong protocol", "file", u1f
0400: .getProtocol());
0401: assertTrue("4fc returns a wrong host", u1f.getHost().equals(""));
0402: assertEquals("4fc returns a wrong port", -1, u1f.getPort());
0403: assertEquals("4fc returns a wrong file", "file.java", u1f
0404: .getFile());
0405: assertNull("4fc returns a wrong anchor", u1f.getRef());
0406:
0407: u1f = new URL(uf, "file:");
0408: assertEquals("4fd returns a wrong protocol", "file", u1f
0409: .getProtocol());
0410: assertTrue("4fd returns a wrong host", u1f.getHost().equals(""));
0411: assertEquals("4fd returns a wrong port", -1, u1f.getPort());
0412: assertTrue("4fd returns a wrong file", u1f.getFile().equals(""));
0413: assertNull("4fd returns a wrong anchor", u1f.getRef());
0414:
0415: u = new URL("http://www.apache.org/testing");
0416: u1 = new URL(u, "file.java");
0417: assertEquals("5 returns a wrong protocol", "http", u1
0418: .getProtocol());
0419: assertEquals("5 returns a wrong host", "www.apache.org", u1
0420: .getHost());
0421: assertEquals("5 returns a wrong port", -1, u1.getPort());
0422: assertEquals("5 returns a wrong file", "/file.java", u1
0423: .getFile());
0424: assertNull("5 returns a wrong anchor", u1.getRef());
0425:
0426: uf = new URL("file://www.apache.org/testing");
0427: u1f = new URL(uf, "file.java");
0428: assertEquals("5f returns a wrong protocol", "file", u1f
0429: .getProtocol());
0430: assertEquals("5f returns a wrong host", "www.apache.org", u1f
0431: .getHost());
0432: assertEquals("5f returns a wrong port", -1, u1f.getPort());
0433: assertEquals("5f returns a wrong file", "/file.java", u1f
0434: .getFile());
0435: assertNull("5f returns a wrong anchor", u1f.getRef());
0436:
0437: uf = new URL("file:/testing");
0438: u1f = new URL(uf, "file.java");
0439: assertEquals("5fa returns a wrong protocol", "file", u1f
0440: .getProtocol());
0441: assertTrue("5fa returns a wrong host", u1f.getHost().equals(""));
0442: assertEquals("5fa returns a wrong port", -1, u1f.getPort());
0443: assertEquals("5fa returns a wrong file", "/file.java", u1f
0444: .getFile());
0445: assertNull("5fa returns a wrong anchor", u1f.getRef());
0446:
0447: uf = new URL("file:testing");
0448: u1f = new URL(uf, "file.java");
0449: assertEquals("5fb returns a wrong protocol", "file", u1f
0450: .getProtocol());
0451: assertTrue("5fb returns a wrong host", u1f.getHost().equals(""));
0452: assertEquals("5fb returns a wrong port", -1, u1f.getPort());
0453: assertEquals("5fb returns a wrong file", "file.java", u1f
0454: .getFile());
0455: assertNull("5fb returns a wrong anchor", u1f.getRef());
0456:
0457: u = new URL("http://www.apache.org/testing/foobaz");
0458: u1 = new URL(u, "/file.java");
0459: assertEquals("6 returns a wrong protocol", "http", u1
0460: .getProtocol());
0461: assertEquals("6 returns a wrong host", "www.apache.org", u1
0462: .getHost());
0463: assertEquals("6 returns a wrong port", -1, u1.getPort());
0464: assertEquals("6 returns a wrong file", "/file.java", u1
0465: .getFile());
0466: assertNull("6 returns a wrong anchor", u1.getRef());
0467:
0468: uf = new URL("file://www.apache.org/testing/foobaz");
0469: u1f = new URL(uf, "/file.java");
0470: assertEquals("6f returns a wrong protocol", "file", u1f
0471: .getProtocol());
0472: assertEquals("6f returns a wrong host", "www.apache.org", u1f
0473: .getHost());
0474: assertEquals("6f returns a wrong port", -1, u1f.getPort());
0475: assertEquals("6f returns a wrong file", "/file.java", u1f
0476: .getFile());
0477: assertNull("6f returns a wrong anchor", u1f.getRef());
0478:
0479: u = new URL("http://www.apache.org:8000/testing/foobaz");
0480: u1 = new URL(u, "/file.java");
0481: assertEquals("7 returns a wrong protocol", "http", u1
0482: .getProtocol());
0483: assertEquals("7 returns a wrong host", "www.apache.org", u1
0484: .getHost());
0485: assertEquals("7 returns a wrong port", 8000, u1.getPort());
0486: assertEquals("7 returns a wrong file", "/file.java", u1
0487: .getFile());
0488: assertNull("7 returns a wrong anchor", u1.getRef());
0489:
0490: u = new URL("http://www.apache.org/index.html");
0491: u1 = new URL(u, "#bar");
0492: assertEquals("8 returns a wrong host", "www.apache.org", u1
0493: .getHost());
0494: assertEquals("8 returns a wrong file", "/index.html", u1
0495: .getFile());
0496: assertEquals("8 returns a wrong anchor", "bar", u1.getRef());
0497:
0498: u = new URL("http://www.apache.org/index.html#foo");
0499: u1 = new URL(u, "http:#bar");
0500: assertEquals("9 returns a wrong host", "www.apache.org", u1
0501: .getHost());
0502: assertEquals("9 returns a wrong file", "/index.html", u1
0503: .getFile());
0504: assertEquals("9 returns a wrong anchor", "bar", u1.getRef());
0505:
0506: u = new URL("http://www.apache.org/index.html");
0507: u1 = new URL(u, "");
0508: assertEquals("10 returns a wrong host", "www.apache.org", u1
0509: .getHost());
0510: assertEquals("10 returns a wrong file", "/index.html", u1
0511: .getFile());
0512: assertNull("10 returns a wrong anchor", u1.getRef());
0513:
0514: uf = new URL("file://www.apache.org/index.html");
0515: u1f = new URL(uf, "");
0516: assertEquals("10f returns a wrong host", "www.apache.org", u1
0517: .getHost());
0518: assertEquals("10f returns a wrong file", "/index.html", u1
0519: .getFile());
0520: assertNull("10f returns a wrong anchor", u1.getRef());
0521:
0522: u = new URL("http://www.apache.org/index.html");
0523: u1 = new URL(u, "http://www.apache.org");
0524: assertEquals("11 returns a wrong host", "www.apache.org", u1
0525: .getHost());
0526: assertTrue("11 returns a wrong file", u1.getFile().equals(""));
0527: assertNull("11 returns a wrong anchor", u1.getRef());
0528:
0529: // test for question mark processing
0530: u = new URL("http://www.foo.com/d0/d1/d2/cgi-bin?foo=bar/baz");
0531:
0532: // test for relative file and out of bound "/../" processing
0533: u1 = new URL(u, "../dir1/./dir2/../file.java");
0534: assertTrue("A) returns a wrong file: " + u1.getFile(), u1
0535: .getFile().equals("/d0/d1/dir1/file.java"));
0536:
0537: // test for absolute and relative file processing
0538: u1 = new URL(u, "/../dir1/./dir2/../file.java");
0539: assertEquals("B) returns a wrong file",
0540: "/../dir1/./dir2/../file.java", u1.getFile());
0541:
0542: try {
0543: // u should raise a MalFormedURLException because u, the context is
0544: // null
0545: u = null;
0546: u1 = new URL(u, "file.java");
0547: fail("didn't throw the expected MalFormedURLException");
0548: } catch (MalformedURLException e) {
0549: // valid
0550: }
0551:
0552: // Regression test for HARMONY-3258
0553: // testing jar context url with relative file
0554: try {
0555: // check that relative path with null context is not canonicalized
0556: String spec = "jar:file:/a!/b/../d";
0557: URL ctx = null;
0558: u = new URL(ctx, spec);
0559: assertEquals("1 Wrong file (jar protocol, relative path)",
0560: spec, u.toString());
0561:
0562: spec = "../d";
0563: ctx = new URL("jar:file:/a!/b");
0564: u = new URL(ctx, spec);
0565: assertEquals("2 Wrong file (jar protocol, relative path)",
0566: "file:/a!/d", u.getFile());
0567:
0568: spec = "../d";
0569: ctx = new URL("jar:file:/a!/b/c");
0570: u = new URL(ctx, spec);
0571: assertEquals("3 Wrong file (jar protocol, relative path)",
0572: "file:/a!/d", u.getFile());
0573:
0574: spec = "../d";
0575: ctx = new URL("jar:file:/a!/b/c/d");
0576: u = new URL(ctx, spec);
0577: assertEquals("4 Wrong file (jar protocol, relative path)",
0578: "file:/a!/b/d", u.getFile());
0579:
0580: // added the real example
0581: spec = "../pdf/PDF.settings";
0582: ctx = new URL(
0583: "jar:file:/C:/Program%20Files/Netbeans-5.5/ide7/modules/org-netbeans-modules-utilities.jar!/org/netbeans/modules/utilities/Layer.xml");
0584: u = new URL(ctx, spec);
0585: assertEquals(
0586: "5 Wrong file (jar protocol, relative path)",
0587: "file:/C:/Program%20Files/Netbeans-5.5/ide7/modules/org-netbeans-modules-utilities.jar!/org/netbeans/modules/pdf/PDF.settings",
0588: u.getFile());
0589: } catch (MalformedURLException e) {
0590: fail("Testing jar protocol, relative path failed: " + e);
0591: }
0592: }
0593:
0594: /**
0595: * @tests java.net.URL#URL(java.net.URL, java.lang.String,
0596: * java.net.URLStreamHandler)
0597: */
0598: public void test_ConstructorLjava_net_URLLjava_lang_StringLjava_net_URLStreamHandler()
0599: throws Exception {
0600: // Test for method java.net.URL(java.net.URL, java.lang.String,
0601: // java.net.URLStreamHandler)
0602: u = new URL("http://www.yahoo.com");
0603: // basic ones
0604: u1 = new URL(u, "file.java", new MyHandler());
0605: assertEquals("1 returns a wrong protocol", "http", u1
0606: .getProtocol());
0607: assertEquals("1 returns a wrong host", "www.yahoo.com", u1
0608: .getHost());
0609: assertEquals("1 returns a wrong port", -1, u1.getPort());
0610: assertEquals("1 returns a wrong file", "/file.java", u1
0611: .getFile());
0612: assertNull("1 returns a wrong anchor", u1.getRef());
0613:
0614: u1 = new URL(u, "systemresource:/+/FILE0/test.java",
0615: new MyHandler());
0616: assertEquals("2 returns a wrong protocol", "systemresource", u1
0617: .getProtocol());
0618: assertTrue("2 returns a wrong host", u1.getHost().equals(""));
0619: assertEquals("2 returns a wrong port", -1, u1.getPort());
0620: assertEquals("2 returns a wrong file", "/+/FILE0/test.java", u1
0621: .getFile());
0622: assertNull("2 returns a wrong anchor", u1.getRef());
0623:
0624: u1 = new URL(u, "dir1/dir2/../file.java", null);
0625: assertEquals("3 returns a wrong protocol", "http", u1
0626: .getProtocol());
0627: assertEquals("3 returns a wrong host", "www.yahoo.com", u1
0628: .getHost());
0629: assertEquals("3 returns a wrong port", -1, u1.getPort());
0630: assertEquals("3 returns a wrong file",
0631: "/dir1/dir2/../file.java", u1.getFile());
0632: assertNull("3 returns a wrong anchor", u1.getRef());
0633:
0634: // test for question mark processing
0635: u = new URL("http://www.foo.com/d0/d1/d2/cgi-bin?foo=bar/baz");
0636:
0637: // test for relative file and out of bound "/../" processing
0638: u1 = new URL(u, "../dir1/dir2/../file.java", new MyHandler());
0639: assertTrue("A) returns a wrong file: " + u1.getFile(), u1
0640: .getFile().equals("/d0/d1/dir1/file.java"));
0641:
0642: // test for absolute and relative file processing
0643: u1 = new URL(u, "/../dir1/dir2/../file.java", null);
0644: assertEquals("B) returns a wrong file",
0645: "/../dir1/dir2/../file.java", u1.getFile());
0646:
0647: URL one;
0648: try {
0649: one = new URL("http://www.ibm.com");
0650: } catch (MalformedURLException ex) {
0651: // Should not happen.
0652: throw new RuntimeException(ex.getMessage());
0653: }
0654: try {
0655: new URL(one, (String) null);
0656: fail("Specifying null spec on URL constructor should throw MalformedURLException");
0657: } catch (MalformedURLException e) {
0658: // expected
0659: }
0660:
0661: try {
0662: // u should raise a MalFormedURLException because u, the context is
0663: // null
0664: u = null;
0665: u1 = new URL(u, "file.java", new MyHandler());
0666: } catch (MalformedURLException e) {
0667: return;
0668: }
0669: fail("didn't throw expected MalFormedURLException");
0670: }
0671:
0672: /**
0673: * @tests java.net.URL#URL(java.lang.String, java.lang.String,
0674: * java.lang.String)
0675: */
0676: public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_lang_String()
0677: throws MalformedURLException {
0678:
0679: u = new URL("http", "www.yahoo.com", "test.html#foo");
0680: assertEquals("http", u.getProtocol());
0681: assertEquals("www.yahoo.com", u.getHost());
0682: assertEquals(-1, u.getPort());
0683: assertEquals("test.html", u.getFile());
0684: assertEquals("foo", u.getRef());
0685:
0686: // Strange behavior in reference, the hostname contains a ':' so it gets
0687: // wrapped in '[', ']'
0688: URL testURL = new URL("http", "www.apache.org:8080",
0689: "test.html#anch");
0690: assertEquals("wrong protocol", "http", testURL.getProtocol());
0691: assertEquals("wrong host", "[www.apache.org:8080]", testURL
0692: .getHost());
0693: assertEquals("wrong port", -1, testURL.getPort());
0694: assertEquals("wrong file", "test.html", testURL.getFile());
0695: assertEquals("wrong anchor", "anch", testURL.getRef());
0696: }
0697:
0698: /**
0699: * @tests java.net.URL#URL(java.lang.String, java.lang.String, int,
0700: * java.lang.String)
0701: */
0702: public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_String()
0703: throws MalformedURLException {
0704: u = new URL("http", "www.yahoo.com", 8080, "test.html#foo");
0705: assertEquals("SSIS returns a wrong protocol", "http", u
0706: .getProtocol());
0707: assertEquals("SSIS returns a wrong host", "www.yahoo.com", u
0708: .getHost());
0709: assertEquals("SSIS returns a wrong port", 8080, u.getPort());
0710: assertEquals("SSIS returns a wrong file", "test.html", u
0711: .getFile());
0712: assertTrue("SSIS returns a wrong anchor: " + u.getRef(), u
0713: .getRef().equals("foo"));
0714:
0715: // Regression for HARMONY-83
0716: new URL("http", "apache.org", 123456789, "file");
0717: try {
0718: new URL("http", "apache.org", -123, "file");
0719: fail("Assert 0: Negative port should throw exception");
0720: } catch (MalformedURLException e) {
0721: // expected
0722: }
0723:
0724: }
0725:
0726: /**
0727: * @tests java.net.URL#URL(java.lang.String, java.lang.String, int,
0728: * java.lang.String, java.net.URLStreamHandler)
0729: */
0730: public void test_ConstructorLjava_lang_StringLjava_lang_StringILjava_lang_StringLjava_net_URLStreamHandler()
0731: throws Exception {
0732: // Test for method java.net.URL(java.lang.String, java.lang.String, int,
0733: // java.lang.String, java.net.URLStreamHandler)
0734: u = new URL("http", "www.yahoo.com", 8080, "test.html#foo",
0735: null);
0736: assertEquals("SSISH1 returns a wrong protocol", "http", u
0737: .getProtocol());
0738: assertEquals("SSISH1 returns a wrong host", "www.yahoo.com", u
0739: .getHost());
0740: assertEquals("SSISH1 returns a wrong port", 8080, u.getPort());
0741: assertEquals("SSISH1 returns a wrong file", "test.html", u
0742: .getFile());
0743: assertTrue("SSISH1 returns a wrong anchor: " + u.getRef(), u
0744: .getRef().equals("foo"));
0745:
0746: u = new URL("http", "www.yahoo.com", 8080, "test.html#foo",
0747: new MyHandler());
0748: assertEquals("SSISH2 returns a wrong protocol", "http", u
0749: .getProtocol());
0750: assertEquals("SSISH2 returns a wrong host", "www.yahoo.com", u
0751: .getHost());
0752: assertEquals("SSISH2 returns a wrong port", 8080, u.getPort());
0753: assertEquals("SSISH2 returns a wrong file", "test.html", u
0754: .getFile());
0755: assertTrue("SSISH2 returns a wrong anchor: " + u.getRef(), u
0756: .getRef().equals("foo"));
0757: }
0758:
0759: /**
0760: * @tests java.net.URL#equals(java.lang.Object)
0761: */
0762: public void test_equalsLjava_lang_Object() {
0763: // Test for method boolean java.net.URL.equals(java.lang.Object)
0764: try {
0765: u = new URL(
0766: "http://www.apache.org:8080/dir::23??????????test.html");
0767: u1 = new URL(
0768: "http://www.apache.org:8080/dir::23??????????test.html");
0769: assertTrue(
0770: "A) equals returns false for two identical URLs", u
0771: .equals(u1));
0772: assertTrue("return true for null comaprison", !u1
0773: .equals(null));
0774: u = new URL(
0775: "ftp://www.apache.org:8080/dir::23??????????test.html");
0776: assertTrue("Returned true for non-equal URLs", !u
0777: .equals(u1));
0778: } catch (MalformedURLException e) {
0779: fail("MalformedURLException during equals test : "
0780: + e.getMessage());
0781: }
0782: }
0783:
0784: /**
0785: * @tests java.net.URL#sameFile(java.net.URL)
0786: */
0787: public void test_sameFileLjava_net_URL() throws Exception {
0788: // Test for method boolean java.net.URL.sameFile(java.net.URL)
0789: u = new URL("http://www.yahoo.com");
0790: u1 = new URL("http", "www.yahoo.com", "");
0791: assertTrue("Should be the same1", u.sameFile(u1));
0792: u = new URL("http://www.yahoo.com/dir1/dir2/test.html#anchor1");
0793: u1 = new URL("http://www.yahoo.com/dir1/dir2/test.html#anchor2");
0794: assertTrue("Should be the same ", u.sameFile(u1));
0795:
0796: // regression test for Harmony-1040
0797: u = new URL("file", null, -1, "/d:/somedir/");
0798: u1 = new URL("file:/d:/somedir/");
0799: assertFalse(u.sameFile(u1));
0800:
0801: // regression test for Harmony-2136
0802: URL url1 = new URL("file:///anyfile");
0803: URL url2 = new URL("file://localhost/anyfile");
0804: assertTrue(url1.sameFile(url2));
0805:
0806: url1 = new URL("http:///anyfile");
0807: url2 = new URL("http://localhost/anyfile");
0808: assertFalse(url1.sameFile(url2));
0809:
0810: url1 = new URL("ftp:///anyfile");
0811: url2 = new URL("ftp://localhost/anyfile");
0812: assertFalse(url1.sameFile(url2));
0813:
0814: url1 = new URL("jar:file:///anyfile.jar!/");
0815: url2 = new URL("jar:file://localhost/anyfile.jar!/");
0816: assertFalse(url1.sameFile(url2));
0817: }
0818:
0819: /**
0820: * @tests java.net.URL#getContent()
0821: */
0822: public void test_getContent() {
0823: // Test for method java.lang.Object java.net.URL.getContent()
0824: byte[] ba;
0825: InputStream is;
0826: String s;
0827: File resources = Support_Resources.createTempFolder();
0828: try {
0829: Support_Resources.copyFile(resources, null,
0830: "hyts_htmltest.html");
0831: u = new URL("file", "", resources.getAbsolutePath()
0832: + "/hyts_htmltest.html");
0833: u.openConnection();
0834: is = (InputStream) u.getContent();
0835: is.read(ba = new byte[4096]);
0836: s = new String(ba);
0837: assertTrue(
0838: "Incorrect content "
0839: + u
0840: + " does not contain: \" A Seemingly Non Important String \"",
0841: s.indexOf("A Seemingly Non Important String") >= 0);
0842: } catch (IOException e) {
0843: fail("IOException thrown : " + e.getMessage());
0844: } finally {
0845: // Support_Resources.deleteTempFolder(resources);
0846: }
0847: }
0848:
0849: // Regression test for HARMONY-1718
0850: public void test_getContent1718() throws IOException {
0851: URL url;
0852:
0853: url = Support_Resources.class
0854: .getResource(Support_Resources.RESOURCE_PACKAGE
0855: + "Harmony.GIF");
0856: assertTrue(
0857: "Returned object doesn't implement ImageProducer interface",
0858: url.getContent() instanceof ImageProducer);
0859:
0860: url = Support_Resources.class
0861: .getResource(Support_Resources.RESOURCE_PACKAGE
0862: + "Harmony.jpg");
0863: assertTrue(
0864: "Returned object doesn't implement ImageProducer interface",
0865: url.getContent() instanceof ImageProducer);
0866:
0867: url = Support_Resources.class
0868: .getResource(Support_Resources.RESOURCE_PACKAGE
0869: + "Harmony.png");
0870: assertTrue(
0871: "Returned object doesn't implement ImageProducer interface",
0872: url.getContent() instanceof ImageProducer);
0873: }
0874:
0875: /**
0876: * @tests java.net.URL#getContent(class[])
0877: */
0878: public void test_getContent_LJavaLangClass() throws Exception {
0879: byte[] ba;
0880: InputStream is;
0881: String s;
0882:
0883: File resources = Support_Resources.createTempFolder();
0884:
0885: Support_Resources.copyFile(resources, null,
0886: "hyts_htmltest.html");
0887: u = new URL("file", "", resources.getAbsolutePath()
0888: + "/hyts_htmltest.html");
0889: u.openConnection();
0890:
0891: is = (InputStream) u.getContent(new Class[] { Object.class });
0892: is.read(ba = new byte[4096]);
0893: s = new String(ba);
0894: assertTrue(
0895: "Incorrect content "
0896: + u
0897: + " does not contain: \" A Seemingly Non Important String \"",
0898: s.indexOf("A Seemingly Non Important String") >= 0);
0899:
0900: }
0901:
0902: /**
0903: * @tests java.net.URL#openStream()
0904: */
0905: public void test_openStream() throws Exception {
0906: // Regression test for Harmony-1700
0907: URL BASE = URLTest.class.getClassLoader().getResource(
0908: URLTest.class.getPackage().getName().replace('.',
0909: File.separatorChar)
0910: + "/lf.jar");
0911: URL url = new URL("jar:" + BASE
0912: + "!/foo.jar!/Bugs/HelloWorld.class");
0913: try {
0914: url.openStream();
0915: fail("should throw FNFE.");
0916: } catch (java.io.FileNotFoundException e) {
0917: // Expected
0918: }
0919:
0920: // Test for method java.io.InputStream java.net.URL.openStream()
0921: File resources = Support_Resources.createTempFolder();
0922: Support_Resources.copyFile(resources, null,
0923: "hyts_htmltest.html");
0924: u = new URL("file", "", resources.getAbsolutePath()
0925: + "/hyts_htmltest.html");
0926: // HTTP connection
0927: InputStream is1 = u.openStream();
0928: assertTrue("Unable to read from stream", is1.read() != 0);
0929: is1.close();
0930:
0931: boolean exception = false;
0932: try {
0933: u = new URL("file:///nonexistenttestdir/tstfile");
0934: u.openStream();
0935: } catch (IOException e) {
0936: // Correct behaviour
0937: exception = true;
0938: }
0939: assertTrue("openStream succeeded for non existent resource",
0940: exception);
0941:
0942: int port = Support_Jetty
0943: .startHttpServerWithDocRoot("./src/test/resources/org/apache/harmony/luni/tests/java/net/");
0944: URL u = new URL("jar:" + "http://localhost:" + port
0945: + "/lf.jar!/plus.bmp");
0946: InputStream in = u.openStream();
0947: byte[] buf = new byte[3];
0948: int result = in.read(buf);
0949: assertTrue("Incompete read: " + result, result == 3);
0950: in.close();
0951: assertTrue("Returned incorrect data", buf[0] == 0x42
0952: && buf[1] == 0x4d && buf[2] == (byte) 0xbe);
0953:
0954: // FIXME Lack of FTP server, comment it out temporarily
0955: /*
0956: * u = new URL("ftp://" + Support_Configuration.FTPTestAddress +
0957: * "/nettest.txt"); in = u.openStream(); buf = new byte[3];
0958: * assertEquals("Incompete read 2", 3, in.read(buf)); in.close();
0959: * assertTrue("Returned incorrect data 2", buf[0] == 0x54 && buf[1] ==
0960: * 0x68 && buf[2] == 0x69);
0961: */
0962:
0963: File test = new File("hytest.$$$");
0964: FileOutputStream out = new FileOutputStream(test);
0965: out.write(new byte[] { 0x55, (byte) 0xaa, 0x14 });
0966: out.close();
0967: u = new URL("file:" + test.getName());
0968: in = u.openStream();
0969: buf = new byte[3];
0970: result = in.read(buf);
0971: in.close();
0972: test.delete();
0973: assertEquals("Incompete read 3", 3, result);
0974: assertTrue("Returned incorrect data 3", buf[0] == 0x55
0975: && buf[1] == (byte) 0xaa && buf[2] == 0x14);
0976: }
0977:
0978: /**
0979: * @tests java.net.URL#openConnection()
0980: */
0981: public void test_openConnection() {
0982: // Test for method java.net.URLConnection java.net.URL.openConnection()
0983: try {
0984: u = new URL("systemresource:/FILE4/+/types.properties");
0985: URLConnection uConn = u.openConnection();
0986: assertNotNull("u.openConnection() returns null", uConn);
0987: } catch (Exception e) {
0988: }
0989: }
0990:
0991: /**
0992: * @tests java.net.URL#toString()
0993: */
0994: public void test_toString() {
0995: // Test for method java.lang.String java.net.URL.toString()
0996: try {
0997: u1 = new URL("http://www.yahoo2.com:9999");
0998: u = new URL(
0999: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
1000: assertEquals(
1001: "a) Does not return the right url string",
1002:
1003: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1",
1004: u.toString());
1005: assertEquals("b) Does not return the right url string",
1006: "http://www.yahoo2.com:9999", u1.toString());
1007: assertTrue("c) Does not return the right url string", u
1008: .equals(new URL(u.toString())));
1009: } catch (Exception e) {
1010: }
1011: }
1012:
1013: /**
1014: * @tests java.net.URL#toExternalForm()
1015: */
1016: public void test_toExternalForm() {
1017: // Test for method java.lang.String java.net.URL.toExternalForm()
1018: try {
1019: u1 = new URL("http://www.yahoo2.com:9999");
1020: u = new URL(
1021: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
1022: assertEquals(
1023: "a) Does not return the right url string",
1024:
1025: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1",
1026: u.toString());
1027: assertEquals("b) Does not return the right url string",
1028: "http://www.yahoo2.com:9999", u1.toString());
1029: assertTrue("c) Does not return the right url string", u
1030: .equals(new URL(u.toString())));
1031:
1032: u = new URL("http:index");
1033: assertEquals("2 wrong external form", "http:index", u
1034: .toExternalForm());
1035:
1036: u = new URL("http", null, "index");
1037: assertEquals("2 wrong external form", "http:index", u
1038: .toExternalForm());
1039: } catch (Exception e) {
1040: }
1041: }
1042:
1043: /**
1044: * @tests java.net.URL#getFile()
1045: */
1046: public void test_getFile() throws Exception {
1047: // Test for method java.lang.String java.net.URL.getFile()
1048: u = new URL("http", "www.yahoo.com:8080", 1233,
1049: "test/!@$%^&*/test.html#foo");
1050: assertEquals("returns a wrong file", "test/!@$%^&*/test.html",
1051: u.getFile());
1052: u = new URL("http", "www.yahoo.com:8080", 1233, "");
1053: assertTrue("returns a wrong file", u.getFile().equals(""));
1054: }
1055:
1056: /**
1057: * @tests java.net.URL#getHost()
1058: */
1059: public void test_getHost() throws MalformedURLException {
1060: // Regression for HARMONY-60
1061: String ipv6Host = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210";
1062: URL url = new URL("http", ipv6Host, -1, "myfile");
1063: assertEquals(("[" + ipv6Host + "]"), url.getHost());
1064: }
1065:
1066: /**
1067: * @tests java.net.URL#getPort()
1068: */
1069: public void test_getPort() throws Exception {
1070: // Test for method int java.net.URL.getPort()
1071: u = new URL("http://member12.c++.com:9999");
1072: assertTrue("return wrong port number " + u.getPort(), u
1073: .getPort() == 9999);
1074: u = new URL("http://member12.c++.com:9999/");
1075: assertEquals("return wrong port number", 9999, u.getPort());
1076: }
1077:
1078: /**
1079: * @throws MalformedURLException
1080: * @tests java.net.URL#getDefaultPort()
1081: */
1082: public void test_getDefaultPort() throws MalformedURLException {
1083: u = new URL("http://member12.c++.com:9999");
1084: assertEquals(80, u.getDefaultPort());
1085: u = new URL("ftp://member12.c++.com:9999/");
1086: assertEquals(21, u.getDefaultPort());
1087: }
1088:
1089: /**
1090: * @tests java.net.URL#getProtocol()
1091: */
1092: public void test_getProtocol() throws Exception {
1093: // Test for method java.lang.String java.net.URL.getProtocol()
1094: u = new URL("http://www.yahoo2.com:9999");
1095: assertTrue("u returns a wrong protocol: " + u.getProtocol(), u
1096: .getProtocol().equals("http"));
1097: }
1098:
1099: /**
1100: * @tests java.net.URL#getRef()
1101: */
1102: public void test_getRef() {
1103: // Test for method java.lang.String java.net.URL.getRef()
1104: try {
1105: u1 = new URL("http://www.yahoo2.com:9999");
1106: u = new URL(
1107: "http://www.yahoo1.com:8080/dir1/dir2/test.cgi?point1.html#anchor1");
1108: assertEquals("returns a wrong anchor1", "anchor1", u
1109: .getRef());
1110: assertNull("returns a wrong anchor2", u1.getRef());
1111: u1 = new URL("http://www.yahoo2.com#ref");
1112: assertEquals("returns a wrong anchor3", "ref", u1.getRef());
1113: u1 = new URL("http://www.yahoo2.com/file#ref1#ref2");
1114: assertEquals("returns a wrong anchor4", "ref1#ref2", u1
1115: .getRef());
1116: } catch (MalformedURLException e) {
1117: fail("Incorrect URL format : " + e.getMessage());
1118: }
1119: }
1120:
1121: /**
1122: * @tests java.net.URL#getAuthority()
1123: */
1124: public void test_getAuthority() throws MalformedURLException {
1125: URL testURL = new URL("http", "hostname", 80, "/java?q1#ref");
1126: assertEquals("hostname:80", testURL.getAuthority());
1127: assertEquals("hostname", testURL.getHost());
1128: assertNull(testURL.getUserInfo());
1129: assertEquals("/java?q1", testURL.getFile());
1130: assertEquals("/java", testURL.getPath());
1131: assertEquals("q1", testURL.getQuery());
1132: assertEquals("ref", testURL.getRef());
1133:
1134: testURL = new URL("http", "u:p@home", 80, "/java?q1#ref");
1135: assertEquals("[u:p@home]:80", testURL.getAuthority());
1136: assertEquals("[u:p@home]", testURL.getHost());
1137: assertNull(testURL.getUserInfo());
1138: assertEquals("/java?q1", testURL.getFile());
1139: assertEquals("/java", testURL.getPath());
1140: assertEquals("q1", testURL.getQuery());
1141: assertEquals("ref", testURL.getRef());
1142:
1143: testURL = new URL("http", "home", -1, "/java");
1144: assertEquals("wrong authority2", "home", testURL.getAuthority());
1145: assertNull("wrong userInfo2", testURL.getUserInfo());
1146: assertEquals("wrong host2", "home", testURL.getHost());
1147: assertEquals("wrong file2", "/java", testURL.getFile());
1148: assertEquals("wrong path2", "/java", testURL.getPath());
1149: assertNull("wrong query2", testURL.getQuery());
1150: assertNull("wrong ref2", testURL.getRef());
1151: }
1152:
1153: /**
1154: * @tests java.net.URL#toURL()
1155: */
1156: public void test_toURI() throws Exception {
1157: u = new URL("http://www.apache.org");
1158: URI uri = u.toURI();
1159: assertTrue(u.equals(uri.toURL()));
1160: }
1161:
1162: /**
1163: * @tests java.net.URL#openConnection()
1164: */
1165: public void test_openConnection_SelectorCalled()
1166: throws MalformedURLException {
1167: URL httpUrl = new URL("http://"
1168: + Support_Configuration.ProxyServerTestHost
1169: + "/cgi-bin/test.pl");
1170: URL ftpUrl = new URL("ftp://"
1171: + Support_Configuration.FTPTestAddress + "/nettest.txt");
1172: URL[] urlList = { httpUrl, ftpUrl };
1173: ProxySelector originalSelector = ProxySelector.getDefault();
1174: ProxySelector.setDefault(new MockProxySelector());
1175: try {
1176: for (int i = 0; i < urlList.length; ++i) {
1177: try {
1178: isSelectCalled = false;
1179: URLConnection conn = urlList[i].openConnection();
1180: conn.getInputStream();
1181: } catch (Exception e) {
1182: // ignore
1183: }
1184: assertTrue(
1185: "openConnection should call ProxySelector.select(), url = "
1186: + urlList[i], isSelectCalled);
1187: }
1188: } finally {
1189: ProxySelector.setDefault(originalSelector);
1190: }
1191: }
1192:
1193: /**
1194: * @throws IOException
1195: * @tests java.net.URL#openConnection(Proxy)
1196: */
1197: public void test_openConnection_proxy_SelectorCalled()
1198: throws IOException {
1199: URL httpUrl = new URL("http://"
1200: + Support_Configuration.ProxyServerTestHost
1201: + "/cgi-bin/test.pl");
1202:
1203: try {
1204: httpUrl.openConnection(null);
1205: fail("should throw IllegalArgumentException");
1206: } catch (IllegalArgumentException e) {
1207: // expected
1208: }
1209:
1210: URLConnection uc = httpUrl.openConnection(new Proxy(Type.SOCKS,
1211: new InetSocketAddress(InetAddress.getLocalHost(), 80)));
1212: assertEquals(uc.getURL(), httpUrl);
1213: }
1214:
1215: /**
1216: * @tests java.net.URL#openConnection()
1217: */
1218: public void test_openConnection_Security() throws Exception {
1219: // regression test for Harmony-1049
1220: System.setSecurityManager(new SecurityManager());
1221: try {
1222: URL u = new URL("http://anyhost");
1223: // openConnection should return successfully, and no exception
1224: // should be thrown.
1225: u.openConnection();
1226: } finally {
1227: System.setSecurityManager(null);
1228: }
1229: }
1230:
1231: /**
1232: * URLStreamHandler implementation class necessary for tests.
1233: */
1234: private class TestURLStreamHandler extends URLStreamHandler {
1235: public URLConnection openConnection(URL arg0)
1236: throws IOException {
1237: try {
1238: return arg0.openConnection();
1239: } catch (Throwable e) {
1240: return null;
1241: }
1242: }
1243:
1244: public URLConnection openConnection(URL arg0, Proxy proxy)
1245: throws IOException {
1246: return super .openConnection(u, proxy);
1247: }
1248: }
1249:
1250: /**
1251: * Check UnsupportedOperationException in openConnection(URL arg0, Proxy
1252: * proxy)
1253: */
1254: public void test_openConnection_URL_Proxy() throws Exception {
1255: // Regression for HARMONY-1131
1256: TestURLStreamHandler lh = new TestURLStreamHandler();
1257: URL httpUrl = new URL("http://"
1258: + Support_Configuration.ProxyServerTestHost
1259: + "/cgi-bin/test.pl");
1260: Proxy proxy = new Proxy(Type.SOCKS, new InetSocketAddress(
1261: InetAddress.getLocalHost(), 80));
1262:
1263: try {
1264: lh.openConnection(null, null);
1265: fail("UnsupportedOperationException expected, but nothing was thrown!");
1266: } catch (UnsupportedOperationException e) {
1267: // Expected
1268: }
1269:
1270: try {
1271: lh.openConnection(httpUrl, proxy);
1272: fail("UnsupportedOperationException expected, but nothing was thrown!");
1273: } catch (UnsupportedOperationException e) {
1274: // Expected
1275: }
1276: }
1277:
1278: /**
1279: * Check NPE throwing in constructor when protocol argument is null and
1280: * URLStreamHandler argument is initialized.
1281: */
1282: public void test_ConstructorLnullLjava_lang_StringILjava_lang_StringLjava_net_URLStreamHandler()
1283: throws Exception {
1284: // Regression for HARMONY-1131
1285: TestURLStreamHandler lh = new TestURLStreamHandler();
1286:
1287: try {
1288: new URL(null, "1", 0, "file", lh);
1289: fail("NullPointerException expected, but nothing was thrown!");
1290: } catch (NullPointerException e) {
1291: // Expected NullPointerException
1292: }
1293:
1294: }
1295:
1296: /**
1297: * Check NPE throwing in constructor when protocol argument is null and
1298: * URLStreamHandler argument is null.
1299: */
1300: public void test_ConstructorLnullLjava_lang_StringILjava_lang_StringLnull()
1301: throws Exception {
1302: // Regression for HARMONY-1131
1303: try {
1304: new URL(null, "1", 0, "file", null);
1305: fail("NullPointerException expected, but nothing was thrown!");
1306: } catch (NullPointerException e) {
1307: // Expected NullPointerException
1308: }
1309: }
1310:
1311: /**
1312: * Check NPE throwing in constructor with 4 params when protocol argument is
1313: * null.
1314: */
1315: public void test_ConstructorLnullLjava_lang_StringILjava_lang_String()
1316: throws Exception {
1317: // Regression for HARMONY-1131
1318: try {
1319: new URL(null, "1", 0, "file");
1320: fail("NullPointerException expected, but nothing was thrown!");
1321: } catch (NullPointerException e) {
1322: // Expected NullPointerException
1323: }
1324: }
1325:
1326: /**
1327: * Check NPE throwing in constructor with 3 params when protocol argument is
1328: * null.
1329: */
1330: public void test_ConstructorLnullLjava_lang_StringLjava_lang_String()
1331: throws Exception {
1332: // Regression for HARMONY-1131
1333: try {
1334: new URL(null, "1", "file");
1335: fail("NullPointerException expected, but nothing was thrown!");
1336: } catch (NullPointerException e) {
1337: // Expected NullPointerException
1338: }
1339: }
1340:
1341: public void test_toExternalForm_Absolute()
1342: throws MalformedURLException {
1343: String strURL = "http://localhost?name=value";
1344: URL url = new URL(strURL);
1345: assertEquals(strURL, url.toExternalForm());
1346:
1347: strURL = "http://localhost?name=value/age=12";
1348: url = new URL(strURL);
1349: assertEquals(strURL, url.toExternalForm());
1350: }
1351:
1352: public void test_toExternalForm_Relative()
1353: throws MalformedURLException {
1354: String strURL = "http://a/b/c/d;p?q";
1355: String ref = "?y";
1356: URL url = new URL(new URL(strURL), ref);
1357: assertEquals("http://a/b/c/?y", url.toExternalForm());
1358:
1359: }
1360:
1361: static class MockProxySelector extends ProxySelector {
1362:
1363: public void connectFailed(URI uri, SocketAddress sa,
1364: IOException ioe) {
1365: System.out.println("connection failed");
1366: }
1367:
1368: public List<Proxy> select(URI uri) {
1369: isSelectCalled = true;
1370: ArrayList<Proxy> proxyList = new ArrayList<Proxy>(1);
1371: proxyList.add(Proxy.NO_PROXY);
1372: return proxyList;
1373: }
1374: }
1375:
1376: static class MockSecurityManager extends SecurityManager {
1377:
1378: public void checkConnect(String host, int port) {
1379: if ("127.0.0.1".equals(host)) {
1380: throw new SecurityException("permission is not allowed");
1381: }
1382: }
1383:
1384: public void checkPermission(Permission permission) {
1385: if ("setSecurityManager".equals(permission.getName())) {
1386: return;
1387: }
1388: super .checkPermission(permission);
1389: }
1390:
1391: }
1392:
1393: static class MyURLStreamHandler extends URLStreamHandler {
1394:
1395: @Override
1396: protected URLConnection openConnection(URL arg0)
1397: throws IOException {
1398: return null;
1399: }
1400:
1401: public void parse(URL url, String spec, int start, int end) {
1402: parseURL(url, spec, start, end);
1403: }
1404: }
1405:
1406: static class MyURLStreamHandlerFactory implements
1407: URLStreamHandlerFactory {
1408:
1409: public static MyURLStreamHandler handler = new MyURLStreamHandler();
1410:
1411: public URLStreamHandler createURLStreamHandler(String arg0) {
1412: handler = new MyURLStreamHandler();
1413: return handler;
1414: }
1415:
1416: }
1417:
1418: // Regression test for harmony-2941
1419: public void test_URLStreamHandler_parseURL()
1420: throws MalformedURLException {
1421: URL url = new URL("http://localhost");
1422: MyURLStreamHandler handler = MyURLStreamHandlerFactory.handler;
1423: try {
1424: handler.parse(url, "//", 0, Integer.MIN_VALUE);
1425: fail("Should throw SIOOBE.");
1426: } catch (StringIndexOutOfBoundsException e) {
1427: // expected;
1428: }
1429: try {
1430: handler.parse(url, "1234//", 4, Integer.MIN_VALUE);
1431: fail("Should throw SIOOBE.");
1432: } catch (StringIndexOutOfBoundsException e) {
1433: // expected;
1434: }
1435: try {
1436: handler.parse(url, "1", -1, 0);
1437: fail("Should throw SIOOBE.");
1438: } catch (StringIndexOutOfBoundsException e) {
1439: // expected;
1440: }
1441: try {
1442: handler.parse(url, "1", 3, 2);
1443: fail("Should throw SecurityException.");
1444: } catch (SecurityException e) {
1445: // expected;
1446: }
1447:
1448: try {
1449: handler.parse(url, "11", 1, Integer.MIN_VALUE);
1450: fail("Should throw SecurityException.");
1451: } catch (SecurityException e) {
1452: // expected;
1453: }
1454: }
1455:
1456: /**
1457: * @tests java.net.URL#URL(String, String, String)
1458: */
1459: public void test_java_protocol_handler_pkgs_prop()
1460: throws MalformedURLException {
1461: // Regression test for Harmony-3094
1462: final String HANDLER_PKGS = "java.protocol.handler.pkgs";
1463: String pkgs = System.getProperty(HANDLER_PKGS);
1464: System.setProperty(HANDLER_PKGS,
1465: "fake|org.apache.harmony.luni.tests.java.net");
1466:
1467: try {
1468: new URL("test_protocol", "", "fake.jar");
1469: } finally {
1470: if (pkgs == null) {
1471: System.clearProperty(HANDLER_PKGS);
1472: } else {
1473: System.setProperty(HANDLER_PKGS, pkgs);
1474: }
1475: }
1476: }
1477: }
|