4358N/A/*
4358N/A * Copyright (c) 2012, 2013 Oracle and/or its affiliates. All rights reserved.
4358N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4358N/A *
4358N/A * This code is free software; you can redistribute it and/or modify it
4358N/A * under the terms of the GNU General Public License version 2 only, as
4358N/A * published by the Free Software Foundation.
4358N/A *
4358N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4358N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4358N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4358N/A * version 2 for more details (a copy is included in the LICENSE file that
4358N/A * accompanied this code).
4358N/A *
4358N/A * You should have received a copy of the GNU General Public License version
4358N/A * 2 along with this work; if not, write to the Free Software Foundation,
4358N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4358N/A *
4358N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4358N/A * or visit www.oracle.com if you need additional information or have any
4358N/A * questions.
4358N/A */
4358N/A
3649N/A/*
3649N/A * @test ParserTest
4358N/A * @summary Test that the diagnostic command arguemnt parser works
4358N/A * @library /testlibrary /testlibrary/whitebox
4358N/A * @build ParserTest
4358N/A * @run main ClassFileInstaller sun.hotspot.WhiteBox
4358N/A * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest
3649N/A */
3649N/A
3649N/Aimport java.math.BigInteger;
3649N/A
3649N/Aimport sun.hotspot.parser.DiagnosticCommand;
3649N/Aimport sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType;
3649N/Aimport sun.hotspot.WhiteBox;
3649N/A
3649N/Apublic class ParserTest {
3649N/A WhiteBox wb;
3649N/A
3649N/A public ParserTest() throws Exception {
3649N/A wb = WhiteBox.getWhiteBox();
3649N/A
3649N/A testNanoTime();
3649N/A testJLong();
3649N/A testBool();
3868N/A testQuotes();
3649N/A testMemorySize();
3649N/A }
3649N/A
3649N/A public static void main(String... args) throws Exception {
3649N/A new ParserTest();
3649N/A }
3649N/A
3649N/A public void testNanoTime() throws Exception {
3649N/A String name = "name";
3649N/A DiagnosticCommand arg = new DiagnosticCommand(name,
3649N/A "desc", DiagnosticArgumentType.NANOTIME,
3649N/A false, "0");
3649N/A DiagnosticCommand[] args = {arg};
3649N/A
3649N/A BigInteger bi = new BigInteger("7");
3649N/A //These should work
3649N/A parse(name, bi.toString(), name + "=7ns", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1000));
3649N/A parse(name, bi.toString(), name + "=7us", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1000));
3649N/A parse(name, bi.toString(), name + "=7ms", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1000));
3649N/A parse(name, bi.toString(), name + "=7s", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(60));
3649N/A parse(name, bi.toString() , name + "=7m", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(60));
3649N/A parse(name, bi.toString() , name + "=7h", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(24));
3649N/A parse(name, bi.toString() , name + "=7d", args);
3649N/A
3649N/A parse(name, "0", name + "=0", args);
3649N/A
3649N/A shouldFail(name + "=7xs", args);
3649N/A shouldFail(name + "=7mms", args);
3649N/A shouldFail(name + "=7f", args);
3649N/A //Currently, only value 0 is allowed without unit
3649N/A shouldFail(name + "=7", args);
3649N/A }
3649N/A
3649N/A public void testJLong() throws Exception {
3649N/A String name = "name";
3649N/A DiagnosticCommand arg = new DiagnosticCommand(name,
3649N/A "desc", DiagnosticArgumentType.JLONG,
3649N/A false, "0");
3649N/A DiagnosticCommand[] args = {arg};
3649N/A
3649N/A wb.parseCommandLine(name + "=10", args);
3649N/A parse(name, "10", name + "=10", args);
3649N/A parse(name, "-5", name + "=-5", args);
3649N/A
3649N/A //shouldFail(name + "=12m", args); <-- should fail, doesn't
3649N/A }
3649N/A
3649N/A public void testBool() throws Exception {
3649N/A String name = "name";
3649N/A DiagnosticCommand arg = new DiagnosticCommand(name,
3649N/A "desc", DiagnosticArgumentType.BOOLEAN,
3649N/A false, "false");
3649N/A DiagnosticCommand[] args = {arg};
3649N/A
3649N/A parse(name, "true", name + "=true", args);
3649N/A parse(name, "false", name + "=false", args);
3649N/A parse(name, "true", name, args);
3649N/A
3649N/A //Empty commandline to parse, tests default value
3649N/A //of the parameter "name"
3649N/A parse(name, "false", "", args);
3649N/A }
3649N/A
3868N/A public void testQuotes() throws Exception {
3868N/A String name = "name";
3868N/A DiagnosticCommand arg1 = new DiagnosticCommand(name,
3868N/A "desc", DiagnosticArgumentType.STRING,
3868N/A false, null);
3868N/A DiagnosticCommand arg2 = new DiagnosticCommand("arg",
3868N/A "desc", DiagnosticArgumentType.STRING,
3868N/A false, null);
3868N/A DiagnosticCommand[] args = {arg1, arg2};
3868N/A
3868N/A // try with a quoted value
3868N/A parse(name, "Recording 1", name + "=\"Recording 1\"", args);
3868N/A // try with a quoted argument
3868N/A parse(name, "myrec", "\"" + name + "\"" + "=myrec", args);
3868N/A // try with both a quoted value and a quoted argument
3868N/A parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\"", args);
3868N/A
3868N/A // now the same thing but with other arguments after
3868N/A
3868N/A // try with a quoted value
3868N/A parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args);
3868N/A // try with a quoted argument
3868N/A parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args);
3868N/A // try with both a quoted value and a quoted argument
3868N/A parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args);
3868N/A }
3868N/A
3649N/A public void testMemorySize() throws Exception {
3649N/A String name = "name";
3649N/A String defaultValue = "1024";
3649N/A DiagnosticCommand arg = new DiagnosticCommand(name,
3649N/A "desc", DiagnosticArgumentType.MEMORYSIZE,
3649N/A false, defaultValue);
3649N/A DiagnosticCommand[] args = {arg};
3649N/A
3649N/A BigInteger bi = new BigInteger("7");
3649N/A parse(name, bi.toString(), name + "=7b", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1024));
3649N/A parse(name, bi.toString(), name + "=7k", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1024));
3649N/A parse(name, bi.toString(), name + "=7m", args);
3649N/A
3649N/A bi = bi.multiply(BigInteger.valueOf(1024));
3649N/A parse(name, bi.toString(), name + "=7g", args);
3649N/A parse(name, defaultValue, "", args);
3649N/A
3649N/A //shouldFail(name + "=7gg", args); <---- should fail, doesn't
3649N/A //shouldFail(name + "=7t", args); <----- should fail, doesn't
3649N/A }
3649N/A
3649N/A public void parse(String searchName, String expectedValue,
3649N/A String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {
3649N/A //parseCommandLine will return an object array that looks like
3649N/A //{<name of parsed object>, <of parsed object> ... }
3649N/A Object[] res = wb.parseCommandLine(cmdLine, argumentTypes);
3649N/A for (int i = 0; i < res.length-1; i+=2) {
3649N/A String parsedName = (String) res[i];
3649N/A if (searchName.equals(parsedName)) {
3649N/A String parsedValue = (String) res[i+1];
3649N/A if (expectedValue.equals(parsedValue)) {
3649N/A return;
3649N/A } else {
3649N/A throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"
3649N/A + searchName + " parsed as " + parsedValue
3649N/A + "! Expected: " + expectedValue);
3649N/A }
3649N/A }
3649N/A }
3649N/A throw new Exception(searchName + " not found as a parsed Argument!");
3649N/A }
3649N/A
3649N/A private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {
3649N/A try {
3649N/A wb.parseCommandLine(argument, argumentTypes);
3649N/A throw new Exception("Parser accepted argument: " + argument);
3649N/A } catch (IllegalArgumentException e) {
3649N/A //expected
3649N/A }
3649N/A }
3649N/A}