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