author | mchung |
Wed, 12 Oct 2016 15:41:00 -0700 | |
changeset 41484 | 834b7539ada3 |
parent 40261 | 86a49ba76f52 |
child 44545 | 83b611b88ac8 |
permissions | -rw-r--r-- |
39303 | 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 |
|
41484
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
29 |
* jdk.jartool |
39303 | 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; |
|
41484
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
40 |
import java.util.spi.ToolProvider; |
39303 | 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"; |
|
61 |
||
62 |
// the module main class |
|
63 |
private static final String MAIN_CLASS = "jdk.test.Main"; |
|
39345
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
64 |
private static final String MAIN_CLINIT_CLASS = "jdk.test.MainWithClinit"; |
39303 | 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, |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
73 |
"--module-source-path", SRC_DIR.toString())); |
39303 | 74 |
|
75 |
assertTrue(CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE), |
|
76 |
MODS_DIR, |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
77 |
"--module-source-path", SRC_DIR.toString())); |
39303 | 78 |
|
79 |
Files.createDirectories(LIBS_DIR); |
|
80 |
||
81 |
// create JAR files with no module-info.class |
|
41484
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
82 |
assertTrue(jar(M_MODULE, "p/Lib.class") == 0); |
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
83 |
assertTrue(jar(TEST_MODULE, "jdk/test/Main.class") == 0); |
39303 | 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 |
||
104 |
// no resolution failure |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
105 |
int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid); |
39303 | 106 |
assertTrue(exitValue == 0); |
107 |
} |
|
108 |
||
109 |
/** |
|
39345
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
110 |
* Test dryrun that does not invoke <clinit> of the main class |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
111 |
*/ |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
112 |
public void testMainClinit() throws Exception { |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
113 |
String dir = MODS_DIR.toString(); |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
114 |
String mid = TEST_MODULE + "/" + MAIN_CLINIT_CLASS; |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
115 |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
116 |
int exitValue = exec("--dry-run", "--module-path", dir, "-m", mid); |
39345
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
117 |
assertTrue(exitValue == 0); |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
118 |
|
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
119 |
// expect the test to fail if main class is initialized |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
120 |
exitValue = exec("--module-path", dir, "-m", mid); |
39345
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
121 |
assertTrue(exitValue != 0); |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
122 |
} |
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
123 |
|
344e8211e184
8160698: java --dry-run should not cause main class be initialized
mchung
parents:
39303
diff
changeset
|
124 |
/** |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
125 |
* Test non-existence module in --add-modules |
39303 | 126 |
*/ |
127 |
public void testNonExistAddModules() throws Exception { |
|
128 |
String dir = MODS_DIR.toString(); |
|
129 |
String mid = TEST_MODULE + "/" + MAIN_CLASS; |
|
130 |
||
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
131 |
int exitValue = exec("--dry-run", "--module-path", dir, |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
132 |
"--add-modules", "non.existence", |
39303 | 133 |
"-m", mid); |
134 |
assertTrue(exitValue != 0); |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Launch main class from class path |
|
139 |
*/ |
|
140 |
public void testClassPath() throws Exception { |
|
141 |
Path testJar = LIBS_DIR.resolve(TEST_MODULE + ".jar"); |
|
142 |
String libs = testJar.toString() + File.pathSeparator + |
|
143 |
LIBS_DIR.resolve(M_MODULE + ".jar").toString(); |
|
144 |
||
145 |
// test pass with m.jar:test.jar |
|
146 |
int exitValue = exec("-classpath", libs, MAIN_CLASS); |
|
147 |
assertTrue(exitValue == 0); |
|
148 |
||
149 |
// m.jar is not on classpath and fails with p.Lib not found |
|
150 |
exitValue = exec("-classpath", testJar.toString(), MAIN_CLASS); |
|
151 |
assertTrue(exitValue != 0); |
|
152 |
||
153 |
// dry pass passes since main is not executed |
|
154 |
exitValue = exec("--dry-run", "-classpath", testJar.toString(), MAIN_CLASS); |
|
155 |
assertTrue(exitValue == 0); |
|
156 |
} |
|
157 |
||
158 |
/** |
|
159 |
* Test automatic modules |
|
160 |
*/ |
|
161 |
public void testAutomaticModule() throws Exception { |
|
162 |
String libs = LIBS_DIR.resolve(M_MODULE + ".jar").toString() + |
|
163 |
File.pathSeparator + |
|
164 |
LIBS_DIR.resolve(TEST_MODULE + ".jar").toString(); |
|
165 |
String mid = TEST_MODULE + "/" + MAIN_CLASS; |
|
166 |
||
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
167 |
// test main method with and without --add-modules mm |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
168 |
int exitValue = exec("--module-path", LIBS_DIR.toString(), |
39303 | 169 |
"-m", mid); |
170 |
assertTrue(exitValue != 0); |
|
171 |
||
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
172 |
exitValue = exec("--module-path", LIBS_DIR.toString(), |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
173 |
"--add-modules", M_MODULE, |
39303 | 174 |
"-m", mid); |
175 |
assertTrue(exitValue == 0); |
|
176 |
||
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
177 |
// test dry run with and without --add-modules m |
39303 | 178 |
// no resolution failure |
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
179 |
exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(), |
39303 | 180 |
"-m", mid); |
181 |
assertTrue(exitValue == 0); |
|
182 |
||
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
183 |
exitValue = exec("--dry-run", "--module-path", LIBS_DIR.toString(), |
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
184 |
"--add-modules", M_MODULE, |
39303 | 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 |
|
40261
86a49ba76f52
8136930: Simplify use of module-system options by custom launchers
mchung
parents:
39345
diff
changeset
|
197 |
int exitValue = exec("--dry-run", "--module-path", subdir, "-m", mid); |
39303 | 198 |
assertTrue(exitValue != 0); |
199 |
} |
|
200 |
||
41484
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
201 |
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar") |
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
202 |
.orElseThrow(() -> |
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
203 |
new RuntimeException("jar tool not found") |
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
204 |
); |
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
205 |
|
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
206 |
private static int jar(String name, String entries) throws IOException { |
39303 | 207 |
Path jar = LIBS_DIR.resolve(name + ".jar"); |
208 |
||
209 |
// jar --create ... |
|
210 |
String classes = MODS_DIR.resolve(name).toString(); |
|
211 |
String[] args = { |
|
212 |
"--create", |
|
213 |
"--file=" + jar, |
|
214 |
"-C", classes, entries |
|
215 |
}; |
|
41484
834b7539ada3
8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents:
40261
diff
changeset
|
216 |
return JAR_TOOL.run(System.out, System.out, args); |
39303 | 217 |
} |
218 |
} |