jdk/test/lib/testlibrary/jdk/testlibrary/ProcessTools.java
changeset 28662 efd0203db371
parent 27258 e66ef2eba095
child 28870 cc64f71a38ec
equal deleted inserted replaced
28661:4fe905a2d72f 28662:efd0203db371
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    28 import java.lang.management.ManagementFactory;
    28 import java.lang.management.ManagementFactory;
    29 import java.lang.management.RuntimeMXBean;
    29 import java.lang.management.RuntimeMXBean;
    30 import java.lang.reflect.Field;
    30 import java.lang.reflect.Field;
    31 import java.lang.reflect.Method;
    31 import java.lang.reflect.Method;
    32 import java.util.ArrayList;
    32 import java.util.ArrayList;
       
    33 import java.util.Arrays;
    33 import java.util.Collections;
    34 import java.util.Collections;
    34 import java.util.concurrent.CountDownLatch;
    35 import java.util.concurrent.CountDownLatch;
    35 import java.util.Map;
    36 import java.util.Map;
    36 import java.util.concurrent.Future;
    37 import java.util.concurrent.Future;
    37 import java.util.concurrent.TimeUnit;
    38 import java.util.concurrent.TimeUnit;
   376         for (String s : pb.command()) {
   377         for (String s : pb.command()) {
   377             cmd.append(s).append(" ");
   378             cmd.append(s).append(" ");
   378         }
   379         }
   379         return cmd.toString().trim();
   380         return cmd.toString().trim();
   380     }
   381     }
       
   382 
       
   383     /**
       
   384      * Executes a process, waits for it to finish, prints the process output
       
   385      * to stdout, and returns the process output.
       
   386      *
       
   387      * The process will have exited before this method returns.
       
   388      *
       
   389      * @param cmds The command line to execute.
       
   390      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
       
   391      */
       
   392     public static OutputAnalyzer executeCommand(String... cmds)
       
   393             throws Throwable {
       
   394         String cmdLine = Arrays.stream(cmds).collect(Collectors.joining(" "));
       
   395         System.out.println("Command line: [" + cmdLine + "]");
       
   396         OutputAnalyzer analyzer = ProcessTools.executeProcess(cmds);
       
   397         System.out.println(analyzer.getOutput());
       
   398         return analyzer;
       
   399     }
       
   400 
       
   401     /**
       
   402      * Executes a process, waits for it to finish, prints the process output
       
   403      * to stdout and returns the process output.
       
   404      *
       
   405      * The process will have exited before this method returns.
       
   406      *
       
   407      * @param pb The ProcessBuilder to execute.
       
   408      * @return The {@linkplain OutputAnalyzer} instance wrapping the process.
       
   409      */
       
   410     public static OutputAnalyzer executeCommand(ProcessBuilder pb)
       
   411             throws Throwable {
       
   412         String cmdLine = pb.command().stream().collect(Collectors.joining(" "));
       
   413         System.out.println("Command line: [" + cmdLine + "]");
       
   414         OutputAnalyzer analyzer = ProcessTools.executeProcess(pb);
       
   415         System.out.println(analyzer.getOutput());
       
   416         return analyzer;
       
   417     }
   381 }
   418 }