8142927: Feed some text to STDIN in ProcessTools.executeProcess()

Reviewed-by: rriggs
This commit is contained in:
Weijun Wang 2015-11-15 09:15:03 +08:00
parent 0793aa183c
commit 4d01763cc8
2 changed files with 21 additions and 1 deletions

View File

@ -38,7 +38,7 @@ public final class OutputAnalyzer {
private final OutputBuffer output;
private final String stdout;
private final String stderr;
private final int exitValue;
private final int exitValue; // useless now. output contains exit value.
/**
* Create an OutputAnalyzer, a utility class for verifying output and exit

View File

@ -365,11 +365,31 @@ public final class ProcessTools {
* @return The {@linkplain OutputAnalyzer} instance wrapping the process.
*/
public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
return executeProcess(pb, null);
}
/**
* Executes a process, pipe some text into its STDIN, waits for it
* to finish and returns the process output. The process will have exited
* before this method returns.
* @param pb The ProcessBuilder to execute.
* @param input The text to pipe into STDIN. Can be null.
* @return The {@linkplain OutputAnalyzer} instance wrapping the process.
*/
public static OutputAnalyzer executeProcess(ProcessBuilder pb, String input)
throws Exception {
OutputAnalyzer output = null;
Process p = null;
boolean failed = false;
try {
p = pb.start();
if (input != null) {
try (OutputStream os = p.getOutputStream();
PrintStream ps = new PrintStream(os)) {
ps.print(input);
ps.flush();
}
}
output = new OutputAnalyzer(p);
p.waitFor();