test/serviceability/ParserTest.java

Thu, 06 Dec 2012 12:04:17 -0800

author
katleman
date
Thu, 06 Dec 2012 12:04:17 -0800
changeset 4309
10587a580c51
parent 3905
5a1f452f8f90
child 4637
1b0dc9f87e75
permissions
-rw-r--r--

Added tag jdk8-b67 for changeset 25bdce771bb3

     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  */
     8 import java.math.BigInteger;
    10 import sun.hotspot.parser.DiagnosticCommand;
    11 import sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType;
    12 import sun.hotspot.WhiteBox;
    14 public class ParserTest {
    15     WhiteBox wb;
    17     public ParserTest() throws Exception {
    18         wb = WhiteBox.getWhiteBox();
    20         testNanoTime();
    21         testJLong();
    22         testBool();
    23         testQuotes();
    24         testMemorySize();
    25     }
    27     public static void main(String... args) throws Exception  {
    28          new ParserTest();
    29     }
    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};
    38         BigInteger bi = new BigInteger("7");
    39         //These should work
    40         parse(name, bi.toString(), name + "=7ns", args);
    42         bi = bi.multiply(BigInteger.valueOf(1000));
    43         parse(name, bi.toString(), name + "=7us", args);
    45         bi = bi.multiply(BigInteger.valueOf(1000));
    46         parse(name, bi.toString(), name + "=7ms", args);
    48         bi = bi.multiply(BigInteger.valueOf(1000));
    49         parse(name, bi.toString(), name + "=7s", args);
    51         bi = bi.multiply(BigInteger.valueOf(60));
    52         parse(name, bi.toString() , name + "=7m", args);
    54         bi = bi.multiply(BigInteger.valueOf(60));
    55         parse(name, bi.toString() , name + "=7h", args);
    57         bi = bi.multiply(BigInteger.valueOf(24));
    58         parse(name, bi.toString() , name + "=7d", args);
    60         parse(name, "0", name + "=0", args);
    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     }
    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};
    76         wb.parseCommandLine(name + "=10", args);
    77         parse(name, "10", name + "=10", args);
    78         parse(name, "-5", name + "=-5", args);
    80         //shouldFail(name + "=12m", args); <-- should fail, doesn't
    81     }
    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};
    90         parse(name, "true", name + "=true", args);
    91         parse(name, "false", name + "=false", args);
    92         parse(name, "true", name, args);
    94         //Empty commandline to parse, tests default value
    95         //of the parameter "name"
    96         parse(name, "false", "", args);
    97     }
    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};
   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);
   116         // now the same thing but with other arguments after
   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     }
   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};
   134         BigInteger bi = new BigInteger("7");
   135         parse(name, bi.toString(), name + "=7b", args);
   137         bi = bi.multiply(BigInteger.valueOf(1024));
   138         parse(name, bi.toString(), name + "=7k", args);
   140         bi = bi.multiply(BigInteger.valueOf(1024));
   141         parse(name, bi.toString(), name + "=7m", args);
   143         bi = bi.multiply(BigInteger.valueOf(1024));
   144         parse(name, bi.toString(), name + "=7g", args);
   145         parse(name, defaultValue, "", args);
   147         //shouldFail(name + "=7gg", args); <---- should fail, doesn't
   148         //shouldFail(name + "=7t", args);  <----- should fail, doesn't
   149     }
   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     }
   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 }

mercurial