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