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(); 79 80 logTestCase("2"); 81 testBootAppendDuplicateModuleClass(); 82 83 logTestCase("3"); 84 testBootAppendExcludedModuleClass(); 85 86 logTestCase("4"); 87 testBootAppendDuplicateExcludedModuleClass(); 88 89 logTestCase("5"); 90 testBootAppendClass(); 91 92 logTestCase("6"); 93 testBootAppendExtraDir(); 94 } 95 96 private static void logTestCase(String msg) { 97 System.out.println(); 98 System.out.printf("TESTCASE: %s", msg); 99 System.out.println(); 100 } 101 102 static void dumpArchive() throws Exception { 103 // create the classlist 104 File classlist = CDSTestUtils.makeClassList(ARCHIVE_CLASSES); 105 106 // build jar files 107 appJar = ClassFileInstaller.writeJar("app.jar", APP_CLASS); 108 bootAppendJar = ClassFileInstaller.writeJar("bootAppend.jar", 109 BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS); 110 111 112 OutputAnalyzer out = CDSTestUtils.createArchiveAndCheck( 113 "-Xbootclasspath/a:" + bootAppendJar, 114 "-XX:SharedClassListFile=" + classlist.getPath()); 115 // Make sure all the classes were successfully archived. 116 for (String archiveClass : ARCHIVE_CLASSES) { 117 out.shouldNotContain("Preload Warning: Cannot find " + archiveClass); 118 } 119 } 120 121 // Test #1: If a class on -Xbootclasspath/a is from a package defined in 122 // bootmodules, the class is not loaded at runtime. 123 // Verify the behavior is the same when the class is archived 124 // with CDS enabled at runtime. 125 // 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 }