1 /* |
|
2 * Copyright (c) 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 package test; |
|
25 |
|
26 import java.lang.invoke.MethodHandle; |
|
27 import java.lang.invoke.MethodHandles; |
|
28 import java.lang.invoke.MethodType; |
|
29 import java.lang.reflect.Constructor; |
|
30 import java.lang.reflect.Module; |
|
31 import java.util.ServiceConfigurationError; |
|
32 import java.util.ServiceLoader; |
|
33 |
|
34 import org.testng.annotations.Test; |
|
35 import static org.testng.Assert.*; |
|
36 |
|
37 /** |
|
38 * Basic test case for Module::addXXXX methods |
|
39 */ |
|
40 |
|
41 @Test |
|
42 public class Main { |
|
43 |
|
44 /** |
|
45 * Test Module::addReads |
|
46 * |
|
47 * module test { } |
|
48 * |
|
49 * module m1 { |
|
50 * exports p1; |
|
51 * } |
|
52 */ |
|
53 public void testAddReads() throws Throwable { |
|
54 Module thisModule = Main.class.getModule(); |
|
55 Class<?> clazz = Class.forName("p1.C"); |
|
56 Module m1 = clazz.getModule(); |
|
57 |
|
58 // test does not read m1 |
|
59 assertFalse(thisModule.canRead(m1)); |
|
60 MethodHandles.Lookup lookup = MethodHandles.lookup(); |
|
61 MethodType mt = MethodType.methodType(void.class); |
|
62 try { |
|
63 lookup.findConstructor(clazz, mt); |
|
64 assertTrue(false); |
|
65 } catch (IllegalAccessException expected) { } |
|
66 |
|
67 // update test to read m1 |
|
68 Module result = thisModule.addReads(m1); |
|
69 assertTrue(result== thisModule); |
|
70 assertTrue(thisModule.canRead(m1)); |
|
71 MethodHandle mh = lookup.findConstructor(clazz, mt); |
|
72 Object obj = mh.invoke(); |
|
73 |
|
74 // attempt to update m1 to read test |
|
75 try { |
|
76 m1.addReads(thisModule); |
|
77 assertTrue(false); |
|
78 } catch (IllegalCallerException expected) { } |
|
79 } |
|
80 |
|
81 |
|
82 /** |
|
83 * Test Module::addExports |
|
84 * |
|
85 * module test { |
|
86 * requires m2; |
|
87 * } |
|
88 * module m2 { |
|
89 * exports p2; |
|
90 * contains package p2.internal; |
|
91 * } |
|
92 */ |
|
93 public void testAddExports() throws Exception { |
|
94 Module thisModule = Main.class.getModule(); |
|
95 Module m2 = p2.C.class.getModule(); |
|
96 Class<?> targetClass = Class.forName("p2.internal.C"); |
|
97 String p2Internal = targetClass.getPackageName(); |
|
98 assertTrue(targetClass.getModule() == m2); |
|
99 |
|
100 // m2 does not export p2.internal to test |
|
101 assertFalse(m2.isExported(p2Internal, thisModule)); |
|
102 Constructor<?> ctor = targetClass.getDeclaredConstructor(); |
|
103 try { |
|
104 ctor.newInstance(); |
|
105 assertTrue(false); |
|
106 } catch (IllegalAccessException expected) { } |
|
107 |
|
108 // update m2 to export p2.internal to test |
|
109 p2.C.export(p2Internal, thisModule); |
|
110 assertTrue(m2.isExported(p2Internal, thisModule)); |
|
111 ctor.newInstance(); // should succeed |
|
112 |
|
113 // attempt to update m2 to export a package to test |
|
114 try { |
|
115 m2.addExports("p2.other", thisModule); |
|
116 assertTrue(false); |
|
117 } catch (IllegalCallerException expected) { } |
|
118 } |
|
119 |
|
120 /** |
|
121 * Test Module::addOpens |
|
122 * |
|
123 * module test { |
|
124 * requires m3; |
|
125 * requires m4; |
|
126 * } |
|
127 * |
|
128 * module m3 { |
|
129 * exports p3 to test; |
|
130 * opens p3 to test; |
|
131 * } |
|
132 * |
|
133 * module m4 { |
|
134 * exports p4; |
|
135 * } |
|
136 */ |
|
137 public void testAddOpens() throws Exception { |
|
138 Module thisModule = Main.class.getModule(); |
|
139 Module m3 = p3.C.class.getModule(); |
|
140 Module m4 = p4.C.class.getModule(); |
|
141 |
|
142 // test does not open package test to m4 |
|
143 assertFalse(thisModule.isOpen("test", m4)); |
|
144 try { |
|
145 p4.C.tryNewInstance(test.C.class); |
|
146 assertTrue(false); |
|
147 } catch (IllegalAccessException expected) { } |
|
148 |
|
149 // open test to m4 |
|
150 thisModule.addOpens("test", m4); |
|
151 p4.C.tryNewInstance(test.C.class); // should succeed |
|
152 |
|
153 |
|
154 // m3 does not open p3 to m4 |
|
155 assertFalse(m3.isOpen("p3", m4)); |
|
156 try { |
|
157 p4.C.tryNewInstance(p3.C.class); |
|
158 assertTrue(false); |
|
159 } catch (IllegalAccessException expected) { } |
|
160 |
|
161 |
|
162 // m3 opens p3 to test => test allowed to open m3/p3 to m4 |
|
163 assertTrue(m3.isOpen("p3", thisModule)); |
|
164 m3.addOpens("p3", m4); |
|
165 assertTrue(m3.isOpen("p3", m4)); |
|
166 p4.C.tryNewInstance(p3.C.class); // should succeed |
|
167 |
|
168 |
|
169 // attempt to update m4 to open package to m3 |
|
170 try { |
|
171 m4.addOpens("p4", m3); |
|
172 assertTrue(false); |
|
173 } catch (IllegalCallerException expected) { } |
|
174 } |
|
175 |
|
176 |
|
177 /** |
|
178 * Test Module::addUses |
|
179 */ |
|
180 public void testAddUses() { |
|
181 Module thisModule = Main.class.getModule(); |
|
182 |
|
183 assertFalse(thisModule.canUse(Service.class)); |
|
184 try { |
|
185 ServiceLoader.load(Service.class); |
|
186 assertTrue(false); |
|
187 } catch (ServiceConfigurationError expected) { } |
|
188 |
|
189 Module result = thisModule.addUses(Service.class); |
|
190 assertTrue(result== thisModule); |
|
191 |
|
192 assertTrue(thisModule.canUse(Service.class)); |
|
193 ServiceLoader.load(Service.class); // no exception |
|
194 } |
|
195 |
|
196 } |
|