< prev index next >

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

Print this page
rev 1530 : 8077953: [TEST_BUG] com/sun/management/OperatingSystemMXBean/TestTotalSwap.java Compilation failed after JDK-8077387
Reviewed-by: sla, dholmes
rev 1531 : 8171415: Remove Java 7 features from testlibrary
Reviewed-by: omajid

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -29,15 +29,18 @@
 import java.lang.management.ManagementFactory;
 import java.lang.management.RuntimeMXBean;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collections;
+import java.util.Iterator;
 import java.util.Map;
+import java.util.concurrent.BrokenBarrierException;
+import java.util.concurrent.CyclicBarrier;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import java.util.concurrent.Phaser;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 
 import sun.management.VMManagement;
 

@@ -63,18 +66,21 @@
      * <span>The default redirects of STDOUT and STDERR are started</span>
      * @param name The process name
      * @param processBuilder The process builder
      * @return Returns the initialized process
      * @throws IOException
+     * @throws BrokenBarrierException
      */
     public static Process startProcess(String name,
                                        ProcessBuilder processBuilder)
-    throws IOException {
+    throws IOException, BrokenBarrierException {
         Process p = null;
         try {
             p = startProcess(name, processBuilder, -1, TimeUnit.NANOSECONDS);
-        } catch (InterruptedException | TimeoutException e) {
+        } catch (InterruptedException e) {
+            // can't ever happen
+        } catch (TimeoutException e) {
             // can't ever happen
         }
         return p;
     }
 

@@ -87,31 +93,48 @@
      * @param unit The timeout {@linkplain TimeUnit}
      * @return Returns the initialized {@linkplain Process}
      * @throws IOException
      * @throws InterruptedException
      * @throws TimeoutException
+     * @throws BrokenBarrierException
      */
     public static Process startProcess(String name,
                                        ProcessBuilder processBuilder,
                                        long timeout,
                                        TimeUnit unit)
-    throws IOException, InterruptedException, TimeoutException {
+    throws IOException, InterruptedException, TimeoutException, BrokenBarrierException {
         Process p = processBuilder.start();
         StreamPumper stdout = new StreamPumper(p.getInputStream());
         StreamPumper stderr = new StreamPumper(p.getErrorStream());
 
         stdout.addPump(new LineForwarder(name, System.out));
         stderr.addPump(new LineForwarder(name, System.err));
-        final Phaser phs = new Phaser(1);
+        final CyclicBarrier cb = new CyclicBarrier(1);
         Future<Void> stdoutTask = stdout.process();
         Future<Void> stderrTask = stderr.process();
 
         try {
             if (timeout > -1) {
-                phs.awaitAdvanceInterruptibly(0, timeout, unit);
+                cb.await(timeout, unit);
             }
-        } catch (TimeoutException | InterruptedException e) {
+        } catch (TimeoutException e) {
+            System.err.println("Failed to start a process (thread dump follows)");
+            for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
+                printStack(s.getKey(), s.getValue());
+            }
+            stdoutTask.cancel(true);
+            stderrTask.cancel(true);
+            throw e;
+        } catch (InterruptedException e) {
+            System.err.println("Failed to start a process (thread dump follows)");
+            for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
+                printStack(s.getKey(), s.getValue());
+            }
+            stdoutTask.cancel(true);
+            stderrTask.cancel(true);
+            throw e;
+        } catch (BrokenBarrierException e) {
             System.err.println("Failed to start a process (thread dump follows)");
             for(Map.Entry<Thread, StackTraceElement[]> s : Thread.getAllStackTraces().entrySet()) {
                 printStack(s.getKey(), s.getValue());
             }
             stdoutTask.cancel(true);

@@ -217,11 +240,11 @@
      */
     public static ProcessBuilder createJavaProcessBuilder(String... command)
             throws Exception {
         String javapath = JDKToolFinder.getJDKTool("java");
 
-        ArrayList<String> args = new ArrayList<>();
+        ArrayList<String> args = new ArrayList<String>();
         args.add(javapath);
         Collections.addAll(args, getPlatformSpecificVMArgs());
         Collections.addAll(args, command);
 
         // Reporting

@@ -317,6 +340,59 @@
         for (String s : pb.command()) {
             cmd.append(s).append(" ");
         }
         return cmd.toString().trim();
     }
+
+    /**
+     * Executes a process, waits for it to finish, prints the process output
+     * to stdout, and returns the process output.
+     *
+     * The process will have exited before this method returns.
+     *
+     * @param cmds The command line to execute.
+     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
+     */
+    public static OutputAnalyzer executeCommand(String... cmds)
+            throws Throwable {
+        String cmdLine = "";
+        if (cmds.length > 0) {
+            StringBuilder cmdLineBuf = new StringBuilder(cmds[0]);
+            for (int a = 1; a < cmds.length; ++a) {
+                cmdLineBuf.append(" ");
+                cmdLineBuf.append(cmds[a]);
+            }
+            cmdLine = cmdLineBuf.toString();
+        }
+        System.out.println("Command line: [" + cmdLine + "]");
+        OutputAnalyzer analyzer = ProcessTools.executeProcess(cmds);
+        System.out.println(analyzer.getOutput());
+        return analyzer;
+    }
+
+    /**
+     * Executes a process, waits for it to finish, prints the process output
+     * to stdout and returns the process output.
+     *
+     * The process will have exited before this method returns.
+     *
+     * @param pb The ProcessBuilder to execute.
+     * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
+     */
+    public static OutputAnalyzer executeCommand(ProcessBuilder pb)
+            throws Throwable {
+        Iterator<String> cmds = pb.command().iterator();
+        String cmdLine = "";
+        if (cmds.hasNext()) {
+            StringBuilder cmdLineBuf = new StringBuilder(cmds.next());
+            while (cmds.hasNext()) {
+                cmdLineBuf.append(" ");
+                cmdLineBuf.append(cmds.next());
+            }
+            cmdLine = cmdLineBuf.toString();
+        }
+        System.out.println("Command line: [" + cmdLine + "]");
+        OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
+        System.out.println(analyzer.getOutput());
+        return analyzer;
+    }
 }
< prev index next >