Path: blob/master/test/jdk/java/net/URL/Constructor.java
41149 views
/*1* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 439367126* @summary URL constructor URL(URL context, String spec) FAILED with specific input27*/2829/*30* This program tests the URL parser in the URL constructor. It31* tries to construct a variety of valid URLs with a given context32* (which may be null) and a variety of specs. It then compares the33* result with an expected value.34*/3536import java.net.MalformedURLException;37import java.net.URL;38import java.util.ArrayList;39import java.util.Arrays;40import java.util.List;4142public class Constructor {4344public static void main(String[] args) throws Exception {45List<Entry> entries = new ArrayList<>();46entries.addAll(Arrays.asList(fileURLs));47entries.addAll(Arrays.asList(jarURLs));48entries.addAll(Arrays.asList(normalHttpURLs));49entries.addAll(Arrays.asList(abnormalHttpURLs));50if (hasFtp())51entries.addAll(Arrays.asList(ftpURLs));52URL url;5354for (Entry e : entries) {55if (e.context == null)56url = new URL(e.spec);57else58url = new URL(new URL(e.context), e.spec);5960if (!(url.toString().equals(e.expected))) {61throw new RuntimeException("error for: \n\tURL:" + e.context +62"\n\tspec: " + e.spec +63"\n\texpected: " + e.expected +64"\n\tactual: " + url.toString());65} else {66//debug67//System.out.println("success for: " + url);68}69}70}7172private static boolean hasFtp() {73try {74return new java.net.URL("ftp://") != null;75} catch (java.net.MalformedURLException x) {76System.out.println("FTP not supported by this runtime.");77return false;78}79}8081static class Entry {82final String context;83final String spec;84final String expected;85Entry(String context, String spec, String expected) {86this.context = context;87this.spec =spec;88this.expected = expected;89}90}9192static Entry[] fileURLs = new Entry[] {93new Entry(null,94"file://JavaSoft/Test",95"file://JavaSoft/Test"),96new Entry(null,97"file:///JavaSoft/Test",98"file:/JavaSoft/Test"),99new Entry(null,100"file:/JavaSoft/Test",101"file:/JavaSoft/Test"),102new Entry(null,103"file:/c:/JavaSoft/Test",104"file:/c:/JavaSoft/Test"),105new Entry(null,106"file:/c:/JavaSoft/Test:something",107"file:/c:/JavaSoft/Test:something"),108new Entry(null,109"file:/c:/JavaSoft/Test#anchor",110"file:/c:/JavaSoft/Test#anchor"),111new Entry("file://JavaSoft/Test",112"Test#bar",113"file://JavaSoft/Test#bar"),114new Entry("file://codrus/c:/jdk/eng/index.html",115"pulsar.html",116"file://codrus/c:/jdk/eng/pulsar.html"),117new Entry("file:///c:/jdk/eng/index.html",118"pulsar.html",119"file:/c:/jdk/eng/pulsar.html"),120new Entry("file:///jdk/eng/index.html",121"pulsar.html",122"file:/jdk/eng/pulsar.html"),123new Entry("file://JavaSoft/Test",124"file://radartoad.com/Test#bar",125"file://radartoad.com/Test#bar"),126new Entry("file://JavaSoft/Test",127"/c:/Test#bar",128"file://JavaSoft/c:/Test#bar"),129};130131static Entry[] jarURLs = new Entry[] {132new Entry(null,133"jar:http://www.foo.com/dir1/jar.jar!/dir2/entry.txt",134"jar:http://www.foo.com/dir1/jar.jar!/dir2/entry.txt"),135new Entry(null,136"jar:http://www.foo.com/dir1/jar.jar!/",137"jar:http://www.foo.com/dir1/jar.jar!/"),138new Entry(null,139"jar:http://www.foo.com/dir1/jar.jar!/",140"jar:http://www.foo.com/dir1/jar.jar!/"),141new Entry("jar:http://www.foo.com/dir1/jar.jar!/",142"entry.txt",143"jar:http://www.foo.com/dir1/jar.jar!/entry.txt"),144new Entry("jar:http://www.foo.com/dir1/jar.jar!/",145"/entry.txt",146"jar:http://www.foo.com/dir1/jar.jar!/entry.txt"),147new Entry("jar:http://www.foo.com/dir1/jar.jar!/",148"dir1/entry.txt",149"jar:http://www.foo.com/dir1/jar.jar!/dir1/entry.txt"),150new Entry("jar:http://www.foo.com/dir1/jar.jar!/",151"/dir1/entry.txt",152"jar:http://www.foo.com/dir1/jar.jar!/dir1/entry.txt"),153new Entry("jar:http://www.foo.com/dir1/jar.jar!/",154"entry.txt",155"jar:http://www.foo.com/dir1/jar.jar!/entry.txt"),156new Entry("jar:http://www.foo.com/dir1/jar.jar!/",157"/entry.txt",158"jar:http://www.foo.com/dir1/jar.jar!/entry.txt"),159new Entry("jar:http://www.foo.com/dir1/jar.jar!/",160"/entry.txt",161"jar:http://www.foo.com/dir1/jar.jar!/entry.txt"),162new Entry("jar:http://www.foo.com/dir1/jar.jar!/dir1/",163"entry.txt",164"jar:http://www.foo.com/dir1/jar.jar!/dir1/entry.txt"),165new Entry("jar:http://www.foo.com/dir1/jar.jar!/dir2/dir3/entry2.txt",166"/dir1/entry.txt",167"jar:http://www.foo.com/dir1/jar.jar!/dir1/entry.txt"),168new Entry("jar:http://www.foo.com/dir1/jar.jar!/",169"/dir1/foo/entry.txt",170"jar:http://www.foo.com/dir1/jar.jar!/dir1/foo/entry.txt"),171new Entry("jar:http://www.foo.com/dir1/jar.jar!/dir1/dir2/dir3/",172"dir4/foo/entry.txt",173"jar:http://www.foo.com/dir1/jar.jar!/dir1/dir2/dir3/dir4/foo/entry.txt"),174new Entry("jar:http://www.foo.com/dir1/jar.jar!/",175"/dir1/foo/entry.txt",176"jar:http://www.foo.com/dir1/jar.jar!/dir1/foo/entry.txt"),177new Entry(null,178"jar:http://www.foo.com/dir1/jar.jar!/foo.txt#anchor",179"jar:http://www.foo.com/dir1/jar.jar!/foo.txt#anchor"),180new Entry("jar:http://www.foo.com/dir1/jar.jar!/foo.txt",181"#anchor",182"jar:http://www.foo.com/dir1/jar.jar!/foo.txt#anchor"),183new Entry("jar:http://www.foo.com/dir1/jar.jar!/foo/bar/",184"baz/quux#anchor",185"jar:http://www.foo.com/dir1/jar.jar!/foo/bar/baz/quux#anchor"),186new Entry("jar:http://balloo.com/olle.jar!/",187"p2",188"jar:http://balloo.com/olle.jar!/p2")189};190191static Entry[] normalHttpURLs = new Entry[] {192new Entry("http://a/b/c/d;p?q", "g", "http://a/b/c/g"),193new Entry("http://a/b/c/d;p?q", "./g", "http://a/b/c/g"),194new Entry("http://a/b/c/d;p?q", "g/", "http://a/b/c/g/"),195new Entry("http://a/b/c/d;p?q", "/g", "http://a/g"),196new Entry("http://a/b/c/d;p?q", "//g", "http://g"),197new Entry("http://a/b/c/d;p?q", "?y", "http://a/b/c/?y"),198new Entry("http://a/b/c/d;p?q", "g?y", "http://a/b/c/g?y"),199new Entry("http://a/b/c/d;p?q", "g#s", "http://a/b/c/g#s"),200new Entry("http://a/b/c/d;p?q", "g?y#s", "http://a/b/c/g?y#s"),201new Entry("http://a/b/c/d;p?q", ";x", "http://a/b/c/;x"),202new Entry("http://a/b/c/d;p?q", "g;x", "http://a/b/c/g;x"),203new Entry("http://a/b/c/d;p?q", "g;x?y#s", "http://a/b/c/g;x?y#s"),204new Entry("http://a/b/c/d;p?q", ".", "http://a/b/c/"),205new Entry("http://a/b/c/d;p?q", "./", "http://a/b/c/"),206new Entry("http://a/b/c/d;p?q", "..", "http://a/b/"),207new Entry("http://a/b/c/d;p?q", "../", "http://a/b/"),208new Entry("http://a/b/c/d;p?q", "../g", "http://a/b/g"),209new Entry("http://a/b/c/d;p?q", "../..", "http://a/"),210new Entry("http://a/b/c/d;p?q", "../../", "http://a/"),211new Entry("http://a/b/c/d;p?q", "../../g", "http://a/g"),212new Entry(null,213"http://www.javasoft.com/jdc/community/chat/index.html#javalive?frontpage-jdc",214"http://www.javasoft.com/jdc/community/chat/index.html#javalive?frontpage-jdc")215};216217static Entry[] abnormalHttpURLs = new Entry[] {218new Entry("http://a/b/c/d;p?q", "../../../g", "http://a/../g"),219new Entry("http://a/b/c/d;p?q", "../../../../g", "http://a/../../g"),220new Entry("http://a/b/c/d;p?q", "/./g", "http://a/./g"),221new Entry("http://a/b/c/d;p?q", "/../g", "http://a/../g"),222new Entry("http://a/b/c/d;p?q", ".g", "http://a/b/c/.g"),223new Entry("http://a/b/c/d;p?q", "g.", "http://a/b/c/g."),224new Entry("http://a/b/c/d;p?q", "./../g", "http://a/b/g"),225new Entry("http://a/b/c/d;p?q", "./g/.", "http://a/b/c/g/"),226new Entry("http://a/b/c/d;p?q", "g/./h", "http://a/b/c/g/h"),227new Entry("http://a/b/c/d;p?q", "g;x=1/./y", "http://a/b/c/g;x=1/y"),228new Entry("http://a/b/c/d;p?q", "g;x=1/../y", "http://a/b/c/y")229};230231static Entry[] ftpURLs = new Entry[] {232new Entry(null,233"ftp://ftp.foo.com/dir1/entry.txt",234"ftp://ftp.foo.com/dir1/entry.txt"),235new Entry(null,236"ftp://br:[email protected]/dir1/jar.jar",237"ftp://br:[email protected]/dir1/jar.jar"),238new Entry("ftp://ftp.foo.com/dir1/foo.txt",239"bar.txt",240"ftp://ftp.foo.com/dir1/bar.txt"),241new Entry("ftp://ftp.foo.com/dir1/jar.jar",242"/entry.txt",243"ftp://ftp.foo.com/entry.txt"),244new Entry("ftp://ftp.foo.com/dir1/jar.jar",245"dir1/entry.txt",246"ftp://ftp.foo.com/dir1/dir1/entry.txt"),247new Entry("ftp://ftp.foo.com/dir1/jar.jar",248"/dir1/entry.txt",249"ftp://ftp.foo.com/dir1/entry.txt"),250new Entry("ftp://br:[email protected]/dir1/jar.jar",251"/dir1/entry.txt",252"ftp://br:[email protected]/dir1/entry.txt")253};254}255256257