Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/testlibrary_tests/whitebox/vm_flags/VmFlagTest.java
32284 views
/*1* Copyright (c) 2014, 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*/2223import java.util.Objects;24import java.util.function.BiConsumer;25import java.util.function.Function;26import sun.hotspot.WhiteBox;27import sun.management.*;28import com.sun.management.*;29import com.oracle.java.testlibrary.*;3031public final class VmFlagTest<T> {32public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();3334private static final String NONEXISTENT_FLAG = "NonexistentFlag";35private final String flagName;36private final BiConsumer<T, T> test;37private final BiConsumer<String, T> set;38private final Function<String, T> get;3940protected VmFlagTest(String flagName, BiConsumer<String, T> set,41Function<String, T> get, boolean isPositive) {42this.flagName = flagName;43this.set = set;44this.get = get;45if (isPositive) {46test = this::testPositive;47} else {48test = this::testNegative;49}50}5152private void setNewValue(T value) {53set.accept(flagName, value);54}5556private T getValue() {57return get.apply(flagName);58}5960protected static <T> void runTest(String existentFlag, T[] tests,61BiConsumer<String, T> set, Function<String, T> get) {62runTest(existentFlag, tests, tests, set, get);63}6465protected static <T> void runTest(String existentFlag, T[] tests,66T[] results, BiConsumer<String, T> set, Function<String, T> get) {67if (existentFlag != null) {68new VmFlagTest(existentFlag, set, get, true).test(tests, results);69}70new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);71}7273public final void test(T[] tests, T[] results) {74Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");75for (int i = 0, n = tests.length ; i < n; ++i) {76test.accept(tests[i], results[i]);77}78}7980protected String getVMOptionAsString() {81HotSpotDiagnosticMXBean diagnostic82= ManagementFactoryHelper.getDiagnosticMXBean();83VMOption tmp;84try {85tmp = diagnostic.getVMOption(flagName);86} catch (IllegalArgumentException e) {87tmp = null;88}89return tmp == null ? null : tmp.getValue();90}9192private void testPositive(T value, T expected) {93Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));94setNewValue(value);95String newValue = getVMOptionAsString();96Asserts.assertEQ(newValue, asString(expected));97Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));98}99100private void testNegative(T value, T expected) {101String oldValue = getVMOptionAsString();102Asserts.assertEQ(oldValue, asString(getValue()));103setNewValue(value);104String newValue = getVMOptionAsString();105Asserts.assertEQ(oldValue, newValue);106}107108private String asString(Object value) {109return value == null ? null : "" + value;110}111}112113114115