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";
61 // the module main class
62 private static final String MAIN_CLASS = "jdk.test.Main";
63 private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit";
64
65
66 @BeforeTest
67 public void compileTestModule() throws Exception {
68
69 // javac -d mods/$TESTMODULE src/$TESTMODULE/**
70 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE),
71 MODS_DIR,
72 "--module-source-path", SRC_DIR.toString()));
73
74 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
75 MODS_DIR,
76 "--module-source-path", SRC_DIR.toString()));
77
78 Files.createDirectories(LIBS_DIR);
79
80 // create JAR files with no module-info.class
81 assertTrue(jar(M_MODULE, "p/Lib.class"));
82 assertTrue(jar(TEST_MODULE, "jdk/test/Main.class"));
83 }
84
85 /**
86 * Execute "java" with the given arguments, returning the exit code.
87 */
88 private int exec(String... args) throws Exception {
89 return ProcessTools.executeTestJava(args)
90 .outputTo(System.out)
91 .errorTo(System.out)
92 .getExitValue();
93 }
94
95
96 /**
97 * Launch module main
98 */
99 public void testModule() throws Exception {
100 String dir = MODS_DIR.toString();
101 String mid = TEST_MODULE + "/" + MAIN_CLASS;
102
180 assertTrue(exitValue == 0);
181
182 exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(),
183 "--add-modules", M_MODULE,
184 "-m", mid);
185 assertTrue(exitValue == 0);
186 }
187
188 /**
189 * module m not found
190 */
191 public void testMissingModule() throws Exception {
192 String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
193 String mid = TEST_MODULE + "/" + MAIN_CLASS;
194
195 // resolution failure
196 int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid);
197 assertTrue(exitValue != 0);
198 }
199
200 private static boolean jar(String name, String entries) throws IOException {
201 Path jar = LIBS_DIR.resolve(name + ".jar");
202
203 // jar --create ...
204 String classes = MODS_DIR.resolve(name).toString();
205 String[] args = {
206 "--create",
207 "--file=" + jar,
208 "-C", classes, entries
209 };
210 boolean success
211 = new sun.tools.jar.Main(System.out, System.out, "jar").run(args);
212 return success;
213 }
214 }
|
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
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 import java.util.spi.ToolProvider;
41
42 import jdk.testlibrary.ProcessTools;
43
44 import org.testng.annotations.BeforeTest;
45 import org.testng.annotations.Test;
46 import static org.testng.Assert.*;
47
48
49 @Test
50 public class DryRunTest {
51
52 private static final String TEST_SRC = System.getProperty("test.src");
53
54 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
55 private static final Path MODS_DIR = Paths.get("mods");
56 private static final Path LIBS_DIR = Paths.get("libs");
57
58 // the module name of the test module
59 private static final String TEST_MODULE = "test";
60 private static final String M_MODULE = "m";
62 // the module main class
63 private static final String MAIN_CLASS = "jdk.test.Main";
64 private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit";
65
66
67 @BeforeTest
68 public void compileTestModule() throws Exception {
69
70 // javac -d mods/$TESTMODULE src/$TESTMODULE/**
71 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(M_MODULE),
72 MODS_DIR,
73 "--module-source-path", SRC_DIR.toString()));
74
75 assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
76 MODS_DIR,
77 "--module-source-path", SRC_DIR.toString()));
78
79 Files.createDirectories(LIBS_DIR);
80
81 // create JAR files with no module-info.class
82 assertTrue(jar(M_MODULE, "p/Lib.class") == 0);
83 assertTrue(jar(TEST_MODULE, "jdk/test/Main.class") == 0);
84 }
85
86 /**
87 * Execute "java" with the given arguments, returning the exit code.
88 */
89 private int exec(String... args) throws Exception {
90 return ProcessTools.executeTestJava(args)
91 .outputTo(System.out)
92 .errorTo(System.out)
93 .getExitValue();
94 }
95
96
97 /**
98 * Launch module main
99 */
100 public void testModule() throws Exception {
101 String dir = MODS_DIR.toString();
102 String mid = TEST_MODULE + "/" + MAIN_CLASS;
103
181 assertTrue(exitValue == 0);
182
183 exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(),
184 "--add-modules", M_MODULE,
185 "-m", mid);
186 assertTrue(exitValue == 0);
187 }
188
189 /**
190 * module m not found
191 */
192 public void testMissingModule() throws Exception {
193 String subdir = MODS_DIR.resolve(TEST_MODULE).toString();
194 String mid = TEST_MODULE + "/" + MAIN_CLASS;
195
196 // resolution failure
197 int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid);
198 assertTrue(exitValue != 0);
199 }
200
201 private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").get();
202
203 private static int jar(String name, String entries) throws IOException {
204 Path jar = LIBS_DIR.resolve(name + ".jar");
205
206 // jar --create ...
207 String classes = MODS_DIR.resolve(name).toString();
208 String[] args = {
209 "--create",
210 "--file=" + jar,
211 "-C", classes, entries
212 };
213 return JAR_TOOL.run(System.out, System.out, args);
214 }
215 }
|