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         testMemorySize();
  24     }
  25 
  26     public static void main(String... args) throws Exception  {
  27          new ParserTest();
  28     }
  29 
  30     public void testNanoTime() throws Exception {
  31         String name = "name";
  32         DiagnosticCommand arg = new DiagnosticCommand(name,
  33                 "desc", DiagnosticArgumentType.NANOTIME,
  34                 false, "0");
  35         DiagnosticCommand[] args = {arg};
  36 
  37         BigInteger bi = new BigInteger("7");
  38         //These should work
  39         parse(name, bi.toString(), name + "=7ns", args);
  40 
  41         bi = bi.multiply(BigInteger.valueOf(1000));
  42         parse(name, bi.toString(), name + "=7us", args);
  43 
  44         bi = bi.multiply(BigInteger.valueOf(1000));
  45         parse(name, bi.toString(), name + "=7ms", args);
  46 
  47         bi = bi.multiply(BigInteger.valueOf(1000));
  48         parse(name, bi.toString(), name + "=7s", args);
  49 
  50         bi = bi.multiply(BigInteger.valueOf(60));
  51         parse(name, bi.toString() , name + "=7m", args);
  52 
  53         bi = bi.multiply(BigInteger.valueOf(60));
  54         parse(name, bi.toString() , name + "=7h", args);
  55 
  56         bi = bi.multiply(BigInteger.valueOf(24));
  57         parse(name, bi.toString() , name + "=7d", args);
  58 
  59         parse(name, "0", name + "=0", args);
  60 
  61         shouldFail(name + "=7xs", args);
  62         shouldFail(name + "=7mms", args);
  63         shouldFail(name + "=7f", args);
  64         //Currently, only value 0 is allowed without unit
  65         shouldFail(name + "=7", args);
  66     }
  67 
  68     public void testJLong() throws Exception {
  69         String name = "name";
  70         DiagnosticCommand arg = new DiagnosticCommand(name,
  71                 "desc", DiagnosticArgumentType.JLONG,
  72                 false, "0");
  73         DiagnosticCommand[] args = {arg};
  74 
  75         wb.parseCommandLine(name + "=10", args);
  76         parse(name, "10", name + "=10", args);
  77         parse(name, "-5", name + "=-5", args);
  78 
  79         //shouldFail(name + "=12m", args); <-- should fail, doesn't
  80     }
  81 
  82     public void testBool() throws Exception {
  83         String name = "name";
  84         DiagnosticCommand arg = new DiagnosticCommand(name,
  85                 "desc", DiagnosticArgumentType.BOOLEAN,
  86                 false, "false");
  87         DiagnosticCommand[] args = {arg};
  88 
  89         parse(name, "true", name + "=true", args);
  90         parse(name, "false", name + "=false", args);
  91         parse(name, "true", name, args);
  92         parse(name, "false", "", args);
  93     }
  94 
  95     public void testMemorySize() throws Exception {
  96         String name = "name";
  97         String defaultValue = "1024";
  98         DiagnosticCommand arg = new DiagnosticCommand(name,
  99                 "desc", DiagnosticArgumentType.MEMORYSIZE,
 100                 false, defaultValue);
 101         DiagnosticCommand[] args = {arg};
 102 
 103         BigInteger bi = new BigInteger("7");
 104         parse(name, bi.toString(), name + "=7b", args);
 105 
 106         bi = bi.multiply(BigInteger.valueOf(1024));
 107         parse(name, bi.toString(), name + "=7k", args);
 108 
 109         bi = bi.multiply(BigInteger.valueOf(1024));
 110         parse(name, bi.toString(), name + "=7m", args);
 111 
 112         bi = bi.multiply(BigInteger.valueOf(1024));
 113         parse(name, bi.toString(), name + "=7g", args);
 114 
 115         parse(name, defaultValue, "", args);
 116 
 117         //shouldFail(name + "=7gg", args); <---- should fail, doesn't
 118         //shouldFail(name + "=7t", args);  <----- should fail, doesn't
 119     }
 120 
 121     public void parse(String searchName, String expectedValue,
 122             String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception {
 123         Object[] res = wb.parseCommandLine(cmdLine, argumentTypes);
 124         for (int i = 0; i < res.length-1; i++) {
 125             String parsedName = (String) res[i];
 126             if (searchName.equals(parsedName)) {
 127                 String parsedValue = (String) res[i+1];
 128                 if (expectedValue.equals(parsedValue)) {
 129                     return;
 130                 } else {
 131                     throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n"
 132                             + searchName + " parsed as " + parsedValue
 133                             + "! Expected: " + expectedValue);
 134                 }
 135             }
 136         }
 137         throw new Exception(searchName + " not found as a parsed Argument!");
 138     }
 139 
 140     private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception {
 141         try {
 142             wb.parseCommandLine(argument, argumentTypes);
 143             throw new Exception("Parser accepted argument: " + argument);
 144         } catch (IllegalArgumentException e) {
 145             //expected
 146         }
 147     }
 148 }