1 /*
2 * Copyright (c) 2015, 2026, 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 * @bug 8142968 8300228
27 * @library /test/lib
28 * @modules java.base/jdk.internal.module
29 * jdk.compiler
30 * jdk.jlink
31 * @build ModuleReaderTest
32 * jdk.test.lib.compiler.CompilerUtils
33 * jdk.test.lib.util.JarUtils
34 * @run junit ModuleReaderTest
35 * @summary Basic tests for java.lang.module.ModuleReader
36 */
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.lang.module.ModuleFinder;
42 import java.lang.module.ModuleReader;
43 import java.lang.module.ModuleReference;
44 import java.net.URI;
45 import java.net.URL;
46 import java.net.URLConnection;
47 import java.nio.ByteBuffer;
48 import java.nio.file.Files;
49 import java.nio.file.Path;
50 import java.nio.file.Paths;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Optional;
54 import java.util.Set;
55 import java.util.spi.ToolProvider;
56 import java.util.stream.Stream;
57
58 import jdk.internal.module.ModulePath;
59 import jdk.test.lib.compiler.CompilerUtils;
60 import jdk.test.lib.util.JarUtils;
61 import org.junit.jupiter.api.BeforeAll;
62 import org.junit.jupiter.api.Test;
63
64 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
65 import static org.junit.jupiter.api.Assertions.assertEquals;
66 import static org.junit.jupiter.api.Assertions.assertFalse;
67 import static org.junit.jupiter.api.Assertions.assertThrows;
68 import static org.junit.jupiter.api.Assertions.assertTrue;
69
70 public class ModuleReaderTest {
71 private static final String TEST_SRC = System.getProperty("test.src");
72
73 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
74 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
75 private static final Path MODS_DIR = Paths.get("mods");
76
77 // the module name of the base module
78 private static final String BASE_MODULE = "java.base";
79
80 // the module name of the test module
81 private static final String TEST_MODULE = "m";
82
83 // resources in the base module
84 private static final String[] BASE_RESOURCES = {
85 "java/lang/Object.class"
86 };
87
88 // (directory) resources that may be in the base module
89 private static final String[] MAYBE_BASE_RESOURCES = {
90 "java",
91 "java/",
92 "java/lang",
93 "java/lang/",
94 };
95
105 "//java/lang/Object.class",
106 "java/lang/Object.class/",
107 "java//lang//Object.class",
108 "./java/lang/Object.class",
109 "java/./lang/Object.class",
110 "java/lang/./Object.class",
111 "../java/lang/Object.class",
112 "java/../lang/Object.class",
113 "java/lang/../Object.class",
114
115 // junk resource names
116 "java\u0000",
117 "C:java",
118 "C:\\java",
119 "java\\lang\\Object.class"
120 };
121
122 // resources in test module (can't use module-info.class as a test
123 // resource as it will be modified by the jmod tool)
124 private static final String[] TEST_RESOURCES = {
125 "p/Main.class"
126 };
127
128 // (directory) resources that may be in the test module
129 private static final String[] MAYBE_TEST_RESOURCES = {
130 "p",
131 "p/"
132 };
133
134 // resource names that should not be found in the test module
135 private static final String[] NOT_TEST_RESOURCES = {
136 "NotFound",
137 "/p",
138 "//p",
139 "/p/Main.class",
140 "//p/Main.class",
141 "p/Main.class/",
142 "p//Main.class",
143 "./p/Main.class",
144 "p/./Main.class",
145 "../p/Main.class",
146 "p/../p/Main.class",
147
148 // junk resource names
149 "p\u0000",
150 "C:p",
151 "C:\\p",
152 "p\\Main.class"
153 };
154
155 @BeforeAll
156 public static void compileTestModule() throws Exception {
157 // javac -d mods/$TESTMODULE src/$TESTMODULE/**
158 boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
159 MODS_DIR.resolve(TEST_MODULE));
160 assertTrue(compiled, "test module did not compile");
161 }
162
163 /**
164 * Test ModuleReader with module in runtime image.
165 */
166 @Test
167 public void testImage() throws IOException {
168 ModuleFinder finder = ModuleFinder.ofSystem();
169 ModuleReference mref = finder.find(BASE_MODULE).get();
170 ModuleReader reader = mref.open();
171
172 try (reader) {
173
174 for (String name : BASE_RESOURCES) {
175 byte[] expectedBytes;
176 Module baseModule = Object.class.getModule();
177 try (InputStream in = baseModule.getResourceAsStream(name)) {
178 expectedBytes = in.readAllBytes();
179 }
180
205 assertThrows(NullPointerException.class, () -> reader.find(null));
206 assertThrows(NullPointerException.class, () -> reader.open(null));
207 assertThrows(NullPointerException.class, () -> reader.read(null));
208 assertThrows(NullPointerException.class, () -> reader.release(null));
209 }
210
211 // test closed ModuleReader
212 assertThrows(IOException.class, () -> reader.open(BASE_RESOURCES[0]));
213 assertThrows(IOException.class, () -> reader.read(BASE_RESOURCES[0]));
214 assertThrows(IOException.class, reader::list);
215 }
216
217 /**
218 * Test ModuleReader with exploded module.
219 */
220 @Test
221 public void testExplodedModule() throws IOException {
222 test(MODS_DIR);
223 }
224
225 /**
226 * Test ModuleReader with module in modular JAR.
227 */
228 @Test
229 public void testModularJar() throws IOException {
230 Path dir = Files.createTempDirectory(USER_DIR, "mlib");
231
232 // jar cf mlib/${TESTMODULE}.jar -C mods .
233 JarUtils.createJarFile(dir.resolve("m.jar"),
234 MODS_DIR.resolve(TEST_MODULE));
235
236 test(dir);
237 }
238
239 /**
240 * Test ModuleReader with module in a JMOD file.
241 */
242 @Test
243 public void testJMod() throws IOException {
244 Path dir = Files.createTempDirectory(USER_DIR, "mlib");
245
246 // jmod create --class-path mods/${TESTMODULE} mlib/${TESTMODULE}.jmod
247 String cp = MODS_DIR.resolve(TEST_MODULE).toString();
248 String jmod = dir.resolve("m.jmod").toString();
249 String[] args = { "create", "--class-path", cp, jmod };
250 ToolProvider jmodTool = ToolProvider.findFirst("jmod")
251 .orElseThrow(() ->
252 new RuntimeException("jmod tool not found")
253 );
254 assertEquals(0, jmodTool.run(System.out, System.out, args), "jmod tool failed");
255
256 test(dir);
257 }
258
259 /**
260 * The test module is found on the given module path. Open a ModuleReader
261 * to the test module and test the reader.
262 */
263 void test(Path mp) throws IOException {
264 ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp);
265 ModuleReference mref = finder.find(TEST_MODULE).get();
266 ModuleReader reader = mref.open();
267
268 try (reader) {
|
1 /*
2 * Copyright (c) 2015, 2023, 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 * @bug 8142968 8300228
27 * @library /test/lib
28 * @modules java.base/jdk.internal.module
29 * jdk.compiler
30 * jdk.jlink
31 * @build ModuleReaderTest
32 * jdk.test.lib.compiler.CompilerUtils
33 * jdk.test.lib.util.JarUtils
34 * @run junit ModuleReaderTest
35 * @summary Basic tests for java.lang.module.ModuleReader
36 */
37
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.lang.module.ModuleFinder;
42 import java.lang.module.ModuleReader;
43 import java.lang.module.ModuleReference;
44 import java.net.URI;
45 import java.net.URL;
46 import java.net.URLConnection;
47 import java.nio.ByteBuffer;
48 import java.nio.charset.StandardCharsets;
49 import java.nio.file.Files;
50 import java.nio.file.Path;
51 import java.util.HashSet;
52 import java.util.List;
53 import java.util.Optional;
54 import java.util.Set;
55 import java.util.spi.ToolProvider;
56 import java.util.stream.Stream;
57
58 import jdk.internal.module.ModulePath;
59 import jdk.test.lib.compiler.CompilerUtils;
60 import jdk.test.lib.util.JarUtils;
61 import org.junit.jupiter.api.BeforeAll;
62 import org.junit.jupiter.api.Test;
63
64 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
65 import static org.junit.jupiter.api.Assertions.assertEquals;
66 import static org.junit.jupiter.api.Assertions.assertFalse;
67 import static org.junit.jupiter.api.Assertions.assertThrows;
68 import static org.junit.jupiter.api.Assertions.assertTrue;
69
70 public class ModuleReaderTest {
71 private static final Path MODS_DIR = Path.of("mods");
72
73 // the module name of the base module
74 private static final String BASE_MODULE = "java.base";
75
76 // the module name of the test module
77 private static final String TEST_MODULE = "m";
78
79 // resources in the base module
80 private static final String[] BASE_RESOURCES = {
81 "java/lang/Object.class"
82 };
83
84 // (directory) resources that may be in the base module
85 private static final String[] MAYBE_BASE_RESOURCES = {
86 "java",
87 "java/",
88 "java/lang",
89 "java/lang/",
90 };
91
101 "//java/lang/Object.class",
102 "java/lang/Object.class/",
103 "java//lang//Object.class",
104 "./java/lang/Object.class",
105 "java/./lang/Object.class",
106 "java/lang/./Object.class",
107 "../java/lang/Object.class",
108 "java/../lang/Object.class",
109 "java/lang/../Object.class",
110
111 // junk resource names
112 "java\u0000",
113 "C:java",
114 "C:\\java",
115 "java\\lang\\Object.class"
116 };
117
118 // resources in test module (can't use module-info.class as a test
119 // resource as it will be modified by the jmod tool)
120 private static final String[] TEST_RESOURCES = {
121 "p/Main.class",
122 "p/test.txt"
123 };
124
125 // (directory) resources that may be in the test module
126 private static final String[] MAYBE_TEST_RESOURCES = {
127 "p",
128 "p/"
129 };
130
131 // resource names that should not be found in the test module
132 private static final String[] NOT_TEST_RESOURCES = {
133 "NotFound",
134 "/p",
135 "//p",
136 "/p/Main.class",
137 "//p/Main.class",
138 "p/Main.class/",
139 "p//Main.class",
140 "./p/Main.class",
141 "p/./Main.class",
142 "../p/Main.class",
143 "p/../p/Main.class",
144
145 // junk resource names
146 "p\u0000",
147 "C:p",
148 "C:\\p",
149 "p\\Main.class"
150 };
151
152 @BeforeAll
153 public static void compileTestModules() throws Exception {
154 // create a simple module-info.java
155 Path srcDir = Path.of("src", TEST_MODULE);
156 Files.createDirectories(srcDir);
157 Files.writeString(srcDir.resolve("module-info.java"), "module " + TEST_MODULE + " {}");
158
159 // write and compile test class "p.Main"
160 Path pkgPath = Path.of("p");
161 Path javaSrc = srcDir.resolve(pkgPath).resolve("Main.java");
162 Files.createDirectories(javaSrc.getParent());
163 Files.writeString(javaSrc,
164 """
165 package p;
166 public class Main {
167 public static void main(String[] args) { }
168 }
169 """);
170
171 // javac -d <outDir> <srcDir>/**
172 Path outDir = MODS_DIR.resolve(TEST_MODULE);
173 boolean compiled = CompilerUtils.compile(srcDir, outDir);
174 assertTrue(compiled, "test module did not compile");
175
176 // add two versions of a resource to allow for preview mode testing
177 Files.writeString(outDir.resolve(pkgPath).resolve("test.txt"), "Original");
178 Path previewDir = outDir.resolve("META-INF", "preview").resolve(pkgPath);
179 Files.createDirectories(previewDir);
180 Files.writeString(previewDir.resolve("test.txt"), "Preview Version");
181 }
182
183 /**
184 * Test ModuleReader with module in runtime image.
185 */
186 @Test
187 public void testImage() throws IOException {
188 ModuleFinder finder = ModuleFinder.ofSystem();
189 ModuleReference mref = finder.find(BASE_MODULE).get();
190 ModuleReader reader = mref.open();
191
192 try (reader) {
193
194 for (String name : BASE_RESOURCES) {
195 byte[] expectedBytes;
196 Module baseModule = Object.class.getModule();
197 try (InputStream in = baseModule.getResourceAsStream(name)) {
198 expectedBytes = in.readAllBytes();
199 }
200
225 assertThrows(NullPointerException.class, () -> reader.find(null));
226 assertThrows(NullPointerException.class, () -> reader.open(null));
227 assertThrows(NullPointerException.class, () -> reader.read(null));
228 assertThrows(NullPointerException.class, () -> reader.release(null));
229 }
230
231 // test closed ModuleReader
232 assertThrows(IOException.class, () -> reader.open(BASE_RESOURCES[0]));
233 assertThrows(IOException.class, () -> reader.read(BASE_RESOURCES[0]));
234 assertThrows(IOException.class, reader::list);
235 }
236
237 /**
238 * Test ModuleReader with exploded module.
239 */
240 @Test
241 public void testExplodedModule() throws IOException {
242 test(MODS_DIR);
243 }
244
245 /**
246 * Test the ModuleReader used for the JDK exploded image. A module finder for
247 * a JDK exploded image can be created to locate resources in META-INF/preview
248 * when running with preview feature enabled. This test exercises the ModuleReader
249 * obtained when the module finder is created with preview features enabled and
250 * disabled.
251 */
252 @Test
253 public void testExplodedImage() throws IOException {
254 // preview features disabled
255 ModuleFinder finder = ModulePath.ofExplodedImage(MODS_DIR, null, false);
256 try (ModuleReader reader = finder.find(TEST_MODULE).get().open()) {
257 testReader(reader, "p/test.txt", "Original");
258 testReader(reader, "META-INF/preview/p/test.txt", "Preview Version");
259 }
260
261 // preview features enabled
262 ModuleFinder previewFinder = ModulePath.ofExplodedImage(MODS_DIR, null, true);
263 try (ModuleReader reader = previewFinder.find(TEST_MODULE).get().open()) {
264 testReader(reader, "p/test.txt", "Preview Version");
265 assertFalse(reader.find("META-INF/preview/p/test.txt").isPresent(), "unexpected preview resource");
266 }
267 }
268
269 /**
270 * Test that a ModuleReader locates, opens and reads a resource.
271 */
272 private void testReader(ModuleReader reader, String name, String expectedContent) throws IOException {
273 // check resource is found and that the URI locates the resource
274 Optional<URI> ouri = reader.find(name);
275 assertTrue(ouri.isPresent(), "resource not found: " + name);
276 URI uri = ouri.get();
277 assertTrue(uri.getPath().endsWith(name), "unexpected URI path component: " + uri);
278
279 // read resource bytes with input stream
280 Optional<InputStream> oin = reader.open(name);
281 assertTrue(oin.isPresent(), "resource cannot be opened: " + name);
282 byte[] bytes;
283 try (InputStream in = oin.get()) {
284 bytes = in.readAllBytes();
285 }
286
287 // read resource bytes into byte buffer
288 Optional<ByteBuffer> obb = reader.read(name);
289 assertTrue(obb.isPresent(), "resource cannot be read: " + name);
290 ByteBuffer bb = obb.get();
291 try {
292 assertEquals(ByteBuffer.wrap(bytes), bb, "resource bytes differ: " + name);
293 } finally {
294 reader.release(bb);
295 }
296
297 // test that resource has the expected contents
298 byte[] expectedBytes = expectedContent.getBytes(StandardCharsets.UTF_8);
299 assertArrayEquals(expectedBytes, bytes, "unexpected content");
300 }
301
302 /**
303 * Test ModuleReader with module in modular JAR.
304 */
305 @Test
306 public void testModularJar() throws IOException {
307 Path dir = Files.createTempDirectory(Path.of("."), "mlib");
308
309 // jar cf mlib/${TESTMODULE}.jar -C mods .
310 JarUtils.createJarFile(dir.resolve(TEST_MODULE + ".jar"),
311 MODS_DIR.resolve(TEST_MODULE));
312
313 test(dir);
314 }
315
316 /**
317 * Test ModuleReader with module in a JMOD file.
318 */
319 @Test
320 public void testJMod() throws IOException {
321 Path dir = Files.createTempDirectory(Path.of("."), "mlib");
322
323 // jmod create --class-path mods/${TESTMODULE} mlib/${TESTMODULE}.jmod
324 String cp = MODS_DIR.resolve(TEST_MODULE).toString();
325 String jmod = dir.resolve(TEST_MODULE + ".jmod").toString();
326 String[] args = { "create", "--class-path", cp, jmod };
327 ToolProvider jmodTool = ToolProvider.findFirst("jmod")
328 .orElseThrow(() ->
329 new RuntimeException("jmod tool not found")
330 );
331 assertEquals(0, jmodTool.run(System.out, System.out, args), "jmod tool failed");
332
333 test(dir);
334 }
335
336 /**
337 * The test module is found on the given module path. Open a ModuleReader
338 * to the test module and test the reader.
339 */
340 void test(Path mp) throws IOException {
341 ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp);
342 ModuleReference mref = finder.find(TEST_MODULE).get();
343 ModuleReader reader = mref.open();
344
345 try (reader) {
|