1 /* |
|
2 * Copyright (c) 2014, 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 import java.lang.module.ModuleDescriptor.Exports; |
|
25 import java.lang.module.ResolvedModule; |
|
26 import java.lang.reflect.Layer; |
|
27 import java.lang.reflect.Module; |
|
28 import java.nio.file.spi.FileSystemProvider; // service type in java.base |
|
29 import java.util.function.Predicate; |
|
30 import java.util.stream.Stream; |
|
31 import javax.print.PrintServiceLookup; // service type in java.desktop |
|
32 |
|
33 import org.testng.annotations.Test; |
|
34 import static org.testng.Assert.*; |
|
35 |
|
36 /* |
|
37 * @test |
|
38 * @summary Basic test of java.lang.reflect.Module |
|
39 * @modules java.desktop java.xml |
|
40 * @run testng BasicModuleTest |
|
41 */ |
|
42 |
|
43 public class BasicModuleTest { |
|
44 |
|
45 /** |
|
46 * Tests that the given module reads all modules in the boot Layer. |
|
47 */ |
|
48 private void testReadsAllBootModules(Module m) { |
|
49 Layer bootLayer = Layer.boot(); |
|
50 bootLayer.configuration() |
|
51 .modules() |
|
52 .stream() |
|
53 .map(ResolvedModule::name) |
|
54 .map(bootLayer::findModule) |
|
55 .forEach(target -> assertTrue(m.canRead(target.get()))); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Returns {@code true} if the array contains the given object. |
|
60 */ |
|
61 private <T> boolean contains(T[] array, T obj) { |
|
62 return Stream.of(array).anyMatch(obj::equals); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Returns a {@code Predicate} to test if a package is exported. |
|
67 */ |
|
68 private Predicate<Exports> doesExport(String pn) { |
|
69 return e -> (e.source().equals(pn) && !e.isQualified()); |
|
70 } |
|
71 |
|
72 |
|
73 |
|
74 @Test |
|
75 public void testThisModule() { |
|
76 Module thisModule = BasicModuleTest.class.getModule(); |
|
77 Module baseModule = Object.class.getModule(); |
|
78 |
|
79 assertFalse(thisModule.isNamed()); |
|
80 assertTrue(thisModule.getName() == null); |
|
81 assertTrue(thisModule.getDescriptor() == null); |
|
82 assertTrue(thisModule.getLayer() == null); |
|
83 assertTrue(thisModule.toString().startsWith("unnamed module ")); |
|
84 |
|
85 ClassLoader thisLoader = BasicModuleTest.class.getClassLoader(); |
|
86 assertTrue(thisLoader == thisModule.getClassLoader()); |
|
87 assertTrue(thisLoader.getUnnamedModule() == thisModule); |
|
88 |
|
89 // unnamed modules read all other modules |
|
90 ClassLoader cl; |
|
91 cl = ClassLoader.getPlatformClassLoader(); |
|
92 assertTrue(thisModule.canRead(cl.getUnnamedModule())); |
|
93 cl = ClassLoader.getSystemClassLoader(); |
|
94 assertTrue(thisModule.canRead(cl.getUnnamedModule())); |
|
95 testReadsAllBootModules(thisModule); |
|
96 |
|
97 // unnamed modules export all packages |
|
98 assertTrue(thisModule.isExported("")); |
|
99 assertTrue(thisModule.isExported("", thisModule)); |
|
100 assertTrue(thisModule.isExported("", baseModule)); |
|
101 assertTrue(thisModule.isExported("p")); |
|
102 assertTrue(thisModule.isExported("p", thisModule)); |
|
103 assertTrue(thisModule.isExported("p", baseModule)); |
|
104 |
|
105 // this test is in the unnamed package |
|
106 assertTrue(contains(thisModule.getPackages(), "")); |
|
107 } |
|
108 |
|
109 |
|
110 @Test |
|
111 public void testUnnamedModules() { |
|
112 Module thisModule = BasicModuleTest.class.getModule(); |
|
113 Module baseModule = Object.class.getModule(); |
|
114 |
|
115 ClassLoader loader1 = ClassLoader.getSystemClassLoader(); |
|
116 ClassLoader loader2 = loader1.getParent(); |
|
117 |
|
118 Module m1 = loader1.getUnnamedModule(); |
|
119 Module m2 = loader2.getUnnamedModule(); |
|
120 |
|
121 assertTrue(m1 != m2); |
|
122 |
|
123 assertFalse(m1.isNamed()); |
|
124 assertFalse(m2.isNamed()); |
|
125 |
|
126 assertTrue(m1.getLayer() == null); |
|
127 assertTrue(m2.getLayer() == null); |
|
128 |
|
129 assertTrue(m1.toString().startsWith("unnamed module ")); |
|
130 assertTrue(m2.toString().startsWith("unnamed module ")); |
|
131 |
|
132 // unnamed module reads all modules |
|
133 assertTrue(m1.canRead(m2)); |
|
134 assertTrue(m2.canRead(m1)); |
|
135 |
|
136 testReadsAllBootModules(m1); |
|
137 testReadsAllBootModules(m2); |
|
138 |
|
139 assertTrue(m1.isExported("")); |
|
140 assertTrue(m1.isExported("", thisModule)); |
|
141 assertTrue(m1.isExported("", baseModule)); |
|
142 assertTrue(m1.isExported("p")); |
|
143 assertTrue(m1.isExported("p", thisModule)); |
|
144 assertTrue(m1.isExported("p", baseModule)); |
|
145 } |
|
146 |
|
147 |
|
148 |
|
149 @Test |
|
150 public void testBaseModule() { |
|
151 Module base = Object.class.getModule(); |
|
152 Module thisModule = BasicModuleTest.class.getModule(); |
|
153 |
|
154 // getName |
|
155 assertTrue(base.getName().equals("java.base")); |
|
156 |
|
157 // getDescriptor |
|
158 assertTrue(base.getDescriptor().exports().stream() |
|
159 .anyMatch(doesExport("java.lang"))); |
|
160 |
|
161 // getClassLoader |
|
162 assertTrue(base.getClassLoader() == null); |
|
163 |
|
164 // getLayer |
|
165 assertTrue(base.getLayer() == Layer.boot()); |
|
166 |
|
167 // toString |
|
168 assertEquals(base.toString(), "module java.base"); |
|
169 |
|
170 // getPackages |
|
171 assertTrue(contains(base.getPackages(), "java.lang")); |
|
172 |
|
173 // canRead |
|
174 assertTrue(base.canRead(base)); |
|
175 assertFalse(base.canRead(thisModule)); |
|
176 |
|
177 // addReads |
|
178 try { |
|
179 base.addReads(thisModule); |
|
180 assertTrue(false); |
|
181 } catch (IllegalCallerException expected) { } |
|
182 assertFalse(base.canRead(thisModule)); |
|
183 |
|
184 // isExported |
|
185 assertTrue(base.isExported("java.lang")); |
|
186 assertTrue(base.isExported("java.lang", thisModule)); |
|
187 assertTrue(base.isExported("java.lang", base)); |
|
188 assertFalse(base.isExported("jdk.internal.misc")); |
|
189 assertFalse(base.isExported("jdk.internal.misc", thisModule)); |
|
190 assertTrue(base.isExported("jdk.internal.misc", base)); |
|
191 assertFalse(base.isExported("java.wombat")); |
|
192 assertFalse(base.isExported("java.wombat", thisModule)); |
|
193 assertFalse(base.isExported("java.wombat", base)); |
|
194 |
|
195 // addExports |
|
196 try { |
|
197 base.addExports("java.lang", thisModule); |
|
198 assertTrue(false); |
|
199 } catch (IllegalCallerException expected) { } |
|
200 try { |
|
201 base.addExports("jdk.internal.misc", thisModule); |
|
202 assertTrue(false); |
|
203 } catch (IllegalCallerException expected) { } |
|
204 assertFalse(base.isExported("jdk.internal.misc")); |
|
205 assertFalse(base.isExported("jdk.internal.misc", thisModule)); |
|
206 |
|
207 // isOpen |
|
208 assertFalse(base.isOpen("java.lang")); |
|
209 assertFalse(base.isOpen("java.lang", thisModule)); |
|
210 assertTrue(base.isOpen("java.lang", base)); |
|
211 assertFalse(base.isOpen("jdk.internal.misc")); |
|
212 assertFalse(base.isOpen("jdk.internal.misc", thisModule)); |
|
213 assertTrue(base.isOpen("jdk.internal.misc", base)); |
|
214 assertFalse(base.isOpen("java.wombat")); |
|
215 assertFalse(base.isOpen("java.wombat", thisModule)); |
|
216 assertFalse(base.isOpen("java.wombat", base)); |
|
217 |
|
218 // addOpens |
|
219 try { |
|
220 base.addOpens("jdk.internal.misc", thisModule); |
|
221 assertTrue(false); |
|
222 } catch (IllegalCallerException expected) { } |
|
223 assertFalse(base.isOpen("jdk.internal.misc")); |
|
224 assertFalse(base.isOpen("jdk.internal.misc", thisModule)); |
|
225 |
|
226 // canUse |
|
227 assertTrue(base.canUse(FileSystemProvider.class)); |
|
228 assertFalse(base.canUse(Thread.class)); |
|
229 |
|
230 // addUses |
|
231 try { |
|
232 base.addUses(FileSystemProvider.class); |
|
233 assertTrue(false); |
|
234 } catch (IllegalCallerException expected) { } |
|
235 try { |
|
236 base.addUses(Thread.class); |
|
237 assertTrue(false); |
|
238 } catch (IllegalCallerException expected) { } |
|
239 assertFalse(base.canUse(Thread.class)); |
|
240 } |
|
241 |
|
242 |
|
243 @Test |
|
244 public void testDesktopModule() { |
|
245 Module desktop = java.awt.Component.class.getModule(); |
|
246 Module base = Object.class.getModule(); |
|
247 Module xml = javax.xml.XMLConstants.class.getModule(); |
|
248 Module thisModule = BasicModuleTest.class.getModule(); |
|
249 |
|
250 // name |
|
251 assertTrue(desktop.getName().equals("java.desktop")); |
|
252 |
|
253 // descriptor |
|
254 assertTrue(desktop.getDescriptor().exports().stream() |
|
255 .anyMatch(doesExport("java.awt"))); |
|
256 |
|
257 // getClassLoader |
|
258 assertTrue(desktop.getClassLoader() == null); |
|
259 |
|
260 // getLayer |
|
261 assertTrue(desktop.getLayer() == Layer.boot()); |
|
262 |
|
263 // toString |
|
264 assertEquals(desktop.toString(), "module java.desktop"); |
|
265 |
|
266 // getPackages |
|
267 assertTrue(contains(desktop.getPackages(), "java.awt")); |
|
268 assertTrue(contains(desktop.getPackages(), "sun.awt")); |
|
269 |
|
270 // canRead |
|
271 assertTrue(desktop.canRead(base)); |
|
272 assertTrue(desktop.canRead(xml)); |
|
273 |
|
274 // addReads |
|
275 try { |
|
276 desktop.addReads(thisModule); |
|
277 assertTrue(false); |
|
278 } catch (IllegalCallerException expected) { } |
|
279 assertFalse(desktop.canRead(thisModule)); |
|
280 |
|
281 // isExported |
|
282 assertTrue(desktop.isExported("java.awt")); |
|
283 assertTrue(desktop.isExported("java.awt", thisModule)); |
|
284 assertFalse(desktop.isExported("sun.awt")); |
|
285 assertFalse(desktop.isExported("sun.awt", thisModule)); |
|
286 assertTrue(desktop.isExported("sun.awt", desktop)); |
|
287 assertFalse(desktop.isExported("java.wombat")); |
|
288 assertFalse(desktop.isExported("java.wombat", thisModule)); |
|
289 assertFalse(desktop.isExported("java.wombat", base)); |
|
290 |
|
291 // addExports |
|
292 try { |
|
293 desktop.addExports("java.awt", thisModule); |
|
294 assertTrue(false); |
|
295 } catch (IllegalCallerException expected) { } |
|
296 try { |
|
297 desktop.addExports("sun.awt", thisModule); |
|
298 assertTrue(false); |
|
299 } catch (IllegalCallerException expected) { } |
|
300 assertFalse(desktop.isExported("sun.awt")); |
|
301 assertFalse(desktop.isExported("sun.awt", thisModule)); |
|
302 |
|
303 // isOpen |
|
304 assertFalse(desktop.isOpen("java.awt")); |
|
305 assertFalse(desktop.isOpen("java.awt", thisModule)); |
|
306 assertTrue(desktop.isOpen("java.awt", desktop)); |
|
307 assertFalse(desktop.isOpen("sun.awt")); |
|
308 assertFalse(desktop.isOpen("sun.awt", thisModule)); |
|
309 assertTrue(desktop.isOpen("sun.awt", desktop)); |
|
310 assertFalse(desktop.isOpen("java.wombat")); |
|
311 assertFalse(desktop.isOpen("java.wombat", thisModule)); |
|
312 assertFalse(desktop.isOpen("java.wombat", desktop)); |
|
313 |
|
314 // addOpens |
|
315 try { |
|
316 base.addOpens("sun.awt", thisModule); |
|
317 assertTrue(false); |
|
318 } catch (IllegalCallerException expected) { } |
|
319 assertFalse(desktop.isOpen("sun.awt")); |
|
320 assertFalse(desktop.isOpen("sun.awt", thisModule)); |
|
321 |
|
322 // canUse |
|
323 assertTrue(base.canUse(FileSystemProvider.class)); |
|
324 assertFalse(base.canUse(Thread.class)); |
|
325 |
|
326 // addUses |
|
327 try { |
|
328 desktop.addUses(PrintServiceLookup.class); |
|
329 assertTrue(false); |
|
330 } catch (IllegalCallerException expected) { } |
|
331 try { |
|
332 desktop.addUses(Thread.class); |
|
333 assertTrue(false); |
|
334 } catch (IllegalCallerException expected) { } |
|
335 assertFalse(desktop.canUse(Thread.class)); |
|
336 } |
|
337 |
|
338 } |
|