author | juh |
Tue, 25 Jun 2013 14:41:46 -0700 | |
changeset 18552 | 005e115dc6ee |
parent 15528 | 18f0bac88177 |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
25 |
* @bug 4482455 |
|
26 |
* @summary URI.toURL() implementation needs to be improved |
|
27 |
* |
|
28 |
*/ |
|
29 |
||
30 |
import java.net.*; |
|
15528
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
31 |
import java.util.ArrayList; |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
32 |
import java.util.List; |
2 | 33 |
|
34 |
public class URIToURLTest { |
|
35 |
public static void main(String args[]) throws Exception { |
|
15528
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
36 |
List<String> uris = new ArrayList<>(); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
37 |
uris.add("http://jag:cafebabe@java.sun.com:94/b/c/d?q#g"); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
38 |
uris.add("http://[1080:0:0:0:8:800:200C:417A]/index.html"); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
39 |
uris.add("http://a/b/c/d;p?q"); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
40 |
uris.add("mailto:mduerst@ifi.unizh.ch"); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
41 |
uris.add("http:comp.infosystems.www.servers.unix"); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
42 |
if (hasFtp()) |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
43 |
uris.add("ftp://ftp.is.co.za/rfc/rfc1808.txt"); |
2 | 44 |
|
15528
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
45 |
for (String uriStr : uris) { |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
46 |
URI uri = new URI(uriStr); |
2 | 47 |
URL url = uri.toURL(); |
48 |
String scheme = uri.getScheme(); |
|
49 |
boolean schemeCheck = scheme == null? url.getProtocol() == null : |
|
50 |
scheme.equals(url.getProtocol()); |
|
51 |
if (!schemeCheck) |
|
52 |
throw new RuntimeException("uri.scheme is " + scheme + |
|
53 |
" url.protocol is " + |
|
54 |
url.getProtocol()); |
|
55 |
||
56 |
if (uri.isOpaque()) { |
|
57 |
String ssp = uri.getSchemeSpecificPart(); |
|
58 |
boolean sspCheck = ssp == null? uri.getPath() == null : |
|
59 |
ssp.equals(url.getPath()); |
|
60 |
if (!sspCheck) { |
|
61 |
throw new RuntimeException("uri.ssp is " + ssp + |
|
62 |
" url.path is " + url.getPath()); |
|
63 |
} |
|
64 |
} else { |
|
65 |
String authority = uri.getAuthority(); |
|
66 |
boolean authorityCheck = authority == null? |
|
67 |
url.getAuthority() == null : |
|
68 |
authority.equals(url.getAuthority()); |
|
69 |
if (!authorityCheck) { |
|
70 |
throw new RuntimeException("uri.authority is " + |
|
71 |
authority + " url's is " + |
|
72 |
url.getAuthority()); |
|
73 |
} |
|
74 |
String host = uri.getHost(); |
|
75 |
boolean hostCheck = host == null ? url.getHost() == null : |
|
76 |
host.equals(url.getHost()); |
|
77 |
if (!hostCheck) |
|
78 |
throw new RuntimeException("uri.host is " + |
|
79 |
host + " url's is " + |
|
80 |
url.getHost()); |
|
81 |
if (host != null) { |
|
82 |
String userInfo = uri.getUserInfo(); |
|
83 |
boolean userInfoCheck = userInfo == null? |
|
84 |
url.getUserInfo() == null : |
|
85 |
userInfo.equals(url.getUserInfo()); |
|
86 |
if (uri.getPort() != url.getPort()) |
|
87 |
throw new RuntimeException("uri.port is " + |
|
88 |
uri.getPort() + " url's is " + |
|
89 |
url.getPort()); |
|
90 |
} |
|
91 |
||
92 |
String path = uri.getPath(); |
|
93 |
boolean pathCheck = path == null? url.getPath() == null : |
|
94 |
path.equals(url.getPath()); |
|
95 |
if (!pathCheck) |
|
96 |
throw new RuntimeException("uri.path is " + path + |
|
97 |
" url.path is " + |
|
98 |
url.getPath()); |
|
99 |
String query = uri.getQuery(); |
|
100 |
boolean queryCheck = query == null? url.getQuery() == null : |
|
101 |
query.equals(url.getQuery()); |
|
102 |
if (!queryCheck) |
|
103 |
throw new RuntimeException("uri.query is " + query + |
|
104 |
" url.query is " + |
|
105 |
url.getQuery()); |
|
106 |
} |
|
107 |
String frag = uri.getFragment(); |
|
108 |
boolean fragCheck = frag == null? url.getRef() == null : |
|
109 |
frag.equals(url.getRef()); |
|
110 |
if (!fragCheck) |
|
111 |
throw new RuntimeException("uri.frag is " + frag + |
|
112 |
" url.ref is " + |
|
113 |
url.getRef()); |
|
114 |
} |
|
115 |
} |
|
15528
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
116 |
|
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
117 |
private static boolean hasFtp() { |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
118 |
try { |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
119 |
return new java.net.URL("ftp://") != null; |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
120 |
} catch (java.net.MalformedURLException x) { |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
121 |
System.out.println("FTP not supported by this runtime."); |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
122 |
return false; |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
123 |
} |
18f0bac88177
8007322: untangle ftp protocol from general networking URL tests
chegar
parents:
5506
diff
changeset
|
124 |
} |
2 | 125 |
} |