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 27 * @summary Testing -Xbootclasspath/a support for CDS 28 * @requires (vm.opt.UseCompressedOops == null) | (vm.opt.UseCompressedOops == true) 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(); 77 78 logTestCase("2"); 79 testBootAppendDuplicateModuleClass(); 80 81 logTestCase("3"); 82 testBootAppendExcludedModuleClass(); 83 84 logTestCase("4"); 85 testBootAppendDuplicateExcludedModuleClass(); 86 87 logTestCase("5"); 88 testBootAppendClass(); 89 } 90 91 private static void logTestCase(String msg) { 92 System.out.println(); 93 System.out.printf("TESTCASE: %s", msg); 94 System.out.println(); 95 } 96 97 static void dumpArchive() throws Exception { 98 // create the classlist 99 File classlist = CDSTestUtils.makeClassList(ARCHIVE_CLASSES); 100 101 // build jar files 102 appJar = ClassFileInstaller.writeJar("app.jar", APP_CLASS); 103 bootAppendJar = ClassFileInstaller.writeJar("bootAppend.jar", 104 BOOT_APPEND_MODULE_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS, BOOT_APPEND_CLASS); 105 106 107 OutputAnalyzer out = CDSTestUtils.createArchiveAndCheck( 108 "-Xbootclasspath/a:" + bootAppendJar, 109 "-XX:SharedClassListFile=" + classlist.getPath()); 110 // Make sure all the classes were successfully archived. 111 for (String archiveClass : ARCHIVE_CLASSES) { 112 out.shouldNotContain("Preload Warning: Cannot find " + archiveClass); 113 } 114 } 115 116 // Test #1: If a class on -Xbootclasspath/a is from a package defined in 117 // bootmodules, the class is not loaded at runtime. 118 // Verify the behavior is the same when the class is archived 119 // with CDS enabled at runtime. 120 // 121 // The javax.sound.sampled package is defined in the java.desktop module. 122 // The archived javax.sound.sampled.MyClass from the -Xbootclasspath/a 123 // should not be loaded at runtime. 124 public static void testBootAppendModuleClass() throws Exception { 125 for (String mode : modes) { 126 CDSOptions opts = (new CDSOptions()) 127 .setXShareMode(mode).setUseVersion(false) 128 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar, "-showversion") 129 .addSuffix(APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 130 131 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 132 CDSTestUtils.checkExec(out, opts, "java.lang.ClassNotFoundException: javax.sound.sampled.MyClass"); 133 } 134 } 135 136 // Test #2: If a class on -Xbootclasspath/a has the same fully qualified 137 // name as a class defined in boot modules, the class is not loaded 138 // from -Xbootclasspath/a. Verify the behavior is the same at runtime 139 // when CDS is enabled. 140 // 141 // The org.omg.CORBA.Context is a boot module class. The class on 142 // the -Xbootclasspath/a path that has the same fully-qualified name 143 // should not be loaded at runtime when CDS is enabled. 144 // The one from the boot modules should be loaded instead. 145 public static void testBootAppendDuplicateModuleClass() throws Exception { 146 for (String mode : modes) { 147 CDSOptions opts = (new CDSOptions()) 148 .setXShareMode(mode).setUseVersion(false) 149 .addPrefix("--add-modules", "java.corba", "-showversion", 150 "-Xbootclasspath/a:" + bootAppendJar, "-cp", appJar) 151 .addSuffix("-Xlog:class+load=info", 152 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 153 154 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 155 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: jrt:/java.corba"); 156 } 157 } 158 159 // Test #3: If a class on -Xbootclasspath/a is from a package defined in boot modules, 160 // the class can be loaded from -Xbootclasspath/a when the module is excluded 161 // using --limit-modules. Verify the behavior is the same at runtime when CDS 162 // is enabled. 163 // 164 // The java.desktop module is excluded using --limit-modules at runtime, 165 // javax.sound.sampled.MyClass is archived from -Xbootclasspath/a. It can be 166 // loaded from the archive at runtime. 167 public static void testBootAppendExcludedModuleClass() throws Exception { 168 for (String mode : modes) { 169 CDSOptions opts = (new CDSOptions()) 170 .setXShareMode(mode).setUseVersion(false) 171 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 172 "--limit-modules=java.base,jdk.internal.vm.compiler", "-cp", appJar) 173 .addSuffix("-Xlog:class+load=info", 174 APP_CLASS, BOOT_APPEND_MODULE_CLASS_NAME); 175 176 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 177 CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass"); 178 179 // When CDS is enabled, the shared class should be loaded from the archive. 180 if (mode.equals("on")) { 181 CDSTestUtils.checkExec(out, opts, "[class,load] javax.sound.sampled.MyClass source: shared objects file"); 182 } 183 } 184 } 185 186 // Test #4: If a class on -Xbootclasspath/a has the same fully qualified 187 // name as a class defined in boot modules, the class is loaded 188 // from -Xbootclasspath/a when the boot module is excluded using 189 // --limit-modules. Verify the behavior is the same at runtime 190 // when CDS is enabled. 191 // 192 // The org.omg.CORBA.Context is a boot module class. The class 193 // on -Xbootclasspath/a that has the same fully-qualified name 194 // as org.omg.CORBA.Context can be loaded at runtime when 195 // java.corba is excluded. 196 public static void testBootAppendDuplicateExcludedModuleClass() throws Exception { 197 for (String mode : modes) { 198 CDSOptions opts = (new CDSOptions()) 199 .setXShareMode(mode).setUseVersion(false) 200 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 201 "--limit-modules=java.base,jdk.internal.vm.compiler", "-cp", appJar) 202 .addSuffix("-Xlog:class+load=info", 203 APP_CLASS, BOOT_APPEND_DUPLICATE_MODULE_CLASS_NAME); 204 205 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 206 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context"); 207 if (!CDSTestUtils.isUnableToMap(out)) { 208 if (mode.equals("off")) { 209 out.shouldMatch(".*\\[class,load\\] org.omg.CORBA.Context source:.*bootAppend.jar"); 210 } else { 211 CDSTestUtils.checkExec(out, opts, "[class,load] org.omg.CORBA.Context source: shared objects file"); 212 } 213 } 214 } 215 } 216 217 // Test #5: If a class on -Xbootclasspath/a is not from named modules, 218 // the class can be loaded at runtime. Verify the behavior is 219 // the same at runtime when CDS is enabled. 220 // 221 // The nonjdk.myPackage is not defined in named modules. The 222 // archived nonjdk.myPackage.MyClass from -Xbootclasspath/a 223 // can be loaded at runtime when CDS is enabled. 224 public static void testBootAppendClass() throws Exception { 225 for (String mode : modes) { 226 CDSOptions opts = (new CDSOptions()) 227 .setXShareMode(mode).setUseVersion(false) 228 .addPrefix("-Xbootclasspath/a:" + bootAppendJar, "-showversion", 229 "--limit-modules=java.base,jdk.internal.vm.compiler", "-cp", appJar) 230 .addSuffix("-Xlog:class+load=info", 231 APP_CLASS, BOOT_APPEND_CLASS_NAME); 232 233 OutputAnalyzer out = CDSTestUtils.runWithArchive(opts); 234 CDSTestUtils.checkExec(out, opts, "[class,load] nonjdk.myPackage.MyClass"); 235 236 // If CDS is enabled, the nonjdk.myPackage.MyClass should be loaded 237 // from the shared archive. 238 if (mode.equals("on")) { 239 CDSTestUtils.checkExec(out, opts, 240 "[class,load] nonjdk.myPackage.MyClass source: shared objects file"); 241 } 242 } 243 } 244 }