Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/cmdLineTests/shareClassTests/utils/src/Utilities/RunCommand.java
6005 views
1
/*******************************************************************************
2
* Copyright (c) 2005, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package Utilities;
23
24
import java.io.InputStream;
25
26
import java.util.*;
27
import java.io.*;
28
29
/**
30
* Instances of StreamGobbler are used to capture output from the spawned process
31
*/
32
class StreamGobbler extends Thread {
33
private InputStream is; // Can be used for stderr/stdout
34
private String type; // some identifier tag for this gobbler (possibly STDERR or STDOUT)
35
36
private List expected = new ArrayList(); // Expected lines of output to be found in the gobbled output
37
38
private StringBuffer gobbledData = new StringBuffer();
39
private List lines = new ArrayList();
40
public boolean finished = false;
41
42
StreamGobbler(InputStream is, String type, String[] expected) {
43
this.is = is;
44
this.type= type;
45
if (expected!=null) {
46
for (int i = 0; i < expected.length; i++) {
47
String string = expected[i];
48
if (string!=null)
49
this.expected.add(string);
50
}
51
}
52
}
53
54
public String getType() { return type; }
55
56
public void run() {
57
try {
58
InputStreamReader isr = new InputStreamReader(is);
59
BufferedReader br = new BufferedReader(isr);
60
String line=null;
61
while ( (line = br.readLine()) != null) {
62
// System.out.println(type + ">" + line);
63
gobbledData.append(/*type + ">" + */line+"\n");
64
lines.add(line);
65
int pos = 0;int found =-1;
66
for (Iterator iterator = expected.iterator(); iterator
67
.hasNext();) {
68
String name = (String) iterator.next();
69
if (line.indexOf(name)!=-1) {found = pos;break;}
70
pos++;
71
}
72
if (found!=-1) expected.remove(found);
73
}
74
finished=true;
75
} catch (IOException ioe) {
76
ioe.printStackTrace();
77
}
78
}
79
80
public boolean allExpectedMessagesWereFound() {
81
return expected.isEmpty();
82
}
83
84
public void dumpOutput() {
85
System.out.println(gobbledData.toString());
86
}
87
88
public String getOutput() {
89
return gobbledData.toString();
90
}
91
92
public String[] getOutputAsLines() {
93
return (String[])lines.toArray(new String[]{});
94
}
95
}
96
97
public class RunCommand {
98
public static String lastCommand;
99
public static String lastCommandStdout;
100
public static String[] lastCommandStdoutLines;
101
public static String lastCommandStderr;
102
public static String[] lastCommandStderrLines;
103
public static boolean logCommands = false;
104
public static int processRunawayTimeout = 120000; // 2 min timer, kill process if its taking longer!
105
106
public static void execute(String cmd) {
107
execute(cmd,(String[])null,(String[])null,true);
108
}
109
110
public static void execute(String cmd,boolean careAboutExitValue) {
111
execute(cmd,(String[])null,(String[])null,careAboutExitValue);
112
}
113
114
public static void execute(String cmd,String sysoutMessages,String syserrMessages,boolean careAboutExitValue) {
115
execute(cmd,new String[]{sysoutMessages},new String[]{syserrMessages},careAboutExitValue);
116
}
117
118
public static void execute(String cmd,String[] expectedSysoutMessages,String[] expectedSyserrMessages,boolean careAboutExitValue)
119
{
120
if (cmd==null) throw new RuntimeException("RunCommand.execute() - dont ask it to run a null command");
121
int exitVal = 0;
122
Runtime rt = Runtime.getRuntime();
123
lastCommand = null;
124
lastCommandStdout = null;lastCommandStderr = null;
125
lastCommandStdoutLines = null;lastCommandStderrLines = null;
126
try {
127
if (logCommands)
128
System.out.println("Executing command: "+cmd);
129
long processStartTime = System.currentTimeMillis();
130
Process proc = rt.exec(cmd);
131
// any error message?
132
StreamGobbler errorGobbler = new
133
StreamGobbler(proc.getErrorStream(), "ERROR",expectedSyserrMessages);
134
135
// any output?
136
StreamGobbler outputGobbler = new
137
StreamGobbler(proc.getInputStream(), "OUTPUT",expectedSysoutMessages);
138
139
// kick them off
140
errorGobbler.start();
141
outputGobbler.start();
142
143
// any error???
144
exitVal = proc.waitFor();
145
146
while (!outputGobbler.finished || !errorGobbler.finished) {
147
try { Thread.sleep(250); } catch (Exception e) {}
148
if ((System.currentTimeMillis()-processStartTime)>processRunawayTimeout) {
149
// kill the process, it is taking too long!
150
proc.destroy();
151
System.err.println("Process killed, was executing for longer than "+(processRunawayTimeout/1000)+"seconds");
152
}
153
}
154
lastCommandStdout = outputGobbler.getOutput();
155
lastCommandStderr = errorGobbler.getOutput();
156
lastCommandStdoutLines = outputGobbler.getOutputAsLines();
157
lastCommandStderrLines = errorGobbler.getOutputAsLines();
158
lastCommand = cmd;
159
if (!outputGobbler.allExpectedMessagesWereFound()) {
160
outputGobbler.dumpOutput();
161
errorGobbler.dumpOutput();
162
throw new RuntimeException("Not all expected messages were found");
163
}
164
if (!errorGobbler.allExpectedMessagesWereFound()) {
165
outputGobbler.dumpOutput();
166
errorGobbler.dumpOutput();
167
throw new RuntimeException("Not all expected messages were found");
168
}
169
//System.out.println("ExitValue: " + exitVal);
170
if (careAboutExitValue && exitVal!=0) {
171
outputGobbler.dumpOutput();
172
errorGobbler.dumpOutput();
173
throw new RuntimeException("TestFailed: exitvalue="+exitVal);
174
}
175
} catch (IOException ioe) {
176
throw new RuntimeException("Problem invoking command: "+ioe.toString());
177
} catch (InterruptedException ie) {
178
throw new RuntimeException("Problem invoking command: "+ie.toString());
179
}
180
181
}
182
}
183
184