< prev index next >

test/java/lang/management/MemoryMXBean/RunUtil.java

Print this page




  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * Utility class for launching a test in a separate JVM.
  26  */
  27 
  28 import java.util.List;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import jdk.testlibrary.OutputAnalyzer;
  32 import jdk.testlibrary.Utils;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.JDKToolFinder;
  35 
  36 public class RunUtil {
  37 
  38     // Used to mark that the test has passed successfully.
  39     public static final String successMessage = "Test passed.";

  40 
  41     public static void runTestClearGcOpts(String main, String... testOpts) throws Throwable {
  42         runTest(main, true, testOpts);
  43     }
  44 
  45     public static void runTestKeepGcOpts(String main, String... testOpts) throws Throwable {
  46         runTest(main, false, testOpts);








  47     }
  48 
  49     /**
  50      * Runs a test in a separate JVM.
  51      * command line like:
  52      * {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main
  53      *
  54      * {defaultopts} are the default java options set by the framework.
  55      * Default GC options in {defaultopts} may be removed.
  56      * This is used when the test specifies its own GC options.
  57      *
  58      * @param main Name of the main class.

  59      * @param clearGcOpts true if the default GC options should be removed.
  60      * @param testOpts java options specified by the test.
  61      */
  62     private static void runTest(String main, boolean clearGcOpts, String... testOpts)
  63                 throws Throwable {
  64         List<String> opts = new ArrayList<>();
  65         opts.add(JDKToolFinder.getJDKTool("java"));
  66         opts.addAll(Arrays.asList(Utils.getTestJavaOpts()));
  67         opts.add("-cp");
  68         opts.add(System.getProperty("test.class.path", "test.class.path"));
  69         opts.add("-XX:+PrintGCDetails");
  70 
  71         if (clearGcOpts) {
  72             opts = Utils.removeGcOpts(opts);
  73         }
  74         opts.addAll(Arrays.asList(testOpts));
  75         opts.add(main);
  76 
  77         OutputAnalyzer output = ProcessTools.executeProcess(opts.toArray(new String[0]));
  78         output.shouldHaveExitValue(0);
  79         if (output.getStdout().indexOf(successMessage) < 0) {
  80             throw new Exception("output missing '" + successMessage + "'");
  81         }
  82     }
  83 
  84 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * Utility class for launching a test in a separate JVM.
  26  */
  27 
  28 import java.util.List;
  29 import java.util.ArrayList;
  30 import java.util.Arrays;
  31 import jdk.testlibrary.OutputAnalyzer;
  32 import jdk.testlibrary.Utils;
  33 import jdk.testlibrary.ProcessTools;
  34 import jdk.testlibrary.JDKToolFinder;
  35 
  36 public class RunUtil {
  37 
  38     // Used to mark that the test has passed successfully.
  39     public static final String successMessage = "Test passed.";
  40     private static final String[] EMPTY_FILTER = new String[0];
  41 
  42     public static void runTestClearGcOpts(String main, String... testOpts) throws Throwable {
  43         runTest(main, EMPTY_FILTER, true, testOpts);
  44     }
  45 
  46     public static void runTestKeepGcOpts(String main, String... testOpts) throws Throwable {
  47         runTest(main, EMPTY_FILTER, false, testOpts);
  48     }
  49 
  50     public static void runTestClearGcOpts(String[] filters, String main, String... testOpts) throws Throwable {
  51         runTest(main, filters, true, testOpts);
  52     }
  53 
  54     public static void runTestKeepGcOpts(String[] filters, String main, String... testOpts) throws Throwable {
  55         runTest(main, filters, false, testOpts);
  56     }
  57    
  58     /**
  59      * Runs a test in a separate JVM.
  60      * command line like:
  61      * {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main
  62      *
  63      * {defaultopts} are the default java options set by the framework.
  64      * Default GC options in {defaultopts} may be removed.
  65      * This is used when the test specifies its own GC options.
  66      *
  67      * @param main Name of the main class.
  68      * @param filters java options to filter out
  69      * @param clearGcOpts true if the default GC options should be removed.
  70      * @param testOpts java options specified by the test.
  71      */
  72     private static void runTest(String main, String[] filters, boolean clearGcOpts, String... testOpts)
  73                 throws Throwable {
  74         List<String> opts = new ArrayList<>();
  75         opts.add(JDKToolFinder.getJDKTool("java"));
  76         opts.addAll(Arrays.asList(Utils.getFilteredTestJavaOpts(filters)));
  77         opts.add("-cp");
  78         opts.add(System.getProperty("test.class.path", "test.class.path"));
  79         opts.add("-XX:+PrintGCDetails");
  80 
  81         if (clearGcOpts) {
  82             opts = Utils.removeGcOpts(opts);
  83         }
  84         opts.addAll(Arrays.asList(testOpts));
  85         opts.add(main);
  86 
  87         OutputAnalyzer output = ProcessTools.executeProcess(opts.toArray(new String[0]));
  88         output.shouldHaveExitValue(0);
  89         if (output.getStdout().indexOf(successMessage) < 0) {
  90             throw new Exception("output missing '" + successMessage + "'");
  91         }
  92     } 

  93 }
< prev index next >