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