jdk

Форк
0
/
ParserTest.java 
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

34
import java.math.BigInteger;
35

36
import jdk.test.whitebox.parser.DiagnosticCommand;
37
import jdk.test.whitebox.parser.DiagnosticCommand.DiagnosticArgumentType;
38
import jdk.test.whitebox.WhiteBox;
39

40
public class ParserTest {
41
    WhiteBox wb;
42

43
    public ParserTest() throws Exception {
44
        wb = WhiteBox.getWhiteBox();
45

46
        testNanoTime();
47
        testJLong();
48
        testBool();
49
        testQuotes();
50
        testMemorySize();
51
        testSingleLetterArg();
52
    }
53

54
    public static void main(String... args) throws Exception  {
55
         new ParserTest();
56
    }
57

58
    public void testNanoTime() throws Exception {
59
        String name = "name";
60
        DiagnosticCommand arg = new DiagnosticCommand(name,
61
                "desc", DiagnosticArgumentType.NANOTIME,
62
                false, "0");
63
        DiagnosticCommand[] args = {arg};
64

65
        BigInteger bi = new BigInteger("7");
66
        //These should work
67
        parse(name, bi.toString(), name + "=7ns", args);
68

69
        bi = bi.multiply(BigInteger.valueOf(1000));
70
        parse(name, bi.toString(), name + "=7us", args);
71

72
        bi = bi.multiply(BigInteger.valueOf(1000));
73
        parse(name, bi.toString(), name + "=7ms", args);
74

75
        bi = bi.multiply(BigInteger.valueOf(1000));
76
        parse(name, bi.toString(), name + "=7s", args);
77

78
        bi = bi.multiply(BigInteger.valueOf(60));
79
        parse(name, bi.toString() , name + "=7m", args);
80

81
        bi = bi.multiply(BigInteger.valueOf(60));
82
        parse(name, bi.toString() , name + "=7h", args);
83

84
        bi = bi.multiply(BigInteger.valueOf(24));
85
        parse(name, bi.toString() , name + "=7d", args);
86

87
        parse(name, "0", name + "=0", args);
88

89
        shouldFail(name + "=7xs", args);
90
        shouldFail(name + "=7mms", args);
91
        shouldFail(name + "=7f", args);
92
        //Currently, only value 0 is allowed without unit
93
        shouldFail(name + "=7", args);
94
    }
95

96
    public void testJLong() throws Exception {
97
        String name = "name";
98
        DiagnosticCommand arg = new DiagnosticCommand(name,
99
                "desc", DiagnosticArgumentType.JLONG,
100
                false, "0");
101
        DiagnosticCommand[] args = {arg};
102

103
        wb.parseCommandLine(name + "=10", ',', args);
104
        parse(name, "10", name + "=10", args);
105
        parse(name, "-5", name + "=-5", args);
106

107
        //shouldFail(name + "=12m", args); <-- should fail, doesn't
108
    }
109

110
    public void testBool() throws Exception {
111
        String name = "name";
112
        DiagnosticCommand arg = new DiagnosticCommand(name,
113
                "desc", DiagnosticArgumentType.BOOLEAN,
114
                false, "false");
115
        DiagnosticCommand[] args = {arg};
116

117
        parse(name, "true", name + "=true", args);
118
        parse(name, "false", name + "=false", args);
119
        parse(name, "true", name, args);
120

121
        //Empty commandline to parse, tests default value
122
        //of the parameter "name"
123
        parse(name, "false", "", args);
124
    }
125

126
    public void testQuotes() throws Exception {
127
        String name = "name";
128
        DiagnosticCommand arg1 = new DiagnosticCommand(name,
129
                "desc", DiagnosticArgumentType.STRING,
130
                false, null);
131
        DiagnosticCommand arg2 = new DiagnosticCommand("arg",
132
                "desc", DiagnosticArgumentType.STRING,
133
                false, null);
134
        DiagnosticCommand[] args = {arg1, arg2};
135

136
        // try with a quoted value
137
        parse(name, "Recording 1", name + "=\"Recording 1\"", args);
138
        // try with a quoted argument
139
        parse(name, "myrec", "\"" + name + "\"" + "=myrec", args);
140
        // try with both a quoted value and a quoted argument
141
        parse(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
146
        parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args);
147
        // try with a quoted argument
148
        parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args);
149
        // try with both a quoted value and a quoted argument
150
        parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args);
151
    }
152

153
    public void testSingleLetterArg() throws Exception {
154
        DiagnosticCommand[] args = new DiagnosticCommand[]{
155
            new DiagnosticCommand("flag", "desc", DiagnosticArgumentType.STRING, true, false, null),
156
            new DiagnosticCommand("value", "desc", DiagnosticArgumentType.STRING, true, false, null)
157
        };
158
        parse("flag", "flag", "flag v", ' ', args);
159
        parse("value", "v", "flag v", ' ', args);
160
    }
161

162
    public void testMemorySize() throws Exception {
163
        String name = "name";
164
        String defaultValue = "1024";
165
        DiagnosticCommand arg = new DiagnosticCommand(name,
166
                "desc", DiagnosticArgumentType.MEMORYSIZE,
167
                false, defaultValue);
168
        DiagnosticCommand[] args = {arg};
169

170
        BigInteger bi = new BigInteger("7");
171
        parse(name, bi.toString(), name + "=7b", args);
172

173
        bi = bi.multiply(BigInteger.valueOf(1024));
174
        parse(name, bi.toString(), name + "=7k", args);
175

176
        bi = bi.multiply(BigInteger.valueOf(1024));
177
        parse(name, bi.toString(), name + "=7m", args);
178

179
        bi = bi.multiply(BigInteger.valueOf(1024));
180
        parse(name, bi.toString(), name + "=7g", args);
181
        parse(name, defaultValue, "", args);
182

183
        //shouldFail(name + "=7gg", args); <---- should fail, doesn't
184
        //shouldFail(name + "=7t", args);  <----- should fail, doesn't
185
    }
186

187
    public void parse(String searchName, String expectedValue,
188
            String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {
189
        parse(searchName, expectedValue, cmdLine, ',', argumentTypes);
190
    }
191
    public void parse(String searchName, String expectedValue,
192
            String 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> ... }
195
        Object[] res = wb.parseCommandLine(cmdLine, delim, argumentTypes);
196
        for (int i = 0; i < res.length-1; i+=2) {
197
            String parsedName = (String) res[i];
198
            if (searchName.equals(parsedName)) {
199
                String parsedValue = (String) res[i+1];
200
                if (expectedValue.equals(parsedValue)) {
201
                    return;
202
                } else {
203
                    throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"
204
                            + searchName + " parsed as " + parsedValue
205
                            + "! Expected: " + expectedValue);
206
                }
207
            }
208
        }
209
        throw new Exception(searchName + " not found as a parsed Argument!");
210
    }
211

212
    private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {
213
        shouldFail(argument, ',', argumentTypes);
214
    }
215
    private void shouldFail(String argument, char delim, DiagnosticCommand[] argumentTypes) throws Exception {
216
        try {
217
            wb.parseCommandLine(argument, delim, argumentTypes);
218
            throw new Exception("Parser accepted argument: " + argument);
219
        } catch (IllegalArgumentException e) {
220
            //expected
221
        }
222
    }
223
}
224

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.