GetSysPkgTest.java revision 12290:8953c0318163
1/*
2 * Copyright (c) 2015, 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 * @modules java.base/jdk.internal.misc
27 * @modules java.base/jdk.internal.loader
28 *          java.desktop
29 * @library /test/lib
30 * @run main/othervm GetSysPkgTest
31 */
32
33import java.io.File;
34import java.lang.reflect.InvocationTargetException;
35import java.lang.reflect.Method;
36import jdk.test.lib.InMemoryJavaCompiler;
37import jdk.test.lib.process.ProcessTools;
38import jdk.test.lib.process.OutputAnalyzer;
39
40// Test that JVM get_system_package() returns the module location for defined packages.
41public class GetSysPkgTest {
42
43    private static Object invoke(Method m, Object obj, Object... args) throws Throwable {
44        try {
45            return m.invoke(obj, args);
46        } catch (InvocationTargetException e) {
47            throw e.getCause();
48        }
49    }
50
51    private static Method findMethod(String name) {
52        for (Method m : jdk.internal.loader.BootLoader.class.getDeclaredMethods()) {
53            if (m.getName().equals(name)) {
54                m.setAccessible(true);
55                return m;
56            }
57        }
58        throw new RuntimeException("Failed to find method " + name + " in java.lang.reflect.Module");
59    }
60
61    // Throw RuntimeException if getSystemPackageLocation() does not return
62    // the expected location.
63    static void getPkg(String name, String expected_loc) throws Throwable {
64        String loc = (String)invoke(findMethod("getSystemPackageLocation"), null, name);
65        if (loc == null) {
66            if (expected_loc == null) return;
67            System.out.println("Expected location: " + expected_loc +
68                ", for package: " + name + ", got: null");
69        } else if (expected_loc == null) {
70            System.out.println("Expected location: null, for package: " +
71                name + ", got: " + loc);
72        } else if (!loc.equals(expected_loc)) {
73            System.out.println("Expected location: " +
74                expected_loc + ", for package: " + name + ", got: " + loc);
75        } else {
76            return;
77        }
78        throw new RuntimeException();
79    }
80
81    public static void main(String args[]) throws Throwable {
82        if (args.length == 0 || !args[0].equals("do_tests")) {
83
84            // Create a package found via -Xbootclasspath/a
85            String source = "package BootLdr_package; " +
86                            "public class BootLdrPkg { " +
87                            "    public int mth() { return 4; } " +
88                            "}";
89            byte[] klassbuf =
90                InMemoryJavaCompiler.compile("BootLdr_package.BootLdrPkg", source);
91            ClassFileInstaller.writeClassToDisk("BootLdr_package/BootLdrPkg", klassbuf, "bl_dir");
92
93            // Create a package found via -cp.
94            source = "package GetSysPkg_package; " +
95                     "public class GetSysClass { " +
96                     "    public int mth() { return 4; } " +
97                     "}";
98            klassbuf =
99                InMemoryJavaCompiler.compile("GetSysPkg_package.GetSysClass", source);
100            ClassFileInstaller.writeClassToDisk("GetSysPkg_package/GetSysClass", klassbuf);
101
102            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xbootclasspath/a:bl_dir",
103                "--add-opens=java.base/jdk.internal.loader=ALL-UNNAMED", "-cp", "." + File.pathSeparator +
104                System.getProperty("test.classes"), "GetSysPkgTest", "do_tests");
105            OutputAnalyzer output = new OutputAnalyzer(pb.start());
106            output.shouldHaveExitValue(0);
107            return;
108        }
109
110        getPkg("java/lang", "jrt:/java.base");
111        getPkg("javax/script", null);          // Package not defined
112
113        // Test a package that does not yet have any referenced classes.
114        // Note: if another class in com/sun/crypto/provider/ happens to get
115        //       loaded or if class PrivateKeyInfo disappears from this package
116        //       then this test will fail.
117        getPkg("com/sun/crypto/provider", null);
118        // Now make sure a class in the package is referenced.
119        Class newClass = Class.forName("com.sun.crypto.provider.PrivateKeyInfo");
120        getPkg("com/sun/crypto/provider", "jrt:/java.base");
121
122        getPkg("java/nio/charset", "jrt:/java.base");
123
124        // Test a package in a module not owned by boot loader.
125        Class clss = Class.forName("jdk.nio.zipfs.ZipPath");
126        if (clss == null)
127            throw new RuntimeException("Could not find class jdk.nio.zipfs.ZipPath");
128        getPkg("javax/activation", null);       // Not owned by boot loader
129
130        // Test a package not in jimage file.
131        clss = Class.forName("GetSysPkg_package.GetSysClass");
132        if (clss == null)
133            throw new RuntimeException("Could not find class GetSysPkg_package.GetSysClass");
134        getPkg("GetSysPkg_package", null);
135
136        // Access a class with a package in a boot loader module other than java.base
137        clss = Class.forName("java.awt.Button");
138        if (clss == null)
139            throw new RuntimeException("Could not find class java.awt.Button");
140        getPkg("java/awt", "jrt:/java.desktop");
141
142        // Test getting the package location from a class found via -Xbootclasspath/a
143        clss = Class.forName("BootLdr_package.BootLdrPkg");
144        if (clss == null)
145            throw new RuntimeException("Could not find class BootLdr_package.BootLdrPkg");
146        String bootldrPkg = (String)invoke(findMethod("getSystemPackageLocation"), null, "BootLdr_package");
147        if (bootldrPkg == null) {
148            throw new RuntimeException("Expected BootLdr_package to return non-null value");
149        }
150        if (!bootldrPkg.equals("bl_dir")) {
151            throw new RuntimeException("Expected BootLdr_package to return bl_dir, got: " + bootldrPkg);
152        }
153
154        // Test when package's class reference is an array.
155        // Note: if another class in javax/crypto happens to get loaded
156        //       or if class AEADBadTagException disappears from this package
157        //       then this test will fail.
158        getPkg("javax/crypto", null);
159        javax.crypto.AEADBadTagException[] blah = new javax.crypto.AEADBadTagException[3];
160        getPkg("javax/crypto", "jrt:/java.base");
161
162    }
163}
164