1/* 2 * Copyright (c) 2002, 2007, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * @test 26 * @bug 4768755 4677045 8147462 27 * @summary URL.equal(URL) is inconsistent for opaque URI.toURL() 28 * and new URL(URI.toString) 29 * URI.toURL() does not always work as specified 30 * Ensure URIs representing invalid/malformed URLs throw similar 31 * exception with new URL(URI.toString()) and URI.toURL() 32 */ 33 34import java.net.*; 35import java.util.Objects; 36 37public class URItoURLTest { 38 39 public static void main(String args[]) throws Exception { 40 41 URL classUrl = new URL("jrt:/java.base/java/lang/Object.class"); 42 43 String[] uris = { 44 "mailto:xyz@abc.de", 45 "file:xyz#ab", 46 "http:abc/xyz/pqr", 47 "http:abc/xyz/pqr?id=x%0a&ca=true", 48 "file:/C:/v700/dev/unitTesting/tests/apiUtil/uri", 49 "http:///p", 50 "file:/C:/v700/dev/unitTesting/tests/apiUtil/uri", 51 "file:/C:/v700/dev%20src/unitTesting/tests/apiUtil/uri", 52 "file:/C:/v700/dev%20src/./unitTesting/./tests/apiUtil/uri", 53 "http://localhost:80/abc/./xyz/../pqr?id=x%0a&ca=true", 54 "file:./test/./x", 55 "file:./././%20#i=3", 56 "file:?hmm", 57 "file:.#hmm", 58 classUrl.toExternalForm(), 59 }; 60 61 // Strings that represent valid URIs but invalid URLs that should throw 62 // MalformedURLException both when calling toURL and new URL(String) 63 String[] malformedUrls = { 64 "test:/test", 65 "fiel:test", 66 }; 67 68 // Non-absolute URIs should throw IAE when calling toURL but will throw 69 // MalformedURLException when calling new URL 70 String[] illegalUris = { 71 "./test", 72 "/test", 73 }; 74 75 boolean isTestFailed = false; 76 boolean isURLFailed = false; 77 78 for (String uriString : uris) { 79 URI uri = URI.create(uriString); 80 81 URL url1 = new URL(uri.toString()); 82 URL url2 = uri.toURL(); 83 System.out.println("Testing URI " + uri); 84 85 if (!url1.equals(url2)) { 86 System.out.println("equals() FAILED"); 87 isURLFailed = true; 88 } 89 if (url1.hashCode() != url2.hashCode()) { 90 System.out.println("hashCode() DIDN'T MATCH"); 91 isURLFailed = true; 92 } 93 if (!url1.sameFile(url2)) { 94 System.out.println("sameFile() FAILED"); 95 isURLFailed = true; 96 } 97 98 if (!equalsComponents("getPath()", url1.getPath(), 99 url2.getPath())) { 100 isURLFailed = true; 101 } 102 if (!equalsComponents("getFile()", url1.getFile(), 103 url2.getFile())) { 104 isURLFailed = true; 105 } 106 if (!equalsComponents("getHost()", url1.getHost(), 107 url2.getHost())) { 108 isURLFailed = true; 109 } 110 if (!equalsComponents("getAuthority()", 111 url1.getAuthority(), url2.getAuthority())) { 112 isURLFailed = true; 113 } 114 if (!equalsComponents("getRef()", url1.getRef(), 115 url2.getRef())) { 116 isURLFailed = true; 117 } 118 if (!equalsComponents("getUserInfo()", url1.getUserInfo(), 119 url2.getUserInfo())) { 120 isURLFailed = true; 121 } 122 if (!equalsComponents("toString()", url1.toString(), 123 url2.toString())) { 124 isURLFailed = true; 125 } 126 127 if (isURLFailed) { 128 isTestFailed = true; 129 } else { 130 System.out.println("PASSED .."); 131 } 132 System.out.println(); 133 isURLFailed = false; 134 } 135 for (String malformedUrl : malformedUrls) { 136 Exception toURLEx = null; 137 Exception newURLEx = null; 138 try { 139 new URI(malformedUrl).toURL(); 140 } catch (Exception e) { 141 // expected 142 toURLEx = e; 143 } 144 try { 145 new URL(new URI(malformedUrl).toString()); 146 } catch (Exception e) { 147 // expected 148 newURLEx = e; 149 } 150 if (!(toURLEx instanceof MalformedURLException) || 151 !(newURLEx instanceof MalformedURLException) || 152 !toURLEx.getMessage().equals(newURLEx.getMessage())) { 153 isTestFailed = true; 154 System.out.println("Expected the same MalformedURLException: " + 155 newURLEx + " vs " + toURLEx); 156 } 157 } 158 for (String illegalUri : illegalUris) { 159 try { 160 new URI(illegalUri).toURL(); 161 } catch (IllegalArgumentException e) { 162 // pass 163 } 164 165 try { 166 new URL(illegalUri); 167 } catch (MalformedURLException e) { 168 // pass 169 } 170 } 171 if (isTestFailed) { 172 throw new Exception("URI.toURL() test failed"); 173 } 174 } 175 176 static boolean equalsComponents(String method, String comp1, String comp2) { 177 if ((comp1 != null) && (!comp1.equals(comp2))) { 178 System.out.println(method + " DIDN'T MATCH" + 179 " ===>"); 180 System.out.println(" URL(URI.toString()) returns:" + comp1); 181 System.out.println(" URI.toURL() returns:" + comp2); 182 return false; 183 } 184 return true; 185 } 186} 187