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