langtools/test/tools/javac/lib/CompileFail.java
author jjg
Fri, 05 Aug 2011 15:57:59 -0700
changeset 10202 f32c7f2c2219
child 10638 c8e9604cf151
permissions -rw-r--r--
7074189: some javac tests fail with latest jtreg 4.1 b03 Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10202
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     1
/*
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     4
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     8
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    13
 * accompanied this code).
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    14
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    18
 *
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    21
 * questions.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    22
 */
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    23
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    24
import java.io.*;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    25
import java.util.*;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    26
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    27
/*
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    28
 * Utility class to emulate jtreg @compile/fail, but also checking the specific
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    29
 * exit code, given as the first arg.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    30
 */
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    31
public class CompileFail {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    32
    public static void main(String... args) {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    33
        if (args.length < 2)
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    34
            throw new IllegalArgumentException("insufficient args");
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    35
        int expected_rc = getReturnCode(args[0]);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    36
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    37
        List<String> javacArgs = new ArrayList<>();
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    38
        javacArgs.addAll(Arrays.asList(
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    39
            "-bootclasspath", System.getProperty("sun.boot.class.path"),
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    40
            "-d", "."
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    41
        ));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    42
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    43
        File testSrc = new File(System.getProperty("test.src"));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    44
        for (int i = 1; i < args.length; i++) { // skip first arg
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    45
            String arg = args[i];
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    46
            if (arg.endsWith(".java"))
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    47
                javacArgs.add(new File(testSrc, arg).getPath());
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    48
            else
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    49
                javacArgs.add(arg);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    50
        }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    51
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    52
        int rc = com.sun.tools.javac.Main.compile(
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    53
            javacArgs.toArray(new String[javacArgs.size()]));
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    54
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    55
        if (rc != expected_rc)
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    56
            throw new Error("unexpected exit code: " + rc
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    57
                        + ", expected: " + expected_rc);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    58
    }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    59
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    60
    static int getReturnCode(String name) {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    61
        switch (name) {
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    62
            case "OK":
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    63
                return EXIT_OK;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    64
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    65
            case "ERROR":
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    66
                return EXIT_ERROR;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    67
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    68
            case "CMDERR":
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    69
                return EXIT_CMDERR;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    70
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    71
            case "SYSERR":
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    72
                return EXIT_SYSERR;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    73
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    74
            case "ABNORMAL":
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    75
                return EXIT_ABNORMAL;
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    76
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    77
            default:
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    78
                throw new IllegalArgumentException(name);
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    79
        }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    80
    }
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    81
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    82
    // The following is cut-n-paste from com.sun.tools.javac.main.Main
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    83
    static final int
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    84
        EXIT_OK = 0,        // Compilation completed with no errors.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    85
        EXIT_ERROR = 1,     // Completed but reported errors.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    86
        EXIT_CMDERR = 2,    // Bad command-line arguments
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    87
        EXIT_SYSERR = 3,    // System error or resource exhaustion.
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    88
        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
f32c7f2c2219 7074189: some javac tests fail with latest jtreg 4.1 b03
jjg
parents:
diff changeset
    89
}