< prev index next >

test/lib/testlibrary/jdk/testlibrary/ProcessTools.java

Print this page
rev 13050 : 8142927: Feed some text to STDIN in ProcessTools.executeProcess()


 348      * The command line will be like:
 349      * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 350      *
 351      * The jvm process will have exited before this method returns.
 352      *
 353      * @param cmds User specifed arguments.
 354      * @return The output from the process.
 355      */
 356     public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception {
 357         ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
 358         return executeProcess(pb);
 359     }
 360 
 361     /**
 362      * Executes a process, waits for it to finish and returns the process output.
 363      * The process will have exited before this method returns.
 364      * @param pb The ProcessBuilder to execute.
 365      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 366      */
 367     public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {













 368         OutputAnalyzer output = null;
 369         Process p = null;
 370         boolean failed = false;
 371         try {
 372             p = pb.start();






 373             output = new OutputAnalyzer(p);
 374             p.waitFor();
 375 
 376             return output;
 377         } catch (Throwable t) {
 378             if (p != null) {
 379                 p.destroyForcibly().waitFor();
 380             }
 381 
 382             failed = true;
 383             System.out.println("executeProcess() failed: " + t);
 384             throw t;
 385         } finally {
 386             if (failed) {
 387                 System.err.println(getProcessLog(pb, output));
 388             }
 389         }
 390     }
 391 
 392     /**




 348      * The command line will be like:
 349      * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 350      *
 351      * The jvm process will have exited before this method returns.
 352      *
 353      * @param cmds User specifed arguments.
 354      * @return The output from the process.
 355      */
 356     public static OutputAnalyzer executeTestJvm(String... cmds) throws Exception {
 357         ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
 358         return executeProcess(pb);
 359     }
 360 
 361     /**
 362      * Executes a process, waits for it to finish and returns the process output.
 363      * The process will have exited before this method returns.
 364      * @param pb The ProcessBuilder to execute.
 365      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 366      */
 367     public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
 368         return executeProcess(pb, null);
 369     }
 370 
 371     /**
 372      * Executes a process, pipe some text into its STDIN, waits for it
 373      * to finish and returns the process output. The process will have exited
 374      * before this method returns.
 375      * @param pb The ProcessBuilder to execute.
 376      * @param input The text to pipe into STDIN. Can be null.
 377      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 378      */
 379     public static OutputAnalyzer executeProcess(ProcessBuilder pb, String input)
 380             throws Exception {
 381         OutputAnalyzer output = null;
 382         Process p = null;
 383         boolean failed = false;
 384         try {
 385             p = pb.start();
 386             if (input != null) {
 387                 try (OutputStream os = p.getOutputStream()) {
 388                     os.write(input.getBytes());
 389                     os.flush();
 390                 }
 391             }
 392             output = new OutputAnalyzer(p);
 393             p.waitFor();
 394 
 395             return output;
 396         } catch (Throwable t) {
 397             if (p != null) {
 398                 p.destroyForcibly().waitFor();
 399             }
 400 
 401             failed = true;
 402             System.out.println("executeProcess() failed: " + t);
 403             throw t;
 404         } finally {
 405             if (failed) {
 406                 System.err.println(getProcessLog(pb, output));
 407             }
 408         }
 409     }
 410 
 411     /**


< prev index next >