jdk/test/java/lang/invoke/lambda/LUtils.java
changeset 16062 c64ef2b01401
child 23010 6dadb192ad81
equal deleted inserted replaced
16061:c80133bafef0 16062:c64ef2b01401
       
     1 /*
       
     2  * Copyright (c) 2012, 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.BufferedReader;
       
    25 import java.io.File;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStreamReader;
       
    28 import java.io.PrintWriter;
       
    29 import java.io.StringWriter;
       
    30 import java.nio.charset.Charset;
       
    31 import java.nio.file.Files;
       
    32 import java.util.ArrayList;
       
    33 import java.util.List;
       
    34 import java.util.Map;
       
    35 
       
    36 /*
       
    37  * support infrastructure to invoke a java class from the command line
       
    38  */
       
    39 class LUtils {
       
    40     static final sun.tools.jar.Main jarTool =
       
    41             new sun.tools.jar.Main(System.out, System.err, "jar-tool");
       
    42     static final com.sun.tools.javac.Main javac =
       
    43             new com.sun.tools.javac.Main();
       
    44     static final File cwd = new File(".").getAbsoluteFile();
       
    45     static final String JAVAHOME = System.getProperty("java.home");
       
    46     static final boolean isWindows =
       
    47             System.getProperty("os.name", "unknown").startsWith("Windows");
       
    48     //static final boolean isSDK = JAVAHOME.endsWith("jre");
       
    49     static final File JAVA_BIN_FILE = new File(JAVAHOME, "bin");
       
    50     static final File JAVA_CMD = new File(JAVA_BIN_FILE,
       
    51             isWindows ? "java.exe" : "java");
       
    52 
       
    53     protected LUtils() {
       
    54     }
       
    55 
       
    56     public static void compile(String... args) {
       
    57         if (javac.compile(args) != 0) {
       
    58             throw new RuntimeException("compilation fails");
       
    59         }
       
    60     }
       
    61 
       
    62     static void createFile(File outFile, List<String> content) {
       
    63         try {
       
    64             Files.write(outFile.getAbsoluteFile().toPath(), content,
       
    65                     Charset.defaultCharset());
       
    66         } catch (IOException ex) {
       
    67             throw new RuntimeException(ex);
       
    68         }
       
    69     }
       
    70 
       
    71     static File getClassFile(File javaFile) {
       
    72         return javaFile.getName().endsWith(".java")
       
    73                 ? new File(javaFile.getName().replace(".java", ".class"))
       
    74                 : null;
       
    75     }
       
    76 
       
    77     static String getSimpleName(File inFile) {
       
    78         String fname = inFile.getName();
       
    79         return fname.substring(0, fname.indexOf("."));
       
    80     }
       
    81 
       
    82     static TestResult doExec(String... cmds) {
       
    83         return doExec(null, null, cmds);
       
    84     }
       
    85 
       
    86     /*
       
    87      * A method which executes a java cmd and returns the results in a container
       
    88      */
       
    89     static TestResult doExec(Map<String, String> envToSet,
       
    90             java.util.Set<String> envToRemove, String... cmds) {
       
    91         String cmdStr = "";
       
    92         for (String x : cmds) {
       
    93             cmdStr = cmdStr.concat(x + " ");
       
    94         }
       
    95         ProcessBuilder pb = new ProcessBuilder(cmds);
       
    96         Map<String, String> env = pb.environment();
       
    97         if (envToRemove != null) {
       
    98             for (String key : envToRemove) {
       
    99                 env.remove(key);
       
   100             }
       
   101         }
       
   102         if (envToSet != null) {
       
   103             env.putAll(envToSet);
       
   104         }
       
   105         BufferedReader rdr = null;
       
   106         try {
       
   107             List<String> outputList = new ArrayList<>();
       
   108             pb.redirectErrorStream(true);
       
   109             Process p = pb.start();
       
   110             rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
       
   111             String in = rdr.readLine();
       
   112             while (in != null) {
       
   113                 outputList.add(in);
       
   114                 in = rdr.readLine();
       
   115             }
       
   116             p.waitFor();
       
   117             p.destroy();
       
   118 
       
   119             return new TestResult(cmdStr, p.exitValue(), outputList,
       
   120                     env, new Throwable("current stack of the test"));
       
   121         } catch (Exception ex) {
       
   122             ex.printStackTrace();
       
   123             throw new RuntimeException(ex.getMessage());
       
   124         }
       
   125     }
       
   126 
       
   127     static class TestResult {
       
   128         String cmd;
       
   129         int exitValue;
       
   130         List<String> testOutput;
       
   131         Map<String, String> env;
       
   132         Throwable t;
       
   133 
       
   134         public TestResult(String str, int rv, List<String> oList,
       
   135                 Map<String, String> env, Throwable t) {
       
   136             cmd = str;
       
   137             exitValue = rv;
       
   138             testOutput = oList;
       
   139             this.env = env;
       
   140             this.t = t;
       
   141         }
       
   142 
       
   143         void assertZero(String message) {
       
   144             if (exitValue != 0) {
       
   145                 System.err.println(this);
       
   146                 throw new RuntimeException(message);
       
   147             }
       
   148         }
       
   149 
       
   150         @Override
       
   151         public String toString() {
       
   152             StringWriter sw = new StringWriter();
       
   153             PrintWriter status = new PrintWriter(sw);
       
   154             status.println("Cmd: " + cmd);
       
   155             status.println("Return code: " + exitValue);
       
   156             status.println("Environment variable:");
       
   157             for (String x : env.keySet()) {
       
   158                 status.println("\t" + x + "=" + env.get(x));
       
   159             }
       
   160             status.println("Output:");
       
   161             for (String x : testOutput) {
       
   162                 status.println("\t" + x);
       
   163             }
       
   164             status.println("Exception:");
       
   165             status.println(t.getMessage());
       
   166             t.printStackTrace(status);
       
   167 
       
   168             return sw.getBuffer().toString();
       
   169         }
       
   170     }
       
   171 }