< prev index next > test/jdk/java/lang/module/ModuleReader/ModuleReaderTest.java
Print this page
/*
! * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
/*
! * Copyright (c) 2015, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
* jdk.compiler
* jdk.jlink
* @build ModuleReaderTest
* jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.util.JarUtils
! * @run testng ModuleReaderTest
* @summary Basic tests for java.lang.module.ModuleReader
*/
import java.io.File;
import java.io.IOException;
* jdk.compiler
* jdk.jlink
* @build ModuleReaderTest
* jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.util.JarUtils
! * @run junit ModuleReaderTest
* @summary Basic tests for java.lang.module.ModuleReader
*/
import java.io.File;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
- import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
- import java.util.stream.Collectors;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;
import jdk.internal.module.ModulePath;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.JarUtils;
! import org.testng.annotations.BeforeTest;
! import org.testng.annotations.Test;
! import static org.testng.Assert.*;
public class ModuleReaderTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.spi.ToolProvider;
import java.util.stream.Stream;
import jdk.internal.module.ModulePath;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.JarUtils;
+ import org.junit.jupiter.api.BeforeAll;
+ import org.junit.jupiter.api.Test;
! import static org.junit.jupiter.api.Assertions.assertArrayEquals;
! import static org.junit.jupiter.api.Assertions.assertEquals;
! import static org.junit.jupiter.api.Assertions.assertFalse;
+ import static org.junit.jupiter.api.Assertions.assertThrows;
+ import static org.junit.jupiter.api.Assertions.assertTrue;
public class ModuleReaderTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
"C:p",
"C:\\p",
"p\\Main.class"
};
! @BeforeTest
! public void compileTestModule() throws Exception {
// javac -d mods/$TESTMODULE src/$TESTMODULE/**
boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
MODS_DIR.resolve(TEST_MODULE));
assertTrue(compiled, "test module did not compile");
}
"C:p",
"C:\\p",
"p\\Main.class"
};
! @BeforeAll
! public static void compileTestModule() throws Exception {
// javac -d mods/$TESTMODULE src/$TESTMODULE/**
boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
MODS_DIR.resolve(TEST_MODULE));
assertTrue(compiled, "test module did not compile");
}
// test resources that may be in the base module
for (String name : MAYBE_BASE_RESOURCES) {
Optional<URI> ouri = reader.find(name);
ouri.ifPresent(uri -> {
if (name.endsWith("/"))
! assertTrue(uri.toString().endsWith("/"));
});
}
// test "not found" in java.base module
for (String name : NOT_BASE_RESOURCES) {
! assertFalse(reader.find(name).isPresent());
! assertFalse(reader.open(name).isPresent());
! assertFalse(reader.read(name).isPresent());
}
// test nulls
! try {
! reader.find(null);
! assertTrue(false);
! } catch (NullPointerException expected) { }
-
- try {
- reader.open(null);
- assertTrue(false);
- } catch (NullPointerException expected) { }
-
- try {
- reader.read(null);
- assertTrue(false);
- } catch (NullPointerException expected) { }
-
- try {
- reader.release(null);
- assertTrue(false);
- } catch (NullPointerException expected) { }
-
}
// test closed ModuleReader
! try {
! reader.open(BASE_RESOURCES[0]);
! assertTrue(false);
- } catch (IOException expected) { }
-
-
- try {
- reader.read(BASE_RESOURCES[0]);
- assertTrue(false);
- } catch (IOException expected) { }
}
/**
* Test ModuleReader with exploded module.
*/
// test resources that may be in the base module
for (String name : MAYBE_BASE_RESOURCES) {
Optional<URI> ouri = reader.find(name);
ouri.ifPresent(uri -> {
if (name.endsWith("/"))
! assertTrue(uri.toString().endsWith("/"),
+ "mismatched directory URI for '" + name + "': " + uri);
});
}
// test "not found" in java.base module
for (String name : NOT_BASE_RESOURCES) {
! assertFalse(reader.find(name).isPresent(), "Unexpected resource found: " + name);
! assertFalse(reader.open(name).isPresent(), "Unexpected resource opened: " + name);
! assertFalse(reader.read(name).isPresent(), "Unexpected resource read: " + name);
}
// test nulls
! assertThrows(NullPointerException.class, () -> reader.find(null));
! assertThrows(NullPointerException.class, () -> reader.open(null));
! assertThrows(NullPointerException.class, () -> reader.read(null));
! assertThrows(NullPointerException.class, () -> reader.release(null));
}
// test closed ModuleReader
! assertThrows(IOException.class, () -> reader.open(BASE_RESOURCES[0]));
! assertThrows(IOException.class, () -> reader.read(BASE_RESOURCES[0]));
! assertThrows(IOException.class, reader::list);
}
/**
* Test ModuleReader with exploded module.
*/
// jmod create --class-path mods/${TESTMODULE} mlib/${TESTMODULE}.jmod
String cp = MODS_DIR.resolve(TEST_MODULE).toString();
String jmod = dir.resolve("m.jmod").toString();
String[] args = { "create", "--class-path", cp, jmod };
ToolProvider jmodTool = ToolProvider.findFirst("jmod")
! .orElseThrow(() ->
! new RuntimeException("jmod tool not found")
! );
! assertEquals(jmodTool.run(System.out, System.out, args), 0);
test(dir);
}
/**
// jmod create --class-path mods/${TESTMODULE} mlib/${TESTMODULE}.jmod
String cp = MODS_DIR.resolve(TEST_MODULE).toString();
String jmod = dir.resolve("m.jmod").toString();
String[] args = { "create", "--class-path", cp, jmod };
ToolProvider jmodTool = ToolProvider.findFirst("jmod")
! .orElseThrow(() ->
! new RuntimeException("jmod tool not found")
! );
! assertEquals(0, jmodTool.run(System.out, System.out, args), "jmod tool failed");
test(dir);
}
/**
for (String name : MAYBE_TEST_RESOURCES) {
System.out.println("resource: " + name);
Optional<URI> ouri = reader.find(name);
ouri.ifPresent(uri -> {
if (name.endsWith("/"))
! assertTrue(uri.toString().endsWith("/"));
});
}
// test "not found" in test module
for (String name : NOT_TEST_RESOURCES) {
System.out.println("resource: " + name);
! assertFalse(reader.find(name).isPresent());
! assertFalse(reader.open(name).isPresent());
! assertFalse(reader.read(name).isPresent());
}
// test nulls
! try {
! reader.find(null);
! assertTrue(false);
! } catch (NullPointerException expected) { }
-
- try {
- reader.open(null);
- assertTrue(false);
- } catch (NullPointerException expected) { }
-
- try {
- reader.read(null);
- assertTrue(false);
- } catch (NullPointerException expected) { }
-
- try {
- reader.release(null);
- throw new RuntimeException();
- } catch (NullPointerException expected) { }
-
}
// test closed ModuleReader
! try {
! reader.open(TEST_RESOURCES[0]);
! assertTrue(false);
- } catch (IOException expected) { }
-
-
- try {
- reader.read(TEST_RESOURCES[0]);
- assertTrue(false);
- } catch (IOException expected) { }
-
- try {
- reader.list();
- assertTrue(false);
- } catch (IOException expected) { }
}
/**
* Test ModuleReader#find
*/
void testFind(ModuleReader reader, String name, byte[] expectedBytes)
throws IOException
{
Optional<URI> ouri = reader.find(name);
! assertTrue(ouri.isPresent());
URL url = ouri.get().toURL();
if (!url.getProtocol().equalsIgnoreCase("jmod")) {
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
try (InputStream in = uc.getInputStream()) {
byte[] bytes = in.readAllBytes();
! assertTrue(Arrays.equals(bytes, expectedBytes));
}
}
}
/**
for (String name : MAYBE_TEST_RESOURCES) {
System.out.println("resource: " + name);
Optional<URI> ouri = reader.find(name);
ouri.ifPresent(uri -> {
if (name.endsWith("/"))
! assertTrue(uri.toString().endsWith("/"),
+ "mismatched directory URI for '" + name + "': " + uri);
});
}
// test "not found" in test module
for (String name : NOT_TEST_RESOURCES) {
System.out.println("resource: " + name);
! assertFalse(reader.find(name).isPresent(), "Unexpected resource found: " + name);
! assertFalse(reader.open(name).isPresent(), "Unexpected resource open: " + name);
! assertFalse(reader.read(name).isPresent(), "Unexpected resource read: " + name);
}
// test nulls
! assertThrows(NullPointerException.class, () -> reader.find(null));
! assertThrows(NullPointerException.class, () -> reader.open(null));
! assertThrows(NullPointerException.class, () -> reader.read(null));
! assertThrows(NullPointerException.class, () -> reader.release(null));
}
// test closed ModuleReader
! assertThrows(IOException.class, () -> reader.open(BASE_RESOURCES[0]));
! assertThrows(IOException.class, () -> reader.read(BASE_RESOURCES[0]));
! assertThrows(IOException.class, reader::list);
}
/**
* Test ModuleReader#find
*/
void testFind(ModuleReader reader, String name, byte[] expectedBytes)
throws IOException
{
Optional<URI> ouri = reader.find(name);
! assertTrue(ouri.isPresent(), "missing URI for: " + name);
URL url = ouri.get().toURL();
if (!url.getProtocol().equalsIgnoreCase("jmod")) {
URLConnection uc = url.openConnection();
uc.setUseCaches(false);
try (InputStream in = uc.getInputStream()) {
byte[] bytes = in.readAllBytes();
! assertArrayEquals(expectedBytes, bytes, "resource bytes differ for: " + name);
}
}
}
/**
*/
void testOpen(ModuleReader reader, String name, byte[] expectedBytes)
throws IOException
{
Optional<InputStream> oin = reader.open(name);
! assertTrue(oin.isPresent());
!
- InputStream in = oin.get();
- try (in) {
byte[] bytes = in.readAllBytes();
! assertTrue(Arrays.equals(bytes, expectedBytes));
}
}
/**
* Test ModuleReader#read
*/
void testOpen(ModuleReader reader, String name, byte[] expectedBytes)
throws IOException
{
Optional<InputStream> oin = reader.open(name);
! assertTrue(oin.isPresent(), "missing input stream for: " + name);
! try (InputStream in = oin.get()) {
byte[] bytes = in.readAllBytes();
! assertArrayEquals(expectedBytes, bytes, "resource bytes differ for: " + name);
}
}
/**
* Test ModuleReader#read
assertTrue(obb.isPresent());
ByteBuffer bb = obb.get();
try {
int rem = bb.remaining();
! assertTrue(rem == expectedBytes.length);
byte[] bytes = new byte[rem];
bb.get(bytes);
! assertTrue(Arrays.equals(bytes, expectedBytes));
} finally {
reader.release(bb);
}
}
assertTrue(obb.isPresent());
ByteBuffer bb = obb.get();
try {
int rem = bb.remaining();
! assertEquals(expectedBytes.length, rem, "resource lengths differ: " + name);
byte[] bytes = new byte[rem];
bb.get(bytes);
! assertArrayEquals(expectedBytes, bytes, "resource bytes differ: " + name);
} finally {
reader.release(bb);
}
}
final List<String> list;
try (Stream<String> stream = reader.list()) {
list = stream.toList();
}
Set<String> names = new HashSet<>(list);
! assertTrue(names.size() == list.size()); // no duplicates
! assertTrue(names.contains("module-info.class"));
! assertTrue(names.contains(name));
// all resources should be locatable via find
for (String e : names) {
! assertTrue(reader.find(e).isPresent());
}
}
}
final List<String> list;
try (Stream<String> stream = reader.list()) {
list = stream.toList();
}
Set<String> names = new HashSet<>(list);
! assertEquals(names.size(), list.size(), "resource list contains duplicates: " + list);
! assertTrue(names.contains("module-info.class"), "resource list did not contain 'module-info.class': " + list);
! assertTrue(names.contains(name), "resource list did not contain '" + name + "'" + list);
// all resources should be locatable via find
for (String e : names) {
! assertTrue(reader.find(e).isPresent(), "resource not found: " + name);
}
}
}
< prev index next >