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