1 /*
   2  * Copyright (c) 2015, 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 Ensure that a package whose module has not been defined to the boot loader
  27  *          is correctly located with -Xbootclasspath/a
  28  * @requires !(os.family == "windows")
  29  * @library /test/lib
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  * @run main/othervm XbootcpVisibility
  33  */
  34 
  35 import java.io.File;
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 
  39 import jdk.test.lib.InMemoryJavaCompiler;
  40 import jdk.test.lib.process.OutputAnalyzer;
  41 import jdk.test.lib.process.ProcessTools;
  42 
  43 public class XbootcpVisibility {
  44 
  45     public static void main(String[] args) throws Throwable {
  46 
  47         String Vis1_B_src =
  48                 "package p2;" +
  49                 "public class Vis1_B { public void m() { System.out.println(\"In B's m()\"); } }";
  50 
  51         ClassFileInstaller.writeClassToDisk("p2/Vis1_B",
  52             InMemoryJavaCompiler.compile("p2.Vis1_B", Vis1_B_src), System.getProperty("test.classes"));
  53         ClassFileInstaller.writeClassToDisk("p2/Vis1_B", "mods1");
  54 
  55         String Vis1_C_src =
  56                 "package p2;" +
  57                 "public class Vis1_C { public void m() { System.out.println(\"In C's m()\"); } }";
  58 
  59         ClassFileInstaller.writeClassToDisk("p2/Vis1_C",
  60             InMemoryJavaCompiler.compile("p2.Vis1_C", Vis1_C_src), System.getProperty("test.classes"));
  61         ClassFileInstaller.writeClassToDisk("p2/Vis1_C", "mods1");
  62 
  63         String Vis1_A_src =
  64             "public class Vis1_A {" +
  65             "    public static void main(String args[]) throws Exception {" +
  66             // Try loading a class within a named package
  67             // in the unnamed module.
  68             // Ensure the class can still be loaded successfully by the
  69             // boot loader since it is located on -Xbootclasspath/a.
  70             "        try {" +
  71             "            p2.Vis1_B b = new p2.Vis1_B();" +
  72             "            if (b.getClass().getClassLoader() != null) {" +
  73             "              throw new RuntimeException(\"XbootcpVisibility FAILED - class B " +
  74                                                       "should be loaded by boot class loader\\n\");" +
  75             "            }" +
  76             "            b.m();" +
  77             // Now that the package p2 has been recorded in the
  78             // unnamed module within the boot loader's PackageEntryTable,
  79             // ensure that a different class within the same package
  80             // can be located on -Xbootclasspath/a as well.
  81             "            p2.Vis1_C c = new p2.Vis1_C();" +
  82             "            if (c.getClass().getClassLoader() != null) {" +
  83             "              throw new RuntimeException(\"XbootcpVisibility FAILED - class C " +
  84                                                        "should be loaded by boot class loader\\n\");" +
  85             "            }" +
  86             "            c.m();" +
  87             "        } catch (Exception e) {" +
  88             "            System.out.println(e);" +
  89             "            throw new RuntimeException(\"XbootcpVisibility FAILED - " +
  90                                                      "test should not throw exception\\n\");" +
  91             "        }" +
  92             "        System.out.println(\"XbootcpVisibility PASSED\\n\");" +
  93             "    }" +
  94             "}";
  95 
  96         ClassFileInstaller.writeClassToDisk("Vis1_A",
  97                 InMemoryJavaCompiler.compile("Vis1_A", Vis1_A_src), System.getProperty("test.classes"));
  98 
  99         // Make sure the classes are actually being loaded from mods1
 100         Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
 101                                                                "p2" + File.separator + "Vis1_B.class"));
 102         Files.delete(Paths.get(System.getProperty("test.classes") +  File.separator +
 103                                                                "p2" + File.separator + "Vis1_C.class"));
 104 
 105         new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(
 106                 "-Xbootclasspath/a:nonexistent.jar",
 107                 "-Xbootclasspath/a:mods1",
 108                 "Vis1_A")
 109             .start()).shouldHaveExitValue(0);
 110     }
 111 }