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

Print this page




  22  */
  23 
  24 package jdk.test.lib;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 import java.lang.management.ManagementFactory;
  29 import java.lang.management.RuntimeMXBean;
  30 import java.util.ArrayList;
  31 import java.util.Collections;
  32 import java.util.List;
  33 
  34 public final class ProcessTools {
  35 
  36   private ProcessTools() {
  37   }
  38 
  39   /**
  40    * Pumps stdout and stderr from running the process into a String.
  41    *
  42    * @param processHandler ProcessHandler to run.
  43    * @return Output from process.
  44    * @throws IOException If an I/O error occurs.
  45    */
  46   public static OutputBuffer getOutput(ProcessBuilder processBuilder) throws IOException {
  47     return getOutput(processBuilder.start());
  48   }
  49 
  50   /**
  51    * Pumps stdout and stderr the running process into a String.
  52    *
  53    * @param process Process to pump.
  54    * @return Output from process.
  55    * @throws IOException If an I/O error occurs.
  56    */
  57   public static OutputBuffer getOutput(Process process) throws IOException {
  58     ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
  59     ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
  60     StreamPumper outPumper = new StreamPumper(process.getInputStream(), stdoutBuffer);
  61     StreamPumper errPumper = new StreamPumper(process.getErrorStream(), stderrBuffer);
  62     Thread outPumperThread = new Thread(outPumper);


  92     return pid;
  93   }
  94 
  95   /**
  96    * Get the string containing input arguments passed to the VM
  97    *
  98    * @return arguments
  99    */
 100   public static String getVmInputArguments() {
 101     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 102 
 103     List<String> args = runtime.getInputArguments();
 104     StringBuilder result = new StringBuilder();
 105     for (String arg : args)
 106         result.append(arg).append(' ');
 107 
 108     return result.toString();
 109   }
 110 
 111   /**











 112    * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 113    *
 114    * @return String[] with platform specific arguments, empty if there are none
 115    */
 116   public static String[] getPlatformSpecificVMArgs() {
 117 
 118     if (Platform.is64bit() && Platform.isSolaris()) {
 119       return new String[] { "-d64" };
 120     }
 121 
 122     return new String[] {};
 123   }
 124 
 125   /**
 126    * Create ProcessBuilder using the java launcher from the jdk to be tested and
 127    * with any platform specific arguments prepended
 128    */
 129   public static ProcessBuilder createJavaProcessBuilder(String... command) throws Exception {
 130     return createJavaProcessBuilder(false, command);
 131   }


 152       cmdLine.append(cmd).append(' ');
 153     }
 154     System.out.println("Command line: [" + cmdLine.toString() + "]");
 155 
 156     return new ProcessBuilder(args.toArray(new String[args.size()]));
 157   }
 158 
 159   /**
 160    * Executes a test jvm process, waits for it to finish and returns the process output.
 161    * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
 162    * The java from the test.jdk is used to execute the command.
 163    *
 164    * The command line will be like:
 165    * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 166    *
 167    * @param cmds User specifed arguments.
 168    * @return The output from the process.
 169    */
 170   public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
 171     ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));




















 172     return executeProcess(pb);
 173   }
 174 
 175     /**
 176      * Executes a process, waits for it to finish and returns the process output.
 177      * The process will have exited before this method returns.
 178      * @param pb The ProcessBuilder to execute.
 179      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 180      */
 181     public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
 182         OutputAnalyzer output = null;
 183         Process p = null;
 184         boolean failed = false;
 185         try {
 186             p = pb.start();
 187             output = new OutputAnalyzer(p);
 188             p.waitFor();
 189 
 190             return output;
 191         } catch (Throwable t) {




  22  */
  23 
  24 package jdk.test.lib;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.IOException;
  28 import java.lang.management.ManagementFactory;
  29 import java.lang.management.RuntimeMXBean;
  30 import java.util.ArrayList;
  31 import java.util.Collections;
  32 import java.util.List;
  33 
  34 public final class ProcessTools {
  35 
  36   private ProcessTools() {
  37   }
  38 
  39   /**
  40    * Pumps stdout and stderr from running the process into a String.
  41    *
  42    * @param processBuilder ProcessBuilder to run.
  43    * @return Output from process.
  44    * @throws IOException If an I/O error occurs.
  45    */
  46   public static OutputBuffer getOutput(ProcessBuilder processBuilder) throws IOException {
  47     return getOutput(processBuilder.start());
  48   }
  49 
  50   /**
  51    * Pumps stdout and stderr the running process into a String.
  52    *
  53    * @param process Process to pump.
  54    * @return Output from process.
  55    * @throws IOException If an I/O error occurs.
  56    */
  57   public static OutputBuffer getOutput(Process process) throws IOException {
  58     ByteArrayOutputStream stderrBuffer = new ByteArrayOutputStream();
  59     ByteArrayOutputStream stdoutBuffer = new ByteArrayOutputStream();
  60     StreamPumper outPumper = new StreamPumper(process.getInputStream(), stdoutBuffer);
  61     StreamPumper errPumper = new StreamPumper(process.getErrorStream(), stderrBuffer);
  62     Thread outPumperThread = new Thread(outPumper);


  92     return pid;
  93   }
  94 
  95   /**
  96    * Get the string containing input arguments passed to the VM
  97    *
  98    * @return arguments
  99    */
 100   public static String getVmInputArguments() {
 101     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 102 
 103     List<String> args = runtime.getInputArguments();
 104     StringBuilder result = new StringBuilder();
 105     for (String arg : args)
 106         result.append(arg).append(' ');
 107 
 108     return result.toString();
 109   }
 110 
 111   /**
 112    * Gets the array of strings containing input arguments passed to the VM
 113    *
 114    * @return arguments
 115    */
 116   public static String[] getVmInputArgs() {
 117     RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
 118     List<String> args = runtime.getInputArguments();
 119     return args.toArray(new String[args.size()]);
 120   }
 121 
 122   /**
 123    * Get platform specific VM arguments (e.g. -d64 on 64bit Solaris)
 124    *
 125    * @return String[] with platform specific arguments, empty if there are none
 126    */
 127   public static String[] getPlatformSpecificVMArgs() {
 128 
 129     if (Platform.is64bit() && Platform.isSolaris()) {
 130       return new String[] { "-d64" };
 131     }
 132 
 133     return new String[] {};
 134   }
 135 
 136   /**
 137    * Create ProcessBuilder using the java launcher from the jdk to be tested and
 138    * with any platform specific arguments prepended
 139    */
 140   public static ProcessBuilder createJavaProcessBuilder(String... command) throws Exception {
 141     return createJavaProcessBuilder(false, command);
 142   }


 163       cmdLine.append(cmd).append(' ');
 164     }
 165     System.out.println("Command line: [" + cmdLine.toString() + "]");
 166 
 167     return new ProcessBuilder(args.toArray(new String[args.size()]));
 168   }
 169 
 170   /**
 171    * Executes a test jvm process, waits for it to finish and returns the process output.
 172    * The default jvm options from jtreg, test.vm.opts and test.java.opts, are added.
 173    * The java from the test.jdk is used to execute the command.
 174    *
 175    * The command line will be like:
 176    * {test.jdk}/bin/java {test.vm.opts} {test.java.opts} cmds
 177    *
 178    * @param cmds User specifed arguments.
 179    * @return The output from the process.
 180    */
 181   public static OutputAnalyzer executeTestJvm(String... cmds) throws Throwable {
 182     ProcessBuilder pb = createJavaProcessBuilder(Utils.addTestJavaOpts(cmds));
 183     return executeProcess(pb);
 184   }
 185 
 186   /**
 187    * Executes a test jvm process, waits for it to finish and returns the process output.
 188    * The default jvm options from the test's run command, jtreg, test.vm.opts and test.java.opts, are added.
 189    * The java from the test.jdk is used to execute the command.
 190    *
 191    * The command line will be like:
 192    * {test.jdk}/bin/java {test.fromRun.opts} {test.vm.opts} {test.java.opts} cmds
 193    *
 194    * @param cmds User specifed arguments.
 195    * @return The output from the process.
 196    */
 197   public static OutputAnalyzer executeTestJvmAllArgs(String... cmds) throws Throwable {
 198     List<String> argsList = new ArrayList<>();
 199     String[] testArgs = getVmInputArgs();
 200     Collections.addAll(argsList, testArgs);
 201     Collections.addAll(argsList, Utils.addTestJavaOpts(cmds));
 202     ProcessBuilder pb = createJavaProcessBuilder(argsList.toArray(new String[argsList.size()]));
 203     return executeProcess(pb);
 204   }
 205 
 206     /**
 207      * Executes a process, waits for it to finish and returns the process output.
 208      * The process will have exited before this method returns.
 209      * @param pb The ProcessBuilder to execute.
 210      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
 211      */
 212     public static OutputAnalyzer executeProcess(ProcessBuilder pb) throws Exception {
 213         OutputAnalyzer output = null;
 214         Process p = null;
 215         boolean failed = false;
 216         try {
 217             p = pb.start();
 218             output = new OutputAnalyzer(p);
 219             p.waitFor();
 220 
 221             return output;
 222         } catch (Throwable t) {