author | dcubed |
Fri, 31 Oct 2014 10:15:29 -0700 | |
changeset 27464 | 37671dcc4d8d |
parent 27417 | 576e2b527e1c |
child 30254 | 224dcf3ebade |
permissions | -rw-r--r-- |
24921 | 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; |
|
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
40 |
private final boolean isPositive; |
24921 | 41 |
|
42 |
protected VmFlagTest(String flagName, BiConsumer<String, T> set, |
|
43 |
Function<String, T> get, boolean isPositive) { |
|
44 |
this.flagName = flagName; |
|
45 |
this.set = set; |
|
46 |
this.get = get; |
|
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
47 |
this.isPositive = isPositive; |
24921 | 48 |
if (isPositive) { |
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
49 |
test = this::testWritePositive; |
24921 | 50 |
} else { |
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
51 |
test = this::testWriteNegative; |
24921 | 52 |
} |
53 |
} |
|
54 |
||
55 |
private void setNewValue(T value) { |
|
56 |
set.accept(flagName, value); |
|
57 |
} |
|
58 |
||
59 |
private T getValue() { |
|
24950
002a4742a989
8044575: testlibrary_tests/whitebox/vm_flags/UintxTest.java failed: assert(!res || TypeEntriesAtCall::arguments_profiling_enabled()) failed: no profiling of arguments
iignatyev
parents:
24921
diff
changeset
|
60 |
return get.apply(flagName); |
24921 | 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 |
||
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
68 |
protected static <T> void runTest(String existentFlag, Function<String, T> get) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
69 |
runTest(existentFlag, null, null, null, get); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
70 |
} |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
71 |
|
24921 | 72 |
protected static <T> void runTest(String existentFlag, T[] tests, |
73 |
T[] results, BiConsumer<String, T> set, Function<String, T> get) { |
|
74 |
if (existentFlag != null) { |
|
75 |
new VmFlagTest(existentFlag, set, get, true).test(tests, results); |
|
76 |
} |
|
77 |
new VmFlagTest(NONEXISTENT_FLAG, set, get, false).test(tests, results); |
|
78 |
} |
|
79 |
||
80 |
public final void test(T[] tests, T[] results) { |
|
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
81 |
if (isPositive) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
82 |
testRead(); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
83 |
} |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
84 |
if (tests != null) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
85 |
Asserts.assertEQ(tests.length, results.length, "[TESTBUG] tests.length != results.length"); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
86 |
for (int i = 0, n = tests.length ; i < n; ++i) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
87 |
test.accept(tests[i], results[i]); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
88 |
} |
24921 | 89 |
} |
90 |
} |
|
91 |
||
92 |
protected String getVMOptionAsString() { |
|
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
93 |
if (WHITE_BOX.isConstantVMFlag(flagName) || WHITE_BOX.isLockedVMFlag(flagName)) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
94 |
// JMM cannot access debug flags in product builds or locked flags, |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
95 |
// use whitebox methods to get such flags value. |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
96 |
return asString(getValue()); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
97 |
} |
24921 | 98 |
HotSpotDiagnosticMXBean diagnostic |
99 |
= ManagementFactoryHelper.getDiagnosticMXBean(); |
|
100 |
VMOption tmp; |
|
101 |
try { |
|
102 |
tmp = diagnostic.getVMOption(flagName); |
|
103 |
} catch (IllegalArgumentException e) { |
|
104 |
tmp = null; |
|
105 |
} |
|
106 |
return tmp == null ? null : tmp.getValue(); |
|
107 |
} |
|
108 |
||
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
109 |
private String testRead() { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
110 |
String value = getVMOptionAsString(); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
111 |
Asserts.assertNotNull(value); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
112 |
Asserts.assertEQ(value, asString(getValue())); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
113 |
Asserts.assertEQ(value, asString(WHITE_BOX.getVMFlag(flagName))); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
114 |
return value; |
24921 | 115 |
} |
116 |
||
27417
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
117 |
private void testWritePositive(T value, T expected) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
118 |
setNewValue(value); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
119 |
String newValue = testRead(); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
120 |
Asserts.assertEQ(newValue, asString(expected)); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
121 |
} |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
122 |
|
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
123 |
private void testWriteNegative(T value, T expected) { |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
124 |
// Should always return false for non-existing flags |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
125 |
Asserts.assertFalse(WHITE_BOX.isConstantVMFlag(flagName)); |
576e2b527e1c
8061443: Whitebox get*VMFlag() methods fail with develop flags in product builds
thartmann
parents:
25736
diff
changeset
|
126 |
Asserts.assertFalse(WHITE_BOX.isLockedVMFlag(flagName)); |
24921 | 127 |
String oldValue = getVMOptionAsString(); |
128 |
Asserts.assertEQ(oldValue, asString(getValue())); |
|
25736 | 129 |
Asserts.assertEQ(oldValue, asString(WHITE_BOX.getVMFlag(flagName))); |
24921 | 130 |
setNewValue(value); |
131 |
String newValue = getVMOptionAsString(); |
|
132 |
Asserts.assertEQ(oldValue, newValue); |
|
133 |
} |
|
134 |
||
135 |
private String asString(Object value) { |
|
136 |
return value == null ? null : "" + value; |
|
137 |
} |
|
138 |
} |