1 /* 2 * Copyright (c) 2016, 2017, 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 * @requires vm.cds & !vm.graal.enabled 27 * @summary Testing -Xbootclasspath/a support for CDS 28 * @requires vm.cds 29 * @library /test/lib 30 * @modules java.base/jdk.internal.misc 31 * java.management 32 * jdk.internal.jvmstat/sun.jvmstat.monitor 33 * @compile javax/sound/sampled/MyClass.jasm 34 * @compile org/omg/CORBA/Context.jasm 35 * @compile nonjdk/myPackage/MyClass.java 36 * @build LoadClass 37 * @run main/othervm BootAppendTests 38 */ 39 40 import java.io.File; 41 import java.io.FileOutputStream; 42 import java.io.IOException; 43 import java.io.PrintStream; 44 45 import java.nio.file.Path; 46 import java.nio.file.Paths; 47 48 import jdk.test.lib.cds.CDSOptions; 49 import jdk.test.lib.cds.CDSTestUtils; 50 import jdk.test.lib.process.ProcessTools; 51 import jdk.test.lib.process.OutputAnalyzer; 52 53 public class BootAppendTests { 54 private static final String APP_CLASS = "LoadClass"; 55 private static final String BOOT_APPEND_MODULE_CLASS = "javax/sound/sampled/MyClass"; 56 private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS = "org/omg/CORBA/Context"; 57 private static final String BOOT_APPEND_CLASS = "nonjdk/myPackage/MyClass"; 58 private static final String BOOT_APPEND_MODULE_CLASS_NAME = 59 BOOT_APPEND_MODULE_CLASS.replace('/', '.'); 60 private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME = 61 BOOT_APPEND_DUPLICATE_MODULE_CLASS.replace('/', '.'); 62 private static final String BOOT_APPEND_CLASS_NAME = 63 BOOT_APPEND_CLASS.replace('/', '.'); 64 private static final String[] ARCHIVE_CLASSES = 65 {BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS}; 66 67 private static final String modes[] = {"on", "off"}; 68 69 private static String appJar; 70 private static String bootAppendJar; 71 72 public static void main(String... args) throws Exception { 73 dumpArchive(); 74 75 logTestCase("1"); 76 testBootAppendModuleClass(); 124 // The javax.sound.sampled package is defined in the java.desktop module. 125 // The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a 126 // should not be loaded at runtime. 127 public static void testBootAppendModuleClass() throws Exception { 128 for (String mode : modes) { 129 CDSOptions opts = (new CDSOptions()) 130 .setXShareMode(mode).setUseVersion(false) 131 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar, "-showversion") 132 .addSuffix(APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 133 134 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 135 CDSTestUtils.checkExec(out, opts, "java.lang.ClassNotFoundException: javax.sound.sampled.MyClass"); 136 } 137 } 138 139 // Test #2: If a class on -Xbootclasspath/a has the same fully qualified 140 // name as a class defined in boot modules, the class is not loaded 141 // from -Xbootclasspath/a. Verify the behavior is the same at runtime 142 // when CDS is enabled. 143 // 144 // The org.omg.CORBA.Context is a boot module class. The class on 145 // the -Xbootclasspath/a path that has the same fully-qualified name 146 // should not be loaded at runtime when CDS is enabled. 147 // The one from the boot modules should be loaded instead. 148 public static void testBootAppendDuplicateModuleClass() throws Exception { 149 for (String mode : modes) { 150 CDSOptions opts = (new CDSOptions()) 151 .setXShareMode(mode).setUseVersion(false) 152 .addPrefix("--add-modules", "java.corba", "-showversion", 153 "-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar) 154 .addSuffix("-Xlog:class+load=info", 155 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 156 157 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 158 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: jrt:/java.corba"); 159 } 160 } 161 162 // Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules, 163 // the class can be loaded from -Xbootclasspath/a when the module is excluded 164 // using --limit-modules. Verify the behavior is the same at runtime when CDS 165 // is enabled. 166 // 167 // The java.desktop module is excluded using --limit-modules at runtime, 168 // javax.sound.sampled.MyClass is archived from -Xbootclasspath/a. It can be 169 // loaded from the archive at runtime. 170 public static void testBootAppendExcludedModuleClass() throws Exception { 171 for (String mode : modes) { 172 CDSOptions opts = (new CDSOptions()) 173 .setXShareMode(mode).setUseVersion(false) 174 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 175 "--limit-modules=java.base", "-cp", appJar) 176 .addSuffix("-Xlog:class+load=info", 177 APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 178 179 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 180 CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass"); 181 182 // When CDS is enabled, the shared class should be loaded from the archive. 183 if (mode.equals("on")) { 184 CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass source: shared objects file"); 185 } 186 } 187 } 188 189 // Test #4: If a class on -Xbootclasspath/a has the same fully qualified 190 // name as a class defined in boot modules, the class is loaded 191 // from -Xbootclasspath/a when the boot module is excluded using 192 // --limit-modules. Verify the behavior is the same at runtime 193 // when CDS is enabled. 194 // 195 // The org.omg.CORBA.Context is a boot module class. The class 196 // on -Xbootclasspath/a that has the same fully-qualified name 197 // as org.omg.CORBA.Context can be loaded at runtime when 198 // java.corba is excluded. 199 public static void testBootAppendDuplicateExcludedModuleClass() throws Exception { 200 for (String mode : modes) { 201 CDSOptions opts = (new CDSOptions()) 202 .setXShareMode(mode).setUseVersion(false) 203 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 204 "--limit-modules=java.base", "-cp", appJar) 205 .addSuffix("-Xlog:class+load=info", 206 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 207 208 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 209 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context"); 210 if (!CDSTestUtils.isUnableToMap(out)) { 211 if (mode.equals("off")) { 212 out.shouldMatch(".*\\[class,load\\] org.omg.CORBA.Context source:.*bootAppend.jar"); 213 } else { 214 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: shared objects file"); 215 } 216 } 217 } 218 } 219 220 // Test #5: If a class on -Xbootclasspath/a is not from named modules, 221 // the class can be loaded at runtime. Verify the behavior is 222 // the same at runtime when CDS is enabled. 223 // 224 // The nonjdk.myPackage is not defined in named modules. The 225 // archived nonjdk.myPackage.MyClass from -Xbootclasspath/a 226 // can be loaded at runtime when CDS is enabled. 227 public static void testBootAppendClass() throws Exception { 228 for (String mode : modes) { 229 CDSOptions opts = (new CDSOptions()) 230 .setXShareMode(mode).setUseVersion(false) 231 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 232 "--limit-modules=java.base", "-cp", appJar) 233 .addSuffix("-Xlog:class+load=info", 234 APP_CLASS, BOOT_APPEND_CLASS_NAME); 235 236 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 237 CDSTestUtils.checkExec(out, opts, "[class,load] nonjdk.myPackage.MyClass"); 238 239 // If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded 240 // from the shared archive. 241 if (mode.equals("on")) { 242 CDSTestUtils.checkExec(out, opts, 243 "[class,load] nonjdk.myPackage.MyClass source: shared objects file"); 244 } 245 } 246 } 247 248 // Test #6: This is similar to Test #5. During runtime, an extra dir 249 // is appended to the bootclasspath. It should not invalidate 250 // the shared archive. 251 public static void testBootAppendExtraDir() throws Exception { 252 for (String mode : modes) { 253 CDSOptions opts = (new CDSOptions()) 254 .setXShareMode(mode).setUseVersion(false) 255 .addPrefix("-Xbootclasspath/a:" + bootAppendJar + File.pathSeparator + appJar, 256 "-showversion", "--limit-modules=java.base", "-cp", appJar) 257 .addSuffix("-Xlog:class+load=info", 258 APP_CLASS, BOOT_APPEND_CLASS_NAME); 259 260 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 261 CDSTestUtils.checkExec(out, opts, "[class,load] nonjdk.myPackage.MyClass"); 262 263 // If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded 264 // from the shared archive. 265 if (mode.equals("on")) { 266 CDSTestUtils.checkExec(out, opts, 267 "[class,load] nonjdk.myPackage.MyClass source: shared objects file"); 268 } 269 } 270 } 271 } | 1 /* 2 * Copyright (c) 2016, 2018, 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 * @requires vm.cds & !vm.graal.enabled 27 * @summary Testing -Xbootclasspath/a support for CDS 28 * @requires vm.cds 29 * @library /test/lib 30 * @modules java.compiler 31 * java.base/jdk.internal.misc 32 * java.management 33 * jdk.internal.jvmstat/sun.jvmstat.monitor 34 * @compile javax/sound/sampled/MyClass.jasm 35 * @compile javax/annotation/processing/FilerException.jasm 36 * @compile nonjdk/myPackage/MyClass.java 37 * @build 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.cds.CDSOptions; 50 import jdk.test.lib.cds.CDSTestUtils; 51 import jdk.test.lib.process.ProcessTools; 52 import jdk.test.lib.process.OutputAnalyzer; 53 54 public class BootAppendTests { 55 private static final String APP_CLASS = "LoadClass"; 56 private static final String BOOT_APPEND_MODULE_CLASS = "javax/sound/sampled/MyClass"; 57 private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS = 58 "javax/annotation/processing/FilerException"; 59 private static final String BOOT_APPEND_CLASS = "nonjdk/myPackage/MyClass"; 60 private static final String BOOT_APPEND_MODULE_CLASS_NAME = 61 BOOT_APPEND_MODULE_CLASS.replace('/', '.'); 62 private static final String BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME = 63 BOOT_APPEND_DUPLICATE_MODULE_CLASS.replace('/', '.'); 64 private static final String BOOT_APPEND_CLASS_NAME = 65 BOOT_APPEND_CLASS.replace('/', '.'); 66 private static final String[] ARCHIVE_CLASSES = 67 {BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS}; 68 69 private static final String modes[] = {"on", "off"}; 70 71 private static String appJar; 72 private static String bootAppendJar; 73 74 public static void main(String... args) throws Exception { 75 dumpArchive(); 76 77 logTestCase("1"); 78 testBootAppendModuleClass(); 126 // The javax.sound.sampled package is defined in the java.desktop module. 127 // The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a 128 // should not be loaded at runtime. 129 public static void testBootAppendModuleClass() throws Exception { 130 for (String mode : modes) { 131 CDSOptions opts = (new CDSOptions()) 132 .setXShareMode(mode).setUseVersion(false) 133 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar, "-showversion") 134 .addSuffix(APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 135 136 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 137 CDSTestUtils.checkExec(out, opts, "java.lang.ClassNotFoundException: javax.sound.sampled.MyClass"); 138 } 139 } 140 141 // Test #2: If a class on -Xbootclasspath/a has the same fully qualified 142 // name as a class defined in boot modules, the class is not loaded 143 // from -Xbootclasspath/a. Verify the behavior is the same at runtime 144 // when CDS is enabled. 145 // 146 // The javax/annotation/processing/FilerException is a platform module 147 // class. The class on the -Xbootclasspath/a path that has the same 148 // fully-qualified name should not be loaded at runtime when CDS is enabled. 149 // The one from the platform modules should be loaded instead. 150 public static void testBootAppendDuplicateModuleClass() throws Exception { 151 for (String mode : modes) { 152 CDSOptions opts = (new CDSOptions()) 153 .setXShareMode(mode).setUseVersion(false) 154 .addPrefix("-showversion", 155 "-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar) 156 .addSuffix("-Xlog:class+load=info", 157 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 158 159 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 160 CDSTestUtils.checkExec(out, opts, "[class,load] javax.annotation.processing.FilerException source: jrt:/java.compiler"); 161 } 162 } 163 164 // Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules, 165 // the class can be loaded from -Xbootclasspath/a when the module is excluded 166 // using --limit-modules. Verify the behavior is the same at runtime when CDS 167 // is enabled. 168 // 169 // The java.desktop module is excluded using --limit-modules at runtime 170 // CDS will be disabled with the --limit-modules option during runtime. 171 // javax.sound.sampled.MyClass will be loaded from the jar at runtime. 172 public static void testBootAppendExcludedModuleClass() throws Exception { 173 for (String mode : modes) { 174 CDSOptions opts = (new CDSOptions()) 175 .setXShareMode(mode).setUseVersion(false) 176 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 177 "--limit-modules=java.base", "-cp", appJar) 178 .addSuffix("-Xlog:class+load=info", 179 APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 180 CDSTestUtils.Result res = CDSTestUtils.run(opts); 181 String MATCH_PATTERN = 182 ".class.load. javax.sound.sampled.MyClass source:.*bootAppend.jar*"; 183 if (mode.equals("on")) { 184 res.assertSilentlyDisabledCDS(out -> { 185 out.shouldHaveExitValue(0) 186 .shouldMatch(MATCH_PATTERN); 187 }); 188 } else { 189 res.assertNormalExit(out -> { 190 out.shouldMatch(MATCH_PATTERN); 191 }); 192 } 193 } 194 } 195 196 // Test #4: If a class on -Xbootclasspath/a has the same fully qualified 197 // name as a class defined in boot modules, the class is loaded 198 // from -Xbootclasspath/a when the boot module is excluded using 199 // --limit-modules. Verify the behavior is the same at runtime 200 // when CDS is enabled. 201 // 202 // The javax.annotation.processing.FilerException is a platform module class. 203 // The class on -Xbootclasspath/a that has the same fully-qualified name 204 // as javax.annotation.processing.FilerException can be loaded at runtime when 205 // java.compiler is excluded. 206 // CDS is disabled during runtime if the --limit-modules option is 207 // specified. 208 public static void testBootAppendDuplicateExcludedModuleClass() throws Exception { 209 for (String mode : modes) { 210 CDSOptions opts = (new CDSOptions()) 211 .setXShareMode(mode).setUseVersion(false) 212 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 213 "--limit-modules=java.base", "-cp", appJar) 214 .addSuffix("-Xlog:class+load=info", 215 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 216 217 CDSTestUtils.Result res = CDSTestUtils.run(opts); 218 String MATCH_PATTERN = 219 ".class.load. javax.annotation.processing.FilerException source:.*bootAppend.jar*"; 220 if (mode.equals("on")) { 221 res.assertSilentlyDisabledCDS(out -> { 222 out.shouldHaveExitValue(0) 223 .shouldMatch(MATCH_PATTERN); 224 }); 225 } else { 226 res.assertNormalExit(out -> { 227 out.shouldMatch(MATCH_PATTERN); 228 }); 229 } 230 } 231 } 232 233 // Test #5: If a class on -Xbootclasspath/a is not from named modules, 234 // the class can be loaded at runtime. Verify the behavior is 235 // the same at runtime when CDS is enabled. 236 // 237 // The nonjdk.myPackage is not defined in named modules. The 238 // nonjdk.myPackage.MyClass will be loaded from the jar in 239 // -Xbootclasspath/a since CDS will be disabled with the 240 // --limit-modules option. 241 public static void testBootAppendClass() throws Exception { 242 for (String mode : modes) { 243 CDSOptions opts = (new CDSOptions()) 244 .setXShareMode(mode).setUseVersion(false) 245 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 246 "--limit-modules=java.base", "-cp", appJar) 247 .addSuffix("-Xlog:class+load=info", 248 APP_CLASS, BOOT_APPEND_CLASS_NAME); 249 250 CDSTestUtils.Result res = CDSTestUtils.run(opts); 251 String MATCH_PATTERN = 252 ".class.load. nonjdk.myPackage.MyClass source:.*bootAppend.jar*"; 253 if (mode.equals("on")) { 254 res.assertSilentlyDisabledCDS(out -> { 255 out.shouldHaveExitValue(0) 256 .shouldMatch(MATCH_PATTERN); 257 }); 258 } else { 259 res.assertNormalExit(out -> { 260 out.shouldMatch(MATCH_PATTERN); 261 }); 262 } 263 } 264 } 265 266 // Test #6: This is similar to Test #5. During runtime, an extra dir 267 // is appended to the bootclasspath. It should not invalidate 268 // the shared archive. However, CDS will be disabled with the 269 // --limit-modules in the command line. 270 public static void testBootAppendExtraDir() throws Exception { 271 for (String mode : modes) { 272 CDSOptions opts = (new CDSOptions()) 273 .setXShareMode(mode).setUseVersion(false) 274 .addPrefix("-Xbootclasspath/a:" + bootAppendJar + File.pathSeparator + appJar, 275 "-showversion", "--limit-modules=java.base", "-cp", appJar) 276 .addSuffix("-Xlog:class+load=info", 277 APP_CLASS, BOOT_APPEND_CLASS_NAME); 278 279 CDSTestUtils.Result res = CDSTestUtils.run(opts); 280 String MATCH_PATTERN = 281 ".class.load. nonjdk.myPackage.MyClass source:.*bootAppend.jar*"; 282 if (mode.equals("on")) { 283 res.assertSilentlyDisabledCDS(out -> { 284 out.shouldHaveExitValue(0) 285 .shouldMatch(MATCH_PATTERN); 286 }); 287 } else { 288 res.assertNormalExit(out -> { 289 out.shouldMatch(MATCH_PATTERN); 290 }); 291 } 292 } 293 } 294 } |