< prev index next >

test/testlibrary/com/oracle/java/testlibrary/ProcessTools.java

Print this page
rev 7753 : 8071909: Port testlibrary improvments in jdk/test to hotspot/test as required for DCMD test port
Reviewed-by:


 169   }
 170 
 171   /**
 172    * Executes a test jvm process, waits for it to finish and returns the process output.
 173    * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
 174    * The java from the test.jdk is used to execute the command.
 175    *
 176    * The command line will be like:
 177    * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 178    *
 179    * @param cmds User specifed arguments.
 180    * @return The output from the process.
 181    */
 182   public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
 183     ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
 184     return executeProcess(pb);
 185   }
 186 
 187   /**
 188    * Executes a process, waits for it to finish and returns the process output.

 189    * @param pb The ProcessBuilder to execute.
 190    * @return The output from the process.
 191    */
 192   public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Throwable {
 193     OutputAnalyzer output = null;


 194     try {
 195       output = new OutputAnalyzer(pb.start());



 196       return output;
 197     } catch (Throwable t) {





 198       System.out.println("executeProcess() failed: " + t);
 199       throw t;
 200     } finally {
 201       System.out.println(getProcessLog(pb, output));


 202     }
 203   }
 204 
 205   /**
 206    * Executes a process, waits for it to finish and returns the process output.
 207    * @param cmds The command line to execute.
 208    * @return The output from the process.
 209    */
 210   public static OutputAnalyzer executeProcess(String... cmds) throws Throwable {
 211     return executeProcess(new ProcessBuilder(cmds));
 212   }
 213 
 214   /**
 215    * Used to log command line, stdout, stderr and exit code from an executed process.
 216    * @param pb The executed process.
 217    * @param output The output from the process.
 218    */
 219   public static String getProcessLog(ProcessBuilder pb, OutputAnalyzer output) {
 220     String stderr = output == null ? "null" : output.getStderr();
 221     String stdout = output == null ? "null" : output.getStdout();




 169   }
 170 
 171   /**
 172    * Executes a test jvm process, waits for it to finish and returns the process output.
 173    * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
 174    * The java from the test.jdk is used to execute the command.
 175    *
 176    * The command line will be like:
 177    * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 178    *
 179    * @param cmds User specifed arguments.
 180    * @return The output from the process.
 181    */
 182   public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
 183     ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
 184     return executeProcess(pb);
 185   }
 186 
 187     /**
 188      * Executes a process, waits for it to finish and returns the process output.
 189      * The process will have exited before this method returns.
 190      * @param pb The ProcessBuilder to execute.
 191      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 192      */
 193     public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
 194         OutputAnalyzer output = null;
 195         Process p = null;
 196         boolean failed = false;
 197         try {
 198             p = pb.start();
 199             output = new OutputAnalyzer(p);
 200             p.waitFor();
 201 
 202             return output;
 203         } catch (Throwable t) {
 204             if (p != null) {
 205                 p.destroyForcibly().waitFor();
 206             }
 207 
 208             failed = true;
 209             System.out.println("executeProcess() failed: " + t);
 210             throw t;
 211         } finally {
 212             if (failed) {
 213                 System.err.println(getProcessLog(pb, output));
 214             }
 215         }
 216     }
 217 
 218   /**
 219    * Executes a process, waits for it to finish and returns the process output.
 220    * @param cmds The command line to execute.
 221    * @return The output from the process.
 222    */
 223   public static OutputAnalyzer executeProcess(String... cmds) throws Throwable {
 224     return executeProcess(new ProcessBuilder(cmds));
 225   }
 226 
 227   /**
 228    * Used to log command line, stdout, stderr and exit code from an executed process.
 229    * @param pb The executed process.
 230    * @param output The output from the process.
 231    */
 232   public static String getProcessLog(ProcessBuilder pb, OutputAnalyzer output) {
 233     String stderr = output == null ? "null" : output.getStderr();
 234     String stdout = output == null ? "null" : output.getStdout();


< prev index next >