Path: blob/master/test/lib-test/sun/hotspot/whitebox/vm_flags/VmFlagTest.java
41155 views
/*1* Copyright (c) 2014, 2016, 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.function.BiConsumer;24import java.util.function.Function;25import sun.hotspot.WhiteBox;26import sun.management.*;27import com.sun.management.*;28import jdk.test.lib.Asserts;29import java.lang.management.ManagementFactory;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;39private final boolean isPositive;4041protected VmFlagTest(String flagName, BiConsumer<String, T> set,42Function<String, T> get, boolean isPositive) {43this.flagName = flagName;44this.set = set;45this.get = get;46this.isPositive = isPositive;47if (isPositive) {48test = this::testWritePositive;49} else {50test = this::testWriteNegative;51}52}5354private void setNewValue(T value) {55set.accept(flagName, value);56}5758private T getValue() {59return get.apply(flagName);60}6162protected static <T> void runTest(String existentFlag, T[] tests,63BiConsumer<String, T> set, Function<String, T> get) {64runTest(existentFlag, tests, tests, set, get);65}6667protected static <T> void runTest(String existentFlag, Function<String, T> get) {68runTest(existentFlag, null, null, null, get);69}7071protected static <T> void runTest(String existentFlag, T[] tests,72T[] results, BiConsumer<String, T> set, Function<String, T> get) {73if (existentFlag != null) {74new VmFlagTest(existentFlag, set, get, true).test(tests, results);75}76new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);77}7879public final void test(T[] tests, T[] results) {80if (isPositive) {81testRead();82}83if (tests != null) {84Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");85for (int i = 0, n = tests.length ; i < n; ++i) {86test.accept(tests[i], results[i]);87}88}89}9091protected String getVMOptionAsString() {92if (WHITE_BOX.isConstantVMFlag(flagName) || WHITE_BOX.isLockedVMFlag(flagName)) {93// JMM cannot access debug flags in product builds or locked flags,94// use whitebox methods to get such flags value.95return asString(getValue());96}97HotSpotDiagnosticMXBean diagnostic98= ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);99VMOption tmp;100try {101tmp = diagnostic.getVMOption(flagName);102} catch (IllegalArgumentException e) {103tmp = null;104}105return tmp == null ? null : tmp.getValue();106}107108private String testRead() {109String value = getVMOptionAsString();110Asserts.assertNotNull(value);111Asserts.assertEQ(value, asString(getValue()));112Asserts.assertEQ(value, asString(WHITE_BOX.getVMFlag(flagName)));113return value;114}115116private void testWritePositive(T value, T expected) {117setNewValue(value);118String newValue = testRead();119Asserts.assertEQ(newValue, asString(expected));120}121122private void testWriteNegative(T value, T expected) {123// Should always return false for non-existing flags124Asserts.assertFalse(WHITE_BOX.isConstantVMFlag(flagName));125Asserts.assertFalse(WHITE_BOX.isLockedVMFlag(flagName));126String oldValue = getVMOptionAsString();127Asserts.assertEQ(oldValue, asString(getValue()));128Asserts.assertEQ(oldValue, asString(WHITE_BOX.getVMFlag(flagName)));129setNewValue(value);130String newValue = getVMOptionAsString();131Asserts.assertEQ(oldValue, newValue);132}133134private String asString(Object value) {135return value == null ? null : "" + value;136}137}138139140