/* * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Return a list of the varargs arguments. * @param args elements to include in the list * @param <T> the type of the elements * @return a {@code List<T>} of the arguments
*/
@SafeVarargs
@SuppressWarnings("varargs") static <T> List<T> asList(T... args) { return Arrays.asList(args);
}
/** * T1 - simple copy between two processes
*/ staticvoid t1_simplePipeline() { try {
String s1 = "Now is the time to check!";
verify(s1, s1,
asList(new ProcessBuilder("cat")));
verify(s1, s1,
asList(new ProcessBuilder("cat"), new ProcessBuilder("cat")));
verify(s1, s1,
asList(new ProcessBuilder("cat"), new ProcessBuilder("cat"), new ProcessBuilder("cat")));
} catch (Throwable t) {
unexpected(t);
}
}
/** * Pipeline that modifies the content.
*/ staticvoid t2_translatePipeline() { try {
String s2 = "Now is the time to check!";
String r2 = s2.replace('e', 'E').replace('o', 'O');
verify(s2, r2,
asList(new ProcessBuilder("tr", "e", "E"), new ProcessBuilder("tr", "o", "O")));
} catch (Throwable t) {
unexpected(t);
}
}
/** * Test that redirectErrorStream sends standard error of the first process * to the standard output. The standard error of the first process should be empty. * The standard output of the 2nd should contain the error message including the bad file name.
*/ staticvoid t3_redirectErrorStream() { try {
File p1err = new File("p1-test.err");
File p2out = new File("p2-test.out");
check("".equals(fileContents(p1err)), "The first process standard error should be empty");
String p2contents = fileContents(p2out);
check(p2contents.contains("NON-EXISTENT-FILE"), "The error from the first process should be in the output of the second: " + p2contents);
} catch (Throwable t) {
unexpected(t);
}
}
/** * Test that no processes are left after a failed startPipeline. * Test illegal combinations of redirects.
*/ staticvoid t4_failStartPipeline() {
File p1err = new File("p1-test.err");
File p2out = new File("p2-test.out");
THROWS(IllegalArgumentException.class,
() -> { // Test that output redirect != PIPE throws IAE
List<Process> processes = ProcessBuilder.startPipeline(
asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE1")
.redirectOutput(p1err), new ProcessBuilder("cat")));
},
() -> { // Test that input redirect != PIPE throws IAE
List<Process> processes = ProcessBuilder.startPipeline(
asList(new ProcessBuilder("cat", "NON-EXISTENT-FILE2"), new ProcessBuilder("cat").redirectInput(p2out)));
}
);
// Check no subprocess are left behind
ProcessHandle.current().children().forEach(PipelineTest::print);
ProcessHandle.current().children()
.filter(p -> p.info().command().orElse("").contains("cat"))
.forEach(p -> fail("process should have been destroyed: " + p));
}
staticvoid verify(String input, String expected, List<ProcessBuilder> builders) throws IOException {
File infile = new File("test.in");
File outfile = new File("test.out");
setFileContents(infile, input); for (int i = 0; i < builders.size(); i++) {
ProcessBuilder b = builders.get(i); if (i == 0) {
b.redirectInput(infile);
} if (i == builders.size() - 1) {
b.redirectOutput(outfile);
}
}
List<Process> processes = ProcessBuilder.startPipeline(builders);
verifyProcesses(processes);
waitForAll(processes);
String result = fileContents(outfile);
check(result.equals(expected), "result not as expected: " + result + ", expected: " + expected);
}
/** * Wait for each of the processes to be done. * * @param processes the list of processes to check
*/ staticvoid waitForAll(List<Process> processes) {
processes.forEach(p -> { try { int status = p.waitFor();
} catch (InterruptedException ie) {
unexpected(ie);
}
});
}
// Check various aspects of the processes staticvoid verifyProcesses(List<Process> processes) { for (int i = 0; i < processes.size(); i++) {
Process p = processes.get(i);
if (i != 0) {
verifyNullStream(p.getOutputStream(), "getOutputStream");
} if (i <= processes.size() - 1) {
verifyNullStream(p.getInputStream(), "getInputStream");
} if (i == processes.size() - 1) {
verifyNullStream(p.getErrorStream(), "getErrorStream");
}
}
}
staticvoid verifyNullStream(OutputStream s, String msg) { try {
s.write(0xff);
fail("Stream should have been a NullStream: " + msg);
} catch (IOException ie) { // expected
}
}
staticvoid verifyNullStream(InputStream s, String msg) { try { int len = s.read();
check(len == -1, "Stream should have been a NullStream: " + msg);
} catch (IOException ie) { // expected
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.