1 /* 2 * Copyright (c) 2015, 2018, 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 * @library /test/lib 27 * @modules jdk.compiler 28 * java.scripting 29 * jdk.zipfs 30 * @build RunWithAutomaticModules 31 * jdk.test.lib.compiler.CompilerUtils 32 * jdk.test.lib.util.JarUtils 33 * jdk.test.lib.process.ProcessTools 34 * @run testng RunWithAutomaticModules 35 * @summary Runs tests that make use of automatic modules 36 */ 37 38 import java.nio.file.Files; 39 import java.nio.file.Path; 40 import java.nio.file.Paths; 41 42 import jdk.test.lib.compiler.CompilerUtils; 43 import jdk.test.lib.util.JarUtils; 44 import static jdk.test.lib.process.ProcessTools.*; 45 46 import org.testng.annotations.Test; 47 import static org.testng.Assert.*; 48 49 @Test 50 public class RunWithAutomaticModules { 51 52 private static final String TEST_SRC = System.getProperty("test.src"); 53 54 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 55 private static final Path CLASSES_DIR = Paths.get("classes"); 56 private static final Path MODS_DIR = Paths.get("mods"); 57 58 /** 59 * Basic test that consists of 3 modules: 60 * 61 * basictest - the test itself 62 * httpserver - a JAR file (automatic module) with a dummy HTTP server 63 * logging - a JAR file (automatic module) with a dummy logging library 64 * 65 * The test runs the dummy HTTP server and checks that has the expected 66 * reads and exported packages. 67 * 68 * The HTTP server uses the logging library. 69 */ 70 71 public void testBasic() throws Exception { 72 boolean compiled; 73 74 Path loggingSrc = SRC_DIR.resolve("logging"); 75 Path loggingClasses = CLASSES_DIR.resolve("logging"); 76 77 Path httpServerSrc = SRC_DIR.resolve("httpserver"); 78 Path httpServerClasses = CLASSES_DIR.resolve("httpserver"); 79 80 String testModule = "basictest"; 81 String mainClass = "test.Main"; 82 83 84 // compile + create mods/logging-1.0.jar 85 86 compiled = CompilerUtils.compile(loggingSrc, loggingClasses); 87 assertTrue(compiled); 88 89 JarUtils.createJarFile(MODS_DIR.resolve("logging-1.0.jar"), 90 loggingClasses); 91 92 93 // compile + create mods/httpserver-9.0.0.jar 94 95 compiled = CompilerUtils.compile(httpServerSrc, 96 httpServerClasses, 97 "-cp", loggingClasses.toString()); 98 assertTrue(compiled); 99 100 JarUtils.createJarFile(MODS_DIR.resolve("httpserver-9.0.0.jar"), 101 httpServerClasses); 102 103 104 // compile basictest to mods/basictest 105 106 compiled = CompilerUtils 107 .compile(SRC_DIR.resolve(testModule), 108 MODS_DIR.resolve(testModule), 109 "--module-path", MODS_DIR.toString()); 110 assertTrue(compiled); 111 112 113 // launch the test. Need --add-mdoules because nothing explicitly depends on logging 114 115 int exitValue 116 = executeTestJava("--module-path", MODS_DIR.toString(), 117 "--add-modules", "logging", 118 "-m", testModule + "/" + mainClass) 119 .outputTo(System.out) 120 .errorTo(System.out) 121 .getExitValue(); 122 123 assertTrue(exitValue == 0); 124 } 125 126 127 128 /** 129 * Test using a JAR file with a service provider as an automatic module. 130 * 131 * The consists of 2 modules: 132 * 133 * sptest - the test itself 134 * bananascript - a JAR file (automatic module) with a dummy ScriptEngineFactory 135 * 136 * The test uses ServiceLoader to locate and load ScriptEngineFactory 137 * implementations. It checks that bananascript is located. 138 */ 139 140 public void testServiceProvider() throws Exception { 141 boolean compiled; 142 143 Path providerSrc = SRC_DIR.resolve("bananascript"); 144 Path providerClasses = CLASSES_DIR.resolve("bananascript"); 145 146 String testModule = "sptest"; 147 String mainClass = "test.Main"; 148 149 150 // create mods/bananascript-0.9.jar 151 152 compiled = CompilerUtils.compile(providerSrc, providerClasses); 153 assertTrue(compiled); 154 155 String config = "META-INF/services/javax.script.ScriptEngineFactory"; 156 Path services = providerClasses.resolve(config).getParent(); 157 Files.createDirectories(services); 158 Files.copy(providerSrc.resolve(config), providerClasses.resolve(config)); 159 160 JarUtils.createJarFile(MODS_DIR.resolve("bananascript-0.9.jar"), providerClasses); 161 162 163 // compile sptest to mods/sptest 164 165 compiled = CompilerUtils 166 .compile(SRC_DIR.resolve(testModule), 167 MODS_DIR.resolve(testModule), 168 "--module-path", MODS_DIR.toString()); 169 170 assertTrue(compiled); 171 172 173 // launch the test 174 175 int exitValue 176 = executeTestJava("--module-path", MODS_DIR.toString(), 177 "-m", testModule + "/" + mainClass) 178 .outputTo(System.out) 179 .errorTo(System.out) 180 .getExitValue(); 181 182 assertTrue(exitValue == 0); 183 184 } 185 186 }