author | mr |
Wed, 16 Jan 2019 16:27:21 -0800 | |
changeset 53372 | 4003935e6e5f |
parent 47216 | 71c04702a3d5 |
child 59020 | aebd72de84b0 |
permissions | -rw-r--r-- |
32267 | 1 |
/* |
2 |
* Copyright (c) 2015, 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 8027634 |
|
27 |
* @summary Argument parsing from file |
|
45944
882cea808912
8179292: a number of launcher tests fail when run with --limit-modules due to CNFE: javax.tools.ToolProvider
anazarov
parents:
43325
diff
changeset
|
28 |
* @modules jdk.compiler |
882cea808912
8179292: a number of launcher tests fail when run with --limit-modules due to CNFE: javax.tools.ToolProvider
anazarov
parents:
43325
diff
changeset
|
29 |
* jdk.zipfs |
32267 | 30 |
* @build TestHelper |
31 |
* @run main ArgsFileTest |
|
32 |
*/ |
|
33 |
import java.io.File; |
|
34 |
import java.io.IOException; |
|
35 |
import java.util.ArrayList; |
|
36 |
import java.util.Arrays; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.HashMap; |
|
39 |
import java.util.List; |
|
40 |
import java.util.Map; |
|
41 |
import java.util.regex.Matcher; |
|
42 |
import java.util.regex.Pattern; |
|
43 |
||
44 |
public class ArgsFileTest extends TestHelper { |
|
45 |
private static File testJar = null; |
|
46 |
private static Map<String, String> env = new HashMap<>(); |
|
47 |
||
48 |
static void init() throws IOException { |
|
49 |
if (testJar != null) { |
|
50 |
return; |
|
51 |
} |
|
52 |
testJar = new File("test.jar"); |
|
53 |
StringBuilder tsrc = new StringBuilder(); |
|
54 |
tsrc.append("public static void main(String... args) {\n"); |
|
55 |
tsrc.append(" for (String x : args) {\n"); |
|
56 |
tsrc.append(" System.out.println(x);\n"); |
|
57 |
tsrc.append(" }\n"); |
|
58 |
tsrc.append("}\n"); |
|
59 |
createJar(testJar, new File("Foo"), tsrc.toString()); |
|
60 |
||
61 |
env.put(JLDEBUG_KEY, "true"); |
|
62 |
} |
|
63 |
||
64 |
private File createArgFile(String fname, List<String> lines) throws IOException { |
|
65 |
File argFile = new File(fname); |
|
66 |
argFile.delete(); |
|
67 |
createAFile(argFile, lines); |
|
68 |
return argFile; |
|
69 |
} |
|
70 |
||
71 |
private void verifyOptions(List<String> args, TestResult tr) { |
|
72 |
if (args.isEmpty()) { |
|
73 |
return; |
|
74 |
} |
|
75 |
||
76 |
int i = 1; |
|
77 |
for (String x : args) { |
|
78 |
tr.matches(".*argv\\[" + i + "\\] = " + Pattern.quote(x) + ".*"); |
|
79 |
i++; |
|
80 |
} |
|
81 |
if (! tr.testStatus) { |
|
82 |
System.out.println(tr); |
|
83 |
throw new RuntimeException("test fails"); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
private void verifyUserArgs(List<String> args, TestResult tr, int index) { |
|
88 |
if (javaCmd != TestHelper.javaCmd) { |
|
89 |
tr.contains("\tFirst application arg index: 1"); |
|
90 |
} else { |
|
91 |
tr.contains("\tFirst application arg index: " + index); |
|
92 |
||
93 |
for (String arg: args) { |
|
94 |
tr.matches("^" + Pattern.quote(arg) + "$"); |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
if (! tr.testStatus) { |
|
99 |
System.out.println(tr); |
|
100 |
throw new RuntimeException("test fails"); |
|
101 |
} |
|
102 |
} |
|
103 |
||
104 |
@Test |
|
105 |
public void expandAll() throws IOException { |
|
106 |
List<String> lines = new ArrayList<>(); |
|
107 |
lines.add("-Xmx32m"); |
|
108 |
lines.add("-Xint"); |
|
109 |
File argFile1 = createArgFile("argFile1", lines); |
|
110 |
lines = new ArrayList<>(); |
|
111 |
lines.add("-jar"); |
|
112 |
lines.add("test.jar"); |
|
113 |
lines.add("uarg1 @uarg2 @@uarg3 -uarg4 uarg5"); |
|
114 |
File argFile2 = createArgFile("argFile2", lines); |
|
115 |
||
116 |
TestResult tr = doExec(env, javaCmd, "@argFile1", "@argFile2"); |
|
117 |
||
118 |
List<String> appArgs = new ArrayList<>(); |
|
119 |
appArgs.add("uarg1"); |
|
120 |
appArgs.add("@uarg2"); |
|
121 |
appArgs.add("@@uarg3"); |
|
122 |
appArgs.add("-uarg4"); |
|
123 |
appArgs.add("uarg5"); |
|
124 |
||
125 |
List<String> options = new ArrayList<>(); |
|
126 |
options.add("-Xmx32m"); |
|
127 |
options.add("-Xint"); |
|
128 |
options.add("-jar"); |
|
129 |
options.add("test.jar"); |
|
130 |
options.addAll(appArgs); |
|
131 |
||
132 |
verifyOptions(options, tr); |
|
133 |
verifyUserArgs(appArgs, tr, 5); |
|
134 |
argFile1.delete(); |
|
135 |
argFile2.delete(); |
|
136 |
||
137 |
File cpFile = createArgFile("cpFile", Arrays.asList("-cp", "test.jar")); |
|
138 |
List<String> appCmd = new ArrayList<>(); |
|
139 |
appCmd.add("Foo"); |
|
140 |
appCmd.addAll(appArgs); |
|
141 |
File appFile = createArgFile("appFile", appCmd); |
|
142 |
||
143 |
tr = doExec(env, javaCmd, "@cpFile", "@appFile"); |
|
144 |
verifyOptions(Arrays.asList("-cp", "test.jar", "Foo", |
|
145 |
"uarg1", "@uarg2", "@@uarg3", "-uarg4", "uarg5"), tr); |
|
146 |
verifyUserArgs(appArgs, tr, 4); |
|
147 |
cpFile.delete(); |
|
148 |
appFile.delete(); |
|
149 |
} |
|
150 |
||
151 |
@Test |
|
152 |
public void escapeArg() throws IOException { |
|
153 |
List<String> lines = new ArrayList<>(); |
|
154 |
lines.add("-Xmx32m"); |
|
155 |
lines.add("-Xint"); |
|
156 |
File argFile1 = createArgFile("argFile1", lines); |
|
157 |
||
158 |
TestResult tr = doExec(env, javaCmd, "-cp", "@@arg", "-cp", "@", |
|
159 |
"-cp", "@@@cp", "@argFile1", "@@@@Main@@@@", "-version"); |
|
160 |
List<String> options = new ArrayList<>(); |
|
161 |
options.add("-cp"); |
|
162 |
options.add("@arg"); |
|
163 |
options.add("-cp"); |
|
164 |
options.add("@"); |
|
165 |
options.add("-cp"); |
|
166 |
options.add("@@cp"); |
|
167 |
options.add("-Xmx32m"); |
|
168 |
options.add("-Xint"); |
|
169 |
options.add("@@@Main@@@@"); |
|
170 |
options.add("-version"); |
|
171 |
verifyOptions(options, tr); |
|
172 |
verifyUserArgs(Collections.emptyList(), tr, options.size()); |
|
173 |
argFile1.delete(); |
|
174 |
} |
|
175 |
||
176 |
@Test |
|
177 |
public void killSwitch() throws IOException { |
|
178 |
List<String> lines = new ArrayList<>(); |
|
179 |
lines.add("-Xmx32m"); |
|
180 |
lines.add("-Xint"); |
|
181 |
File argFile1 = createArgFile("argFile1", lines); |
|
182 |
lines = new ArrayList<>(); |
|
183 |
lines.add("-jar"); |
|
184 |
lines.add("test.jar"); |
|
185 |
lines.add("uarg1 @uarg2 @@uarg3 -uarg4 uarg5"); |
|
186 |
File argFile2 = createArgFile("argFile2", lines); |
|
187 |
File argKill = createArgFile("argKill", |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
188 |
Collections.singletonList("--disable-@files")); |
32267 | 189 |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
190 |
TestResult tr = doExec(env, javaCmd, "@argFile1", "--disable-@files", "@argFile2"); |
32267 | 191 |
List<String> options = new ArrayList<>(); |
192 |
options.add("-Xmx32m"); |
|
193 |
options.add("-Xint"); |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
194 |
options.add("--disable-@files"); |
32267 | 195 |
options.add("@argFile2"); |
196 |
verifyOptions(options, tr); |
|
197 |
// Main class is @argFile2 |
|
198 |
verifyUserArgs(Collections.emptyList(), tr, 5); |
|
199 |
||
200 |
// Specify in file is same as specify inline |
|
201 |
tr = doExec(env, javaCmd, "@argFile1", "@argKill", "@argFile2"); |
|
202 |
verifyOptions(options, tr); |
|
203 |
// Main class is @argFile2 |
|
204 |
verifyUserArgs(Collections.emptyList(), tr, 5); |
|
205 |
||
206 |
// multiple is fine, once on is on. |
|
43325
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
207 |
tr = doExec(env, javaCmd, "@argKill", "@argFile1", "--disable-@files", "@argFile2"); |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
208 |
options = Arrays.asList("--disable-@files", "@argFile1", |
033e4cced1dc
8170832: Add a new launcher environment variable JAVA_OPTIONS
henryjen
parents:
32267
diff
changeset
|
209 |
"--disable-@files", "@argFile2"); |
32267 | 210 |
verifyOptions(options, tr); |
211 |
verifyUserArgs(Collections.emptyList(), tr, 3); |
|
212 |
||
213 |
// after main class, becoming an user application argument |
|
214 |
tr = doExec(env, javaCmd, "@argFile2", "@argKill"); |
|
215 |
options = Arrays.asList("-jar", "test.jar", "uarg1", "@uarg2", "@@uarg3", |
|
216 |
"-uarg4", "uarg5", "@argKill"); |
|
217 |
verifyOptions(options, tr); |
|
218 |
verifyUserArgs(Arrays.asList("uarg1", "@uarg2", "@@uarg3", |
|
219 |
"-uarg4", "uarg5", "@argKill"), tr, 3); |
|
220 |
||
221 |
argFile1.delete(); |
|
222 |
argFile2.delete(); |
|
223 |
argKill.delete(); |
|
224 |
} |
|
225 |
||
226 |
@Test |
|
227 |
public void userApplication() throws IOException { |
|
228 |
List<String> lines = new ArrayList<>(); |
|
229 |
lines.add("-Xmx32m"); |
|
230 |
lines.add("-Xint"); |
|
231 |
File vmArgs = createArgFile("vmArgs", lines); |
|
232 |
File jarOpt = createArgFile("jarOpt", Arrays.asList("-jar")); |
|
233 |
File cpOpt = createArgFile("cpOpt", Arrays.asList("-cp")); |
|
234 |
File jarArg = createArgFile("jarArg", Arrays.asList("test.jar")); |
|
235 |
File userArgs = createArgFile("userArgs", Arrays.asList("-opt", "arg", "--longopt")); |
|
236 |
||
237 |
TestResult tr = doExec(env, javaCmd, |
|
238 |
"@vmArgs", "@jarOpt", "test.jar", "-opt", "arg", "--longopt"); |
|
239 |
verifyOptions(Arrays.asList( |
|
240 |
"-Xmx32m", "-Xint", "-jar", "test.jar", "-opt", "arg", "--longopt"), tr); |
|
241 |
verifyUserArgs(Arrays.asList("-opt", "arg", "--longopt"), tr, 5); |
|
242 |
||
243 |
tr = doExec(env, javaCmd, "@jarOpt", "@jarArg", "@vmArgs"); |
|
244 |
verifyOptions(Arrays.asList("-jar", "test.jar", "@vmArgs"), tr); |
|
245 |
verifyUserArgs(Arrays.asList("@vmArgs"), tr, 3); |
|
246 |
||
247 |
tr = doExec(env, javaCmd, "-cp", "@jarArg", "@vmArgs", "Foo", "@userArgs"); |
|
248 |
verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint", |
|
249 |
"Foo", "@userArgs"), tr); |
|
250 |
verifyUserArgs(Arrays.asList("@userArgs"), tr, 6); |
|
251 |
||
252 |
tr = doExec(env, javaCmd, "@cpOpt", "@jarArg", "@vmArgs", "Foo", "@userArgs"); |
|
253 |
verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint", |
|
254 |
"Foo", "@userArgs"), tr); |
|
255 |
verifyUserArgs(Arrays.asList("@userArgs"), tr, 6); |
|
256 |
||
257 |
tr = doExec(env, javaCmd, "@cpOpt", "test.jar", "@vmArgs", "Foo", "@userArgs"); |
|
258 |
verifyOptions(Arrays.asList("-cp", "test.jar", "-Xmx32m", "-Xint", |
|
259 |
"Foo", "@userArgs"), tr); |
|
260 |
verifyUserArgs(Arrays.asList("@userArgs"), tr, 6); |
|
261 |
||
262 |
vmArgs.delete(); |
|
263 |
jarOpt.delete(); |
|
264 |
cpOpt.delete(); |
|
265 |
jarArg.delete(); |
|
266 |
userArgs.delete(); |
|
267 |
} |
|
268 |
||
269 |
// test with missing file |
|
270 |
@Test |
|
271 |
public void missingFileNegativeTest() throws IOException { |
|
272 |
TestResult tr = doExec(javaCmd, "@" + "missing.cmd"); |
|
273 |
tr.checkNegative(); |
|
274 |
tr.contains("Error: could not open `missing.cmd'"); |
|
275 |
if (!tr.testStatus) { |
|
276 |
System.out.println(tr); |
|
277 |
throw new RuntimeException("test fails"); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
public static void main(String... args) throws Exception { |
|
282 |
init(); |
|
283 |
ArgsFileTest a = new ArgsFileTest(); |
|
284 |
a.run(args); |
|
285 |
if (testExitValue > 0) { |
|
286 |
System.out.println("Total of " + testExitValue + " failed"); |
|
287 |
System.exit(1); |
|
288 |
} else { |
|
289 |
System.out.println("All tests pass"); |
|
290 |
} |
|
291 |
} |
|
292 |
} |