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