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