36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 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 |
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.util.function.Predicate;
|
|
29 |
import java.util.stream.Stream;
|
|
30 |
|
|
31 |
import org.testng.annotations.Test;
|
|
32 |
import static org.testng.Assert.*;
|
|
33 |
|
|
34 |
/*
|
|
35 |
* @test
|
|
36 |
* @summary Basic test of java.lang.reflect.Module
|
|
37 |
* @modules java.desktop java.xml
|
|
38 |
* @run testng BasicModuleTest
|
|
39 |
*/
|
|
40 |
|
|
41 |
public class BasicModuleTest {
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Tests that the given module reads all modules in the boot Layer.
|
|
45 |
*/
|
|
46 |
private void testReadsAllBootModules(Module m) {
|
|
47 |
Layer bootLayer = Layer.boot();
|
|
48 |
bootLayer.configuration()
|
|
49 |
.modules()
|
|
50 |
.stream()
|
|
51 |
.map(ResolvedModule::name)
|
|
52 |
.map(bootLayer::findModule)
|
|
53 |
.forEach(target -> assertTrue(m.canRead(target.get())));
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Returns {@code true} if the array contains the given object.
|
|
58 |
*/
|
|
59 |
private <T> boolean contains(T[] array, T obj) {
|
|
60 |
return Stream.of(array).anyMatch(obj::equals);
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Returns a {@code Predicate} to test if a package is exported.
|
|
65 |
*/
|
|
66 |
private Predicate<Exports> doesExport(String pn) {
|
|
67 |
return e -> (e.source().equals(pn) && !e.isQualified());
|
|
68 |
}
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
@Test
|
|
73 |
public void testThisModule() {
|
|
74 |
Module thisModule = BasicModuleTest.class.getModule();
|
|
75 |
Module baseModule = Object.class.getModule();
|
|
76 |
|
|
77 |
assertFalse(thisModule.isNamed());
|
|
78 |
assertTrue(thisModule.getName() == null);
|
|
79 |
assertTrue(thisModule.getDescriptor() == null);
|
|
80 |
assertTrue(thisModule.getLayer() == null);
|
|
81 |
assertTrue(thisModule.toString().startsWith("unnamed module "));
|
|
82 |
|
|
83 |
ClassLoader thisLoader = BasicModuleTest.class.getClassLoader();
|
|
84 |
assertTrue(thisLoader == thisModule.getClassLoader());
|
|
85 |
assertTrue(thisLoader.getUnnamedModule() == thisModule);
|
|
86 |
|
|
87 |
// unnamed modules read all other modules
|
|
88 |
ClassLoader cl;
|
|
89 |
cl = ClassLoader.getPlatformClassLoader();
|
|
90 |
assertTrue(thisModule.canRead(cl.getUnnamedModule()));
|
|
91 |
cl = ClassLoader.getSystemClassLoader();
|
|
92 |
assertTrue(thisModule.canRead(cl.getUnnamedModule()));
|
|
93 |
testReadsAllBootModules(thisModule);
|
|
94 |
|
|
95 |
// unnamed modules export all packages
|
|
96 |
assertTrue(thisModule.isExported(""));
|
|
97 |
assertTrue(thisModule.isExported("", thisModule));
|
|
98 |
assertTrue(thisModule.isExported("", baseModule));
|
|
99 |
assertTrue(thisModule.isExported("p"));
|
|
100 |
assertTrue(thisModule.isExported("p", thisModule));
|
|
101 |
assertTrue(thisModule.isExported("p", baseModule));
|
|
102 |
|
|
103 |
// this test is in the unnamed package
|
|
104 |
assertTrue(contains(thisModule.getPackages(), ""));
|
|
105 |
}
|
|
106 |
|
|
107 |
|
|
108 |
@Test
|
|
109 |
public void testUnnamedModules() {
|
|
110 |
Module thisModule = BasicModuleTest.class.getModule();
|
|
111 |
Module baseModule = Object.class.getModule();
|
|
112 |
|
|
113 |
ClassLoader loader1 = ClassLoader.getSystemClassLoader();
|
|
114 |
ClassLoader loader2 = loader1.getParent();
|
|
115 |
|
|
116 |
Module m1 = loader1.getUnnamedModule();
|
|
117 |
Module m2 = loader2.getUnnamedModule();
|
|
118 |
|
|
119 |
assertTrue(m1 != m2);
|
|
120 |
|
|
121 |
assertFalse(m1.isNamed());
|
|
122 |
assertFalse(m2.isNamed());
|
|
123 |
|
|
124 |
assertTrue(m1.getLayer() == null);
|
|
125 |
assertTrue(m2.getLayer() == null);
|
|
126 |
|
|
127 |
assertTrue(m1.toString().startsWith("unnamed module "));
|
|
128 |
assertTrue(m2.toString().startsWith("unnamed module "));
|
|
129 |
|
|
130 |
// unnamed module reads all modules
|
|
131 |
assertTrue(m1.canRead(m2));
|
|
132 |
assertTrue(m2.canRead(m1));
|
|
133 |
|
|
134 |
testReadsAllBootModules(m1);
|
|
135 |
testReadsAllBootModules(m2);
|
|
136 |
|
|
137 |
assertTrue(m1.isExported(""));
|
|
138 |
assertTrue(m1.isExported("", thisModule));
|
|
139 |
assertTrue(m1.isExported("", baseModule));
|
|
140 |
assertTrue(m1.isExported("p"));
|
|
141 |
assertTrue(m1.isExported("p", thisModule));
|
|
142 |
assertTrue(m1.isExported("p", baseModule));
|
|
143 |
}
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
@Test
|
|
148 |
public void testBaseModule() {
|
|
149 |
Module base = Object.class.getModule();
|
|
150 |
Module thisModule = BasicModuleTest.class.getModule();
|
|
151 |
|
|
152 |
// getName
|
|
153 |
assertTrue(base.getName().equals("java.base"));
|
|
154 |
|
|
155 |
// getDescriptor
|
|
156 |
assertTrue(base.getDescriptor().exports().stream()
|
|
157 |
.anyMatch(doesExport("java.lang")));
|
|
158 |
|
|
159 |
// getClassLoader
|
|
160 |
assertTrue(base.getClassLoader() == null);
|
|
161 |
|
|
162 |
// getLayer
|
|
163 |
assertTrue(base.getLayer() == Layer.boot());
|
|
164 |
|
|
165 |
// toString
|
|
166 |
assertEquals(base.toString(), "module java.base");
|
|
167 |
|
|
168 |
// getPackages
|
|
169 |
assertTrue(contains(base.getPackages(), "java.lang"));
|
|
170 |
|
|
171 |
// canRead
|
|
172 |
assertTrue(base.canRead(base));
|
|
173 |
|
|
174 |
// isExported
|
|
175 |
assertTrue(base.isExported("java.lang"));
|
|
176 |
assertTrue(base.isExported("java.lang", thisModule));
|
|
177 |
assertFalse(base.isExported("java.wombat"));
|
|
178 |
assertFalse(base.isExported("java.wombat", thisModule));
|
|
179 |
}
|
|
180 |
|
|
181 |
|
|
182 |
@Test
|
|
183 |
public void testDesktopModule() {
|
|
184 |
Module desktop = java.awt.Component.class.getModule();
|
|
185 |
Module base = Object.class.getModule();
|
|
186 |
Module xml = javax.xml.XMLConstants.class.getModule();
|
|
187 |
Module thisModule = BasicModuleTest.class.getModule();
|
|
188 |
|
|
189 |
// name
|
|
190 |
assertTrue(desktop.getName().equals("java.desktop"));
|
|
191 |
|
|
192 |
// descriptor
|
|
193 |
assertTrue(desktop.getDescriptor().exports().stream()
|
|
194 |
.anyMatch(doesExport("java.awt")));
|
|
195 |
|
|
196 |
// getClassLoader
|
|
197 |
assertTrue(desktop.getClassLoader() == null);
|
|
198 |
|
|
199 |
// getLayer
|
|
200 |
assertTrue(desktop.getLayer() == Layer.boot());
|
|
201 |
|
|
202 |
// toString
|
|
203 |
assertEquals(desktop.toString(), "module java.desktop");
|
|
204 |
|
|
205 |
// getPackages
|
|
206 |
assertTrue(contains(desktop.getPackages(), "java.awt"));
|
|
207 |
assertTrue(contains(desktop.getPackages(), "sun.awt"));
|
|
208 |
|
|
209 |
// canRead
|
|
210 |
assertTrue(desktop.canRead(base));
|
|
211 |
assertTrue(desktop.canRead(xml));
|
|
212 |
|
|
213 |
// isExported
|
|
214 |
assertTrue(desktop.isExported("java.awt"));
|
|
215 |
assertTrue(desktop.isExported("java.awt", thisModule));
|
|
216 |
assertFalse(desktop.isExported("java.wombat"));
|
|
217 |
assertFalse(desktop.isExported("java.wombat", thisModule));
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
@Test(expectedExceptions = { NullPointerException.class })
|
|
222 |
public void testIsExportedNull() {
|
|
223 |
Module thisModule = this.getClass().getModule();
|
|
224 |
thisModule.isExported(null, thisModule);
|
|
225 |
}
|
|
226 |
|
|
227 |
|
|
228 |
@Test(expectedExceptions = { NullPointerException.class })
|
|
229 |
public void testIsExportedToNull() {
|
|
230 |
Module thisModule = this.getClass().getModule();
|
|
231 |
thisModule.isExported("", null);
|
|
232 |
}
|
|
233 |
|
|
234 |
|
|
235 |
}
|