Path: blob/master/test/hotspot/jtreg/serviceability/ParserTest.java
41144 views
/*1* Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test that the diagnostic command arguemnt parser works26* @modules java.base/jdk.internal.misc27* @library /test/lib28* @build sun.hotspot.WhiteBox29* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox30* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest31*/3233import java.math.BigInteger;3435import sun.hotspot.parser.DiagnosticCommand;36import sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType;37import sun.hotspot.WhiteBox;3839public class ParserTest {40WhiteBox wb;4142public ParserTest() throws Exception {43wb = WhiteBox.getWhiteBox();4445testNanoTime();46testJLong();47testBool();48testQuotes();49testMemorySize();50testSingleLetterArg();51}5253public static void main(String... args) throws Exception {54new ParserTest();55}5657public void testNanoTime() throws Exception {58String name = "name";59DiagnosticCommand arg = new DiagnosticCommand(name,60"desc", DiagnosticArgumentType.NANOTIME,61false, "0");62DiagnosticCommand[] args = {arg};6364BigInteger bi = new BigInteger("7");65//These should work66parse(name, bi.toString(), name + "=7ns", args);6768bi = bi.multiply(BigInteger.valueOf(1000));69parse(name, bi.toString(), name + "=7us", args);7071bi = bi.multiply(BigInteger.valueOf(1000));72parse(name, bi.toString(), name + "=7ms", args);7374bi = bi.multiply(BigInteger.valueOf(1000));75parse(name, bi.toString(), name + "=7s", args);7677bi = bi.multiply(BigInteger.valueOf(60));78parse(name, bi.toString() , name + "=7m", args);7980bi = bi.multiply(BigInteger.valueOf(60));81parse(name, bi.toString() , name + "=7h", args);8283bi = bi.multiply(BigInteger.valueOf(24));84parse(name, bi.toString() , name + "=7d", args);8586parse(name, "0", name + "=0", args);8788shouldFail(name + "=7xs", args);89shouldFail(name + "=7mms", args);90shouldFail(name + "=7f", args);91//Currently, only value 0 is allowed without unit92shouldFail(name + "=7", args);93}9495public void testJLong() throws Exception {96String name = "name";97DiagnosticCommand arg = new DiagnosticCommand(name,98"desc", DiagnosticArgumentType.JLONG,99false, "0");100DiagnosticCommand[] args = {arg};101102wb.parseCommandLine(name + "=10", ',', args);103parse(name, "10", name + "=10", args);104parse(name, "-5", name + "=-5", args);105106//shouldFail(name + "=12m", args); <-- should fail, doesn't107}108109public void testBool() throws Exception {110String name = "name";111DiagnosticCommand arg = new DiagnosticCommand(name,112"desc", DiagnosticArgumentType.BOOLEAN,113false, "false");114DiagnosticCommand[] args = {arg};115116parse(name, "true", name + "=true", args);117parse(name, "false", name + "=false", args);118parse(name, "true", name, args);119120//Empty commandline to parse, tests default value121//of the parameter "name"122parse(name, "false", "", args);123}124125public void testQuotes() throws Exception {126String name = "name";127DiagnosticCommand arg1 = new DiagnosticCommand(name,128"desc", DiagnosticArgumentType.STRING,129false, null);130DiagnosticCommand arg2 = new DiagnosticCommand("arg",131"desc", DiagnosticArgumentType.STRING,132false, null);133DiagnosticCommand[] args = {arg1, arg2};134135// try with a quoted value136parse(name, "Recording 1", name + "=\"Recording 1\"", args);137// try with a quoted argument138parse(name, "myrec", "\"" + name + "\"" + "=myrec", args);139// try with both a quoted value and a quoted argument140parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\"", args);141142// now the same thing but with other arguments after143144// try with a quoted value145parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args);146// try with a quoted argument147parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args);148// try with both a quoted value and a quoted argument149parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args);150}151152public void testSingleLetterArg() throws Exception {153DiagnosticCommand[] args = new DiagnosticCommand[]{154new DiagnosticCommand("flag", "desc", DiagnosticArgumentType.STRING, true, false, null),155new DiagnosticCommand("value", "desc", DiagnosticArgumentType.STRING, true, false, null)156};157parse("flag", "flag", "flag v", ' ', args);158parse("value", "v", "flag v", ' ', args);159}160161public void testMemorySize() throws Exception {162String name = "name";163String defaultValue = "1024";164DiagnosticCommand arg = new DiagnosticCommand(name,165"desc", DiagnosticArgumentType.MEMORYSIZE,166false, defaultValue);167DiagnosticCommand[] args = {arg};168169BigInteger bi = new BigInteger("7");170parse(name, bi.toString(), name + "=7b", args);171172bi = bi.multiply(BigInteger.valueOf(1024));173parse(name, bi.toString(), name + "=7k", args);174175bi = bi.multiply(BigInteger.valueOf(1024));176parse(name, bi.toString(), name + "=7m", args);177178bi = bi.multiply(BigInteger.valueOf(1024));179parse(name, bi.toString(), name + "=7g", args);180parse(name, defaultValue, "", args);181182//shouldFail(name + "=7gg", args); <---- should fail, doesn't183//shouldFail(name + "=7t", args); <----- should fail, doesn't184}185186public void parse(String searchName, String expectedValue,187String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {188parse(searchName, expectedValue, cmdLine, ',', argumentTypes);189}190public void parse(String searchName, String expectedValue,191String cmdLine, char delim, DiagnosticCommand[] argumentTypes) throws Exception {192//parseCommandLine will return an object array that looks like193//{<name of parsed object>, <of parsed object> ... }194Object[] res = wb.parseCommandLine(cmdLine, delim, argumentTypes);195for (int i = 0; i < res.length-1; i+=2) {196String parsedName = (String) res[i];197if (searchName.equals(parsedName)) {198String parsedValue = (String) res[i+1];199if (expectedValue.equals(parsedValue)) {200return;201} else {202throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"203+ searchName + " parsed as " + parsedValue204+ "! Expected: " + expectedValue);205}206}207}208throw new Exception(searchName + " not found as a parsed Argument!");209}210211private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {212shouldFail(argument, ',', argumentTypes);213}214private void shouldFail(String argument, char delim, DiagnosticCommand[] argumentTypes) throws Exception {215try {216wb.parseCommandLine(argument, delim, argumentTypes);217throw new Exception("Parser accepted argument: " + argument);218} catch (IllegalArgumentException e) {219//expected220}221}222}223224225