8142927: Feed some text to STDIN in ProcessTools.executeProcess()
authorweijun
Sun, 15 Nov 2015 09:15:03 +0800
changeset 33829 9b347aad251a
parent 33828 705656955311
child 33830 9f4be4f1c8b6
8142927: Feed some text to STDIN in ProcessTools.executeProcess() Reviewed-by: rriggs
jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java
jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Sat Nov 14 11:00:40 2015 +0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Sun Nov 15 09:15:03 2015 +0800
@@ -38,7 +38,7 @@
     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
--- a/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Sat Nov 14 11:00:40 2015 +0800
+++ b/jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Sun Nov 15 09:15:03 2015 +0800
@@ -365,11 +365,31 @@
      * @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();