Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/LUtils.java
48795 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.File;25import java.io.IOException;26import java.io.InputStreamReader;27import java.io.PrintWriter;28import java.io.StringWriter;29import java.nio.charset.Charset;30import java.nio.file.Files;31import java.util.ArrayList;32import java.util.List;33import java.util.Map;3435/*36* support infrastructure to invoke a java class from the command line37*/38class LUtils {39static final sun.tools.jar.Main jarTool =40new sun.tools.jar.Main(System.out, System.err, "jar-tool");41static final com.sun.tools.javac.Main javac =42new com.sun.tools.javac.Main();43static final File cwd = new File(".").getAbsoluteFile();44static final String JAVAHOME = System.getProperty("java.home");45static final boolean isWindows =46System.getProperty("os.name", "unknown").startsWith("Windows");47//static final boolean isSDK = JAVAHOME.endsWith("jre");48static final File JAVA_BIN_FILE = new File(JAVAHOME, "bin");49static final File JAVA_CMD = new File(JAVA_BIN_FILE,50isWindows ? "java.exe" : "java");5152protected LUtils() {53}5455public static void compile(String... args) {56if (javac.compile(args) != 0) {57throw new RuntimeException("compilation fails");58}59}6061static void createFile(File outFile, List<String> content) {62try {63Files.write(outFile.getAbsoluteFile().toPath(), content,64Charset.defaultCharset());65} catch (IOException ex) {66throw new RuntimeException(ex);67}68}6970static File getClassFile(File javaFile) {71return javaFile.getName().endsWith(".java")72? new File(javaFile.getName().replace(".java", ".class"))73: null;74}7576static String getSimpleName(File inFile) {77String fname = inFile.getName();78return fname.substring(0, fname.indexOf("."));79}8081static TestResult doExec(String... cmds) {82return doExec(null, null, cmds);83}8485/*86* A method which executes a java cmd and returns the results in a container87*/88static TestResult doExec(Map<String, String> envToSet,89java.util.Set<String> envToRemove, String... cmds) {90String cmdStr = "";91for (String x : cmds) {92cmdStr = cmdStr.concat(x + " ");93}94ProcessBuilder pb = new ProcessBuilder(cmds);95Map<String, String> env = pb.environment();96if (envToRemove != null) {97for (String key : envToRemove) {98env.remove(key);99}100}101if (envToSet != null) {102env.putAll(envToSet);103}104BufferedReader rdr = null;105try {106List<String> outputList = new ArrayList<>();107pb.redirectErrorStream(true);108Process p = pb.start();109rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));110String in = rdr.readLine();111while (in != null) {112outputList.add(in);113in = rdr.readLine();114}115p.waitFor();116p.destroy();117118return new TestResult(cmdStr, p.exitValue(), outputList,119env, new Throwable("current stack of the test"));120} catch (Exception ex) {121ex.printStackTrace();122throw new RuntimeException(ex.getMessage());123}124}125126static class TestResult {127String cmd;128int exitValue;129List<String> testOutput;130Map<String, String> env;131Throwable t;132133public TestResult(String str, int rv, List<String> oList,134Map<String, String> env, Throwable t) {135cmd = str;136exitValue = rv;137testOutput = oList;138this.env = env;139this.t = t;140}141142void assertZero(String message) {143if (exitValue != 0) {144System.err.println(this);145throw new RuntimeException(message);146}147}148149@Override150public String toString() {151StringWriter sw = new StringWriter();152PrintWriter status = new PrintWriter(sw);153status.println("Cmd: " + cmd);154status.println("Return code: " + exitValue);155status.println("Environment variable:");156for (String x : env.keySet()) {157status.println("\t" + x + "=" + env.get(x));158}159status.println("Output:");160for (String x : testOutput) {161status.println("\t" + x);162}163status.println("Exception:");164status.println(t.getMessage());165t.printStackTrace(status);166167return sw.getBuffer().toString();168}169}170}171172173