comparison test/testlibrary_tests/whitebox/vm_flags/VmFlagTest.java @ 21817:12478c5eb000

Merge
author asaha
date Fri, 24 Oct 2014 17:09:30 -0700
parents 0c48231c5c84
children
comparison
equal deleted inserted replaced
21815:37179dcf830a 21817:12478c5eb000
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 */
23
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.*;
31
32 public final class VmFlagTest<T> {
33 public static final WhiteBox WHITE_BOX = WhiteBox.getWhiteBox();
34
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;
40
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 }
52
53 private void setNewValue(T value) {
54 set.accept(flagName, value);
55 }
56
57 private T getValue() {
58 T t = get.apply(flagName);
59 System.out.println("T = " + t);
60 return t;
61 }
62
63 protected static <T> void runTest(String existentFlag, T[] tests,
64 BiConsumer<String, T> set, Function<String, T> get) {
65 runTest(existentFlag, tests, tests, set, get);
66 }
67
68 protected static <T> void runTest(String existentFlag, T[] tests,
69 T[] results, BiConsumer<String, T> set, Function<String, T> get) {
70 if (existentFlag != null) {
71 new VmFlagTest(existentFlag, set, get, true).test(tests, results);
72 }
73 new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results);
74 }
75
76 public final void test(T[] tests, T[] results) {
77 Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length");
78 for (int i = 0, n = tests.length ; i < n; ++i) {
79 test.accept(tests[i], results[i]);
80 }
81 }
82
83 protected String getVMOptionAsString() {
84 HotSpotDiagnosticMXBean diagnostic
85 = ManagementFactoryHelper.getDiagnosticMXBean();
86 VMOption tmp;
87 try {
88 tmp = diagnostic.getVMOption(flagName);
89 } catch (IllegalArgumentException e) {
90 tmp = null;
91 }
92 return tmp == null ? null : tmp.getValue();
93 }
94
95 private void testPositive(T value, T expected) {
96 Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
97 setNewValue(value);
98 String newValue = getVMOptionAsString();
99 Asserts.assertEQ(newValue, asString(expected));
100 Asserts.assertEQ(getVMOptionAsString(), asString(getValue()));
101 }
102
103 private void testNegative(T value, T expected) {
104 String oldValue = getVMOptionAsString();
105 Asserts.assertEQ(oldValue, asString(getValue()));
106 setNewValue(value);
107 String newValue = getVMOptionAsString();
108 Asserts.assertEQ(oldValue, newValue);
109 }
110
111 private String asString(Object value) {
112 return value == null ? null : "" + value;
113 }
114 }
115