Path: blob/master/test/jdk/java/net/URI/URItoURLTest.java
41149 views
/*1* Copyright (c) 2002, 2007, 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 4768755 4677045 814746226* @summary URL.equal(URL) is inconsistent for opaque URI.toURL()27* and new URL(URI.toString)28* URI.toURL() does not always work as specified29* Ensure URIs representing invalid/malformed URLs throw similar30* exception with new URL(URI.toString()) and URI.toURL()31*/3233import java.net.*;34import java.util.Objects;3536public class URItoURLTest {3738public static void main(String args[]) throws Exception {3940URL classUrl = new URL("jrt:/java.base/java/lang/Object.class");4142String[] uris = {43"mailto:[email protected]",44"file:xyz#ab",45"http:abc/xyz/pqr",46"http:abc/xyz/pqr?id=x%0a&ca=true",47"file:/C:/v700/dev/unitTesting/tests/apiUtil/uri",48"http:///p",49"file:/C:/v700/dev/unitTesting/tests/apiUtil/uri",50"file:/C:/v700/dev%20src/unitTesting/tests/apiUtil/uri",51"file:/C:/v700/dev%20src/./unitTesting/./tests/apiUtil/uri",52"http://localhost:80/abc/./xyz/../pqr?id=x%0a&ca=true",53"file:./test/./x",54"file:./././%20#i=3",55"file:?hmm",56"file:.#hmm",57classUrl.toExternalForm(),58};5960// Strings that represent valid URIs but invalid URLs that should throw61// MalformedURLException both when calling toURL and new URL(String)62String[] malformedUrls = {63"test:/test",64"fiel:test",65};6667// Non-absolute URIs should throw IAE when calling toURL but will throw68// MalformedURLException when calling new URL69String[] illegalUris = {70"./test",71"/test",72};7374boolean isTestFailed = false;75boolean isURLFailed = false;7677for (String uriString : uris) {78URI uri = URI.create(uriString);7980URL url1 = new URL(uri.toString());81URL url2 = uri.toURL();82System.out.println("Testing URI " + uri);8384if (!url1.equals(url2)) {85System.out.println("equals() FAILED");86isURLFailed = true;87}88if (url1.hashCode() != url2.hashCode()) {89System.out.println("hashCode() DIDN'T MATCH");90isURLFailed = true;91}92if (!url1.sameFile(url2)) {93System.out.println("sameFile() FAILED");94isURLFailed = true;95}9697if (!equalsComponents("getPath()", url1.getPath(),98url2.getPath())) {99isURLFailed = true;100}101if (!equalsComponents("getFile()", url1.getFile(),102url2.getFile())) {103isURLFailed = true;104}105if (!equalsComponents("getHost()", url1.getHost(),106url2.getHost())) {107isURLFailed = true;108}109if (!equalsComponents("getAuthority()",110url1.getAuthority(), url2.getAuthority())) {111isURLFailed = true;112}113if (!equalsComponents("getRef()", url1.getRef(),114url2.getRef())) {115isURLFailed = true;116}117if (!equalsComponents("getUserInfo()", url1.getUserInfo(),118url2.getUserInfo())) {119isURLFailed = true;120}121if (!equalsComponents("toString()", url1.toString(),122url2.toString())) {123isURLFailed = true;124}125126if (isURLFailed) {127isTestFailed = true;128} else {129System.out.println("PASSED ..");130}131System.out.println();132isURLFailed = false;133}134for (String malformedUrl : malformedUrls) {135Exception toURLEx = null;136Exception newURLEx = null;137try {138new URI(malformedUrl).toURL();139} catch (Exception e) {140// expected141toURLEx = e;142}143try {144new URL(new URI(malformedUrl).toString());145} catch (Exception e) {146// expected147newURLEx = e;148}149if (!(toURLEx instanceof MalformedURLException) ||150!(newURLEx instanceof MalformedURLException) ||151!toURLEx.getMessage().equals(newURLEx.getMessage())) {152isTestFailed = true;153System.out.println("Expected the same MalformedURLException: " +154newURLEx + " vs " + toURLEx);155}156}157for (String illegalUri : illegalUris) {158try {159new URI(illegalUri).toURL();160} catch (IllegalArgumentException e) {161// pass162}163164try {165new URL(illegalUri);166} catch (MalformedURLException e) {167// pass168}169}170if (isTestFailed) {171throw new Exception("URI.toURL() test failed");172}173}174175static boolean equalsComponents(String method, String comp1, String comp2) {176if ((comp1 != null) && (!comp1.equals(comp2))) {177System.out.println(method + " DIDN'T MATCH" +178" ===>");179System.out.println(" URL(URI.toString()) returns:" + comp1);180System.out.println(" URI.toURL() returns:" + comp2);181return false;182}183return true;184}185}186187188