1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @requires vm.cds & !vm.graal.enabled
* @summary Testing -Xbootclasspath/a support for CDS
* @requires vm.cds
* @library /test/lib
* @modules java.compiler
* java.base/jdk.internal.misc
* java.management
* jdk.internal.jvmstat/sun.jvmstat.monitor
* @compile javax/sound/sampled/MyClass.jasm
* @compile javax/annotation/processing/FilerException.jasm
* @compile nonjdk/myPackage/MyClass.java
* @build LoadClass
* @run main/othervm BootAppendTests
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.test.lib.cds.CDSOptions;
import jdk.test.lib.cds.CDSTestUtils;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.process.OutputAnalyzer;
public class BootAppendTests {
private static final String APP_CLASS = "LoadClass";
private static final String BOOT_APPEND_MODULE_CLASS = "javax/sound/sampled/MyClass";
private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS =
"javax/annotation/processing/FilerException";
private static final String BOOT_APPEND_CLASS = "nonjdk/myPackage/MyClass";
private static final String BOOT_APPEND_MODULE_CLASS_NAME =
BOOT_APPEND_MODULE_CLASS.replace('/', '.');
private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME =
BOOT_APPEND_DUPLICATE_MODULE_CLASS.replace('/', '.');
private static final String BOOT_APPEND_CLASS_NAME =
BOOT_APPEND_CLASS.replace('/', '.');
private static final String[] ARCHIVE_CLASSES =
{BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS};
private static final String modes[] = {"on", "off"};
private static String appJar;
private static String bootAppendJar;
public static void main(String... args) throws Exception {
dumpArchive();
logTestCase("1");
testBootAppendModuleClass();
logTestCase("2");
testBootAppendDuplicateModuleClass();
logTestCase("3");
testBootAppendExcludedModuleClass();
logTestCase("4");
testBootAppendDuplicateExcludedModuleClass();
logTestCase("5");
testBootAppendClass();
logTestCase("6");
testBootAppendExtraDir();
}
private static void logTestCase(String msg) {
System.out.println();
System.out.printf("TESTCASE: %s", msg);
System.out.println();
}
static void dumpArchive() throws Exception {
// create the classlist
File classlist = CDSTestUtils.makeClassList(ARCHIVE_CLASSES);
// build jar files
appJar = ClassFileInstaller.writeJar("app.jar", APP_CLASS);
bootAppendJar = ClassFileInstaller.writeJar("bootAppend.jar",
BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS);
OutputAnalyzer out = CDSTestUtils.createArchiveAndCheck(
"-Xbootclasspath/a:" + bootAppendJar,
"-cp", appJar,
"-XX:SharedClassListFile=" + classlist.getPath());
// Make sure all the classes were successfully archived.
for (String archiveClass : ARCHIVE_CLASSES) {
String msg = "Preload Warning: Cannot find " + archiveClass;
if (archiveClass.equals(BOOT_APPEND_MODULE_CLASS)) {
// We shouldn't archive a class in the appended boot class path that
// are the java.desktop module. Such a class cannot be loaded
// at runtime anyway.
out.shouldContain(msg);
} else {
out.shouldNotContain(msg);
}
}
}
// Test #1: If a class on -Xbootclasspath/a is from a package defined in
// bootmodules, the class is not loaded at runtime.
// Verify the behavior is the same when the class is archived
// with CDS enabled at runtime.
//
// The javax.sound.sampled package is defined in the java.desktop module.
// The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a
// should not be loaded at runtime.
public static void testBootAppendModuleClass() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar, "-showversion")
.addSuffix(APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME);
OutputAnalyzer out = CDSTestUtils.runWithArchive(opts);
CDSTestUtils.checkExec(out, opts, "java.lang.ClassNotFoundException: javax.sound.sampled.MyClass");
}
}
// Test #2: If a class on -Xbootclasspath/a has the same fully qualified
// name as a class defined in boot modules, the class is not loaded
// from -Xbootclasspath/a. Verify the behavior is the same at runtime
// when CDS is enabled.
//
// The javax/annotation/processing/FilerException is a platform module
// class. The class on the -Xbootclasspath/a path that has the same
// fully-qualified name should not be loaded at runtime when CDS is enabled.
// The one from the platform modules should be loaded instead.
public static void testBootAppendDuplicateModuleClass() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-showversion",
"-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar)
.addSuffix("-Xlog:class+load=info",
APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
String MATCH_PATTERN = ".class.load. javax.annotation.processing.FilerException source:.*bootAppend.jar*";
CDSTestUtils.run(opts)
.assertNormalExit(out -> {
out.shouldNotMatch(MATCH_PATTERN);
});
}
}
// Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules,
// the class can be loaded from -Xbootclasspath/a when the module is excluded
// using --limit-modules. Verify the behavior is the same at runtime when CDS
// is enabled.
//
// The java.desktop module is excluded using --limit-modules at runtime
// CDS will be disabled with the --limit-modules option during runtime.
// javax.sound.sampled.MyClass will be loaded from the jar at runtime.
public static void testBootAppendExcludedModuleClass() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
"--limit-modules=java.base", "-cp", appJar)
.addSuffix("-Xlog:class+load=info",
APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME);
CDSTestUtils.Result res = CDSTestUtils.run(opts);
String MATCH_PATTERN =
".class.load. javax.sound.sampled.MyClass source:.*bootAppend.jar*";
if (mode.equals("on")) {
res.assertSilentlyDisabledCDS(out -> {
out.shouldHaveExitValue(0)
.shouldMatch(MATCH_PATTERN);
});
} else {
res.assertNormalExit(out -> {
out.shouldMatch(MATCH_PATTERN);
});
}
}
}
// Test #4: If a class on -Xbootclasspath/a has the same fully qualified
// name as a class defined in boot modules, the class is loaded
// from -Xbootclasspath/a when the boot module is excluded using
// --limit-modules. Verify the behavior is the same at runtime
// when CDS is enabled.
//
// The javax.annotation.processing.FilerException is a platform module class.
// The class on -Xbootclasspath/a that has the same fully-qualified name
// as javax.annotation.processing.FilerException can be loaded at runtime when
// java.compiler is excluded.
// CDS is disabled during runtime if the --limit-modules option is
// specified.
public static void testBootAppendDuplicateExcludedModuleClass() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
"--limit-modules=java.base", "-cp", appJar)
.addSuffix("-Xlog:class+load=info",
APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME);
CDSTestUtils.Result res = CDSTestUtils.run(opts);
String MATCH_PATTERN =
".class.load. javax.annotation.processing.FilerException source:.*bootAppend.jar*";
if (mode.equals("on")) {
res.assertSilentlyDisabledCDS(out -> {
out.shouldHaveExitValue(0)
.shouldMatch(MATCH_PATTERN);
});
} else {
res.assertNormalExit(out -> {
out.shouldMatch(MATCH_PATTERN);
});
}
}
}
// Test #5: If a class on -Xbootclasspath/a is not from named modules,
// the class can be loaded at runtime. Verify the behavior is
// the same at runtime when CDS is enabled.
//
// The nonjdk.myPackage is not defined in named modules. The
// nonjdk.myPackage.MyClass will be loaded from the jar in
// -Xbootclasspath/a since CDS will be disabled with the
// --limit-modules option.
public static void testBootAppendClass() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion",
"--limit-modules=java.base", "-cp", appJar)
.addSuffix("-Xlog:class+load=info",
APP_CLASS, BOOT_APPEND_CLASS_NAME);
CDSTestUtils.Result res = CDSTestUtils.run(opts);
String MATCH_PATTERN =
".class.load. nonjdk.myPackage.MyClass source:.*bootAppend.jar*";
if (mode.equals("on")) {
res.assertSilentlyDisabledCDS(out -> {
out.shouldHaveExitValue(0)
.shouldMatch(MATCH_PATTERN);
});
} else {
res.assertNormalExit(out -> {
out.shouldMatch(MATCH_PATTERN);
});
}
}
}
// Test #6: This is similar to Test #5. During runtime, an extra dir
// is appended to the bootclasspath. It should not invalidate
// the shared archive. However, CDS will be disabled with the
// --limit-modules in the command line.
public static void testBootAppendExtraDir() throws Exception {
for (String mode : modes) {
CDSOptions opts = (new CDSOptions())
.setXShareMode(mode).setUseVersion(false)
.addPrefix("-Xbootclasspath/a:" + bootAppendJar + File.pathSeparator + appJar,
"-showversion", "--limit-modules=java.base", "-cp", appJar)
.addSuffix("-Xlog:class+load=info",
APP_CLASS, BOOT_APPEND_CLASS_NAME);
CDSTestUtils.Result res = CDSTestUtils.run(opts);
String MATCH_PATTERN =
".class.load. nonjdk.myPackage.MyClass source:.*bootAppend.jar*";
if (mode.equals("on")) {
res.assertSilentlyDisabledCDS(out -> {
out.shouldHaveExitValue(0)
.shouldMatch(MATCH_PATTERN);
});
} else {
res.assertNormalExit(out -> {
out.shouldMatch(MATCH_PATTERN);
});
}
}
}
}
|