1 /*
   2  * Copyright (c) 2015, 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 /*
  26  * @test
  27  * @summary Test a few scenarios if an empty class, which has the same name as the one in the jimage, is specified in the -Xbootclasspath/a
  28  *     1) boot loader will always load the class from the bootclasspath
  29  *     2) app loader will load the class from the jimage by default;
  30  *        app loader will load the class from the bootclasspath if the
  31  *        "--limit-modules java.base" option is specified
  32  * @requires vm.cds & !vm.graal.enabled
  33  * @library /test/lib /test/hotspot/jtreg/runtime/appcds
  34  * @modules java.base/jdk.internal.access
  35  *          java.management
  36  *          jdk.jartool/sun.tools.jar
  37  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  38  * @compile ../../test-classes/EmptyClassHelper.java
  39  * @compile ../../test-classes/com/sun/tools/javac/Main.jasm
  40  * @run main EmptyClassInBootClassPath
  41  */
  42 
  43 import java.io.File;
  44 import java.lang.*;
  45 import java.lang.reflect.*;
  46 import java.util.List;
  47 import java.util.ArrayList;
  48 import jdk.test.lib.process.OutputAnalyzer;
  49 
  50 public class EmptyClassInBootClassPath {
  51     static final String EXPECTED_EXCEPTION =
  52         "java.lang.NoSuchMethodException: com.sun.tools.javac.Main.main([Ljava.lang.String;)";
  53     public static void main(String[] args) throws Exception {
  54         String[] className = {"com/sun/tools/javac/Main"};
  55         JarBuilder.build("emptyClass", className);
  56         String appJar = TestCommon.getTestJar("emptyClass.jar");
  57         JarBuilder.build("EmptyClassHelper", "EmptyClassHelper");
  58         String helperJar = TestCommon.getTestJar("EmptyClassHelper.jar");
  59         OutputAnalyzer dumpOutput = TestCommon.dump(
  60             appJar, className, "-Xbootclasspath/a:" + appJar);
  61         TestCommon.checkDump(dumpOutput);
  62         dumpOutput.shouldNotContain("Preload Warning: skipping class from -Xbootclasspath/a " + className[0]);
  63 
  64         String bootclasspath = "-Xbootclasspath/a:" + appJar;
  65         String classPath = "-Djava.class.path=" + appJar + File.pathSeparator + helperJar;
  66         List<String> argsList = new ArrayList<String>();
  67         argsList.add(classPath);
  68         argsList.add(bootclasspath);
  69         argsList.add("--add-exports=java.base/jdk.internal.access=ALL-UNNAMED");
  70         argsList.add("EmptyClassHelper");
  71 
  72         // case 1: load class in bootclasspath using app loader
  73         argsList.add("useAppLoader");
  74         String[] opts = new String[argsList.size()];
  75         opts = argsList.toArray(opts);
  76         TestCommon.run(opts).assertNormalExit("appLoader found method main");
  77 
  78         // case 2: load class in bootclasspath using boot loader
  79         argsList.remove(argsList.size() - 1);
  80         argsList.add("useBootLoader");
  81         opts = new String[argsList.size()];
  82         opts = argsList.toArray(opts);
  83         TestCommon.run(opts).assertNormalExit(EXPECTED_EXCEPTION);
  84 
  85         // case 3: load class in bootclasspath using app loader with '--limit-modules java.base'
  86         argsList.add(0, "--limit-modules");
  87         argsList.add(1, "java.base");
  88         argsList.remove(argsList.size() - 1);
  89         argsList.add("useAppLoader");
  90         opts = new String[argsList.size()];
  91         opts = argsList.toArray(opts);
  92         TestCommon.run(opts)
  93             .assertSilentlyDisabledCDS(0, EXPECTED_EXCEPTION);
  94 
  95         // case 4: load class in bootclasspath using boot loader with '--limit-modules java.base'
  96         argsList.remove(argsList.size() - 1);
  97         argsList.add("useBootLoader");
  98         opts = new String[argsList.size()];
  99         opts = argsList.toArray(opts);
 100         TestCommon.run(opts)
 101             .assertSilentlyDisabledCDS(0, EXPECTED_EXCEPTION);
 102     }
 103 }