|
1 /* |
|
2 * Copyright (c) 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 * @library /lib/testlibrary |
|
27 * @build LayerControllerTest ModuleUtils |
|
28 * @run testng LayerControllerTest |
|
29 * @summary Basic tests for java.lang.ModuleLayer.Controller |
|
30 */ |
|
31 |
|
32 import java.lang.module.Configuration; |
|
33 import java.lang.module.ModuleDescriptor; |
|
34 import java.lang.module.ModuleFinder; |
|
35 import java.util.List; |
|
36 import java.util.Set; |
|
37 |
|
38 import org.testng.annotations.Test; |
|
39 import static org.testng.Assert.*; |
|
40 |
|
41 @Test |
|
42 public class LayerControllerTest { |
|
43 |
|
44 /** |
|
45 * Creates a Controller for a module layer containing modules m1 and m2. |
|
46 * Module m1 contains p1, reads java.base, does not export/open any package |
|
47 * Module m2 contains p2, reads java.base, does not export/open any package |
|
48 */ |
|
49 private ModuleLayer.Controller createTestLayer() { |
|
50 ModuleDescriptor descriptor1 |
|
51 = ModuleDescriptor.newModule("m1") |
|
52 .packages(Set.of("p1")) |
|
53 .requires("java.base") |
|
54 .build(); |
|
55 |
|
56 ModuleDescriptor descriptor2 |
|
57 = ModuleDescriptor.newModule("m2") |
|
58 .requires("java.base") |
|
59 .packages(Set.of("p2")) |
|
60 .build(); |
|
61 |
|
62 ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2); |
|
63 ModuleLayer bootLayer = ModuleLayer.boot(); |
|
64 |
|
65 Configuration cf = bootLayer.configuration() |
|
66 .resolve(finder, ModuleFinder.of(), Set.of("m1", "m2")); |
|
67 |
|
68 ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
69 |
|
70 ModuleLayer.Controller controller |
|
71 = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl); |
|
72 |
|
73 ModuleLayer layer = controller.layer(); |
|
74 |
|
75 assertTrue(layer.modules().size() == 2); |
|
76 assertTrue(layer.findModule("m1").isPresent()); |
|
77 assertTrue(layer.findModule("m2").isPresent()); |
|
78 |
|
79 return controller; |
|
80 } |
|
81 |
|
82 /** |
|
83 * Basic test of Controller to update modules m1 and m2 to read and |
|
84 * open packages to each other. |
|
85 */ |
|
86 public void testBasic() { |
|
87 ModuleLayer.Controller controller = createTestLayer(); |
|
88 ModuleLayer layer = controller.layer(); |
|
89 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); |
|
90 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); |
|
91 |
|
92 assertFalse(m1.canRead(m2)); |
|
93 assertFalse(m1.isExported("p1")); |
|
94 assertFalse(m1.isOpen("p1")); |
|
95 assertFalse(m1.isExported("p1", m2)); |
|
96 assertFalse(m1.isOpen("p1", m2)); |
|
97 |
|
98 assertFalse(m2.canRead(m1)); |
|
99 assertFalse(m2.isExported("p2")); |
|
100 assertFalse(m2.isOpen("p2")); |
|
101 assertFalse(m2.isExported("p2", m1)); |
|
102 assertFalse(m2.isOpen("p2", m1)); |
|
103 |
|
104 // update m1 to read m2 |
|
105 assertTrue(controller.addReads(m1, m2) == controller); |
|
106 assertTrue(m1.canRead(m2)); |
|
107 assertFalse(m2.canRead(m1)); |
|
108 |
|
109 // update m2 to read m1 |
|
110 assertTrue(controller.addReads(m2, m1) == controller); |
|
111 assertTrue(m1.canRead(m2)); |
|
112 assertTrue(m1.canRead(m1)); |
|
113 |
|
114 // update m1 to open p1 to m2 |
|
115 assertTrue(controller.addOpens(m1, "p1", m2) == controller); |
|
116 assertTrue(m1.isExported("p1", m2)); |
|
117 assertTrue(m1.isOpen("p1", m2)); |
|
118 assertFalse(m1.isExported("p1")); |
|
119 assertFalse(m1.isOpen("p1")); |
|
120 |
|
121 // update m2 to open p2 to m1 |
|
122 assertTrue(controller.addOpens(m2, "p2", m1) == controller); |
|
123 assertTrue(m2.isExported("p2", m1)); |
|
124 assertTrue(m2.isOpen("p2", m1)); |
|
125 assertFalse(m2.isExported("p2")); |
|
126 assertFalse(m2.isOpen("p2")); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Test invalid argument handling |
|
131 */ |
|
132 public void testBadArguments() { |
|
133 ModuleLayer.Controller controller = createTestLayer(); |
|
134 ModuleLayer layer = controller.layer(); |
|
135 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); |
|
136 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); |
|
137 Module base = Object.class.getModule(); |
|
138 |
|
139 // java.base is not in layer |
|
140 try { |
|
141 controller.addReads(base, m2); |
|
142 assertTrue(false); |
|
143 } catch (IllegalArgumentException expected) { } |
|
144 |
|
145 // java.base is not in layer |
|
146 try { |
|
147 controller.addOpens(base, "java.lang", m2); |
|
148 assertTrue(false); |
|
149 } catch (IllegalArgumentException expected) { } |
|
150 |
|
151 // m1 does not contain java.lang |
|
152 try { |
|
153 controller.addOpens(m1, "java.lang", m2); |
|
154 assertTrue(false); |
|
155 } catch (IllegalArgumentException expected) { } |
|
156 } |
|
157 |
|
158 /** |
|
159 * Test null handling |
|
160 */ |
|
161 public void testNulls() { |
|
162 ModuleLayer.Controller controller = createTestLayer(); |
|
163 ModuleLayer layer = controller.layer(); |
|
164 Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new); |
|
165 Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new); |
|
166 assertTrue(m1 != null); |
|
167 assertTrue(m2 != null); |
|
168 |
|
169 try { |
|
170 controller.addReads(null, m2); |
|
171 assertTrue(false); |
|
172 } catch (NullPointerException expected) { } |
|
173 |
|
174 try { |
|
175 controller.addReads(m1, null); |
|
176 assertTrue(false); |
|
177 } catch (NullPointerException expected) { } |
|
178 |
|
179 try { |
|
180 controller.addOpens(null, "p1", m2); |
|
181 assertTrue(false); |
|
182 } catch (NullPointerException expected) { } |
|
183 |
|
184 try { |
|
185 controller.addOpens(m1, null, m2); |
|
186 assertTrue(false); |
|
187 } catch (NullPointerException expected) { } |
|
188 |
|
189 try { |
|
190 controller.addOpens(m1, "p1", null); |
|
191 assertTrue(false); |
|
192 } catch (NullPointerException expected) { } |
|
193 } |
|
194 } |