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