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 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 }