Path: blob/master/test/jdk/java/lang/management/MemoryMXBean/RunUtil.java
41155 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* Utility class for launching a test in a separate JVM.25*/2627import java.util.List;28import java.util.ArrayList;29import java.util.Arrays;30import jdk.test.lib.JDKToolFinder;31import jdk.test.lib.process.OutputAnalyzer;32import jdk.test.lib.process.ProcessTools;33import jdk.test.lib.Utils;3435public class RunUtil {3637// Used to mark that the test has passed successfully.38public static final String successMessage = "Test passed.";3940public static void runTestClearGcOpts(String main, String... testOpts) throws Throwable {41runTest(main, true, testOpts);42}4344public static void runTestKeepGcOpts(String main, String... testOpts) throws Throwable {45runTest(main, false, testOpts);46}4748/**49* Runs a test in a separate JVM.50* command line like:51* {test_jdk}/bin/java {defaultopts} -cp {test.class.path} {testopts} main52*53* {defaultopts} are the default java options set by the framework.54* Default GC options in {defaultopts} may be removed.55* This is used when the test specifies its own GC options.56*57* @param main Name of the main class.58* @param clearGcOpts true if the default GC options should be removed.59* @param testOpts java options specified by the test.60*/61private static void runTest(String main, boolean clearGcOpts, String... testOpts)62throws Throwable {63List<String> opts = new ArrayList<>();64opts.add(JDKToolFinder.getJDKTool("java"));65opts.addAll(Arrays.asList(Utils.getTestJavaOpts()));66opts.add("-cp");67opts.add(System.getProperty("test.class.path", "test.class.path"));68opts.add("-Xlog:gc*=debug");6970if (clearGcOpts) {71opts = Utils.removeGcOpts(opts);72}73opts.addAll(Arrays.asList(testOpts));74opts.add(main);7576OutputAnalyzer output = ProcessTools.executeProcess(opts.toArray(new String[0]));77output.shouldHaveExitValue(0);78if (output.getStdout().indexOf(successMessage) < 0) {79throw new Exception("output missing '" + successMessage + "'");80}81}8283}848586