changeset 14567:c27db69fe577

8142927: Feed some text to STDIN in ProcessTools.executeProcess() Reviewed-by: rriggs
author weijun
date Sun, 15 Nov 2015 09:15:03 +0800
parents b39bfadab299
children 68adc82d5090
files test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
diffstat 2 files changed, 21 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java	Sat Nov 14 11:00:40 2015 +0800
+++ b/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/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java	Sat Nov 14 11:00:40 2015 +0800
+++ b/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();