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 |
* @summary Testing -Xbootclasspath/a support for CDS
|
|
27 |
* @library /testlibrary
|
|
28 |
* @modules java.base/jdk.internal.misc
|
|
29 |
* java.management
|
|
30 |
* jdk.jartool/sun.tools.jar
|
|
31 |
* jdk.jvmstat/sun.jvmstat.monitor
|
|
32 |
* @ignore 8150683
|
|
33 |
* @compile javax/sound/sampled/MyClass.jasm
|
|
34 |
* @compile org/omg/CORBA/Context.jasm
|
|
35 |
* @compile nonjdk/myPackage/MyClass.java
|
|
36 |
* @build jdk.test.lib.* LoadClass
|
|
37 |
* @run main ClassFileInstaller LoadClass
|
|
38 |
* @run main/othervm BootAppendTests
|
|
39 |
*/
|
|
40 |
|
|
41 |
import java.io.File;
|
|
42 |
import java.io.FileOutputStream;
|
|
43 |
import java.io.IOException;
|
|
44 |
import java.io.PrintStream;
|
|
45 |
|
|
46 |
import java.nio.file.Path;
|
|
47 |
import java.nio.file.Paths;
|
|
48 |
|
|
49 |
import jdk.test.lib.ProcessTools;
|
|
50 |
import jdk.test.lib.OutputAnalyzer;
|
|
51 |
|
|
52 |
public class BootAppendTests {
|
|
53 |
private static final String APP_CLASS = "LoadClass";
|
|
54 |
private static final String BOOT_APPEND_MODULE_CLASS = "javax/sound/sampled/MyClass";
|
|
55 |
private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS = "org/omg/CORBA/Context";
|
|
56 |
private static final String BOOT_APPEND_CLASS = "nonjdk/myPackage/MyClass";
|
|
57 |
private static final String BOOT_APPEND_MODULE_CLASS_NAME =
|
|
58 |
BOOT_APPEND_MODULE_CLASS.replace('/', '.');
|
|
59 |
private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME =
|
|
60 |
BOOT_APPEND_DUPLICATE_MODULE_CLASS.replace('/', '.');
|
|
61 |
private static final String BOOT_APPEND_CLASS_NAME =
|
|
62 |
BOOT_APPEND_CLASS.replace('/', '.');
|
|
63 |
private static final String[] ARCHIVE_CLASSES =
|
|
64 |
{BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS};
|
|
65 |
|
|
66 |
private static final String modes[] = {"on", "off"};
|
|
67 |
|
|
68 |
private static String appJar;
|
|
69 |
private static String bootAppendJar;
|
|
70 |
|
|
71 |
public static void main(String... args) throws Exception {
|
|
72 |
dumpArchive();
|
|
73 |
testBootAppendModuleClass();
|
|
74 |
testBootAppendDuplicateModuleClass();
|
|
75 |
testBootAppendExcludedModuleClass();
|
|
76 |
testBootAppendDuplicateExcludedModuleClass();
|
|
77 |
testBootAppendClass();
|
|
78 |
}
|
|
79 |
|
|
80 |
static void dumpArchive() throws Exception {
|
|
81 |
// create the classlist
|
|
82 |
File classlist = new File(new File(System.getProperty("test.classes", ".")),
|
|
83 |
"BootAppendTest.classlist");
|
|
84 |
FileOutputStream fos = new FileOutputStream(classlist);
|
|
85 |
PrintStream ps = new PrintStream(fos);
|
|
86 |
for (String s : ARCHIVE_CLASSES) {
|
|
87 |
ps.println(s);
|
|
88 |
}
|
|
89 |
ps.close();
|
|
90 |
fos.close();
|
|
91 |
|
|
92 |
// build jar files
|
|
93 |
BasicJarBuilder.build(true, "app", APP_CLASS);
|
|
94 |
appJar = BasicJarBuilder.getTestJar("app.jar");
|
|
95 |
BasicJarBuilder.build("bootAppend",
|
|
96 |
BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS);
|
|
97 |
bootAppendJar = BasicJarBuilder.getTestJar("bootAppend.jar");
|
|
98 |
|
|
99 |
// dump
|
|
100 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
101 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
102 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
103 |
"-XX:SharedClassListFile=" + classlist.getPath(),
|
|
104 |
"-XX:+PrintSharedSpaces",
|
|
105 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
106 |
"-Xshare:dump");
|
|
107 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
108 |
output.shouldContain("Loading classes to share")
|
|
109 |
.shouldHaveExitValue(0);
|
|
110 |
|
|
111 |
// Make sure all the classes were successfully archived.
|
|
112 |
for (String archiveClass : ARCHIVE_CLASSES) {
|
|
113 |
output.shouldNotContain("Preload Warning: Cannot find " + archiveClass);
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
// Test #1: If a class on -Xbootclasspath/a is from a package defined in
|
|
118 |
// bootmodules, the class is not loaded at runtime.
|
|
119 |
// Verify the behavior is the same when the class is archived
|
|
120 |
// with CDS enabled at runtime.
|
|
121 |
//
|
|
122 |
// The javax.sound.sampled package is defined in the java.desktop module.
|
|
123 |
// The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a
|
|
124 |
// should not be loaded at runtime.
|
|
125 |
public static void testBootAppendModuleClass() throws Exception {
|
|
126 |
for (String mode : modes) {
|
|
127 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
128 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
129 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
130 |
"-cp", appJar,
|
|
131 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
132 |
"-Xshare:" + mode,
|
|
133 |
APP_CLASS,
|
|
134 |
BOOT_APPEND_MODULE_CLASS_NAME);
|
|
135 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
136 |
output.shouldContain("java.lang.ClassNotFoundException: javax.sound.sampled.MyClass");
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
// Test #2: If a class on -Xbootclasspath/a has the same fully qualified
|
|
141 |
// name as a class defined in boot modules, the class is not loaded
|
|
142 |
// from -Xbootclasspath/a. Verify the behavior is the same at runtime
|
|
143 |
// when CDS is enabled.
|
|
144 |
//
|
|
145 |
// The org.omg.CORBA.Context is a boot module class. The class on
|
|
146 |
// the -Xbootclasspath/a path that has the same fully-qualified name
|
|
147 |
// should not be loaded at runtime when CDS is enabled.
|
|
148 |
// The one from the boot modules should be loaded instead.
|
|
149 |
public static void testBootAppendDuplicateModuleClass() throws Exception {
|
|
150 |
for (String mode : modes) {
|
|
151 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
152 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
153 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
154 |
"-XX:+TraceClassLoading",
|
|
155 |
"-cp", appJar,
|
|
156 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
157 |
"-Xshare:" + mode,
|
|
158 |
APP_CLASS,
|
|
159 |
BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
|
|
160 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
161 |
output.shouldContain("[classload] org.omg.CORBA.Context source: jrt:/java.corba");
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
// Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules,
|
|
166 |
// the class can be loaded from -Xbootclasspath/a when the module is excluded
|
|
167 |
// using -limitmods. Verify the behavior is the same at runtime when CDS is
|
|
168 |
// enabled.
|
|
169 |
//
|
|
170 |
// The java.desktop module is excluded using -limitmods at runtime,
|
|
171 |
// javax.sound.sampled.MyClass is archived from -Xbootclasspath/a. It can be
|
|
172 |
// loaded from the archive at runtime.
|
|
173 |
public static void testBootAppendExcludedModuleClass() throws Exception {
|
|
174 |
for (String mode : modes) {
|
|
175 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
176 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
177 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
178 |
"-XX:+TraceClassLoading",
|
|
179 |
"-cp", appJar,
|
|
180 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
181 |
"-limitmods", "java.base",
|
|
182 |
"-Xshare:" + mode,
|
|
183 |
APP_CLASS,
|
|
184 |
BOOT_APPEND_MODULE_CLASS_NAME);
|
|
185 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
186 |
output.shouldContain("[classload] javax.sound.sampled.MyClass");
|
|
187 |
|
|
188 |
// When CDS is enabled, the shared class should be loaded from the archive.
|
|
189 |
if (mode.equals("on")) {
|
|
190 |
output.shouldContain("[classload] javax.sound.sampled.MyClass source: shared objects file");
|
|
191 |
}
|
|
192 |
}
|
|
193 |
}
|
|
194 |
|
|
195 |
// Test #4: If a class on -Xbootclasspath/a has the same fully qualified
|
|
196 |
// name as a class defined in boot modules, the class is loaded
|
|
197 |
// from -Xbootclasspath/a when the boot module is excluded using
|
|
198 |
// -limitmods. Verify the behavior is the same at runtime when CDS is
|
|
199 |
// enabled.
|
|
200 |
//
|
|
201 |
// The org.omg.CORBA.Context is a boot module class. The class
|
|
202 |
// on -Xbootclasspath/a that has the same fully-qualified name
|
|
203 |
// as org.omg.CORBA.Context can be loaded at runtime when
|
|
204 |
// java.corba is excluded.
|
|
205 |
public static void testBootAppendDuplicateExcludedModuleClass() throws Exception {
|
|
206 |
for (String mode : modes) {
|
|
207 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
208 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
209 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
210 |
"-XX:+TraceClassLoading",
|
|
211 |
"-cp", appJar,
|
|
212 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
213 |
"-limitmods", "java.base",
|
|
214 |
"-Xshare:" + mode,
|
|
215 |
APP_CLASS,
|
|
216 |
BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
|
|
217 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
218 |
output.shouldContain("[classload] org.omg.CORBA.Context");
|
|
219 |
output.shouldMatch(".*\\[classload\\] org.omg.CORBA.Context source:.*bootAppend.jar");
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
// Test #5: If a class on -Xbootclasspath/a is not from named modules,
|
|
224 |
// the class can be loaded at runtime. Verify the behavior is
|
|
225 |
// the same at runtime when CDS is enabled.
|
|
226 |
//
|
|
227 |
// The nonjdk.myPackage is not defined in named modules. The
|
|
228 |
// archived nonjdk.myPackage.MyClass from -Xbootclasspath/a
|
|
229 |
// can be loaded at runtime when CDS is enabled.
|
|
230 |
public static void testBootAppendClass() throws Exception {
|
|
231 |
for (String mode : modes) {
|
|
232 |
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
|
|
233 |
"-XX:+UnlockDiagnosticVMOptions",
|
|
234 |
"-XX:SharedArchiveFile=./BootAppendTests.jsa",
|
|
235 |
"-XX:+TraceClassLoading",
|
|
236 |
"-cp", appJar,
|
|
237 |
"-Xbootclasspath/a:" + bootAppendJar,
|
|
238 |
"-Xshare:" + mode,
|
|
239 |
APP_CLASS,
|
|
240 |
BOOT_APPEND_CLASS_NAME);
|
|
241 |
OutputAnalyzer output = new OutputAnalyzer(pb.start());
|
|
242 |
output.shouldContain("[classload] nonjdk.myPackage.MyClass");
|
|
243 |
|
|
244 |
// If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded
|
|
245 |
// from the shared archive.
|
|
246 |
if (mode.equals("on")) {
|
|
247 |
output.shouldContain(
|
|
248 |
"[classload] nonjdk.myPackage.MyClass source: shared objects file");
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|