1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   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 /**
  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("-Xlog:gc*=debug");
  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 }