test/testlibrary_tests/whitebox/vm_flags/VmFlagTest.java

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 8441
cf1faa9100dd
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

     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  */
    24 import java.util.Objects;
    25 import java.util.function.BiConsumer;
    26 import java.util.function.Function;
    27 import sun.hotspot.WhiteBox;
    28 import sun.management.*;
    29 import com.sun.management.*;
    30 import com.oracle.java.testlibrary.*;
    32 public final class VmFlagTest<T> {
    33     public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    35     private static final String NONEXISTENT_FLAG = "NonexistentFlag";
    36     private final String flagName;
    37     private final BiConsumer<T, T> test;
    38     private final BiConsumer<String, T> set;
    39     private final Function<String, T> get;
    41     protected VmFlagTest(String flagName, BiConsumer<String, T> set,
    42             Function<String, T> get, boolean isPositive) {
    43         this.flagName = flagName;
    44         this.set = set;
    45         this.get = get;
    46         if (isPositive) {
    47             test = this::testPositive;
    48         } else {
    49             test = this::testNegative;
    50         }
    51     }
    53     private void setNewValue(T value) {
    54         set.accept(flagName, value);
    55     }
    57     private T getValue() {
    58         return get.apply(flagName);
    59     }
    61     protected static <T> void runTest(String existentFlag, T[] tests,
    62             BiConsumer<String, T> set, Function<String, T> get) {
    63         runTest(existentFlag, tests, tests, set, get);
    64     }
    66     protected static <T> void runTest(String existentFlag, T[] tests,
    67             T[] results, BiConsumer<String, T> set, Function<String, T> get) {
    68         if (existentFlag != null) {
    69             new VmFlagTest(existentFlag, set, get, true).test(tests, results);
    70         }
    71         new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);
    72     }
    74     public final void test(T[] tests, T[] results) {
    75         Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");
    76         for (int i = 0, n = tests.length ; i < n; ++i) {
    77             test.accept(tests[i], results[i]);
    78         }
    79     }
    81     protected String getVMOptionAsString() {
    82         HotSpotDiagnosticMXBean diagnostic
    83                 = ManagementFactoryHelper.getDiagnosticMXBean();
    84         VMOption tmp;
    85         try {
    86             tmp = diagnostic.getVMOption(flagName);
    87         } catch (IllegalArgumentException e) {
    88             tmp = null;
    89         }
    90         return tmp == null ? null : tmp.getValue();
    91     }
    93     private void testPositive(T value, T expected) {
    94         Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
    95         setNewValue(value);
    96         String newValue = getVMOptionAsString();
    97         Asserts.assertEQ(newValue, asString(expected));
    98         Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
    99     }
   101     private void testNegative(T value, T expected) {
   102         String oldValue = getVMOptionAsString();
   103         Asserts.assertEQ(oldValue, asString(getValue()));
   104         setNewValue(value);
   105         String newValue = getVMOptionAsString();
   106         Asserts.assertEQ(oldValue, newValue);
   107     }
   109     private String asString(Object value) {
   110         return value == null ? null : "" + value;
   111     }
   112 }

mercurial