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