rev 3388 : imported patch dcmd-fixes

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