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