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