jdk
223 строки · 8.5 Кб
1/*
2* Copyright (c) 2012, 2022, 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/*
25* @test
26* @summary Test that the diagnostic command arguemnt parser works
27* @modules java.base/jdk.internal.misc
28* @library /test/lib
29* @build jdk.test.whitebox.WhiteBox
30* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
31* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest
32*/
33
34import java.math.BigInteger;
35
36import jdk.test.whitebox.parser.DiagnosticCommand;
37import jdk.test.whitebox.parser.DiagnosticCommand.DiagnosticArgumentType;
38import jdk.test.whitebox.WhiteBox;
39
40public class ParserTest {
41WhiteBox wb;
42
43public ParserTest() throws Exception {
44wb = WhiteBox.getWhiteBox();
45
46testNanoTime();
47testJLong();
48testBool();
49testQuotes();
50testMemorySize();
51testSingleLetterArg();
52}
53
54public static void main(String... args) throws Exception {
55new ParserTest();
56}
57
58public void testNanoTime() throws Exception {
59String name = "name";
60DiagnosticCommand arg = new DiagnosticCommand(name,
61"desc", DiagnosticArgumentType.NANOTIME,
62false, "0");
63DiagnosticCommand[] args = {arg};
64
65BigInteger bi = new BigInteger("7");
66//These should work
67parse(name, bi.toString(), name + "=7ns", args);
68
69bi = bi.multiply(BigInteger.valueOf(1000));
70parse(name, bi.toString(), name + "=7us", args);
71
72bi = bi.multiply(BigInteger.valueOf(1000));
73parse(name, bi.toString(), name + "=7ms", args);
74
75bi = bi.multiply(BigInteger.valueOf(1000));
76parse(name, bi.toString(), name + "=7s", args);
77
78bi = bi.multiply(BigInteger.valueOf(60));
79parse(name, bi.toString() , name + "=7m", args);
80
81bi = bi.multiply(BigInteger.valueOf(60));
82parse(name, bi.toString() , name + "=7h", args);
83
84bi = bi.multiply(BigInteger.valueOf(24));
85parse(name, bi.toString() , name + "=7d", args);
86
87parse(name, "0", name + "=0", args);
88
89shouldFail(name + "=7xs", args);
90shouldFail(name + "=7mms", args);
91shouldFail(name + "=7f", args);
92//Currently, only value 0 is allowed without unit
93shouldFail(name + "=7", args);
94}
95
96public void testJLong() throws Exception {
97String name = "name";
98DiagnosticCommand arg = new DiagnosticCommand(name,
99"desc", DiagnosticArgumentType.JLONG,
100false, "0");
101DiagnosticCommand[] args = {arg};
102
103wb.parseCommandLine(name + "=10", ',', args);
104parse(name, "10", name + "=10", args);
105parse(name, "-5", name + "=-5", args);
106
107//shouldFail(name + "=12m", args); <-- should fail, doesn't
108}
109
110public void testBool() throws Exception {
111String name = "name";
112DiagnosticCommand arg = new DiagnosticCommand(name,
113"desc", DiagnosticArgumentType.BOOLEAN,
114false, "false");
115DiagnosticCommand[] args = {arg};
116
117parse(name, "true", name + "=true", args);
118parse(name, "false", name + "=false", args);
119parse(name, "true", name, args);
120
121//Empty commandline to parse, tests default value
122//of the parameter "name"
123parse(name, "false", "", args);
124}
125
126public void testQuotes() throws Exception {
127String name = "name";
128DiagnosticCommand arg1 = new DiagnosticCommand(name,
129"desc", DiagnosticArgumentType.STRING,
130false, null);
131DiagnosticCommand arg2 = new DiagnosticCommand("arg",
132"desc", DiagnosticArgumentType.STRING,
133false, null);
134DiagnosticCommand[] args = {arg1, arg2};
135
136// try with a quoted value
137parse(name, "Recording 1", name + "=\"Recording 1\"", args);
138// try with a quoted argument
139parse(name, "myrec", "\"" + name + "\"" + "=myrec", args);
140// try with both a quoted value and a quoted argument
141parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\"", args);
142
143// now the same thing but with other arguments after
144
145// try with a quoted value
146parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args);
147// try with a quoted argument
148parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args);
149// try with both a quoted value and a quoted argument
150parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args);
151}
152
153public void testSingleLetterArg() throws Exception {
154DiagnosticCommand[] args = new DiagnosticCommand[]{
155new DiagnosticCommand("flag", "desc", DiagnosticArgumentType.STRING, true, false, null),
156new DiagnosticCommand("value", "desc", DiagnosticArgumentType.STRING, true, false, null)
157};
158parse("flag", "flag", "flag v", ' ', args);
159parse("value", "v", "flag v", ' ', args);
160}
161
162public void testMemorySize() throws Exception {
163String name = "name";
164String defaultValue = "1024";
165DiagnosticCommand arg = new DiagnosticCommand(name,
166"desc", DiagnosticArgumentType.MEMORYSIZE,
167false, defaultValue);
168DiagnosticCommand[] args = {arg};
169
170BigInteger bi = new BigInteger("7");
171parse(name, bi.toString(), name + "=7b", args);
172
173bi = bi.multiply(BigInteger.valueOf(1024));
174parse(name, bi.toString(), name + "=7k", args);
175
176bi = bi.multiply(BigInteger.valueOf(1024));
177parse(name, bi.toString(), name + "=7m", args);
178
179bi = bi.multiply(BigInteger.valueOf(1024));
180parse(name, bi.toString(), name + "=7g", args);
181parse(name, defaultValue, "", args);
182
183//shouldFail(name + "=7gg", args); <---- should fail, doesn't
184//shouldFail(name + "=7t", args); <----- should fail, doesn't
185}
186
187public void parse(String searchName, String expectedValue,
188String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {
189parse(searchName, expectedValue, cmdLine, ',', argumentTypes);
190}
191public void parse(String searchName, String expectedValue,
192String cmdLine, char delim, DiagnosticCommand[] argumentTypes) throws Exception {
193//parseCommandLine will return an object array that looks like
194//{<name of parsed object>, <of parsed object> ... }
195Object[] res = wb.parseCommandLine(cmdLine, delim, argumentTypes);
196for (int i = 0; i < res.length-1; i+=2) {
197String parsedName = (String) res[i];
198if (searchName.equals(parsedName)) {
199String parsedValue = (String) res[i+1];
200if (expectedValue.equals(parsedValue)) {
201return;
202} else {
203throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"
204+ searchName + " parsed as " + parsedValue
205+ "! Expected: " + expectedValue);
206}
207}
208}
209throw new Exception(searchName + " not found as a parsed Argument!");
210}
211
212private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {
213shouldFail(argument, ',', argumentTypes);
214}
215private void shouldFail(String argument, char delim, DiagnosticCommand[] argumentTypes) throws Exception {
216try {
217wb.parseCommandLine(argument, delim, argumentTypes);
218throw new Exception("Parser accepted argument: " + argument);
219} catch (IllegalArgumentException e) {
220//expected
221}
222}
223}
224