36508
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 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 |
* @library /testlibrary /test/lib /compiler/whitebox ..
|
|
27 |
* @build sun.hotspot.WhiteBox
|
|
28 |
* @compile/module=java.base java/lang/reflect/ModuleHelper.java
|
|
29 |
* @run main ClassFileInstaller sun.hotspot.WhiteBox
|
|
30 |
* sun.hotspot.WhiteBox$WhiteBoxPermission
|
|
31 |
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMDefineModule
|
|
32 |
*/
|
|
33 |
|
|
34 |
import static jdk.test.lib.Asserts.*;
|
|
35 |
|
|
36 |
public class JVMDefineModule {
|
|
37 |
|
|
38 |
public static void main(String args[]) throws Throwable {
|
|
39 |
MyClassLoader cl = new MyClassLoader();
|
|
40 |
Object m;
|
|
41 |
|
|
42 |
// NULL classloader argument, expect success
|
|
43 |
m = ModuleHelper.ModuleObject("mymodule", null, new String[] { "mypackage" });
|
|
44 |
assertNotNull(m, "Module should not be null");
|
|
45 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage" });
|
|
46 |
|
|
47 |
/* Invalid test, won't compile.
|
|
48 |
// Invalid classloader argument, expect an IAE
|
|
49 |
try {
|
|
50 |
m = ModuleHelper.ModuleObject("mymodule1", new Object(), new String[] { "mypackage1" });
|
|
51 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage1" });
|
|
52 |
throw new RuntimeException("Failed to get expected IAE for bad loader");
|
|
53 |
} catch(IllegalArgumentException e) {
|
|
54 |
// Expected
|
|
55 |
}
|
|
56 |
*/
|
|
57 |
|
|
58 |
// NULL package argument, should not throw an exception
|
|
59 |
m = ModuleHelper.ModuleObject("mymodule2", cl, new String[] { "nullpkg" });
|
|
60 |
assertNotNull(m, "Module should not be null");
|
|
61 |
ModuleHelper.DefineModule(m, "9.0", "mymodule2/here", null);
|
|
62 |
|
|
63 |
// Null module argument, expect an NPE
|
|
64 |
try {
|
|
65 |
ModuleHelper.DefineModule(null, "9.0", "mymodule/here", new String[] { "mypackage1" });
|
|
66 |
throw new RuntimeException("Failed to get expected NPE for null module");
|
|
67 |
} catch(NullPointerException e) {
|
|
68 |
if (!e.getMessage().contains("Null module object")) {
|
|
69 |
throw new RuntimeException("Failed to get expected IAE message for null module: " + e.getMessage());
|
|
70 |
}
|
|
71 |
// Expected
|
|
72 |
}
|
|
73 |
|
|
74 |
// Invalid module argument, expect an IAE
|
|
75 |
try {
|
|
76 |
ModuleHelper.DefineModule(new Object(), "9.0", "mymodule/here", new String[] { "mypackage1" });
|
|
77 |
throw new RuntimeException("Failed to get expected IAE or NPE for bad module");
|
|
78 |
} catch(IllegalArgumentException e) {
|
|
79 |
if (!e.getMessage().contains("module is not a subclass")) {
|
|
80 |
throw new RuntimeException("Failed to get expected IAE message for bad module: " + e.getMessage());
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
// NULL module name, expect an IAE or NPE
|
|
85 |
try {
|
|
86 |
m = ModuleHelper.ModuleObject(null, cl, new String[] { "mypackage2" });
|
|
87 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage2" });
|
|
88 |
throw new RuntimeException("Failed to get expected NPE for NULL module");
|
|
89 |
} catch(IllegalArgumentException e) {
|
|
90 |
// Expected
|
|
91 |
} catch(NullPointerException e) {
|
|
92 |
// Expected
|
|
93 |
}
|
|
94 |
|
|
95 |
// module name is java.base, expect an IAE
|
|
96 |
m = ModuleHelper.ModuleObject("java.base", cl, new String[] { "mypackage3" });
|
|
97 |
try {
|
|
98 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage3" });
|
|
99 |
throw new RuntimeException("Failed to get expected IAE for java.base, not defined with boot class loader");
|
|
100 |
} catch(IllegalArgumentException e) {
|
|
101 |
if (!e.getMessage().contains("Class loader must be the boot class loader")) {
|
|
102 |
throw new RuntimeException("Failed to get expected IAE message for java.base: " + e.getMessage());
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
// Duplicates in package list, expect an IAE
|
|
107 |
m = ModuleHelper.ModuleObject("module.x", cl, new String[] { "mypackage4", "mypackage5" });
|
|
108 |
try {
|
|
109 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackage4", "mypackage5", "mypackage4" });
|
|
110 |
throw new RuntimeException("Failed to get IAE for duplicate packages");
|
|
111 |
} catch(IllegalArgumentException e) {
|
|
112 |
if (!e.getMessage().contains("Duplicate package name")) {
|
|
113 |
throw new RuntimeException("Failed to get expected IAE message for duplicate package: " + e.getMessage());
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
// Empty entry in package list, expect an IAE
|
|
118 |
m = ModuleHelper.ModuleObject("module.y", cl, new String[] { "mypackageX", "mypackageY" });
|
|
119 |
try {
|
|
120 |
ModuleHelper.DefineModule(m, "9.0", "mymodule/here", new String[] { "mypackageX", "", "mypackageY" });
|
|
121 |
throw new RuntimeException("Failed to get IAE for empty package");
|
|
122 |
} catch(IllegalArgumentException e) {
|
|
123 |
if (!e.getMessage().contains("Invalid package name")) {
|
|
124 |
throw new RuntimeException("Failed to get expected IAE message empty package entry: " + e.getMessage());
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
// Duplicate module name, expect an IAE
|
|
129 |
m = ModuleHelper.ModuleObject("module.name", cl, new String[] { "mypackage6" });
|
|
130 |
assertNotNull(m, "Module should not be null");
|
|
131 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6" });
|
|
132 |
try {
|
|
133 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6a" });
|
|
134 |
throw new RuntimeException("Failed to get IAE for duplicate module");
|
|
135 |
} catch(IllegalArgumentException e) {
|
|
136 |
if (!e.getMessage().contains("Module module.name is already defined")) {
|
|
137 |
throw new RuntimeException("Failed to get expected IAE message for duplicate module: " + e.getMessage());
|
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
// Package is already defined for class loader, expect an IAE
|
|
142 |
m = ModuleHelper.ModuleObject("dupl.pkg.module", cl, new String[] { "mypackage6b" });
|
|
143 |
try {
|
|
144 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage6" });
|
|
145 |
throw new RuntimeException("Failed to get IAE for existing package");
|
|
146 |
} catch(IllegalArgumentException e) {
|
|
147 |
if (!e.getMessage().contains("Package mypackage6 for module dupl.pkg.module already exists for class loader")) {
|
|
148 |
throw new RuntimeException("Failed to get expected IAE message for duplicate package: " + e.getMessage());
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
// Empty module name, expect an IAE
|
|
153 |
try {
|
|
154 |
m = ModuleHelper.ModuleObject("", cl, new String[] { "mypackage8" });
|
|
155 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage8" });
|
|
156 |
throw new RuntimeException("Failed to get expected IAE for empty module name");
|
|
157 |
} catch(IllegalArgumentException e) {
|
|
158 |
// Expected
|
|
159 |
}
|
|
160 |
|
|
161 |
// Bad module name, expect an IAE
|
|
162 |
try {
|
|
163 |
m = ModuleHelper.ModuleObject("bad;name", cl, new String[] { "mypackage9" });
|
|
164 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9" });
|
|
165 |
throw new RuntimeException("Failed to get expected IAE for bad;name");
|
|
166 |
} catch(IllegalArgumentException e) {
|
|
167 |
// Expected
|
|
168 |
}
|
|
169 |
|
|
170 |
// Bad module name, expect an IAE
|
|
171 |
try {
|
|
172 |
m = ModuleHelper.ModuleObject(".leadingdot", cl, new String[] { "mypackage9a" });
|
|
173 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9a" });
|
|
174 |
throw new RuntimeException("Failed to get expected IAE for .leadingdot");
|
|
175 |
} catch(IllegalArgumentException e) {
|
|
176 |
// Expected
|
|
177 |
}
|
|
178 |
|
|
179 |
// Bad module name, expect an IAE
|
|
180 |
try {
|
|
181 |
m = ModuleHelper.ModuleObject("trailingdot.", cl, new String[] { "mypackage9b" });
|
|
182 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9b" });
|
|
183 |
throw new RuntimeException("Failed to get expected IAE for trailingdot.");
|
|
184 |
} catch(IllegalArgumentException e) {
|
|
185 |
// Expected
|
|
186 |
}
|
|
187 |
|
|
188 |
// Bad module name, expect an IAE
|
|
189 |
m = ModuleHelper.ModuleObject("consecutive..dots", cl, new String[] { "mypackage9c" });
|
|
190 |
try {
|
|
191 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9c" });
|
|
192 |
throw new RuntimeException("Failed to get expected IAE for consecutive..dots");
|
|
193 |
} catch(IllegalArgumentException e) {
|
|
194 |
// Expected
|
|
195 |
}
|
|
196 |
|
|
197 |
// module name with multiple dots, should be okay
|
|
198 |
m = ModuleHelper.ModuleObject("more.than.one.dat", cl, new String[] { "mypackage9d" });
|
|
199 |
assertNotNull(m, "Module should not be null");
|
|
200 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "mypackage9d" });
|
|
201 |
|
|
202 |
// Zero length package list, should be okay
|
|
203 |
m = ModuleHelper.ModuleObject("zero.packages", cl, new String[] { });
|
|
204 |
assertNotNull(m, "Module should not be null");
|
|
205 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { });
|
|
206 |
|
|
207 |
// Invalid package name, expect an IAE
|
|
208 |
m = ModuleHelper.ModuleObject("module5", cl, new String[] { "your.package" });
|
|
209 |
try {
|
|
210 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "your.package" });
|
|
211 |
throw new RuntimeException("Failed to get expected IAE for your.package");
|
|
212 |
} catch(IllegalArgumentException e) {
|
|
213 |
if (!e.getMessage().contains("Invalid package name")) {
|
|
214 |
throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
|
|
215 |
}
|
|
216 |
}
|
|
217 |
|
|
218 |
// Invalid package name, expect an IAE
|
|
219 |
m = ModuleHelper.ModuleObject("module6", cl, new String[] { "foo" }); // Name irrelevant
|
|
220 |
try {
|
|
221 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { ";your/package" });
|
|
222 |
throw new RuntimeException("Failed to get expected IAE for ;your.package");
|
|
223 |
} catch(IllegalArgumentException e) {
|
|
224 |
if (!e.getMessage().contains("Invalid package name")) {
|
|
225 |
throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
// Invalid package name, expect an IAE
|
|
230 |
m = ModuleHelper.ModuleObject("module7", cl, new String[] { "foo" }); // Name irrelevant
|
|
231 |
try {
|
|
232 |
ModuleHelper.DefineModule(m, "9.0", "module.name/here", new String[] { "7[743" });
|
|
233 |
throw new RuntimeException("Failed to get expected IAE for package 7[743");
|
|
234 |
} catch(IllegalArgumentException e) {
|
|
235 |
if (!e.getMessage().contains("Invalid package name")) {
|
|
236 |
throw new RuntimeException("Failed to get expected IAE message for bad package name: " + e.getMessage());
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
// module version that is null, should be okay
|
|
241 |
m = ModuleHelper.ModuleObject("module8", cl, new String[] { "a_package_8" });
|
|
242 |
assertNotNull(m, "Module should not be null");
|
|
243 |
ModuleHelper.DefineModule(m, null, "module8/here", new String[] { "a_package_8" });
|
|
244 |
|
|
245 |
// module version that is "", should be okay
|
|
246 |
m = ModuleHelper.ModuleObject("module9", cl, new String[] { "a_package_9" });
|
|
247 |
assertNotNull(m, "Module should not be null");
|
|
248 |
ModuleHelper.DefineModule(m, "", "module9/here", new String[] { "a_package_9" });
|
|
249 |
|
|
250 |
// module location that is null, should be okay
|
|
251 |
m = ModuleHelper.ModuleObject("module10", cl, new String[] { "a_package_10" });
|
|
252 |
assertNotNull(m, "Module should not be null");
|
|
253 |
ModuleHelper.DefineModule(m, "9.0", null, new String[] { "a_package_10" });
|
|
254 |
|
|
255 |
// module location that is "", should be okay
|
|
256 |
m = ModuleHelper.ModuleObject("module11", cl, new String[] { "a_package_11" });
|
|
257 |
assertNotNull(m, "Module should not be null");
|
|
258 |
ModuleHelper.DefineModule(m, "9.0", "", new String[] { "a_package_11" });
|
|
259 |
}
|
|
260 |
|
|
261 |
static class MyClassLoader extends ClassLoader { }
|
|
262 |
}
|