1 /* 2 * Copyright (c) 2020, 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 /** 26 * @test 27 * @bug 8244778 28 * @requires vm.cds 29 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds 30 * @run driver NewModuleFinderTest 31 * @summary Make sure the archived module graph can co-exist with modules that are 32 * dynamically defined at runtime using the ModuleFinder API. 33 */ 34 35 import java.lang.module.Configuration; 36 import java.lang.module.ModuleFinder; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.Set; 40 import jdk.test.lib.cds.CDSTestUtils; 41 import jdk.test.lib.process.ProcessTools; 42 import jdk.test.lib.process.OutputAnalyzer; 43 44 public class NewModuleFinderTest { 45 46 private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); 47 private static final String TEST_SRC = System.getProperty("test.src"); 48 private static final Path SRC_DIR = Paths.get(TEST_SRC, "modulepath/src"); 49 private static final Path MODS_DIR = Paths.get("mods"); 50 51 // the module name of the test module 52 private static final String TEST_MODULE = "com.simple"; 53 54 // the module main class 55 private static final String MAIN_CLASS = "com.simple.Main"; 56 57 private static final Set<String> modules = Set.of(TEST_MODULE); 58 59 public static void buildTestModule() throws Exception { 60 // javac -d mods/$TESTMODULE --module-path MOD_DIR modulepath/src/$TESTMODULE/** 61 JarBuilder.compileModule(SRC_DIR.resolve(TEST_MODULE), 62 MODS_DIR.resolve(TEST_MODULE), 63 MODS_DIR.toString()); 64 } 65 66 public static void main(String... args) throws Exception { 67 // compile the modules and create the modular jar files 68 buildTestModule(); 69 70 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 71 "-Xlog:cds", 72 "-Xlog:module=debug", 73 "NewModuleFinderTest$Helper"); 74 OutputAnalyzer out = CDSTestUtils.executeAndLog(pb, "exec"); 75 out.shouldHaveExitValue(0); 76 out.shouldContain("define_module(): creation of module: com.simple,"); 77 } 78 79 static class Helper { 80 public static void main(String... args) { 81 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 82 Configuration parent = ModuleLayer.boot().configuration(); 83 Configuration cf = parent.resolveAndBind(ModuleFinder.of(), 84 finder, 85 modules); 86 87 ClassLoader scl = ClassLoader.getSystemClassLoader(); 88 ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl); 89 90 Module m1 = layer.findModule(TEST_MODULE).get(); 91 System.out.println("Module = " + m1); 92 if (m1 != null) { 93 System.out.println("Success"); 94 } else { 95 throw new RuntimeException("Module should not be null"); 96 } 97 } 98 } 99 }