1 /* 2 * Copyright (c) 2016, 2017, 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 * @bug 8159596 27 * @library /test/lib 28 * @modules jdk.compiler 29 * jdk.jartool 30 * @build DryRunTest jdk.test.lib.process.ProcessTools 31 * jdk.test.lib.compiler.CompilerUtils 32 * @run testng DryRunTest 33 * @summary Test java --dry-run 34 */ 35 36 import java.io.File; 37 import java.io.IOException; 38 import java.nio.file.Files; 39 import java.nio.file.Path; 40 import java.nio.file.Paths; 41 import java.util.spi.ToolProvider; 42 43 import jdk.test.lib.compiler.CompilerUtils; 44 import jdk.test.lib.process.ProcessTools; 45 46 import org.testng.annotations.BeforeTest; 47 import org.testng.annotations.Test; 48 import static org.testng.Assert.*; 49 50 @Test 51 public class DryRunTest { 52 53 private static final String TEST_SRC = System.getProperty("test.src"); 54 55 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 56 private static final Path MODS_DIR = Paths.get("mods"); 57 private static final Path LIBS_DIR = Paths.get("libs"); 58 59 // the module name of the test module 60 private static final String TEST_MODULE = "test"; 61 private static final String M_MODULE = "m"; 62 63 // the module main class 64 private static final String MAIN_CLASS = "jdk.test.Main"; 65 private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit"; 66 67 68 @BeforeTest 69 public void compileTestModule() throws Exception { 70 71 // javac -d mods/$TESTMODULE src/$TESTMODULE/** 72 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE), 73 MODS_DIR, 74 "--module-source-path", SRC_DIR.toString())); 75 76 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), 77 MODS_DIR, 78 "--module-source-path", SRC_DIR.toString())); 79 80 Files.createDirectories(LIBS_DIR); 81 82 // create JAR files with no module-info.class 83 assertTrue(jar(M_MODULE, "p/Lib.class") == 0); 84 assertTrue(jar(TEST_MODULE, "jdk/test/Main.class") == 0); 85 } 86 87 /** 88 * Execute "java" with the given arguments, returning the exit code. 89 */ 90 private int exec(String... args) throws Exception { 91 return ProcessTools.executeTestJava(args) 92 .outputTo(System.out) 93 .errorTo(System.out) 94 .getExitValue(); 95 } 96 97 98 /** 99 * Launch module main 100 */ 101 public void testModule() throws Exception { 102 String dir = MODS_DIR.toString(); 103 String mid = TEST_MODULE + "/" + MAIN_CLASS; 104 105 // no resolution failure 106 int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid); 107 assertTrue(exitValue == 0); 108 } 109 110 /** 111 * Test dryrun that does not invoke <clinit> of the main class 112 */ 113 public void testMainClinit() throws Exception { 114 String dir = MODS_DIR.toString(); 115 String mid = TEST_MODULE + "/" + MAIN_CLINIT_CLASS; 116 117 int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid); 118 assertTrue(exitValue == 0); 119 120 // expect the test to fail if main class is initialized 121 exitValue = exec("--module-path", dir, "-m", mid); 122 assertTrue(exitValue != 0); 123 } 124 125 /** 126 * Test non-existence module in --add-modules 127 */ 128 public void testNonExistAddModules() throws Exception { 129 String dir = MODS_DIR.toString(); 130 String mid = TEST_MODULE + "/" + MAIN_CLASS; 131 132 int exitValue = exec("--dry-run", "--module-path", dir, 133 "--add-modules", "non.existence", 134 "-m", mid); 135 assertTrue(exitValue != 0); 136 } 137 138 /** 139 * Launch main class from class path 140 */ 141 public void testClassPath() throws Exception { 142 Path testJar = LIBS_DIR.resolve(TEST_MODULE + ".jar"); 143 String libs = testJar.toString() + File.pathSeparator + 144 LIBS_DIR.resolve(M_MODULE + ".jar").toString(); 145 146 // test pass with m.jar:test.jar 147 int exitValue = exec("-classpath", libs, MAIN_CLASS); 148 assertTrue(exitValue == 0); 149 150 // m.jar is not on classpath and fails with p.Lib not found 151 exitValue = exec("-classpath", testJar.toString(), MAIN_CLASS); 152 assertTrue(exitValue != 0); 153 154 // dry pass passes since main is not executed 155 exitValue = exec("--dry-run", "-classpath", testJar.toString(), MAIN_CLASS); 156 assertTrue(exitValue == 0); 157 } 158 159 /** 160 * Test automatic modules 161 */ 162 public void testAutomaticModule() throws Exception { 163 String libs = LIBS_DIR.resolve(M_MODULE + ".jar").toString() + 164 File.pathSeparator + 165 LIBS_DIR.resolve(TEST_MODULE + ".jar").toString(); 166 String mid = TEST_MODULE + "/" + MAIN_CLASS; 167 168 // test main method with and without --add-modules mm 169 int exitValue = exec("--module-path", LIBS_DIR.toString(), 170 "-m", mid); 171 assertTrue(exitValue == 0); 172 } 173 174 /** 175 * module m not found 176 */ 177 public void testMissingModule() throws Exception { 178 String subdir = MODS_DIR.resolve(TEST_MODULE).toString(); 179 String mid = TEST_MODULE + "/" + MAIN_CLASS; 180 181 // resolution failure 182 int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid); 183 assertTrue(exitValue != 0); 184 } 185 186 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") 187 .orElseThrow(() -> 188 new RuntimeException("jar tool not found") 189 ); 190 191 private static int jar(String name, String entries) throws IOException { 192 Path jar = LIBS_DIR.resolve(name + ".jar"); 193 194 // jar --create ... 195 String classes = MODS_DIR.resolve(name).toString(); 196 String[] args = { 197 "--create", 198 "--file=" + jar, 199 "-C", classes, entries 200 }; 201 return JAR_TOOL.run(System.out, System.out, args); 202 } 203 }