langtools/test/tools/javac/lib/CompileFail.java
changeset 10202 f32c7f2c2219
child 10638 c8e9604cf151
equal deleted inserted replaced
10201:52da97a48f81 10202:f32c7f2c2219
       
     1 /*
       
     2  * Copyright (c) 2011, 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 import java.io.*;
       
    25 import java.util.*;
       
    26 
       
    27 /*
       
    28  * Utility class to emulate jtreg @compile/fail, but also checking the specific
       
    29  * exit code, given as the first arg.
       
    30  */
       
    31 public class CompileFail {
       
    32     public static void main(String... args) {
       
    33         if (args.length < 2)
       
    34             throw new IllegalArgumentException("insufficient args");
       
    35         int expected_rc = getReturnCode(args[0]);
       
    36 
       
    37         List<String> javacArgs = new ArrayList<>();
       
    38         javacArgs.addAll(Arrays.asList(
       
    39             "-bootclasspath", System.getProperty("sun.boot.class.path"),
       
    40             "-d", "."
       
    41         ));
       
    42 
       
    43         File testSrc = new File(System.getProperty("test.src"));
       
    44         for (int i = 1; i < args.length; i++) { // skip first arg
       
    45             String arg = args[i];
       
    46             if (arg.endsWith(".java"))
       
    47                 javacArgs.add(new File(testSrc, arg).getPath());
       
    48             else
       
    49                 javacArgs.add(arg);
       
    50         }
       
    51 
       
    52         int rc = com.sun.tools.javac.Main.compile(
       
    53             javacArgs.toArray(new String[javacArgs.size()]));
       
    54 
       
    55         if (rc != expected_rc)
       
    56             throw new Error("unexpected exit code: " + rc
       
    57                         + ", expected: " + expected_rc);
       
    58     }
       
    59 
       
    60     static int getReturnCode(String name) {
       
    61         switch (name) {
       
    62             case "OK":
       
    63                 return EXIT_OK;
       
    64 
       
    65             case "ERROR":
       
    66                 return EXIT_ERROR;
       
    67 
       
    68             case "CMDERR":
       
    69                 return EXIT_CMDERR;
       
    70 
       
    71             case "SYSERR":
       
    72                 return EXIT_SYSERR;
       
    73 
       
    74             case "ABNORMAL":
       
    75                 return EXIT_ABNORMAL;
       
    76 
       
    77             default:
       
    78                 throw new IllegalArgumentException(name);
       
    79         }
       
    80     }
       
    81 
       
    82     // The following is cut-n-paste from com.sun.tools.javac.main.Main
       
    83     static final int
       
    84         EXIT_OK = 0,        // Compilation completed with no errors.
       
    85         EXIT_ERROR = 1,     // Completed but reported errors.
       
    86         EXIT_CMDERR = 2,    // Bad command-line arguments
       
    87         EXIT_SYSERR = 3,    // System error or resource exhaustion.
       
    88         EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
       
    89 }