1 /*
2  * Copyright (c) 2016, 2017, 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  * @library /test/lib ..
28  * @build sun.hotspot.WhiteBox
29  * @compile/module=java.base java/lang/ModuleHelper.java
30  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
31  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
32  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI JVMAddModuleExports
33  */
34 
35 import static jdk.test.lib.Asserts.*;
36 
37 public class JVMAddModuleExports {
38 
main(String args[])39     public static void main(String args[]) throws Throwable {
40         MyClassLoader from_cl = new MyClassLoader();
41         MyClassLoader to_cl = new MyClassLoader();
42         Module from_module, to_module;
43 
44         from_module = ModuleHelper.ModuleObject("from_module", from_cl, new String[] { "mypackage", "x/apackage" });
45         assertNotNull(from_module, "Module should not be null");
46         ModuleHelper.DefineModule(from_module, false, "9.0", "from_module/here", new String[] { "mypackage", "x/apackage" });
47         to_module = ModuleHelper.ModuleObject("to_module", to_cl, new String[] { "yourpackage", "that/apackage" });
48         assertNotNull(to_module, "Module should not be null");
49         ModuleHelper.DefineModule(to_module, false, "9.0", "to_module/here", new String[] { "yourpackage", "that/apackage" });
50 
51         // Null from_module argument, expect an NPE
52         try {
53             ModuleHelper.AddModuleExports((Module)null, "mypackage", to_module);
54             throw new RuntimeException("Failed to get the expected NPE for null from_module");
55         } catch(NullPointerException e) {
56             // Expected
57         }
58 
59         // Null to_module argument, expect an NPE
60         try {
61             ModuleHelper.AddModuleExports(from_module, "mypackage", (Module)null);
62             throw new RuntimeException("Failed to get the expected NPE for null to_module");
63         } catch(NullPointerException e) {
64             // Expected
65         }
66 
67         // Bad from_module argument, expect an IAE
68         try {
69             ModuleHelper.AddModuleExports(to_cl, "mypackage", to_module);
70             throw new RuntimeException("Failed to get the expected IAE");
71         } catch(IllegalArgumentException e) {
72             // Expected
73         }
74 
75         // Null package argument, expect an NPE
76         try {
77             ModuleHelper.AddModuleExports(from_module, null, to_module);
78             throw new RuntimeException("Failed to get the expected NPE");
79         } catch(NullPointerException e) {
80             // Expected
81         }
82 
83         // Bad to_module argument, expect an IAE
84         try {
85             ModuleHelper.AddModuleExports(from_module, "mypackage", from_cl);
86             throw new RuntimeException("Failed to get the expected IAE");
87         } catch(IllegalArgumentException e) {
88             // Expected
89         }
90 
91         // Exporting a package to the same module
92         ModuleHelper.AddModuleExports(from_module, "mypackage", from_module);
93 
94         // Export a package that does not exist to to_module
95         try {
96             ModuleHelper.AddModuleExports(from_module, "notmypackage", to_module);
97             throw new RuntimeException("Failed to get the expected IAE");
98         } catch(IllegalArgumentException e) {
99             // Expected
100         }
101 
102         // Export a package, that is not in from_module, to to_module
103         try {
104             ModuleHelper.AddModuleExports(from_module, "yourpackage", to_module);
105             throw new RuntimeException("Failed to get the expected IAE");
106         } catch(IllegalArgumentException e) {
107             // Expected
108         }
109 
110         // Export a package, that does not exist, to from_module
111         try {
112             ModuleHelper.AddModuleExports(from_module, "notmypackage", from_module);
113             throw new RuntimeException("Failed to get the expected IAE");
114         } catch(IllegalArgumentException e) {
115             // Expected
116         }
117 
118         // Export a package, that is not in from_module, to from_module
119         try {
120             ModuleHelper.AddModuleExports(from_module, "that/apackage", from_module);
121             throw new RuntimeException("Failed to get the expected IAE");
122         } catch(IllegalArgumentException e) {
123             // Expected
124         }
125 
126         // Export the same package twice to the same module
127         ModuleHelper.AddModuleExports(from_module, "x/apackage", to_module);
128         ModuleHelper.AddModuleExports(from_module, "x/apackage", to_module);
129 
130         // Export a package, using '.' instead of '/'
131         try {
132             ModuleHelper.AddModuleExports(from_module, "x.apackage", to_module);
133             throw new RuntimeException("Failed to get the expected IAE");
134         } catch(IllegalArgumentException e) {
135             // Expected
136         }
137 
138         // Export a package to the unnamed module and then to a specific module.
139         // The qualified export should be ignored.
140         ModuleHelper.AddModuleExportsToAll(to_module, "that/apackage");
141         ModuleHelper.AddModuleExports(to_module, "that/apackage", from_module);
142     }
143 
144     static class MyClassLoader extends ClassLoader { }
145 }
146