J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4768755 4677045 |
| 27 | * @summary URL.equal(URL) is inconsistant for opaque URI.toURL() |
| 28 | * and new URL(URI.toString) |
| 29 | * URI.toURL() does not always work as specified |
| 30 | */ |
| 31 | |
| 32 | import java.net.*; |
| 33 | |
| 34 | public class URItoURLTest { |
| 35 | |
| 36 | public static void main(String args[]) throws Exception { |
| 37 | |
| 38 | URItoURLTest testClass = new URItoURLTest(); |
| 39 | URL classUrl = testClass.getClass(). |
| 40 | getResource("/java/lang/Object.class"); |
| 41 | |
| 42 | String[] uris = { "mailto:xyz@abc.de", |
| 43 | "file:xyz#ab", |
| 44 | "http:abc/xyz/pqr", |
| 45 | "file:/C:/v700/dev/unitTesting/tests/apiUtil/uri", |
| 46 | "http:///p", |
| 47 | classUrl.toExternalForm(), |
| 48 | }; |
| 49 | |
| 50 | boolean isTestFailed = false; |
| 51 | boolean isURLFailed = false; |
| 52 | |
| 53 | for (int i = 0; i < uris.length; i++) { |
| 54 | URI uri = URI.create(uris[i]); |
| 55 | |
| 56 | URL url1 = new URL(uri.toString()); |
| 57 | URL url2 = uri.toURL(); |
| 58 | System.out.println("Testing URI " + uri); |
| 59 | |
| 60 | if (!url1.equals(url2)) { |
| 61 | System.out.println("equals() FAILED"); |
| 62 | isURLFailed = true; |
| 63 | } |
| 64 | if (url1.hashCode() != url2.hashCode()) { |
| 65 | System.out.println("hashCode() DIDN'T MATCH"); |
| 66 | isURLFailed = true; |
| 67 | } |
| 68 | if (!url1.sameFile(url2)) { |
| 69 | System.out.println("sameFile() FAILED"); |
| 70 | isURLFailed = true; |
| 71 | } |
| 72 | |
| 73 | if (!equalsComponents("getPath()", url1.getPath(), |
| 74 | url2.getPath())) { |
| 75 | isURLFailed = true; |
| 76 | } |
| 77 | if (!equalsComponents("getFile()", url1.getFile(), |
| 78 | url2.getFile())) { |
| 79 | isURLFailed = true; |
| 80 | } |
| 81 | if (!equalsComponents("getHost()", url1.getHost(), |
| 82 | url2.getHost())) { |
| 83 | isURLFailed = true; |
| 84 | } |
| 85 | if (!equalsComponents("getAuthority()", |
| 86 | url1.getAuthority(), url2.getAuthority())) { |
| 87 | isURLFailed = true; |
| 88 | } |
| 89 | if (!equalsComponents("getRef()", url1.getRef(), |
| 90 | url2.getRef())) { |
| 91 | isURLFailed = true; |
| 92 | } |
| 93 | if (!equalsComponents("getUserInfo()", url1.getUserInfo(), |
| 94 | url2.getUserInfo())) { |
| 95 | isURLFailed = true; |
| 96 | } |
| 97 | if (!equalsComponents("toString()", url1.toString(), |
| 98 | url2.toString())) { |
| 99 | isURLFailed = true; |
| 100 | } |
| 101 | |
| 102 | if (isURLFailed) { |
| 103 | isTestFailed = true; |
| 104 | } else { |
| 105 | System.out.println("PASSED .."); |
| 106 | } |
| 107 | System.out.println(); |
| 108 | isURLFailed = false; |
| 109 | } |
| 110 | if (isTestFailed) { |
| 111 | throw new Exception("URI.toURL() test failed"); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static boolean equalsComponents(String method, String comp1, String comp2) { |
| 116 | if ((comp1 != null) && (!comp1.equals(comp2))) { |
| 117 | System.out.println(method + " DIDN'T MATCH" + |
| 118 | " ===>"); |
| 119 | System.out.println(" URL(URI.toString()) returns:" + comp1); |
| 120 | System.out.println(" URI.toURL() returns:" + comp2); |
| 121 | return false; |
| 122 | } |
| 123 | return true; |
| 124 | } |
| 125 | } |