|
1 /* |
|
2 * Copyright (c) 2007, 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 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938 |
|
27 * 6894719 6968053 7151434 7146424 8007333 8077822 8143640 8132379 |
|
28 * @summary Argument parsing validation. |
|
29 * @modules jdk.compiler |
|
30 * jdk.zipfs |
|
31 * @compile -XDignore.symbol.file Arrrghs.java |
|
32 * @run main/othervm Arrrghs |
|
33 */ |
|
34 |
|
35 |
|
36 import java.io.File; |
|
37 import java.io.FileNotFoundException; |
|
38 import java.io.IOException; |
|
39 import java.util.ArrayList; |
|
40 import java.util.Arrays; |
|
41 import java.util.HashMap; |
|
42 import java.util.List; |
|
43 import java.util.Map; |
|
44 import java.util.regex.Matcher; |
|
45 import java.util.regex.Pattern; |
|
46 |
|
47 public class Arrrghs extends TestHelper { |
|
48 private Arrrghs(){} |
|
49 /** |
|
50 * This class provides various tests for arguments processing. |
|
51 * |
|
52 * History: these set of tests were part of Arrrghs.sh. The MKS shell |
|
53 * implementations were notoriously buggy. Implementing these tests purely |
|
54 * in Java is not only portable but also robust. |
|
55 * |
|
56 */ |
|
57 |
|
58 /* |
|
59 * SIGH, On Windows all strings are quoted, we need to unwrap it |
|
60 */ |
|
61 private static String removeExtraQuotes(String in) { |
|
62 if (isWindows) { |
|
63 // Trim the string and remove the enclosed quotes if any. |
|
64 in = in.trim(); |
|
65 if (in.startsWith("\"") && in.endsWith("\"")) { |
|
66 return in.substring(1, in.length()-1); |
|
67 } |
|
68 } |
|
69 return in; |
|
70 } |
|
71 |
|
72 // the pattern we hope to see in the output |
|
73 static final Pattern ArgPattern = Pattern.compile("\\s*argv\\[[0-9]*\\].*=.*"); |
|
74 |
|
75 void checkArgumentParsing(String inArgs, String... expArgs) throws IOException { |
|
76 List<String> scratchpad = new ArrayList<>(); |
|
77 scratchpad.add("set " + JLDEBUG_KEY + "=true"); |
|
78 // GAK, -version needs to be added so that windows can flush its stderr |
|
79 // exiting the process prematurely can terminate the stderr. |
|
80 scratchpad.add(javaCmd + " -version " + inArgs); |
|
81 File batFile = new File("atest.bat"); |
|
82 createAFile(batFile, scratchpad); |
|
83 |
|
84 TestResult tr = doExec(batFile.getName()); |
|
85 |
|
86 ArrayList<String> expList = new ArrayList<>(); |
|
87 expList.add(javaCmd); |
|
88 expList.add("-version"); |
|
89 expList.addAll(Arrays.asList(expArgs)); |
|
90 |
|
91 List<String> gotList = new ArrayList<>(); |
|
92 for (String x : tr.testOutput) { |
|
93 Matcher m = ArgPattern.matcher(x); |
|
94 if (m.matches()) { |
|
95 String a[] = x.split("="); |
|
96 gotList.add(a[a.length - 1].trim()); |
|
97 } |
|
98 } |
|
99 if (!gotList.equals(expList)) { |
|
100 System.out.println(tr); |
|
101 System.out.println("Expected args:"); |
|
102 System.out.println(expList); |
|
103 System.out.println("Obtained args:"); |
|
104 System.out.println(gotList); |
|
105 throw new RuntimeException("Error: args do not match"); |
|
106 } |
|
107 System.out.println("\'" + inArgs + "\'" + " - Test passed"); |
|
108 } |
|
109 |
|
110 /* |
|
111 * This tests general quoting and are specific to Windows, *nixes |
|
112 * need not worry about this, these have been tested with Windows |
|
113 * implementation and those that are known to work are used against |
|
114 * the java implementation. Note that the ProcessBuilder gets in the |
|
115 * way when testing some of these arguments, therefore we need to |
|
116 * create and execute a .bat file containing the arguments. |
|
117 */ |
|
118 @Test |
|
119 void testArgumentParsing() throws IOException { |
|
120 if (!isWindows) |
|
121 return; |
|
122 // no quotes |
|
123 checkArgumentParsing("a b c d", "a", "b", "c", "d"); |
|
124 |
|
125 // single quotes |
|
126 checkArgumentParsing("\"a b c d\"", "a b c d"); |
|
127 |
|
128 //double quotes |
|
129 checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d"); |
|
130 |
|
131 // triple quotes |
|
132 checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\""); |
|
133 |
|
134 // a literal within single quotes |
|
135 checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de"); |
|
136 |
|
137 // a literal within double quotes |
|
138 checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de"); |
|
139 |
|
140 // a literal quote |
|
141 checkArgumentParsing("a\\\"b", "a\"b"); |
|
142 |
|
143 // double back-slash |
|
144 checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\"); |
|
145 |
|
146 // triple back-slash |
|
147 checkArgumentParsing("a\\\\\\\"b", "a\\\"b"); |
|
148 |
|
149 // dangling quote |
|
150 checkArgumentParsing("\"a b c\"\"", "a b c\""); |
|
151 |
|
152 // expansions of white space separators |
|
153 checkArgumentParsing("a b", "a", "b"); |
|
154 checkArgumentParsing("a\tb", "a", "b"); |
|
155 checkArgumentParsing("a \t b", "a", "b"); |
|
156 |
|
157 checkArgumentParsing("\"C:\\TEST A\\\\\"", "C:\\TEST A\\"); |
|
158 checkArgumentParsing("\"\"C:\\TEST A\\\\\"\"", "C:\\TEST", "A\\"); |
|
159 |
|
160 // MS Windows tests |
|
161 // triple back-slash |
|
162 checkArgumentParsing("a\\\\\\d", "a\\\\\\d"); |
|
163 |
|
164 // triple back-slash in quotes |
|
165 checkArgumentParsing("\"a\\\\\\d\"", "a\\\\\\d"); |
|
166 |
|
167 // slashes separating characters |
|
168 checkArgumentParsing("X\\Y\\Z", "X\\Y\\Z"); |
|
169 checkArgumentParsing("\\X\\Y\\Z", "\\X\\Y\\Z"); |
|
170 |
|
171 // literals within dangling quotes, etc. |
|
172 checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e"); |
|
173 checkArgumentParsing("\"ab\\\"c\" \"\\\\\" d", "ab\"c", "\\", "d"); |
|
174 checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h"); |
|
175 checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d"); |
|
176 checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e"); |
|
177 |
|
178 // treatment of back-slashes |
|
179 checkArgumentParsing("*\\", "*\\"); |
|
180 checkArgumentParsing("*/", "*/"); |
|
181 checkArgumentParsing(".\\*", ".\\*"); |
|
182 checkArgumentParsing("./*", "./*"); |
|
183 checkArgumentParsing("..\\..\\*", "..\\..\\*"); |
|
184 checkArgumentParsing("../../*", "../../*"); |
|
185 checkArgumentParsing("..\\..\\", "..\\..\\"); |
|
186 checkArgumentParsing("../../", "../../"); |
|
187 checkArgumentParsing("a b\\ c", "a", "b\\", "c"); |
|
188 // 2 back-slashes |
|
189 checkArgumentParsing("\\\\?", "\\\\?"); |
|
190 // 3 back-slashes |
|
191 checkArgumentParsing("\\\\\\?", "\\\\\\?"); |
|
192 // 4 back-slashes |
|
193 checkArgumentParsing("\\\\\\\\?", "\\\\\\\\?"); |
|
194 // 5 back-slashes |
|
195 checkArgumentParsing("\\\\\\\\\\?", "\\\\\\\\\\?"); |
|
196 // 6 back-slashes |
|
197 checkArgumentParsing("\\\\\\\\\\\\?", "\\\\\\\\\\\\?"); |
|
198 |
|
199 // more treatment of mixed slashes |
|
200 checkArgumentParsing("f1/ f3\\ f4/", "f1/", "f3\\", "f4/"); |
|
201 checkArgumentParsing("f1/ f2\' ' f3/ f4/", "f1/", "f2\'", "'", "f3/", "f4/"); |
|
202 |
|
203 checkArgumentParsing("a\\*\\b", "a\\*\\b"); |
|
204 } |
|
205 |
|
206 private void initEmptyDir(File emptyDir) throws IOException { |
|
207 if (emptyDir.exists()) { |
|
208 recursiveDelete(emptyDir); |
|
209 } |
|
210 emptyDir.mkdir(); |
|
211 } |
|
212 |
|
213 private void initDirWithJavaFiles(File libDir) throws IOException { |
|
214 |
|
215 if (libDir.exists()) { |
|
216 recursiveDelete(libDir); |
|
217 } |
|
218 libDir.mkdirs(); |
|
219 ArrayList<String> scratchpad = new ArrayList<>(); |
|
220 scratchpad.add("package lib;"); |
|
221 scratchpad.add("public class Fbo {"); |
|
222 scratchpad.add("public static void main(String... args){Foo.f();}"); |
|
223 scratchpad.add("public static void f(){}"); |
|
224 scratchpad.add("}"); |
|
225 createFile(new File(libDir, "Fbo.java"), scratchpad); |
|
226 |
|
227 scratchpad.clear(); |
|
228 scratchpad.add("package lib;"); |
|
229 scratchpad.add("public class Foo {"); |
|
230 scratchpad.add("public static void main(String... args){"); |
|
231 scratchpad.add("for (String x : args) {"); |
|
232 scratchpad.add("System.out.println(x);"); |
|
233 scratchpad.add("}"); |
|
234 scratchpad.add("Fbo.f();"); |
|
235 scratchpad.add("}"); |
|
236 scratchpad.add("public static void f(){}"); |
|
237 scratchpad.add("}"); |
|
238 createFile(new File(libDir, "Foo.java"), scratchpad); |
|
239 } |
|
240 |
|
241 void checkArgumentWildcard(String inArgs, String... expArgs) throws IOException { |
|
242 String[] in = {inArgs}; |
|
243 checkArgumentWildcard(in, expArgs); |
|
244 |
|
245 // now add arbitrary arguments before and after |
|
246 String[] outInArgs = { "-Q", inArgs, "-R"}; |
|
247 |
|
248 String[] outExpArgs = new String[expArgs.length + 2]; |
|
249 outExpArgs[0] = "-Q"; |
|
250 System.arraycopy(expArgs, 0, outExpArgs, 1, expArgs.length); |
|
251 outExpArgs[expArgs.length + 1] = "-R"; |
|
252 checkArgumentWildcard(outInArgs, outExpArgs); |
|
253 } |
|
254 |
|
255 void checkArgumentWildcard(String[] inArgs, String[] expArgs) throws IOException { |
|
256 ArrayList<String> argList = new ArrayList<>(); |
|
257 argList.add(javaCmd); |
|
258 argList.add("-cp"); |
|
259 argList.add("lib" + File.separator + "*"); |
|
260 argList.add("lib.Foo"); |
|
261 argList.addAll(Arrays.asList(inArgs)); |
|
262 String[] cmds = new String[argList.size()]; |
|
263 argList.toArray(cmds); |
|
264 TestResult tr = doExec(cmds); |
|
265 if (!tr.isOK()) { |
|
266 System.out.println(tr); |
|
267 throw new RuntimeException("Error: classpath single entry wildcard entry"); |
|
268 } |
|
269 |
|
270 ArrayList<String> expList = new ArrayList<>(); |
|
271 expList.addAll(Arrays.asList(expArgs)); |
|
272 |
|
273 List<String> gotList = new ArrayList<>(); |
|
274 for (String x : tr.testOutput) { |
|
275 gotList.add(x.trim()); |
|
276 } |
|
277 if (!gotList.equals(expList)) { |
|
278 System.out.println(tr); |
|
279 System.out.println("Expected args:"); |
|
280 System.out.println(expList); |
|
281 System.out.println("Obtained args:"); |
|
282 System.out.println(gotList); |
|
283 throw new RuntimeException("Error: args do not match"); |
|
284 } |
|
285 System.out.print("\'"); |
|
286 for (String x : inArgs) { |
|
287 System.out.print(x + " "); |
|
288 } |
|
289 System.out.println("\'" + " - Test passed"); |
|
290 } |
|
291 |
|
292 /* |
|
293 * These tests are not expected to work on *nixes, and are ignored. |
|
294 */ |
|
295 @Test |
|
296 void testWildCardArgumentProcessing() throws IOException { |
|
297 if (!isWindows) |
|
298 return; |
|
299 File cwd = new File("."); |
|
300 File libDir = new File(cwd, "lib"); |
|
301 initDirWithJavaFiles(libDir); |
|
302 initEmptyDir(new File(cwd, "empty")); |
|
303 |
|
304 // test if javac (the command) can compile *.java |
|
305 TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java"); |
|
306 if (!tr.isOK()) { |
|
307 System.out.println(tr); |
|
308 throw new RuntimeException("Error: compiling java wildcards"); |
|
309 } |
|
310 |
|
311 // test if javac (the command) can compile *.java with a vmoption |
|
312 tr = doExec(javacCmd, "-cp", ".", |
|
313 "-J-showversion", "-J-Dsomeproperty=foo", |
|
314 libDir.getName() + File.separator + "*.java"); |
|
315 if (!tr.isOK()) { |
|
316 System.out.println(tr); |
|
317 throw new RuntimeException("Error: compiling java wildcards with vmoptions"); |
|
318 } |
|
319 |
|
320 |
|
321 // use the jar cmd to create jars using the ? wildcard |
|
322 File jarFoo = new File(libDir, "Foo.jar"); |
|
323 tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class"); |
|
324 if (!tr.isOK()) { |
|
325 System.out.println(tr); |
|
326 throw new RuntimeException("Error: creating jar with wildcards"); |
|
327 } |
|
328 |
|
329 // now the litmus test!, this should work |
|
330 checkArgumentWildcard("a", "a"); |
|
331 |
|
332 // test for basic expansion |
|
333 checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java"); |
|
334 |
|
335 // basic expansion in quotes |
|
336 checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java"); |
|
337 |
|
338 checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java", |
|
339 "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java"); |
|
340 |
|
341 checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java", |
|
342 "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java"); |
|
343 |
|
344 checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java", |
|
345 "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java"); |
|
346 |
|
347 checkArgumentWildcard("lib\\?", "lib\\?"); |
|
348 |
|
349 // test for basic expansion |
|
350 checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java"); |
|
351 |
|
352 // basic expansion in quotes |
|
353 checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java"); |
|
354 |
|
355 // suffix expansion |
|
356 checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class"); |
|
357 |
|
358 // suffix expansion in quotes |
|
359 checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class"); |
|
360 |
|
361 // check for ? expansion now |
|
362 checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java"); |
|
363 |
|
364 // check ? in quotes |
|
365 checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java"); |
|
366 |
|
367 // check ? as suffixes |
|
368 checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java"); |
|
369 |
|
370 // check ? in a leading role |
|
371 checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java"); |
|
372 checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java"); |
|
373 |
|
374 // check ? prefixed with - |
|
375 checkArgumentWildcard("-?", "-?"); |
|
376 |
|
377 // check * prefixed with - |
|
378 checkArgumentWildcard("-*", "-*"); |
|
379 |
|
380 // check on empty directory |
|
381 checkArgumentWildcard("empty\\*", "empty\\*"); |
|
382 checkArgumentWildcard("empty\\**", "empty\\**"); |
|
383 checkArgumentWildcard("empty\\?", "empty\\?"); |
|
384 checkArgumentWildcard("empty\\??", "empty\\??"); |
|
385 checkArgumentWildcard("empty\\*?", "empty\\*?"); |
|
386 checkArgumentWildcard("empty\\?*", "empty\\?*"); |
|
387 |
|
388 // 8132379: java should not filter out -J options for application |
|
389 String[] args = { "-J-one", "-Jtwo", "lib\\???.java", "-J-Dsomething", |
|
390 "a", "-J-Dlast.arg" }; |
|
391 String[] expected = { "-J-one", "-Jtwo", "lib\\Fbo.java", |
|
392 "lib\\Foo.java", "-J-Dsomething", "a", "-J-Dlast.arg" }; |
|
393 checkArgumentWildcard(args, expected); |
|
394 } |
|
395 |
|
396 void doArgumentCheck(String inArgs, String... expArgs) { |
|
397 Map<String, String> env = new HashMap<>(); |
|
398 env.put(JLDEBUG_KEY, "true"); |
|
399 TestResult tr = doExec(env, javaCmd, inArgs); |
|
400 System.out.println(tr); |
|
401 int sindex = tr.testOutput.indexOf("Command line args:"); |
|
402 if (sindex < 0) { |
|
403 System.out.println(tr); |
|
404 throw new RuntimeException("Error: no output"); |
|
405 } |
|
406 sindex++; // skip over the tag |
|
407 List<String> gotList = new ArrayList<>(); |
|
408 for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) { |
|
409 String a[] = x.split("="); |
|
410 gotList.add(a[a.length - 1].trim()); |
|
411 } |
|
412 List<String> expList = Arrays.asList(expArgs); |
|
413 if (!gotList.equals(expList)) { |
|
414 System.out.println(tr); |
|
415 System.out.println("Expected args:"); |
|
416 System.out.println(expList); |
|
417 System.out.println("Obtained args:"); |
|
418 System.out.println(gotList); |
|
419 throw new RuntimeException("Error: args do not match"); |
|
420 } |
|
421 } |
|
422 |
|
423 |
|
424 /* |
|
425 * These tests are usually run on non-existent targets to check error results |
|
426 */ |
|
427 @Test |
|
428 void testBasicErrorMessages() { |
|
429 // Tests for 5030233 |
|
430 TestResult tr = doExec(javaCmd, "-cp"); |
|
431 tr.checkNegative(); |
|
432 tr.isNotZeroOutput(); |
|
433 if (!tr.testStatus) |
|
434 System.out.println(tr); |
|
435 |
|
436 tr = doExec(javaCmd, "-classpath"); |
|
437 tr.checkNegative(); |
|
438 tr.isNotZeroOutput(); |
|
439 if (!tr.testStatus) |
|
440 System.out.println(tr); |
|
441 |
|
442 tr = doExec(javaCmd, "-jar"); |
|
443 tr.checkNegative(); |
|
444 tr.isNotZeroOutput(); |
|
445 if (!tr.testStatus) |
|
446 System.out.println(tr); |
|
447 |
|
448 tr = doExec(javacCmd, "-cp"); |
|
449 tr.checkNegative(); |
|
450 tr.isNotZeroOutput(); |
|
451 if (!tr.testStatus) |
|
452 System.out.println(tr); |
|
453 |
|
454 // Test for 6356475 "REGRESSION:"java -X" from cmdline fails" |
|
455 tr = doExec(javaCmd, "-X"); |
|
456 tr.checkPositive(); |
|
457 tr.isNotZeroOutput(); |
|
458 if (!tr.testStatus) |
|
459 System.out.println(tr); |
|
460 |
|
461 tr = doExec(javaCmd, "-help"); |
|
462 tr.checkPositive(); |
|
463 tr.isNotZeroOutput(); |
|
464 if (!tr.testStatus) |
|
465 System.out.println(tr); |
|
466 |
|
467 // 6753938, test for non-negative exit value for an incorrectly formed |
|
468 // command line, '% java' |
|
469 tr = doExec(javaCmd); |
|
470 tr.checkNegative(); |
|
471 tr.isNotZeroOutput(); |
|
472 if (!tr.testStatus) |
|
473 System.out.println(tr); |
|
474 |
|
475 // 6753938, test for non-negative exit value for an incorrectly formed |
|
476 // command line, '% java -Xcomp' |
|
477 tr = doExec(javaCmd, "-Xcomp"); |
|
478 tr.checkNegative(); |
|
479 tr.isNotZeroOutput(); |
|
480 if (!tr.testStatus) |
|
481 System.out.println(tr); |
|
482 |
|
483 // 7151434, test for non-negative exit value for an incorrectly formed |
|
484 // command line, '% java -jar -W', note the bogus -W |
|
485 tr = doExec(javaCmd, "-jar", "-W"); |
|
486 tr.checkNegative(); |
|
487 tr.contains("Unrecognized option: -W"); |
|
488 if (!tr.testStatus) |
|
489 System.out.println(tr); |
|
490 } |
|
491 |
|
492 /* |
|
493 * Tests various dispositions of the main method, these tests are limited |
|
494 * to English locales as they check for error messages that are localized. |
|
495 */ |
|
496 @Test |
|
497 void testMainMethod() throws FileNotFoundException { |
|
498 if (!isEnglishLocale()) { |
|
499 return; |
|
500 } |
|
501 |
|
502 TestResult tr; |
|
503 |
|
504 // a missing class |
|
505 createJar("MIA", new File("some.jar"), new File("Foo"), |
|
506 (String[])null); |
|
507 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
508 tr.contains("Error: Could not find or load main class MIA"); |
|
509 if (!tr.testStatus) |
|
510 System.out.println(tr); |
|
511 // use classpath to check |
|
512 tr = doExec(javaCmd, "-cp", "some.jar", "MIA"); |
|
513 tr.contains("Error: Could not find or load main class MIA"); |
|
514 if (!tr.testStatus) |
|
515 System.out.println(tr); |
|
516 |
|
517 // incorrect method access |
|
518 createJar(new File("some.jar"), new File("Foo"), |
|
519 "private static void main(String[] args){}"); |
|
520 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
521 tr.contains("Error: Main method not found in class Foo"); |
|
522 if (!tr.testStatus) |
|
523 System.out.println(tr); |
|
524 // use classpath to check |
|
525 tr = doExec(javaCmd, "-cp", "some.jar", "Foo"); |
|
526 tr.contains("Error: Main method not found in class Foo"); |
|
527 if (!tr.testStatus) |
|
528 System.out.println(tr); |
|
529 |
|
530 // incorrect return type |
|
531 createJar(new File("some.jar"), new File("Foo"), |
|
532 "public static int main(String[] args){return 1;}"); |
|
533 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
534 tr.contains("Error: Main method must return a value of type void in class Foo"); |
|
535 if (!tr.testStatus) |
|
536 System.out.println(tr); |
|
537 // use classpath to check |
|
538 tr = doExec(javaCmd, "-cp", "some.jar", "Foo"); |
|
539 tr.contains("Error: Main method must return a value of type void in class Foo"); |
|
540 if (!tr.testStatus) |
|
541 System.out.println(tr); |
|
542 |
|
543 // incorrect parameter type |
|
544 createJar(new File("some.jar"), new File("Foo"), |
|
545 "public static void main(Object[] args){}"); |
|
546 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
547 tr.contains("Error: Main method not found in class Foo"); |
|
548 if (!tr.testStatus) |
|
549 System.out.println(tr); |
|
550 // use classpath to check |
|
551 tr = doExec(javaCmd, "-cp", "some.jar", "Foo"); |
|
552 tr.contains("Error: Main method not found in class Foo"); |
|
553 if (!tr.testStatus) |
|
554 System.out.println(tr); |
|
555 |
|
556 // incorrect method type - non-static |
|
557 createJar(new File("some.jar"), new File("Foo"), |
|
558 "public void main(String[] args){}"); |
|
559 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
560 tr.contains("Error: Main method is not static in class Foo"); |
|
561 if (!tr.testStatus) |
|
562 System.out.println(tr); |
|
563 // use classpath to check |
|
564 tr = doExec(javaCmd, "-cp", "some.jar", "Foo"); |
|
565 tr.contains("Error: Main method is not static in class Foo"); |
|
566 if (!tr.testStatus) |
|
567 System.out.println(tr); |
|
568 |
|
569 // amongst a potpourri of kindred main methods, is the right one chosen ? |
|
570 createJar(new File("some.jar"), new File("Foo"), |
|
571 "void main(Object[] args){}", |
|
572 "int main(Float[] args){return 1;}", |
|
573 "private void main() {}", |
|
574 "private static void main(int x) {}", |
|
575 "public int main(int argc, String[] argv) {return 1;}", |
|
576 "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}"); |
|
577 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
578 tr.contains("THE_CHOSEN_ONE"); |
|
579 if (!tr.testStatus) |
|
580 System.out.println(tr); |
|
581 // use classpath to check |
|
582 tr = doExec(javaCmd, "-cp", "some.jar", "Foo"); |
|
583 tr.contains("THE_CHOSEN_ONE"); |
|
584 if (!tr.testStatus) |
|
585 System.out.println(tr); |
|
586 |
|
587 // test for extraneous whitespace in the Main-Class attribute |
|
588 createJar(" Foo ", new File("some.jar"), new File("Foo"), |
|
589 "public static void main(String... args){}"); |
|
590 tr = doExec(javaCmd, "-jar", "some.jar"); |
|
591 tr.checkPositive(); |
|
592 if (!tr.testStatus) |
|
593 System.out.println(tr); |
|
594 } |
|
595 /* |
|
596 * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if |
|
597 * the suppressed stack traces are exposed, ignore these tests for localized |
|
598 * locales, limiting to English only. |
|
599 */ |
|
600 @Test |
|
601 void testDiagOptions() throws FileNotFoundException { |
|
602 if (!isEnglishLocale()) { // only english version |
|
603 return; |
|
604 } |
|
605 TestResult tr; |
|
606 // a missing class |
|
607 createJar("MIA", new File("some.jar"), new File("Foo"), |
|
608 (String[])null); |
|
609 tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar"); |
|
610 tr.contains("Error: Could not find or load main class MIA"); |
|
611 tr.contains("java.lang.ClassNotFoundException: MIA"); |
|
612 if (!tr.testStatus) |
|
613 System.out.println(tr); |
|
614 |
|
615 // use classpath to check |
|
616 tr = doExec(javaCmd, "-Xdiag", "-cp", "some.jar", "MIA"); |
|
617 tr.contains("Error: Could not find or load main class MIA"); |
|
618 tr.contains("java.lang.ClassNotFoundException: MIA"); |
|
619 if (!tr.testStatus) |
|
620 System.out.println(tr); |
|
621 |
|
622 // a missing class on the classpath |
|
623 tr = doExec(javaCmd, "-Xdiag", "NonExistentClass"); |
|
624 tr.contains("Error: Could not find or load main class NonExistentClass"); |
|
625 tr.contains("java.lang.ClassNotFoundException: NonExistentClass"); |
|
626 if (!tr.testStatus) |
|
627 System.out.println(tr); |
|
628 } |
|
629 |
|
630 /** |
|
631 * @param args the command line arguments |
|
632 * @throws java.io.FileNotFoundException |
|
633 */ |
|
634 public static void main(String[] args) throws Exception { |
|
635 if (debug) { |
|
636 System.out.println("Starting Arrrghs tests"); |
|
637 } |
|
638 Arrrghs a = new Arrrghs(); |
|
639 a.run(args); |
|
640 if (testExitValue > 0) { |
|
641 System.out.println("Total of " + testExitValue + " failed"); |
|
642 System.exit(1); |
|
643 } else { |
|
644 System.out.println("All tests pass"); |
|
645 } |
|
646 } |
|
647 } |