1 /* 2 * Copyright (c) 2014, 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 /lib/testlibrary /test/lib 27 * @modules jdk.jlink/jdk.tools.jmod 28 * jdk.compiler 29 * @build AddModsTest jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.* 30 * @run testng AddModsTest 31 * @summary Basic test for java --add-modules 32 */ 33 34 import java.io.File; 35 import java.nio.file.Path; 36 import java.nio.file.Paths; 37 38 import jdk.test.lib.compiler.CompilerUtils; 39 import static jdk.testlibrary.ProcessTools.*; 40 41 import org.testng.annotations.BeforeTest; 42 import org.testng.annotations.Test; 43 import static org.testng.Assert.*; 44 45 46 @Test 47 public class AddModsTest { 48 49 private static final String TEST_SRC = System.getProperty("test.src"); 50 51 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 52 private static final Path MODS1_DIR = Paths.get("mods1"); 53 private static final Path MODS2_DIR = Paths.get("mods2"); 54 55 // test module / main class 56 private static final String TEST_MODULE = "test"; 57 private static final String TEST_MAIN_CLASS = "test.Main"; 58 private static final String TEST_MID = TEST_MODULE + "/" + TEST_MAIN_CLASS; 59 60 // logger module 61 private static final String LOGGER_MODULE = "logger"; 62 63 64 @BeforeTest 65 public void compile() throws Exception { 66 // javac -d mods1/test src/test/** 67 boolean compiled = CompilerUtils.compile( 68 SRC_DIR.resolve(TEST_MODULE), 69 MODS1_DIR.resolve(TEST_MODULE) 70 ); 71 assertTrue(compiled, "test did not compile"); 72 73 // javac -d mods1/logger src/logger/** 74 compiled= CompilerUtils.compile( 75 SRC_DIR.resolve(LOGGER_MODULE), 76 MODS2_DIR.resolve(LOGGER_MODULE) 77 ); 78 assertTrue(compiled, "test did not compile"); 79 } 80 81 82 /** 83 * Basic test of --add-modules ALL-DEFAULT. Module java.sql should be 84 * resolved and the types in that module should be visible. 85 */ 86 public void testAddDefaultModules1() throws Exception { 87 88 // java --add-modules ALL-DEFAULT --module-path mods1 -m test ... 89 int exitValue 90 = executeTestJava("--module-path", MODS1_DIR.toString(), 91 "--add-modules", "ALL-DEFAULT", 92 "-m", TEST_MID, 93 "java.sql.Connection") 94 .outputTo(System.out) 95 .errorTo(System.out) 96 .getExitValue(); 97 98 assertTrue(exitValue == 0); 99 } 100 101 102 /** 103 * Run test on class path to load a type in a module on the application 104 * module path, uses {@code --add-modules logger}. 105 */ 106 public void testRunWithAddMods() throws Exception { 107 108 // java --module-path mods --add-modules logger -cp classes test.Main 109 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 110 String modulepath = MODS2_DIR.toString(); 111 int exitValue 112 = executeTestJava("--module-path", modulepath, 113 "--add-modules", LOGGER_MODULE, 114 "-cp", classpath, 115 TEST_MAIN_CLASS, 116 "logger.Logger") 117 .outputTo(System.out) 118 .errorTo(System.out) 119 .getExitValue(); 120 121 assertTrue(exitValue == 0); 122 } 123 124 /** 125 * Run application on class path that makes use of module on the 126 * application module path. Does not use --add-modules and so should 127 * fail at run-time. 128 */ 129 public void testRunMissingAddMods() throws Exception { 130 131 // java --module-path mods -cp classes test.Main 132 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 133 String modulepath = MODS1_DIR.toString(); 134 int exitValue 135 = executeTestJava("--module-path", modulepath, 136 "-cp", classpath, 137 TEST_MAIN_CLASS, 138 "logger.Logger") 139 .outputTo(System.out) 140 .errorTo(System.out) 141 .shouldContain("ClassNotFoundException") 142 .getExitValue(); 143 144 assertTrue(exitValue != 0); 145 } 146 147 148 /** 149 * Run test on class path to load a type in a module on the application 150 * module path, uses {@code --add-modules ALL-MODULE-PATH}. 151 */ 152 public void testAddAllModulePath() throws Exception { 153 154 // java --module-path mods --add-modules ALL-MODULE-PATH -cp classes test.Main 155 String classpath = MODS1_DIR.resolve(TEST_MODULE).toString(); 156 String modulepath = MODS1_DIR.toString(); 157 int exitValue 158 = executeTestJava("--module-path", modulepath, 159 "--add-modules", "ALL-MODULE-PATH", 160 "-cp", classpath, 161 TEST_MAIN_CLASS) 162 .outputTo(System.out) 163 .errorTo(System.out) 164 .getExitValue(); 165 166 assertTrue(exitValue == 0); 167 } 168 169 170 /** 171 * Test {@code --add-modules ALL-MODULE-PATH} without {@code --module-path}. 172 */ 173 public void testAddAllModulePathWithNoModulePath() throws Exception { 174 175 // java --add-modules ALL-MODULE-PATH -version 176 int exitValue 177 = executeTestJava("--add-modules", "ALL-MODULE-PATH", 178 "-version") 179 .outputTo(System.out) 180 .errorTo(System.out) 181 .getExitValue(); 182 183 assertTrue(exitValue == 0); 184 } 185 186 187 /** 188 * Tests {@code --add-modules} be specified more than once. 189 */ 190 public void testWithMultipleAddModules() throws Exception { 191 192 String modulepath = MODS1_DIR.toString() + File.pathSeparator + 193 MODS2_DIR.toString(); 194 int exitValue 195 = executeTestJava("--module-path", modulepath, 196 "--add-modules", LOGGER_MODULE, 197 "--add-modules", TEST_MODULE, 198 "-m", TEST_MID, 199 "logger.Logger") 200 .outputTo(System.out) 201 .errorTo(System.out) 202 .getExitValue(); 203 204 assertTrue(exitValue == 0); 205 } 206 207 208 /** 209 * Attempt to run with a bad module name specified to --add-modules 210 */ 211 public void testRunWithBadAddMods() throws Exception { 212 213 // java --module-path mods --add-modules DoesNotExist -m test ... 214 int exitValue 215 = executeTestJava("--module-path", MODS1_DIR.toString(), 216 "--add-modules", "DoesNotExist", 217 "-m", TEST_MID) 218 .outputTo(System.out) 219 .errorTo(System.out) 220 .shouldContain("DoesNotExist") 221 .getExitValue(); 222 223 assertTrue(exitValue != 0); 224 } 225 226 }