2
|
1 |
/*
|
|
2 |
* Copyright 2004-2005 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 5057532
|
|
27 |
* @ignore Test will fail until 6338951 is resolved (java.net.URI now
|
|
28 |
* accepts "http://-a").
|
|
29 |
* @summary Tests that host names are parsed correctly in URLs
|
|
30 |
* @author Eamonn McManus
|
|
31 |
* @run clean URLTest
|
|
32 |
* @run build URLTest
|
|
33 |
* @run main URLTest
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.net.MalformedURLException;
|
|
37 |
import java.net.URISyntaxException;
|
|
38 |
import java.net.URI;
|
|
39 |
import javax.management.remote.JMXServiceURL;
|
|
40 |
|
|
41 |
public class URLTest {
|
|
42 |
private static final String[] good = {
|
|
43 |
"",
|
|
44 |
"a",
|
|
45 |
"a.b",
|
|
46 |
"a.b.c.d.e.f.g",
|
|
47 |
"aaa.bbb",
|
|
48 |
"a-a.b-b",
|
|
49 |
"a-a",
|
|
50 |
"a--b",
|
|
51 |
"1.2.3.4",
|
|
52 |
"1.2.3.x",
|
|
53 |
"111.222.222.111",
|
|
54 |
"1",
|
|
55 |
"23skiddoo",
|
|
56 |
"23skiddoo.sfbay",
|
|
57 |
"a1.b2",
|
|
58 |
"1234.sfbay",
|
|
59 |
"[::]",
|
|
60 |
"[ffff::ffff]",
|
|
61 |
};
|
|
62 |
private static final String[] bad = {
|
|
63 |
"-a",
|
|
64 |
"a-",
|
|
65 |
"-",
|
|
66 |
"_",
|
|
67 |
"a_b",
|
|
68 |
"a_b.sfbay",
|
|
69 |
".",
|
|
70 |
"..",
|
|
71 |
".a",
|
|
72 |
"a.",
|
|
73 |
"a..",
|
|
74 |
"a..b",
|
|
75 |
".a.b",
|
|
76 |
"a.b.",
|
|
77 |
"a.b..",
|
|
78 |
"1.2",
|
|
79 |
"111.222.333.444",
|
|
80 |
"a.23skiddoo",
|
|
81 |
"[:::]",
|
|
82 |
"[:]",
|
|
83 |
};
|
|
84 |
|
|
85 |
public static void main(String[] args) throws Exception {
|
|
86 |
System.out.println("Testing that JMXServiceURL accepts the same " +
|
|
87 |
"hosts as java.net.URI");
|
|
88 |
System.out.println("(Except that it allows empty host names and " +
|
|
89 |
"forbids a trailing \".\")");
|
|
90 |
System.out.println();
|
|
91 |
|
|
92 |
int failures = 0;
|
|
93 |
|
|
94 |
for (int pass = 1; pass <= 2; pass++) {
|
|
95 |
final boolean accept = (pass == 1);
|
|
96 |
System.out.println(" Hosts that should " +
|
|
97 |
(accept ? "" : "not ") + "work");
|
|
98 |
String[] hosts = accept ? good : bad;
|
|
99 |
|
|
100 |
for (int i = 0; i < hosts.length; i++) {
|
|
101 |
final String host = hosts[i];
|
|
102 |
System.out.print(" " + host + ": ");
|
|
103 |
|
|
104 |
boolean jmxAccept = true;
|
|
105 |
try {
|
|
106 |
new JMXServiceURL("rmi", hosts[i], 0);
|
|
107 |
} catch (MalformedURLException e) {
|
|
108 |
jmxAccept = false;
|
|
109 |
}
|
|
110 |
|
|
111 |
boolean uriAccept;
|
|
112 |
try {
|
|
113 |
final URI uri = new URI("http://" + host + "/");
|
|
114 |
uriAccept = (uri.getHost() != null);
|
|
115 |
} catch (URISyntaxException e) {
|
|
116 |
uriAccept = false;
|
|
117 |
}
|
|
118 |
|
|
119 |
final int len = host.length();
|
|
120 |
if (accept != uriAccept && len != 0 &&
|
|
121 |
!(len > 1 && host.charAt(len - 1) == '.'
|
|
122 |
&& host.charAt(len - 2) != '.')) {
|
|
123 |
// JMXServiceURL allows empty host name; also
|
|
124 |
// java.net.URI allows trailing dot in hostname,
|
|
125 |
// following RFC 2396, but JMXServiceURL doesn't,
|
|
126 |
// following RFC 2609
|
|
127 |
System.out.println("TEST BUG: URI accept=" + uriAccept);
|
|
128 |
failures++;
|
|
129 |
} else {
|
|
130 |
if (jmxAccept == accept)
|
|
131 |
System.out.println("OK");
|
|
132 |
else {
|
|
133 |
System.out.println("FAILED");
|
|
134 |
failures++;
|
|
135 |
}
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
System.out.println();
|
|
140 |
}
|
|
141 |
|
|
142 |
if (failures == 0)
|
|
143 |
System.out.println("Test passed");
|
|
144 |
else {
|
|
145 |
System.out.println("TEST FAILURES: " + failures);
|
|
146 |
System.exit(1);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|