|
1 /* |
|
2 * @test ParserTest |
|
3 * @summary verify that whitebox functions can be linked and executed |
|
4 * @run compile -J-XX:+UnlockDiagnosticVMOptions -J-XX:+WhiteBoxAPI ParserTest.java |
|
5 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest |
|
6 */ |
|
7 |
|
8 import java.math.BigInteger; |
|
9 |
|
10 import sun.hotspot.parser.DiagnosticCommand; |
|
11 import sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType; |
|
12 import sun.hotspot.WhiteBox; |
|
13 |
|
14 public class ParserTest { |
|
15 WhiteBox wb; |
|
16 |
|
17 public ParserTest() throws Exception { |
|
18 wb = WhiteBox.getWhiteBox(); |
|
19 |
|
20 testNanoTime(); |
|
21 testJLong(); |
|
22 testBool(); |
|
23 testMemorySize(); |
|
24 } |
|
25 |
|
26 public static void main(String... args) throws Exception { |
|
27 new ParserTest(); |
|
28 } |
|
29 |
|
30 public void testNanoTime() throws Exception { |
|
31 String name = "name"; |
|
32 DiagnosticCommand arg = new DiagnosticCommand(name, |
|
33 "desc", DiagnosticArgumentType.NANOTIME, |
|
34 false, "0"); |
|
35 DiagnosticCommand[] args = {arg}; |
|
36 |
|
37 BigInteger bi = new BigInteger("7"); |
|
38 //These should work |
|
39 parse(name, bi.toString(), name + "=7ns", args); |
|
40 |
|
41 bi = bi.multiply(BigInteger.valueOf(1000)); |
|
42 parse(name, bi.toString(), name + "=7us", args); |
|
43 |
|
44 bi = bi.multiply(BigInteger.valueOf(1000)); |
|
45 parse(name, bi.toString(), name + "=7ms", args); |
|
46 |
|
47 bi = bi.multiply(BigInteger.valueOf(1000)); |
|
48 parse(name, bi.toString(), name + "=7s", args); |
|
49 |
|
50 bi = bi.multiply(BigInteger.valueOf(60)); |
|
51 parse(name, bi.toString() , name + "=7m", args); |
|
52 |
|
53 bi = bi.multiply(BigInteger.valueOf(60)); |
|
54 parse(name, bi.toString() , name + "=7h", args); |
|
55 |
|
56 bi = bi.multiply(BigInteger.valueOf(24)); |
|
57 parse(name, bi.toString() , name + "=7d", args); |
|
58 |
|
59 parse(name, "0", name + "=0", args); |
|
60 |
|
61 shouldFail(name + "=7xs", args); |
|
62 shouldFail(name + "=7mms", args); |
|
63 shouldFail(name + "=7f", args); |
|
64 //Currently, only value 0 is allowed without unit |
|
65 shouldFail(name + "=7", args); |
|
66 } |
|
67 |
|
68 public void testJLong() throws Exception { |
|
69 String name = "name"; |
|
70 DiagnosticCommand arg = new DiagnosticCommand(name, |
|
71 "desc", DiagnosticArgumentType.JLONG, |
|
72 false, "0"); |
|
73 DiagnosticCommand[] args = {arg}; |
|
74 |
|
75 wb.parseCommandLine(name + "=10", args); |
|
76 parse(name, "10", name + "=10", args); |
|
77 parse(name, "-5", name + "=-5", args); |
|
78 |
|
79 //shouldFail(name + "=12m", args); <-- should fail, doesn't |
|
80 } |
|
81 |
|
82 public void testBool() throws Exception { |
|
83 String name = "name"; |
|
84 DiagnosticCommand arg = new DiagnosticCommand(name, |
|
85 "desc", DiagnosticArgumentType.BOOLEAN, |
|
86 false, "false"); |
|
87 DiagnosticCommand[] args = {arg}; |
|
88 |
|
89 parse(name, "true", name + "=true", args); |
|
90 parse(name, "false", name + "=false", args); |
|
91 parse(name, "true", name, args); |
|
92 |
|
93 //Empty commandline to parse, tests default value |
|
94 //of the parameter "name" |
|
95 parse(name, "false", "", args); |
|
96 } |
|
97 |
|
98 public void testMemorySize() throws Exception { |
|
99 String name = "name"; |
|
100 String defaultValue = "1024"; |
|
101 DiagnosticCommand arg = new DiagnosticCommand(name, |
|
102 "desc", DiagnosticArgumentType.MEMORYSIZE, |
|
103 false, defaultValue); |
|
104 DiagnosticCommand[] args = {arg}; |
|
105 |
|
106 BigInteger bi = new BigInteger("7"); |
|
107 parse(name, bi.toString(), name + "=7b", args); |
|
108 |
|
109 bi = bi.multiply(BigInteger.valueOf(1024)); |
|
110 parse(name, bi.toString(), name + "=7k", args); |
|
111 |
|
112 bi = bi.multiply(BigInteger.valueOf(1024)); |
|
113 parse(name, bi.toString(), name + "=7m", args); |
|
114 |
|
115 bi = bi.multiply(BigInteger.valueOf(1024)); |
|
116 parse(name, bi.toString(), name + "=7g", args); |
|
117 parse(name, defaultValue, "", args); |
|
118 |
|
119 //shouldFail(name + "=7gg", args); <---- should fail, doesn't |
|
120 //shouldFail(name + "=7t", args); <----- should fail, doesn't |
|
121 } |
|
122 |
|
123 public void parse(String searchName, String expectedValue, |
|
124 String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception { |
|
125 //parseCommandLine will return an object array that looks like |
|
126 //{<name of parsed object>, <of parsed object> ... } |
|
127 Object[] res = wb.parseCommandLine(cmdLine, argumentTypes); |
|
128 for (int i = 0; i < res.length-1; i+=2) { |
|
129 String parsedName = (String) res[i]; |
|
130 if (searchName.equals(parsedName)) { |
|
131 String parsedValue = (String) res[i+1]; |
|
132 if (expectedValue.equals(parsedValue)) { |
|
133 return; |
|
134 } else { |
|
135 throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n" |
|
136 + searchName + " parsed as " + parsedValue |
|
137 + "! Expected: " + expectedValue); |
|
138 } |
|
139 } |
|
140 } |
|
141 throw new Exception(searchName + " not found as a parsed Argument!"); |
|
142 } |
|
143 |
|
144 private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception { |
|
145 try { |
|
146 wb.parseCommandLine(argument, argumentTypes); |
|
147 throw new Exception("Parser accepted argument: " + argument); |
|
148 } catch (IllegalArgumentException e) { |
|
149 //expected |
|
150 } |
|
151 } |
|
152 } |