Path: blob/master/test/jdk/javax/net/ssl/TLSCommon/interop/ProcUtils.java
41154 views
/*1* Copyright (c) 2020, 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.IOException;24import java.nio.file.Path;25import java.util.ArrayList;26import java.util.Arrays;27import java.util.List;28import java.util.Map;2930import jdk.test.lib.process.OutputAnalyzer;31import jdk.test.lib.process.ProcessTools;3233/*34* Utilities for process operations.35*/36public class ProcUtils {3738/*39* Executes java program.40* After the program finishes, it returns OutputAnalyzer.41*/42public static OutputAnalyzer java(Path javaPath, Class<?> clazz,43Map<String, String> props) {44ProcessBuilder pb = createJavaProcessBuilder(javaPath, clazz, props);45try {46return ProcessTools.executeCommand(pb);47} catch (Throwable e) {48throw new RuntimeException("Executes java program failed!", e);49}50}5152private static ProcessBuilder createJavaProcessBuilder(Path javaPath,53Class<?> clazz, Map<String, String> props) {54List<String> cmds = new ArrayList<>();55cmds.add(javaPath.toString());5657if (props != null) {58for (Map.Entry<String, String> prop : props.entrySet()) {59cmds.add("-D" + prop.getKey() + "=" + prop.getValue());60}61}6263cmds.add("-cp");64cmds.add(System.getProperty("test.class.path"));65cmds.add(clazz.getName());66ProcessBuilder pb = new ProcessBuilder(cmds);67pb.redirectErrorStream(true);68return pb;69}7071/*72* Executes a shell command and return a OutputAnalyzer wrapping the process.73*/74public static OutputAnalyzer shell(String command, Map<String, String> env)75throws IOException {76Process process = shellProc(command, null, env);77return getProcessOutput(process);78}7980/*81* Executes win command and return a OutputAnalyzer wrapping the process.82*/83public static OutputAnalyzer win(String command, Map<String, String> env)84throws IOException {85Process process = winProc(command, null, env);86return getProcessOutput(process);87}8889/*90* Executes a shell command and return the process.91*/92public static Process shellProc(String command, Path outputPath,93Map<String, String> env) throws IOException {94String[] cmds = new String[3];95cmds[0] = "sh";96cmds[1] = "-c";97cmds[2] = command;98return startAndGetProc(cmds, outputPath, env);99}100101/*102* Executes a win command and returns the process.103*/104public static Process winProc(String command, Path outputPath,105Map<String, String> env)106throws IOException {107String[] cmds = new String[3];108cmds[0] = "cmd.exe";109cmds[1] = "/C";110cmds[2] = command;111return startAndGetProc(cmds, outputPath, env);112}113114/*115* Returns a OutputAnalyzer wrapping the process.116*/117private static OutputAnalyzer getProcessOutput (Process process) throws IOException {118OutputAnalyzer oa = new OutputAnalyzer(process);119try {120process.waitFor();121return oa;122} catch (InterruptedException e) {123throw new RuntimeException("Process is interrupted!", e);124}125}126127/*128* Executes a command, redirects the output to a local file and returns the process.129*/130private static Process startAndGetProc(String[] cmds, Path outputPath, Map<String,131String> env) throws IOException {132System.out.println("command to run: " + Arrays.toString(cmds));133ProcessBuilder pb = new ProcessBuilder(cmds);134if (env != null) {135pb.environment().putAll(env);136}137pb.redirectErrorStream(true);138if (outputPath != null) {139pb.redirectOutput(outputPath.toFile());140}141return pb.start();142}143}144145146