0N/A/*
2362N/A * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4768755 4677045
0N/A * @summary URL.equal(URL) is inconsistant for opaque URI.toURL()
0N/A * and new URL(URI.toString)
0N/A * URI.toURL() does not always work as specified
0N/A */
0N/A
0N/Aimport java.net.*;
0N/A
0N/Apublic class URItoURLTest {
0N/A
0N/A public static void main(String args[]) throws Exception {
0N/A
0N/A URItoURLTest testClass = new URItoURLTest();
0N/A URL classUrl = testClass.getClass().
0N/A getResource("/java/lang/Object.class");
0N/A
0N/A String[] uris = { "mailto:xyz@abc.de",
0N/A "file:xyz#ab",
0N/A "http:abc/xyz/pqr",
0N/A "file:/C:/v700/dev/unitTesting/tests/apiUtil/uri",
0N/A "http:///p",
0N/A classUrl.toExternalForm(),
0N/A };
0N/A
0N/A boolean isTestFailed = false;
0N/A boolean isURLFailed = false;
0N/A
0N/A for (int i = 0; i < uris.length; i++) {
0N/A URI uri = URI.create(uris[i]);
0N/A
0N/A URL url1 = new URL(uri.toString());
0N/A URL url2 = uri.toURL();
0N/A System.out.println("Testing URI " + uri);
0N/A
0N/A if (!url1.equals(url2)) {
0N/A System.out.println("equals() FAILED");
0N/A isURLFailed = true;
0N/A }
0N/A if (url1.hashCode() != url2.hashCode()) {
0N/A System.out.println("hashCode() DIDN'T MATCH");
0N/A isURLFailed = true;
0N/A }
0N/A if (!url1.sameFile(url2)) {
0N/A System.out.println("sameFile() FAILED");
0N/A isURLFailed = true;
0N/A }
0N/A
0N/A if (!equalsComponents("getPath()", url1.getPath(),
0N/A url2.getPath())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("getFile()", url1.getFile(),
0N/A url2.getFile())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("getHost()", url1.getHost(),
0N/A url2.getHost())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("getAuthority()",
0N/A url1.getAuthority(), url2.getAuthority())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("getRef()", url1.getRef(),
0N/A url2.getRef())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("getUserInfo()", url1.getUserInfo(),
0N/A url2.getUserInfo())) {
0N/A isURLFailed = true;
0N/A }
0N/A if (!equalsComponents("toString()", url1.toString(),
0N/A url2.toString())) {
0N/A isURLFailed = true;
0N/A }
0N/A
0N/A if (isURLFailed) {
0N/A isTestFailed = true;
0N/A } else {
0N/A System.out.println("PASSED ..");
0N/A }
0N/A System.out.println();
0N/A isURLFailed = false;
0N/A }
0N/A if (isTestFailed) {
0N/A throw new Exception("URI.toURL() test failed");
0N/A }
0N/A }
0N/A
0N/A static boolean equalsComponents(String method, String comp1, String comp2) {
0N/A if ((comp1 != null) && (!comp1.equals(comp2))) {
0N/A System.out.println(method + " DIDN'T MATCH" +
0N/A " ===>");
0N/A System.out.println(" URL(URI.toString()) returns:" + comp1);
0N/A System.out.println(" URI.toURL() returns:" + comp2);
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A}