test/com/sun/tools/attach/RunnerUtil.java

Print this page
rev 9799 : Backed out changeset 2b5b8d82173e


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.io.File;
  26 import java.nio.file.Files;

  27 import java.util.Arrays;
  28 import java.util.regex.Pattern;
  29 import java.util.regex.Matcher;
  30 
  31 import jdk.testlibrary.OutputAnalyzer;

  32 import jdk.testlibrary.ProcessTools;
  33 import jdk.testlibrary.Utils;
  34 import jdk.testlibrary.ProcessThread;
  35 
  36 /*
  37  * Utility functions for test runners.
  38  * (Test runner = class that launch a test)
  39  */
  40 public class RunnerUtil {
  41 
  42     /**
  43      * The Application process must be run concurrently with our tests since
  44      * the tests will attach to the Application.
  45      * We will run the Application process in a separate thread.
  46      *
  47      * The Application must be started with flag "-Xshare:off" for the Retransform
  48      * test in TestBasics to pass on all platforms.
  49      *
  50      * The Application will write its pid and shutdownPort in the given outFile.
  51      */
  52     public static ProcessThread startApplication(String outFile, String... additionalOpts) throws Throwable {
  53         String classpath = System.getProperty("test.class.path", ".");
  54         String[] myArgs = concat(additionalOpts, new String [] { "-Dattach.test=true", "-classpath", classpath, "Application", outFile });
  55         String[] args = Utils.addTestJavaOpts(myArgs);
  56         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
  57         ProcessThread pt = new ProcessThread("runApplication", pb);
  58         pt.start();
  59         return pt;
  60     }
  61 
  62     public static String[] concat(String[] a, String[] b) {
  63         if (a == null) {
  64             return b;
  65         }
  66         if (b == null) {
  67             return a;
  68         }
  69         int aLen = a.length;
  70         int bLen = b.length;
  71         String[] c = new String[aLen + bLen];
  72         System.arraycopy(a, 0, c, 0, aLen);
  73         System.arraycopy(b, 0, c, aLen, bLen);
  74         return c;
  75      }
  76 
  77     /**
  78      * Will stop the running Application.
  79      * First tries to shutdown nicely by connecting to the shut down port.
  80      * If that fails, the process will be killed hard with stopProcess().
  81      *
  82      * If the nice shutdown fails, then an Exception is thrown and the test should fail.
  83      *
  84      * @param port The shut down port.
  85      * @param processThread The process to stop.
  86      */
  87     public static void stopApplication(int port, ProcessThread processThread) throws Throwable {
  88         if (processThread == null) {
  89             System.out.println("RunnerUtil.stopApplication ignored since proc is null");
  90             return;
  91         }
  92         try {
  93             System.out.println("RunnerUtil.stopApplication waiting to for shutdown");
  94             OutputAnalyzer output = ProcessTools.executeTestJvm(
  95                     "-classpath",
  96                     System.getProperty("test.class.path", "."),




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.IOException;
  25 import java.io.File;
  26 import java.nio.file.Files;
  27 import java.nio.file.Path;
  28 import java.util.Arrays;
  29 import java.util.regex.Pattern;
  30 import java.util.regex.Matcher;

  31 import jdk.testlibrary.OutputAnalyzer;
  32 import jdk.testlibrary.JDKToolLauncher;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.Utils;
  35 import jdk.testlibrary.ProcessThread;
  36 
  37 /*
  38  * Utility functions for test runners.
  39  * (Test runner = class that launch a test)
  40  */
  41 public class RunnerUtil {

  42     /**
  43      * The Application process must be run concurrently with our tests since
  44      * the tests will attach to the Application.
  45      * We will run the Application process in a separate thread.
  46      *
  47      * The Application must be started with flag "-Xshare:off" for the Retransform
  48      * test in TestBasics to pass on all platforms.
  49      *
  50      * The Application will write its pid and shutdownPort in the given outFile.
  51      */
  52     public static ProcessThread startApplication(String outFile) throws Throwable {
  53         String classpath = System.getProperty("test.class.path", ".");
  54         String[] args = Utils.addTestJavaOpts(
  55             "-Dattach.test=true", "-classpath", classpath, "Application", outFile);
  56         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(args);
  57         ProcessThread pt = new ProcessThread("runApplication", pb);
  58         pt.start();
  59         return pt;
  60     }
  61 















  62     /**
  63      * Will stop the running Application.
  64      * First tries to shutdown nicely by connecting to the shut down port.
  65      * If that fails, the process will be killed hard with stopProcess().
  66      *
  67      * If the nice shutdown fails, then an Exception is thrown and the test should fail.
  68      *
  69      * @param port The shut down port.
  70      * @param processThread The process to stop.
  71      */
  72     public static void stopApplication(int port, ProcessThread processThread) throws Throwable {
  73         if (processThread == null) {
  74             System.out.println("RunnerUtil.stopApplication ignored since proc is null");
  75             return;
  76         }
  77         try {
  78             System.out.println("RunnerUtil.stopApplication waiting to for shutdown");
  79             OutputAnalyzer output = ProcessTools.executeTestJvm(
  80                     "-classpath",
  81                     System.getProperty("test.class.path", "."),