Path: blob/master/test/jdk/java/lang/module/ModuleReader/ModuleReaderTest.java
41152 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib26* @modules java.base/jdk.internal.module27* jdk.compiler28* jdk.jlink29* @build ModuleReaderTest30* jdk.test.lib.compiler.CompilerUtils31* jdk.test.lib.util.JarUtils32* @run testng ModuleReaderTest33* @summary Basic tests for java.lang.module.ModuleReader34*/3536import java.io.File;37import java.io.IOException;38import java.io.InputStream;39import java.lang.module.ModuleFinder;40import java.lang.module.ModuleReader;41import java.lang.module.ModuleReference;42import java.net.URI;43import java.net.URL;44import java.net.URLConnection;45import java.nio.ByteBuffer;46import java.nio.file.Files;47import java.nio.file.Path;48import java.nio.file.Paths;49import java.util.Arrays;50import java.util.HashSet;51import java.util.List;52import java.util.Optional;53import java.util.Set;54import java.util.stream.Collectors;55import java.util.spi.ToolProvider;5657import jdk.internal.module.ModulePath;58import jdk.test.lib.compiler.CompilerUtils;59import jdk.test.lib.util.JarUtils;6061import org.testng.annotations.BeforeTest;62import org.testng.annotations.Test;63import static org.testng.Assert.*;6465@Test66public class ModuleReaderTest {6768private static final String TEST_SRC = System.getProperty("test.src");6970private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));71private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");72private static final Path MODS_DIR = Paths.get("mods");7374// the module name of the base module75private static final String BASE_MODULE = "java.base";7677// the module name of the test module78private static final String TEST_MODULE = "m";7980// resources in the base module81private static final String[] BASE_RESOURCES = {82"java/lang/Object.class"83};8485// (directory) resources that may be in the base module86private static final String[] MAYBE_BASE_RESOURCES = {87"java",88"java/",89"java/lang",90"java/lang/",91};9293// resource names that should not be found in the base module94private static final String[] NOT_BASE_RESOURCES = {95"NotFound",96"/java",97"//java",98"/java/lang",99"//java/lang",100"java//lang",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"java/lang/../Object.class",111};112113// resources in test module (can't use module-info.class as a test114// resource as it will be modified by the jmod tool)115private static final String[] TEST_RESOURCES = {116"p/Main.class"117};118119// (directory) resources that may be in the test module120private static final String[] MAYBE_TEST_RESOURCES = {121"p",122"p/"123};124125// resource names that should not be found in the test module126private static final String[] NOT_TEST_RESOURCES = {127"NotFound",128"/p",129"//p",130"/p/Main.class",131"//p/Main.class",132"p/Main.class/",133"p//Main.class",134"./p/Main.class",135"p/./Main.class",136"../p/Main.class",137"p/../p/Main.class"138};139140141@BeforeTest142public void compileTestModule() throws Exception {143144// javac -d mods/$TESTMODULE src/$TESTMODULE/**145boolean compiled146= CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),147MODS_DIR.resolve(TEST_MODULE));148assertTrue(compiled, "test module did not compile");149}150151152/**153* Test ModuleReader to module in runtime image154*/155public void testImage() throws IOException {156157ModuleFinder finder = ModuleFinder.ofSystem();158ModuleReference mref = finder.find(BASE_MODULE).get();159ModuleReader reader = mref.open();160161try (reader) {162163for (String name : BASE_RESOURCES) {164byte[] expectedBytes;165Module baseModule = Object.class.getModule();166try (InputStream in = baseModule.getResourceAsStream(name)) {167expectedBytes = in.readAllBytes();168}169170testFind(reader, name, expectedBytes);171testOpen(reader, name, expectedBytes);172testRead(reader, name, expectedBytes);173testList(reader, name);174}175176// test resources that may be in the base module177for (String name : MAYBE_BASE_RESOURCES) {178Optional<URI> ouri = reader.find(name);179ouri.ifPresent(uri -> {180if (name.endsWith("/"))181assertTrue(uri.toString().endsWith("/"));182});183}184185// test "not found" in java.base module186for (String name : NOT_BASE_RESOURCES) {187assertFalse(reader.find(name).isPresent());188assertFalse(reader.open(name).isPresent());189assertFalse(reader.read(name).isPresent());190}191192// test nulls193try {194reader.find(null);195assertTrue(false);196} catch (NullPointerException expected) { }197198try {199reader.open(null);200assertTrue(false);201} catch (NullPointerException expected) { }202203try {204reader.read(null);205assertTrue(false);206} catch (NullPointerException expected) { }207208try {209reader.release(null);210assertTrue(false);211} catch (NullPointerException expected) { }212213}214215// test closed ModuleReader216try {217reader.open(BASE_RESOURCES[0]);218assertTrue(false);219} catch (IOException expected) { }220221222try {223reader.read(BASE_RESOURCES[0]);224assertTrue(false);225} catch (IOException expected) { }226}227228229/**230* Test ModuleReader to exploded module231*/232public void testExplodedModule() throws IOException {233test(MODS_DIR);234}235236237/**238* Test ModuleReader to modular JAR239*/240public void testModularJar() throws IOException {241Path dir = Files.createTempDirectory(USER_DIR, "mlib");242243// jar cf mlib/${TESTMODULE}.jar -C mods .244JarUtils.createJarFile(dir.resolve("m.jar"),245MODS_DIR.resolve(TEST_MODULE));246247test(dir);248}249250251/**252* Test ModuleReader to JMOD253*/254public void testJMod() throws IOException {255Path dir = Files.createTempDirectory(USER_DIR, "mlib");256257// jmod create --class-path mods/${TESTMODULE} mlib/${TESTMODULE}.jmod258String cp = MODS_DIR.resolve(TEST_MODULE).toString();259String jmod = dir.resolve("m.jmod").toString();260String[] args = { "create", "--class-path", cp, jmod };261ToolProvider jmodTool = ToolProvider.findFirst("jmod")262.orElseThrow(() ->263new RuntimeException("jmod tool not found")264);265assertEquals(jmodTool.run(System.out, System.out, args), 0);266267test(dir);268}269270271/**272* The test module is found on the given module path. Open a ModuleReader273* to the test module and test the reader.274*/275void test(Path mp) throws IOException {276277ModuleFinder finder = ModulePath.of(Runtime.version(), true, mp);278ModuleReference mref = finder.find(TEST_MODULE).get();279ModuleReader reader = mref.open();280281try (reader) {282283// test resources in test module284for (String name : TEST_RESOURCES) {285byte[] expectedBytes286= Files.readAllBytes(MODS_DIR287.resolve(TEST_MODULE)288.resolve(name.replace('/', File.separatorChar)));289290testFind(reader, name, expectedBytes);291testOpen(reader, name, expectedBytes);292testRead(reader, name, expectedBytes);293testList(reader, name);294}295296// test resources that may be in the test module297for (String name : MAYBE_TEST_RESOURCES) {298System.out.println(name);299Optional<URI> ouri = reader.find(name);300ouri.ifPresent(uri -> {301if (name.endsWith("/"))302assertTrue(uri.toString().endsWith("/"));303});304}305306// test "not found" in test module307for (String name : NOT_TEST_RESOURCES) {308assertFalse(reader.find(name).isPresent());309assertFalse(reader.open(name).isPresent());310assertFalse(reader.read(name).isPresent());311}312313// test nulls314try {315reader.find(null);316assertTrue(false);317} catch (NullPointerException expected) { }318319try {320reader.open(null);321assertTrue(false);322} catch (NullPointerException expected) { }323324try {325reader.read(null);326assertTrue(false);327} catch (NullPointerException expected) { }328329try {330reader.release(null);331throw new RuntimeException();332} catch (NullPointerException expected) { }333334}335336// test closed ModuleReader337try {338reader.open(TEST_RESOURCES[0]);339assertTrue(false);340} catch (IOException expected) { }341342343try {344reader.read(TEST_RESOURCES[0]);345assertTrue(false);346} catch (IOException expected) { }347348try {349reader.list();350assertTrue(false);351} catch (IOException expected) { }352}353354/**355* Test ModuleReader#find356*/357void testFind(ModuleReader reader, String name, byte[] expectedBytes)358throws IOException359{360Optional<URI> ouri = reader.find(name);361assertTrue(ouri.isPresent());362363URL url = ouri.get().toURL();364if (!url.getProtocol().equalsIgnoreCase("jmod")) {365URLConnection uc = url.openConnection();366uc.setUseCaches(false);367try (InputStream in = uc.getInputStream()) {368byte[] bytes = in.readAllBytes();369assertTrue(Arrays.equals(bytes, expectedBytes));370}371}372}373374/**375* Test ModuleReader#open376*/377void testOpen(ModuleReader reader, String name, byte[] expectedBytes)378throws IOException379{380Optional<InputStream> oin = reader.open(name);381assertTrue(oin.isPresent());382383InputStream in = oin.get();384try (in) {385byte[] bytes = in.readAllBytes();386assertTrue(Arrays.equals(bytes, expectedBytes));387}388}389390/**391* Test ModuleReader#read392*/393void testRead(ModuleReader reader, String name, byte[] expectedBytes)394throws IOException395{396Optional<ByteBuffer> obb = reader.read(name);397assertTrue(obb.isPresent());398399ByteBuffer bb = obb.get();400try {401int rem = bb.remaining();402assertTrue(rem == expectedBytes.length);403byte[] bytes = new byte[rem];404bb.get(bytes);405assertTrue(Arrays.equals(bytes, expectedBytes));406} finally {407reader.release(bb);408}409}410411/**412* Test ModuleReader#list413*/414void testList(ModuleReader reader, String name) throws IOException {415List<String> list = reader.list().collect(Collectors.toList());416Set<String> names = new HashSet<>(list);417assertTrue(names.size() == list.size()); // no duplicates418419assertTrue(names.contains("module-info.class"));420assertTrue(names.contains(name));421422// all resources should be locatable via find423for (String e : names) {424assertTrue(reader.find(e).isPresent());425}426}427428}429430431