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