File: Arrrghs.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (647 lines) | stat: -rw-r--r-- 24,466 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/**
 * @test
 * @bug 5030233 6214916 6356475 6571029 6684582 6742159 4459600 6758881 6753938
 *      6894719 6968053 7151434 7146424 8007333 8077822 8143640 8132379
 * @summary Argument parsing validation.
 * @modules jdk.compiler
 *          jdk.zipfs
 * @compile -XDignore.symbol.file Arrrghs.java
 * @run main/othervm Arrrghs
 */


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Arrrghs extends TestHelper {
    private Arrrghs(){}
    /**
     * This class provides various tests for arguments processing.
     *
     * History: these set of tests  were part of Arrrghs.sh. The MKS shell
     * implementations were notoriously buggy. Implementing these tests purely
     * in Java is not only portable but also robust.
     *
     */

    /*
     * SIGH, On Windows all strings are quoted, we need to unwrap it
     */
    private static String removeExtraQuotes(String in) {
        if (isWindows) {
            // Trim the string and remove the enclosed quotes if any.
            in = in.trim();
            if (in.startsWith("\"") && in.endsWith("\"")) {
                return in.substring(1, in.length()-1);
            }
        }
        return in;
    }

    // the pattern we hope to see in the output
    static final Pattern ArgPattern = Pattern.compile("\\s*argv\\[[0-9]*\\].*=.*");

    void checkArgumentParsing(String inArgs, String... expArgs) throws IOException {
        List<String> scratchpad = new ArrayList<>();
        scratchpad.add("set " + JLDEBUG_KEY + "=true");
        // GAK, -version needs to be added so that windows can flush its stderr
        // exiting the process prematurely can terminate the stderr.
        scratchpad.add(javaCmd + " -version " + inArgs);
        File batFile = new File("atest.bat");
        createAFile(batFile, scratchpad);

        TestResult tr = doExec(batFile.getName());

        ArrayList<String> expList = new ArrayList<>();
        expList.add(javaCmd);
        expList.add("-version");
        expList.addAll(Arrays.asList(expArgs));

        List<String> gotList = new ArrayList<>();
        for (String x : tr.testOutput) {
            Matcher m = ArgPattern.matcher(x);
            if (m.matches()) {
                String a[] = x.split("=");
                gotList.add(a[a.length - 1].trim());
            }
        }
        if (!gotList.equals(expList)) {
            System.out.println(tr);
            System.out.println("Expected args:");
            System.out.println(expList);
            System.out.println("Obtained args:");
            System.out.println(gotList);
            throw new RuntimeException("Error: args do not match");
        }
        System.out.println("\'" + inArgs + "\'" + " - Test passed");
    }

    /*
     * This tests general quoting and are specific to Windows, *nixes
     * need not worry about this, these have been tested with Windows
     * implementation and those that are known to work are used against
     * the java implementation. Note that the ProcessBuilder gets in the
     * way when testing some of these arguments, therefore we need to
     * create and execute a .bat file containing the arguments.
     */
    @Test
    void testArgumentParsing() throws IOException {
        if (!isWindows)
            return;
        // no quotes
        checkArgumentParsing("a b c d", "a", "b", "c", "d");

        // single quotes
        checkArgumentParsing("\"a b c d\"", "a b c d");

        //double quotes
        checkArgumentParsing("\"\"a b c d\"\"", "a", "b", "c", "d");

        // triple quotes
        checkArgumentParsing("\"\"\"a b c d\"\"\"", "\"a b c d\"");

        // a literal within single quotes
        checkArgumentParsing("\"a\"b c d\"e\"", "ab", "c", "de");

        // a literal within double quotes
        checkArgumentParsing("\"\"a\"b c d\"e\"\"", "ab c de");

        // a literal quote
        checkArgumentParsing("a\\\"b", "a\"b");

        // double back-slash
        checkArgumentParsing("\"a b c d\\\\\"", "a b c d\\");

        // triple back-slash
        checkArgumentParsing("a\\\\\\\"b", "a\\\"b");

        // dangling quote
        checkArgumentParsing("\"a b c\"\"", "a b c\"");

        // expansions of white space separators
        checkArgumentParsing("a b", "a", "b");
        checkArgumentParsing("a\tb", "a", "b");
        checkArgumentParsing("a \t b", "a", "b");

        checkArgumentParsing("\"C:\\TEST A\\\\\"", "C:\\TEST A\\");
        checkArgumentParsing("\"\"C:\\TEST A\\\\\"\"", "C:\\TEST", "A\\");

        // MS Windows tests
        // triple back-slash
        checkArgumentParsing("a\\\\\\d", "a\\\\\\d");

        // triple back-slash in quotes
        checkArgumentParsing("\"a\\\\\\d\"", "a\\\\\\d");

        // slashes separating characters
        checkArgumentParsing("X\\Y\\Z", "X\\Y\\Z");
        checkArgumentParsing("\\X\\Y\\Z", "\\X\\Y\\Z");

        // literals within dangling quotes, etc.
        checkArgumentParsing("\"a b c\" d e", "a b c", "d", "e");
        checkArgumentParsing("\"ab\\\"c\"  \"\\\\\"  d", "ab\"c", "\\", "d");
        checkArgumentParsing("a\\\\\\c d\"e f\"g h", "a\\\\\\c", "de fg", "h");
        checkArgumentParsing("a\\\\\\\"b c d", "a\\\"b", "c", "d");
        checkArgumentParsing("a\\\\\\\\\"g c\" d e", "a\\\\g c", "d", "e");

        // treatment of back-slashes
        checkArgumentParsing("*\\", "*\\");
        checkArgumentParsing("*/", "*/");
        checkArgumentParsing(".\\*", ".\\*");
        checkArgumentParsing("./*", "./*");
        checkArgumentParsing("..\\..\\*", "..\\..\\*");
        checkArgumentParsing("../../*", "../../*");
        checkArgumentParsing("..\\..\\", "..\\..\\");
        checkArgumentParsing("../../", "../../");
        checkArgumentParsing("a b\\ c", "a", "b\\", "c");
        // 2 back-slashes
        checkArgumentParsing("\\\\?", "\\\\?");
        // 3 back-slashes
        checkArgumentParsing("\\\\\\?", "\\\\\\?");
        // 4 back-slashes
        checkArgumentParsing("\\\\\\\\?", "\\\\\\\\?");
        // 5 back-slashes
        checkArgumentParsing("\\\\\\\\\\?", "\\\\\\\\\\?");
        // 6 back-slashes
        checkArgumentParsing("\\\\\\\\\\\\?", "\\\\\\\\\\\\?");

        // more treatment of  mixed slashes
        checkArgumentParsing("f1/ f3\\ f4/", "f1/", "f3\\", "f4/");
        checkArgumentParsing("f1/ f2\' ' f3/ f4/", "f1/", "f2\'", "'", "f3/", "f4/");

        checkArgumentParsing("a\\*\\b", "a\\*\\b");
    }

    private void initEmptyDir(File emptyDir) throws IOException {
        if (emptyDir.exists()) {
            recursiveDelete(emptyDir);
        }
        emptyDir.mkdir();
    }

    private void initDirWithJavaFiles(File libDir) throws IOException {

        if (libDir.exists()) {
            recursiveDelete(libDir);
        }
        libDir.mkdirs();
        ArrayList<String> scratchpad = new ArrayList<>();
        scratchpad.add("package lib;");
        scratchpad.add("public class Fbo {");
        scratchpad.add("public static void main(String... args){Foo.f();}");
        scratchpad.add("public static void f(){}");
        scratchpad.add("}");
        createFile(new File(libDir, "Fbo.java"), scratchpad);

        scratchpad.clear();
        scratchpad.add("package lib;");
        scratchpad.add("public class Foo {");
        scratchpad.add("public static void main(String... args){");
        scratchpad.add("for (String x : args) {");
        scratchpad.add("System.out.println(x);");
        scratchpad.add("}");
        scratchpad.add("Fbo.f();");
        scratchpad.add("}");
        scratchpad.add("public static void f(){}");
        scratchpad.add("}");
        createFile(new File(libDir, "Foo.java"), scratchpad);
    }

    void checkArgumentWildcard(String inArgs, String... expArgs) throws IOException {
        String[] in = {inArgs};
        checkArgumentWildcard(in, expArgs);

        // now add arbitrary arguments before and after
        String[] outInArgs = { "-Q", inArgs, "-R"};

        String[] outExpArgs = new String[expArgs.length + 2];
        outExpArgs[0] = "-Q";
        System.arraycopy(expArgs, 0, outExpArgs, 1, expArgs.length);
        outExpArgs[expArgs.length + 1] = "-R";
        checkArgumentWildcard(outInArgs, outExpArgs);
    }

    void checkArgumentWildcard(String[] inArgs, String[] expArgs) throws IOException {
        ArrayList<String> argList = new ArrayList<>();
        argList.add(javaCmd);
        argList.add("-cp");
        argList.add("lib" + File.separator + "*");
        argList.add("lib.Foo");
        argList.addAll(Arrays.asList(inArgs));
        String[] cmds = new String[argList.size()];
        argList.toArray(cmds);
        TestResult tr = doExec(cmds);
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("Error: classpath single entry wildcard entry");
        }

        ArrayList<String> expList = new ArrayList<>();
        expList.addAll(Arrays.asList(expArgs));

        List<String> gotList = new ArrayList<>();
        for (String x : tr.testOutput) {
            gotList.add(x.trim());
        }
        if (!gotList.equals(expList)) {
            System.out.println(tr);
            System.out.println("Expected args:");
            System.out.println(expList);
            System.out.println("Obtained args:");
            System.out.println(gotList);
            throw new RuntimeException("Error: args do not match");
        }
        System.out.print("\'");
        for (String x : inArgs) {
            System.out.print(x + " ");
        }
        System.out.println("\'" + " - Test passed");
    }

    /*
     * These tests are not expected to work on *nixes, and are ignored.
     */
    @Test
    void testWildCardArgumentProcessing() throws IOException {
        if (!isWindows)
            return;
        File cwd = new File(".");
        File libDir = new File(cwd, "lib");
        initDirWithJavaFiles(libDir);
        initEmptyDir(new File(cwd, "empty"));

        // test if javac (the command) can compile *.java
        TestResult tr = doExec(javacCmd, libDir.getName() + File.separator + "*.java");
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("Error: compiling java wildcards");
        }

        // test if javac (the command) can compile *.java with a vmoption
        tr = doExec(javacCmd, "-cp", ".",
                    "-J-showversion", "-J-Dsomeproperty=foo",
                    libDir.getName() + File.separator + "*.java");
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("Error: compiling java wildcards with vmoptions");
        }


        // use the jar cmd to create jars using the ? wildcard
        File jarFoo = new File(libDir, "Foo.jar");
        tr = doExec(jarCmd, "cvf", jarFoo.getAbsolutePath(), "lib" + File.separator + "F?o.class");
        if (!tr.isOK()) {
            System.out.println(tr);
            throw new RuntimeException("Error: creating jar with wildcards");
        }

        // now the litmus test!, this should work
        checkArgumentWildcard("a", "a");

        // test for basic expansion
        checkArgumentWildcard("lib\\F*java", "lib\\Fbo.java", "lib\\Foo.java");

        // basic expansion in quotes
        checkArgumentWildcard("\"lib\\F*java\"", "lib\\F*java");

        checkArgumentWildcard("lib\\**", "lib\\Fbo.class", "lib\\Fbo.java",
                              "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");

        checkArgumentWildcard("lib\\*?", "lib\\Fbo.class", "lib\\Fbo.java",
                              "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");

        checkArgumentWildcard("lib\\?*", "lib\\Fbo.class", "lib\\Fbo.java",
                "lib\\Foo.class", "lib\\Foo.jar", "lib\\Foo.java");

        checkArgumentWildcard("lib\\?", "lib\\?");

        // test for basic expansion
        checkArgumentWildcard("lib\\*java", "lib\\Fbo.java", "lib\\Foo.java");

        // basic expansion in quotes
        checkArgumentWildcard("\"lib\\*.java\"", "lib\\*.java");

        // suffix expansion
        checkArgumentWildcard("lib\\*.class", "lib\\Fbo.class", "lib\\Foo.class");

        // suffix expansion in quotes
        checkArgumentWildcard("\"lib\\*.class\"", "lib\\*.class");

        // check for ? expansion now
        checkArgumentWildcard("lib\\F?o.java", "lib\\Fbo.java", "lib\\Foo.java");

        // check ? in quotes
        checkArgumentWildcard("\"lib\\F?o.java\"", "lib\\F?o.java");

        // check ? as suffixes
        checkArgumentWildcard("lib\\F?o.????", "lib\\Fbo.java", "lib\\Foo.java");

        // check ? in a leading role
        checkArgumentWildcard("lib\\???.java", "lib\\Fbo.java", "lib\\Foo.java");
        checkArgumentWildcard("\"lib\\???.java\"", "lib\\???.java");

        // check ? prefixed with -
        checkArgumentWildcard("-?", "-?");

        // check * prefixed with -
        checkArgumentWildcard("-*", "-*");

        // check on empty directory
        checkArgumentWildcard("empty\\*", "empty\\*");
        checkArgumentWildcard("empty\\**", "empty\\**");
        checkArgumentWildcard("empty\\?", "empty\\?");
        checkArgumentWildcard("empty\\??", "empty\\??");
        checkArgumentWildcard("empty\\*?", "empty\\*?");
        checkArgumentWildcard("empty\\?*", "empty\\?*");

        // 8132379: java should not filter out -J options for application
        String[] args = { "-J-one", "-Jtwo", "lib\\???.java", "-J-Dsomething",
           "a", "-J-Dlast.arg" };
        String[] expected = { "-J-one", "-Jtwo", "lib\\Fbo.java",
           "lib\\Foo.java", "-J-Dsomething", "a", "-J-Dlast.arg" };
        checkArgumentWildcard(args, expected);
    }

    void doArgumentCheck(String inArgs, String... expArgs) {
        Map<String, String> env = new HashMap<>();
        env.put(JLDEBUG_KEY, "true");
        TestResult tr = doExec(env, javaCmd, inArgs);
        System.out.println(tr);
        int sindex = tr.testOutput.indexOf("Command line args:");
        if (sindex < 0) {
            System.out.println(tr);
            throw new RuntimeException("Error: no output");
        }
        sindex++; // skip over the tag
        List<String> gotList = new ArrayList<>();
        for (String x : tr.testOutput.subList(sindex, sindex + expArgs.length)) {
            String a[] = x.split("=");
            gotList.add(a[a.length - 1].trim());
        }
        List<String> expList = Arrays.asList(expArgs);
        if (!gotList.equals(expList)) {
            System.out.println(tr);
            System.out.println("Expected args:");
            System.out.println(expList);
            System.out.println("Obtained args:");
            System.out.println(gotList);
            throw new RuntimeException("Error: args do not match");
        }
    }


    /*
     * These tests are usually run on non-existent targets to check error results
     */
    @Test
    void testBasicErrorMessages() {
        // Tests for 5030233
        TestResult tr = doExec(javaCmd, "-cp");
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        tr = doExec(javaCmd, "-classpath");
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        tr = doExec(javaCmd, "-jar");
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        tr = doExec(javacCmd, "-cp");
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        // Test for 6356475 "REGRESSION:"java -X" from cmdline fails"
        tr = doExec(javaCmd, "-X");
        tr.checkPositive();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        tr = doExec(javaCmd, "-help");
        tr.checkPositive();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        // 6753938, test for non-negative exit value for an incorrectly formed
        // command line,  '% java'
        tr = doExec(javaCmd);
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        // 6753938, test for non-negative exit value for an incorrectly formed
        // command line,  '% java -Xcomp'
        tr = doExec(javaCmd, "-Xcomp");
        tr.checkNegative();
        tr.isNotZeroOutput();
        if (!tr.testStatus)
            System.out.println(tr);

        // 7151434, test for non-negative exit value for an incorrectly formed
        // command line, '% java -jar -W', note the bogus -W
        tr = doExec(javaCmd, "-jar", "-W");
        tr.checkNegative();
        tr.contains("Unrecognized option: -W");
        if (!tr.testStatus)
            System.out.println(tr);
    }

    /*
     * Tests various dispositions of the main method, these tests are limited
     * to English locales as they check for error messages that are localized.
     */
    @Test
    void testMainMethod() throws FileNotFoundException {
        if (!isEnglishLocale()) {
            return;
        }

        TestResult tr;

        // a missing class
        createJar("MIA", new File("some.jar"), new File("Foo"),
                (String[])null);
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("Error: Could not find or load main class MIA");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "MIA");
        tr.contains("Error: Could not find or load main class MIA");
        if (!tr.testStatus)
            System.out.println(tr);

        // incorrect method access
        createJar(new File("some.jar"), new File("Foo"),
                "private static void main(String[] args){}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("Error: Main method not found in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
        tr.contains("Error: Main method not found in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);

        // incorrect return type
        createJar(new File("some.jar"), new File("Foo"),
                "public static int main(String[] args){return 1;}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("Error: Main method must return a value of type void in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
        tr.contains("Error: Main method must return a value of type void in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);

        // incorrect parameter type
        createJar(new File("some.jar"), new File("Foo"),
                "public static void main(Object[] args){}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("Error: Main method not found in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
        tr.contains("Error: Main method not found in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);

        // incorrect method type - non-static
         createJar(new File("some.jar"), new File("Foo"),
                "public void main(String[] args){}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("Error: Main method is not static in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
        tr.contains("Error: Main method is not static in class Foo");
        if (!tr.testStatus)
            System.out.println(tr);

        // amongst a potpourri of kindred main methods, is the right one chosen ?
        createJar(new File("some.jar"), new File("Foo"),
            "void main(Object[] args){}",
            "int  main(Float[] args){return 1;}",
            "private void main() {}",
            "private static void main(int x) {}",
            "public int main(int argc, String[] argv) {return 1;}",
            "public static void main(String[] args) {System.out.println(\"THE_CHOSEN_ONE\");}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.contains("THE_CHOSEN_ONE");
        if (!tr.testStatus)
            System.out.println(tr);
        // use classpath to check
        tr = doExec(javaCmd, "-cp", "some.jar", "Foo");
        tr.contains("THE_CHOSEN_ONE");
        if (!tr.testStatus)
            System.out.println(tr);

        // test for extraneous whitespace in the Main-Class attribute
        createJar(" Foo ", new File("some.jar"), new File("Foo"),
                "public static void main(String... args){}");
        tr = doExec(javaCmd, "-jar", "some.jar");
        tr.checkPositive();
        if (!tr.testStatus)
            System.out.println(tr);
    }
    /*
     * tests 6968053, ie. we turn on the -Xdiag (for now) flag and check if
     * the suppressed stack traces are exposed, ignore these tests for localized
     * locales, limiting to English only.
     */
    @Test
    void testDiagOptions() throws FileNotFoundException {
        if (!isEnglishLocale()) { // only english version
            return;
        }
        TestResult tr;
        // a missing class
        createJar("MIA", new File("some.jar"), new File("Foo"),
                (String[])null);
        tr = doExec(javaCmd, "-Xdiag", "-jar", "some.jar");
        tr.contains("Error: Could not find or load main class MIA");
        tr.contains("java.lang.ClassNotFoundException: MIA");
        if (!tr.testStatus)
            System.out.println(tr);

        // use classpath to check
        tr = doExec(javaCmd,  "-Xdiag", "-cp", "some.jar", "MIA");
        tr.contains("Error: Could not find or load main class MIA");
        tr.contains("java.lang.ClassNotFoundException: MIA");
        if (!tr.testStatus)
            System.out.println(tr);

        // a missing class on the classpath
        tr = doExec(javaCmd, "-Xdiag", "NonExistentClass");
        tr.contains("Error: Could not find or load main class NonExistentClass");
        tr.contains("java.lang.ClassNotFoundException: NonExistentClass");
        if (!tr.testStatus)
            System.out.println(tr);
    }

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */
    public static void main(String[] args) throws Exception {
        if (debug) {
            System.out.println("Starting Arrrghs tests");
        }
        Arrrghs a = new Arrrghs();
        a.run(args);
        if (testExitValue > 0) {
            System.out.println("Total of " + testExitValue + " failed");
            System.exit(1);
        } else {
            System.out.println("All tests pass");
        }
    }
}