Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/serviceability/ParserTest.java
32281 views
/*1* Copyright (c) 2012, 2013, 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* @library /testlibrary /testlibrary/whitebox27* @build ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.parser.*28* @run main ClassFileInstaller sun.hotspot.WhiteBox29* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest30*/3132import java.math.BigInteger;3334import sun.hotspot.parser.DiagnosticCommand;35import sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType;36import sun.hotspot.WhiteBox;3738public class ParserTest {39WhiteBox wb;4041public ParserTest() throws Exception {42wb = WhiteBox.getWhiteBox();4344testNanoTime();45testJLong();46testBool();47testQuotes();48testMemorySize();49}5051public static void main(String... args) throws Exception {52new ParserTest();53}5455public void testNanoTime() throws Exception {56String name = "name";57DiagnosticCommand arg = new DiagnosticCommand(name,58"desc", DiagnosticArgumentType.NANOTIME,59false, "0");60DiagnosticCommand[] args = {arg};6162BigInteger bi = new BigInteger("7");63//These should work64parse(name, bi.toString(), name + "=7ns", args);6566bi = bi.multiply(BigInteger.valueOf(1000));67parse(name, bi.toString(), name + "=7us", args);6869bi = bi.multiply(BigInteger.valueOf(1000));70parse(name, bi.toString(), name + "=7ms", args);7172bi = bi.multiply(BigInteger.valueOf(1000));73parse(name, bi.toString(), name + "=7s", args);7475bi = bi.multiply(BigInteger.valueOf(60));76parse(name, bi.toString() , name + "=7m", args);7778bi = bi.multiply(BigInteger.valueOf(60));79parse(name, bi.toString() , name + "=7h", args);8081bi = bi.multiply(BigInteger.valueOf(24));82parse(name, bi.toString() , name + "=7d", args);8384parse(name, "0", name + "=0", args);8586shouldFail(name + "=7xs", args);87shouldFail(name + "=7mms", args);88shouldFail(name + "=7f", args);89//Currently, only value 0 is allowed without unit90shouldFail(name + "=7", args);91}9293public void testJLong() throws Exception {94String name = "name";95DiagnosticCommand arg = new DiagnosticCommand(name,96"desc", DiagnosticArgumentType.JLONG,97false, "0");98DiagnosticCommand[] args = {arg};99100wb.parseCommandLine(name + "=10", args);101parse(name, "10", name + "=10", args);102parse(name, "-5", name + "=-5", args);103104//shouldFail(name + "=12m", args); <-- should fail, doesn't105}106107public void testBool() throws Exception {108String name = "name";109DiagnosticCommand arg = new DiagnosticCommand(name,110"desc", DiagnosticArgumentType.BOOLEAN,111false, "false");112DiagnosticCommand[] args = {arg};113114parse(name, "true", name + "=true", args);115parse(name, "false", name + "=false", args);116parse(name, "true", name, args);117118//Empty commandline to parse, tests default value119//of the parameter "name"120parse(name, "false", "", args);121}122123public void testQuotes() throws Exception {124String name = "name";125DiagnosticCommand arg1 = new DiagnosticCommand(name,126"desc", DiagnosticArgumentType.STRING,127false, null);128DiagnosticCommand arg2 = new DiagnosticCommand("arg",129"desc", DiagnosticArgumentType.STRING,130false, null);131DiagnosticCommand[] args = {arg1, arg2};132133// try with a quoted value134parse(name, "Recording 1", name + "=\"Recording 1\"", args);135// try with a quoted argument136parse(name, "myrec", "\"" + name + "\"" + "=myrec", args);137// try with both a quoted value and a quoted argument138parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\"", args);139140// now the same thing but with other arguments after141142// try with a quoted value143parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args);144// try with a quoted argument145parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args);146// try with both a quoted value and a quoted argument147parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args);148}149150public void testMemorySize() throws Exception {151String name = "name";152String defaultValue = "1024";153DiagnosticCommand arg = new DiagnosticCommand(name,154"desc", DiagnosticArgumentType.MEMORYSIZE,155false, defaultValue);156DiagnosticCommand[] args = {arg};157158BigInteger bi = new BigInteger("7");159parse(name, bi.toString(), name + "=7b", args);160161bi = bi.multiply(BigInteger.valueOf(1024));162parse(name, bi.toString(), name + "=7k", args);163164bi = bi.multiply(BigInteger.valueOf(1024));165parse(name, bi.toString(), name + "=7m", args);166167bi = bi.multiply(BigInteger.valueOf(1024));168parse(name, bi.toString(), name + "=7g", args);169parse(name, defaultValue, "", args);170171//shouldFail(name + "=7gg", args); <---- should fail, doesn't172//shouldFail(name + "=7t", args); <----- should fail, doesn't173}174175public void parse(String searchName, String expectedValue,176String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {177//parseCommandLine will return an object array that looks like178//{<name of parsed object>, <of parsed object> ... }179Object[] res = wb.parseCommandLine(cmdLine, argumentTypes);180for (int i = 0; i < res.length-1; i+=2) {181String parsedName = (String) res[i];182if (searchName.equals(parsedName)) {183String parsedValue = (String) res[i+1];184if (expectedValue.equals(parsedValue)) {185return;186} else {187throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"188+ searchName + " parsed as " + parsedValue189+ "! Expected: " + expectedValue);190}191}192}193throw new Exception(searchName + " not found as a parsed Argument!");194}195196private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {197try {198wb.parseCommandLine(argument, argumentTypes);199throw new Exception("Parser accepted argument: " + argument);200} catch (IllegalArgumentException e) {201//expected202}203}204}205206207