Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URI/URItoURLTest.java
47165 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 467704526* @summary URL.equal(URL) is inconsistant for opaque URI.toURL()27* and new URL(URI.toString)28* URI.toURL() does not always work as specified29*/3031import java.net.*;3233public class URItoURLTest {3435public static void main(String args[]) throws Exception {3637URItoURLTest testClass = new URItoURLTest();38URL classUrl = testClass.getClass().39getResource("/java/lang/Object.class");4041String[] uris = { "mailto:[email protected]",42"file:xyz#ab",43"http:abc/xyz/pqr",44"file:/C:/v700/dev/unitTesting/tests/apiUtil/uri",45"http:///p",46classUrl.toExternalForm(),47};4849boolean isTestFailed = false;50boolean isURLFailed = false;5152for (int i = 0; i < uris.length; i++) {53URI uri = URI.create(uris[i]);5455URL url1 = new URL(uri.toString());56URL url2 = uri.toURL();57System.out.println("Testing URI " + uri);5859if (!url1.equals(url2)) {60System.out.println("equals() FAILED");61isURLFailed = true;62}63if (url1.hashCode() != url2.hashCode()) {64System.out.println("hashCode() DIDN'T MATCH");65isURLFailed = true;66}67if (!url1.sameFile(url2)) {68System.out.println("sameFile() FAILED");69isURLFailed = true;70}7172if (!equalsComponents("getPath()", url1.getPath(),73url2.getPath())) {74isURLFailed = true;75}76if (!equalsComponents("getFile()", url1.getFile(),77url2.getFile())) {78isURLFailed = true;79}80if (!equalsComponents("getHost()", url1.getHost(),81url2.getHost())) {82isURLFailed = true;83}84if (!equalsComponents("getAuthority()",85url1.getAuthority(), url2.getAuthority())) {86isURLFailed = true;87}88if (!equalsComponents("getRef()", url1.getRef(),89url2.getRef())) {90isURLFailed = true;91}92if (!equalsComponents("getUserInfo()", url1.getUserInfo(),93url2.getUserInfo())) {94isURLFailed = true;95}96if (!equalsComponents("toString()", url1.toString(),97url2.toString())) {98isURLFailed = true;99}100101if (isURLFailed) {102isTestFailed = true;103} else {104System.out.println("PASSED ..");105}106System.out.println();107isURLFailed = false;108}109if (isTestFailed) {110throw new Exception("URI.toURL() test failed");111}112}113114static boolean equalsComponents(String method, String comp1, String comp2) {115if ((comp1 != null) && (!comp1.equals(comp2))) {116System.out.println(method + " DIDN'T MATCH" +117" ===>");118System.out.println(" URL(URI.toString()) returns:" + comp1);119System.out.println(" URI.toURL() returns:" + comp2);120return false;121}122return true;123}124}125126127