author | redestad |
Thu, 06 Nov 2014 15:25:56 +0100 | |
changeset 27350 | 94a990d3754a |
parent 25736 | 6f35dbe32581 |
child 27417 | 576e2b527e1c |
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; |
|
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() { |
|
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
|
58 |
return get.apply(flagName); |
24921 | 59 |
} |
60 |
||
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 |
} |
|
65 |
||
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 |
} |
|
73 |
||
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 |
} |
|
80 |
||
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 |
} |
|
92 |
||
93 |
private void testPositive(T value, T expected) { |
|
25736 | 94 |
String oldValue = getVMOptionAsString(); |
95 |
Asserts.assertEQ(oldValue, asString(getValue())); |
|
96 |
Asserts.assertEQ(oldValue, asString(WHITE_BOX.getVMFlag(flagName))); |
|
24921 | 97 |
setNewValue(value); |
98 |
String newValue = getVMOptionAsString(); |
|
99 |
Asserts.assertEQ(newValue, asString(expected)); |
|
25736 | 100 |
Asserts.assertEQ(newValue, asString(getValue())); |
101 |
Asserts.assertEQ(newValue, asString(WHITE_BOX.getVMFlag(flagName))); |
|
24921 | 102 |
} |
103 |
||
104 |
private void testNegative(T value, T expected) { |
|
105 |
String oldValue = getVMOptionAsString(); |
|
106 |
Asserts.assertEQ(oldValue, asString(getValue())); |
|
25736 | 107 |
Asserts.assertEQ(oldValue, asString(WHITE_BOX.getVMFlag(flagName))); |
24921 | 108 |
setNewValue(value); |
109 |
String newValue = getVMOptionAsString(); |
|
110 |
Asserts.assertEQ(oldValue, newValue); |
|
111 |
} |
|
112 |
||
113 |
private String asString(Object value) { |
|
114 |
return value == null ? null : "" + value; |
|
115 |
} |
|
116 |
} |
|
117 |