test/testlibrary_tests/whitebox/vm_flags/VmFlagTest.java

changeset 6963
0c48231c5c84
child 8441
cf1faa9100dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testlibrary_tests/whitebox/vm_flags/VmFlagTest.java	Fri May 30 17:20:48 2014 +0400
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +import java.util.Objects;
    1.28 +import java.util.function.BiConsumer;
    1.29 +import java.util.function.Function;
    1.30 +import sun.hotspot.WhiteBox;
    1.31 +import sun.management.*;
    1.32 +import com.sun.management.*;
    1.33 +import com.oracle.java.testlibrary.*;
    1.34 +
    1.35 +public final class VmFlagTest<T> {
    1.36 +    public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
    1.37 +
    1.38 +    private static final String NONEXISTENT_FLAG = "NonexistentFlag";
    1.39 +    private final String flagName;
    1.40 +    private final BiConsumer<T, T> test;
    1.41 +    private final BiConsumer<String, T> set;
    1.42 +    private final Function<String, T> get;
    1.43 +
    1.44 +    protected VmFlagTest(String flagName, BiConsumer<String, T> set,
    1.45 +            Function<String, T> get, boolean isPositive) {
    1.46 +        this.flagName = flagName;
    1.47 +        this.set = set;
    1.48 +        this.get = get;
    1.49 +        if (isPositive) {
    1.50 +            test = this::testPositive;
    1.51 +        } else {
    1.52 +            test = this::testNegative;
    1.53 +        }
    1.54 +    }
    1.55 +
    1.56 +    private void setNewValue(T value) {
    1.57 +        set.accept(flagName, value);
    1.58 +    }
    1.59 +
    1.60 +    private T getValue() {
    1.61 +        T t = get.apply(flagName);
    1.62 +        System.out.println("T = " + t);
    1.63 +        return t;
    1.64 +    }
    1.65 +
    1.66 +    protected static <T> void runTest(String existentFlag, T[] tests,
    1.67 +            BiConsumer<String, T> set, Function<String, T> get) {
    1.68 +        runTest(existentFlag, tests, tests, set, get);
    1.69 +    }
    1.70 +
    1.71 +    protected static <T> void runTest(String existentFlag, T[] tests,
    1.72 +            T[] results, BiConsumer<String, T> set, Function<String, T> get) {
    1.73 +        if (existentFlag != null) {
    1.74 +            new VmFlagTest(existentFlag, set, get, true).test(tests, results);
    1.75 +        }
    1.76 +        new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);
    1.77 +    }
    1.78 +
    1.79 +    public final void test(T[] tests, T[] results) {
    1.80 +        Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");
    1.81 +        for (int i = 0, n = tests.length ; i < n; ++i) {
    1.82 +            test.accept(tests[i], results[i]);
    1.83 +        }
    1.84 +    }
    1.85 +
    1.86 +    protected String getVMOptionAsString() {
    1.87 +        HotSpotDiagnosticMXBean diagnostic
    1.88 +                = ManagementFactoryHelper.getDiagnosticMXBean();
    1.89 +        VMOption tmp;
    1.90 +        try {
    1.91 +            tmp = diagnostic.getVMOption(flagName);
    1.92 +        } catch (IllegalArgumentException e) {
    1.93 +            tmp = null;
    1.94 +        }
    1.95 +        return tmp == null ? null : tmp.getValue();
    1.96 +    }
    1.97 +
    1.98 +    private void testPositive(T value, T expected) {
    1.99 +        Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
   1.100 +        setNewValue(value);
   1.101 +        String newValue = getVMOptionAsString();
   1.102 +        Asserts.assertEQ(newValue, asString(expected));
   1.103 +        Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
   1.104 +    }
   1.105 +
   1.106 +    private void testNegative(T value, T expected) {
   1.107 +        String oldValue = getVMOptionAsString();
   1.108 +        Asserts.assertEQ(oldValue, asString(getValue()));
   1.109 +        setNewValue(value);
   1.110 +        String newValue = getVMOptionAsString();
   1.111 +        Asserts.assertEQ(oldValue, newValue);
   1.112 +    }
   1.113 +
   1.114 +    private String asString(Object value) {
   1.115 +        return value == null ? null : "" + value;
   1.116 +    }
   1.117 +}
   1.118 +

mercurial