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);
}
}
/** *TestthatnoprocessesareleftafterafailedstartPipeline. *Testillegalcombinationsofredirects.
*/ 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);
}
// 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.