Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/ProcessBuilder/PipelineTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2019, 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.File;
25
import java.io.FileReader;
26
import java.io.FileWriter;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.OutputStream;
30
import java.io.Reader;
31
import java.io.Writer;
32
import java.util.Arrays;
33
import java.util.List;
34
35
/*
36
* @test PipelineTest
37
* @bug 8211844
38
* @summary Tests for ProcessBuilder.startPipeline
39
*/
40
41
public class PipelineTest {
42
43
private static void realMain(String[] args) throws Throwable {
44
t1_simplePipeline();
45
t2_translatePipeline();
46
t3_redirectErrorStream();
47
t4_failStartPipeline();
48
}
49
50
/**
51
* Return a list of the varargs arguments.
52
* @param args elements to include in the list
53
* @param <T> the type of the elements
54
* @return a {@code List<T>} of the arguments
55
*/
56
@SafeVarargs
57
@SuppressWarnings("varargs")
58
static <T> List<T> asList(T... args) {
59
return Arrays.asList(args);
60
}
61
62
/**
63
* T1 - simple copy between two processes
64
*/
65
static void t1_simplePipeline() {
66
try {
67
String s1 = "Now is the time to check!";
68
verify(s1, s1,
69
asList(new ProcessBuilder("cat")));
70
verify(s1, s1,
71
asList(new ProcessBuilder("cat"),
72
new ProcessBuilder("cat")));
73
verify(s1, s1,
74
asList(new ProcessBuilder("cat"),
75
new ProcessBuilder("cat"),
76
new ProcessBuilder("cat")));
77
} catch (Throwable t) {
78
unexpected(t);
79
}
80
}
81
82
/**
83
* Pipeline that modifies the content.
84
*/
85
static void t2_translatePipeline() {
86
try {
87
String s2 = "Now is the time to check!";
88
String r2 = s2.replace('e', 'E').replace('o', 'O');
89
verify(s2, r2,
90
asList(new ProcessBuilder("tr", "e", "E"),
91
new ProcessBuilder("tr", "o", "O")));
92
} catch (Throwable t) {
93
unexpected(t);
94
}
95
}
96
97
/**
98
* Test that redirectErrorStream sends standard error of the first process
99
* to the standard output. The standard error of the first process should be empty.
100
* The standard output of the 2nd should contain the error message including the bad file name.
101
*/
102
static void t3_redirectErrorStream() {
103
try {
104
File p1err = new File("p1-test.err");
105
File p2out = new File("p2-test.out");
106
107
List<Process> processes = ProcessBuilder.startPipeline(
108
asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE")
109
.redirectErrorStream(true)
110
.redirectError(p1err),
111
new ProcessBuilder("cat").redirectOutput(p2out)));
112
waitForAll(processes);
113
114
check("".equals(fileContents(p1err)), "The first process standard error should be empty");
115
String p2contents = fileContents(p2out);
116
check(p2contents.contains("NON-EXISTENT-FILE"),
117
"The error from the first process should be in the output of the second: " + p2contents);
118
} catch (Throwable t) {
119
unexpected(t);
120
}
121
}
122
123
/**
124
* Test that no processes are left after a failed startPipeline.
125
* Test illegal combinations of redirects.
126
*/
127
static void t4_failStartPipeline() {
128
File p1err = new File("p1-test.err");
129
File p2out = new File("p2-test.out");
130
131
THROWS(IllegalArgumentException.class,
132
() -> {
133
// Test that output redirect != PIPE throws IAE
134
List<Process> processes = ProcessBuilder.startPipeline(
135
asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE1")
136
.redirectOutput(p1err),
137
new ProcessBuilder("cat")));
138
},
139
() -> {
140
// Test that input redirect != PIPE throws IAE
141
List<Process> processes = ProcessBuilder.startPipeline(
142
asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE2"),
143
new ProcessBuilder("cat").redirectInput(p2out)));
144
}
145
);
146
147
THROWS(NullPointerException.class,
148
() -> {
149
List<Process> processes = ProcessBuilder.startPipeline(
150
asList(new ProcessBuilder("cat", "a"), null));
151
},
152
() -> {
153
List<Process> processes = ProcessBuilder.startPipeline(
154
asList(null, new ProcessBuilder("cat", "b")));
155
}
156
);
157
158
THROWS(IOException.class,
159
() -> {
160
List<Process> processes = ProcessBuilder.startPipeline(
161
asList(new ProcessBuilder("cat", "c"),
162
new ProcessBuilder("NON-EXISTENT-COMMAND")));
163
});
164
165
// Check no subprocess are left behind
166
ProcessHandle.current().children().forEach(PipelineTest::print);
167
ProcessHandle.current().children()
168
.filter(p -> p.info().command().orElse("").contains("cat"))
169
.forEach(p -> fail("process should have been destroyed: " + p));
170
}
171
172
static void verify(String input, String expected, List<ProcessBuilder> builders) throws IOException {
173
File infile = new File("test.in");
174
File outfile = new File("test.out");
175
setFileContents(infile, input);
176
for (int i = 0; i < builders.size(); i++) {
177
ProcessBuilder b = builders.get(i);
178
if (i == 0) {
179
b.redirectInput(infile);
180
}
181
if (i == builders.size() - 1) {
182
b.redirectOutput(outfile);
183
}
184
}
185
List<Process> processes = ProcessBuilder.startPipeline(builders);
186
verifyProcesses(processes);
187
waitForAll(processes);
188
String result = fileContents(outfile);
189
check(result.equals(expected),
190
"result not as expected: " + result + ", expected: " + expected);
191
}
192
193
/**
194
* Wait for each of the processes to be done.
195
*
196
* @param processes the list of processes to check
197
*/
198
static void waitForAll(List<Process> processes) {
199
processes.forEach(p -> {
200
try {
201
int status = p.waitFor();
202
} catch (InterruptedException ie) {
203
unexpected(ie);
204
}
205
});
206
}
207
208
static void print(ProcessBuilder pb) {
209
if (pb != null) {
210
System.out.printf(" pb: %s%n", pb);
211
System.out.printf(" cmd: %s%n", pb.command());
212
}
213
}
214
215
static void print(ProcessHandle p) {
216
System.out.printf("process: pid: %d, info: %s%n",
217
p.pid(), p.info());
218
}
219
220
// Check various aspects of the processes
221
static void verifyProcesses(List<Process> processes) {
222
for (int i = 0; i < processes.size(); i++) {
223
Process p = processes.get(i);
224
225
if (i != 0) {
226
verifyNullStream(p.getOutputStream(), "getOutputStream");
227
}
228
if (i <= processes.size() - 1) {
229
verifyNullStream(p.getInputStream(), "getInputStream");
230
}
231
if (i == processes.size() - 1) {
232
verifyNullStream(p.getErrorStream(), "getErrorStream");
233
}
234
}
235
}
236
237
static void verifyNullStream(OutputStream s, String msg) {
238
try {
239
s.write(0xff);
240
fail("Stream should have been a NullStream: " + msg);
241
} catch (IOException ie) {
242
// expected
243
}
244
}
245
246
static void verifyNullStream(InputStream s, String msg) {
247
try {
248
int len = s.read();
249
check(len == -1, "Stream should have been a NullStream: " + msg);
250
} catch (IOException ie) {
251
// expected
252
}
253
}
254
255
static void setFileContents(File file, String contents) {
256
try {
257
Writer w = new FileWriter(file);
258
w.write(contents);
259
w.close();
260
} catch (Throwable t) { unexpected(t); }
261
}
262
263
static String fileContents(File file) {
264
try {
265
Reader r = new FileReader(file);
266
StringBuilder sb = new StringBuilder();
267
char[] buffer = new char[1024];
268
int n;
269
while ((n = r.read(buffer)) != -1)
270
sb.append(buffer,0,n);
271
r.close();
272
return new String(sb);
273
} catch (Throwable t) { unexpected(t); return ""; }
274
}
275
276
//--------------------- Infrastructure ---------------------------
277
static volatile int passed = 0, failed = 0;
278
static void pass() {passed++;}
279
static void fail() {failed++; new Exception("Stack trace").printStackTrace(System.out);}
280
static void fail(String msg) {
281
System.out.println(msg); failed++;
282
new Exception("Stack trace: " + msg).printStackTrace(System.out);
283
}
284
static void unexpected(Throwable t) {failed++; t.printStackTrace(System.out);}
285
static void check(boolean cond) {if (cond) pass(); else fail();}
286
static void check(boolean cond, String m) {if (cond) pass(); else fail(m);}
287
static void equal(Object x, Object y) {
288
if (x == null ? y == null : x.equals(y)) pass();
289
else fail(">'" + x + "'<" + " not equal to " + "'" + y + "'");
290
}
291
292
public static void main(String[] args) throws Throwable {
293
try {realMain(args);} catch (Throwable t) {unexpected(t);}
294
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
295
if (failed > 0) throw new AssertionError("Some tests failed");
296
}
297
interface Fun {void f() throws Throwable;}
298
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
299
for (Fun f : fs)
300
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
301
catch (Throwable t) {
302
if (k.isAssignableFrom(t.getClass())) pass();
303
else unexpected(t);}
304
}
305
306
}
307
308