Fri, 25 Jul 2014 14:14:14 +0100
Merge
mgerdin@4637 | 1 | /* |
katleman@4916 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
mgerdin@4637 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mgerdin@4637 | 4 | * |
mgerdin@4637 | 5 | * This code is free software; you can redistribute it and/or modify it |
mgerdin@4637 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mgerdin@4637 | 7 | * published by the Free Software Foundation. |
mgerdin@4637 | 8 | * |
mgerdin@4637 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mgerdin@4637 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mgerdin@4637 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mgerdin@4637 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mgerdin@4637 | 13 | * accompanied this code). |
mgerdin@4637 | 14 | * |
mgerdin@4637 | 15 | * You should have received a copy of the GNU General Public License version |
mgerdin@4637 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mgerdin@4637 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mgerdin@4637 | 18 | * |
mgerdin@4637 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mgerdin@4637 | 20 | * or visit www.oracle.com if you need additional information or have any |
mgerdin@4637 | 21 | * questions. |
mgerdin@4637 | 22 | */ |
mgerdin@4637 | 23 | |
nloodin@3681 | 24 | /* |
ykantser@6717 | 25 | * @test |
mgerdin@4637 | 26 | * @summary Test that the diagnostic command arguemnt parser works |
mgerdin@4637 | 27 | * @library /testlibrary /testlibrary/whitebox |
ykantser@6717 | 28 | * @build ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.parser.* |
mgerdin@4637 | 29 | * @run main ClassFileInstaller sun.hotspot.WhiteBox |
mgerdin@4637 | 30 | * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ParserTest |
nloodin@3681 | 31 | */ |
nloodin@3681 | 32 | |
nloodin@3681 | 33 | import java.math.BigInteger; |
nloodin@3681 | 34 | |
nloodin@3681 | 35 | import sun.hotspot.parser.DiagnosticCommand; |
nloodin@3681 | 36 | import sun.hotspot.parser.DiagnosticCommand.DiagnosticArgumentType; |
nloodin@3681 | 37 | import sun.hotspot.WhiteBox; |
nloodin@3681 | 38 | |
nloodin@3681 | 39 | public class ParserTest { |
nloodin@3681 | 40 | WhiteBox wb; |
nloodin@3681 | 41 | |
nloodin@3681 | 42 | public ParserTest() throws Exception { |
nloodin@3681 | 43 | wb = WhiteBox.getWhiteBox(); |
nloodin@3681 | 44 | |
nloodin@3681 | 45 | testNanoTime(); |
nloodin@3681 | 46 | testJLong(); |
nloodin@3681 | 47 | testBool(); |
sla@3905 | 48 | testQuotes(); |
nloodin@3681 | 49 | testMemorySize(); |
nloodin@3681 | 50 | } |
nloodin@3681 | 51 | |
nloodin@3681 | 52 | public static void main(String... args) throws Exception { |
nloodin@3681 | 53 | new ParserTest(); |
nloodin@3681 | 54 | } |
nloodin@3681 | 55 | |
nloodin@3681 | 56 | public void testNanoTime() throws Exception { |
nloodin@3681 | 57 | String name = "name"; |
nloodin@3681 | 58 | DiagnosticCommand arg = new DiagnosticCommand(name, |
nloodin@3681 | 59 | "desc", DiagnosticArgumentType.NANOTIME, |
nloodin@3681 | 60 | false, "0"); |
nloodin@3681 | 61 | DiagnosticCommand[] args = {arg}; |
nloodin@3681 | 62 | |
nloodin@3681 | 63 | BigInteger bi = new BigInteger("7"); |
nloodin@3681 | 64 | //These should work |
nloodin@3681 | 65 | parse(name, bi.toString(), name + "=7ns", args); |
nloodin@3681 | 66 | |
nloodin@3681 | 67 | bi = bi.multiply(BigInteger.valueOf(1000)); |
nloodin@3681 | 68 | parse(name, bi.toString(), name + "=7us", args); |
nloodin@3681 | 69 | |
nloodin@3681 | 70 | bi = bi.multiply(BigInteger.valueOf(1000)); |
nloodin@3681 | 71 | parse(name, bi.toString(), name + "=7ms", args); |
nloodin@3681 | 72 | |
nloodin@3681 | 73 | bi = bi.multiply(BigInteger.valueOf(1000)); |
nloodin@3681 | 74 | parse(name, bi.toString(), name + "=7s", args); |
nloodin@3681 | 75 | |
nloodin@3681 | 76 | bi = bi.multiply(BigInteger.valueOf(60)); |
nloodin@3681 | 77 | parse(name, bi.toString() , name + "=7m", args); |
nloodin@3681 | 78 | |
nloodin@3681 | 79 | bi = bi.multiply(BigInteger.valueOf(60)); |
nloodin@3681 | 80 | parse(name, bi.toString() , name + "=7h", args); |
nloodin@3681 | 81 | |
nloodin@3681 | 82 | bi = bi.multiply(BigInteger.valueOf(24)); |
nloodin@3681 | 83 | parse(name, bi.toString() , name + "=7d", args); |
nloodin@3681 | 84 | |
nloodin@3681 | 85 | parse(name, "0", name + "=0", args); |
nloodin@3681 | 86 | |
nloodin@3681 | 87 | shouldFail(name + "=7xs", args); |
nloodin@3681 | 88 | shouldFail(name + "=7mms", args); |
nloodin@3681 | 89 | shouldFail(name + "=7f", args); |
nloodin@3681 | 90 | //Currently, only value 0 is allowed without unit |
nloodin@3681 | 91 | shouldFail(name + "=7", args); |
nloodin@3681 | 92 | } |
nloodin@3681 | 93 | |
nloodin@3681 | 94 | public void testJLong() throws Exception { |
nloodin@3681 | 95 | String name = "name"; |
nloodin@3681 | 96 | DiagnosticCommand arg = new DiagnosticCommand(name, |
nloodin@3681 | 97 | "desc", DiagnosticArgumentType.JLONG, |
nloodin@3681 | 98 | false, "0"); |
nloodin@3681 | 99 | DiagnosticCommand[] args = {arg}; |
nloodin@3681 | 100 | |
nloodin@3681 | 101 | wb.parseCommandLine(name + "=10", args); |
nloodin@3681 | 102 | parse(name, "10", name + "=10", args); |
nloodin@3681 | 103 | parse(name, "-5", name + "=-5", args); |
nloodin@3681 | 104 | |
nloodin@3681 | 105 | //shouldFail(name + "=12m", args); <-- should fail, doesn't |
nloodin@3681 | 106 | } |
nloodin@3681 | 107 | |
nloodin@3681 | 108 | public void testBool() throws Exception { |
nloodin@3681 | 109 | String name = "name"; |
nloodin@3681 | 110 | DiagnosticCommand arg = new DiagnosticCommand(name, |
nloodin@3681 | 111 | "desc", DiagnosticArgumentType.BOOLEAN, |
nloodin@3681 | 112 | false, "false"); |
nloodin@3681 | 113 | DiagnosticCommand[] args = {arg}; |
nloodin@3681 | 114 | |
nloodin@3681 | 115 | parse(name, "true", name + "=true", args); |
nloodin@3681 | 116 | parse(name, "false", name + "=false", args); |
nloodin@3681 | 117 | parse(name, "true", name, args); |
nloodin@3681 | 118 | |
nloodin@3681 | 119 | //Empty commandline to parse, tests default value |
nloodin@3681 | 120 | //of the parameter "name" |
nloodin@3681 | 121 | parse(name, "false", "", args); |
nloodin@3681 | 122 | } |
nloodin@3681 | 123 | |
sla@3905 | 124 | public void testQuotes() throws Exception { |
sla@3905 | 125 | String name = "name"; |
sla@3905 | 126 | DiagnosticCommand arg1 = new DiagnosticCommand(name, |
sla@3905 | 127 | "desc", DiagnosticArgumentType.STRING, |
sla@3905 | 128 | false, null); |
sla@3905 | 129 | DiagnosticCommand arg2 = new DiagnosticCommand("arg", |
sla@3905 | 130 | "desc", DiagnosticArgumentType.STRING, |
sla@3905 | 131 | false, null); |
sla@3905 | 132 | DiagnosticCommand[] args = {arg1, arg2}; |
sla@3905 | 133 | |
sla@3905 | 134 | // try with a quoted value |
sla@3905 | 135 | parse(name, "Recording 1", name + "=\"Recording 1\"", args); |
sla@3905 | 136 | // try with a quoted argument |
sla@3905 | 137 | parse(name, "myrec", "\"" + name + "\"" + "=myrec", args); |
sla@3905 | 138 | // try with both a quoted value and a quoted argument |
sla@3905 | 139 | parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\"", args); |
sla@3905 | 140 | |
sla@3905 | 141 | // now the same thing but with other arguments after |
sla@3905 | 142 | |
sla@3905 | 143 | // try with a quoted value |
sla@3905 | 144 | parse(name, "Recording 1", name + "=\"Recording 1\",arg=value", args); |
sla@3905 | 145 | // try with a quoted argument |
sla@3905 | 146 | parse(name, "myrec", "\"" + name + "\"" + "=myrec,arg=value", args); |
sla@3905 | 147 | // try with both a quoted value and a quoted argument |
sla@3905 | 148 | parse(name, "Recording 1", "\"" + name + "\"" + "=\"Recording 1\",arg=value", args); |
sla@3905 | 149 | } |
sla@3905 | 150 | |
nloodin@3681 | 151 | public void testMemorySize() throws Exception { |
nloodin@3681 | 152 | String name = "name"; |
nloodin@3681 | 153 | String defaultValue = "1024"; |
nloodin@3681 | 154 | DiagnosticCommand arg = new DiagnosticCommand(name, |
nloodin@3681 | 155 | "desc", DiagnosticArgumentType.MEMORYSIZE, |
nloodin@3681 | 156 | false, defaultValue); |
nloodin@3681 | 157 | DiagnosticCommand[] args = {arg}; |
nloodin@3681 | 158 | |
nloodin@3681 | 159 | BigInteger bi = new BigInteger("7"); |
nloodin@3681 | 160 | parse(name, bi.toString(), name + "=7b", args); |
nloodin@3681 | 161 | |
nloodin@3681 | 162 | bi = bi.multiply(BigInteger.valueOf(1024)); |
nloodin@3681 | 163 | parse(name, bi.toString(), name + "=7k", args); |
nloodin@3681 | 164 | |
nloodin@3681 | 165 | bi = bi.multiply(BigInteger.valueOf(1024)); |
nloodin@3681 | 166 | parse(name, bi.toString(), name + "=7m", args); |
nloodin@3681 | 167 | |
nloodin@3681 | 168 | bi = bi.multiply(BigInteger.valueOf(1024)); |
nloodin@3681 | 169 | parse(name, bi.toString(), name + "=7g", args); |
nloodin@3681 | 170 | parse(name, defaultValue, "", args); |
nloodin@3681 | 171 | |
nloodin@3681 | 172 | //shouldFail(name + "=7gg", args); <---- should fail, doesn't |
nloodin@3681 | 173 | //shouldFail(name + "=7t", args); <----- should fail, doesn't |
nloodin@3681 | 174 | } |
nloodin@3681 | 175 | |
nloodin@3681 | 176 | public void parse(String searchName, String expectedValue, |
nloodin@3681 | 177 | String cmdLine, DiagnosticCommand[] argumentTypes) throws Exception { |
nloodin@3681 | 178 | //parseCommandLine will return an object array that looks like |
nloodin@3681 | 179 | //{<name of parsed object>, <of parsed object> ... } |
nloodin@3681 | 180 | Object[] res = wb.parseCommandLine(cmdLine, argumentTypes); |
nloodin@3681 | 181 | for (int i = 0; i < res.length-1; i+=2) { |
nloodin@3681 | 182 | String parsedName = (String) res[i]; |
nloodin@3681 | 183 | if (searchName.equals(parsedName)) { |
nloodin@3681 | 184 | String parsedValue = (String) res[i+1]; |
nloodin@3681 | 185 | if (expectedValue.equals(parsedValue)) { |
nloodin@3681 | 186 | return; |
nloodin@3681 | 187 | } else { |
nloodin@3681 | 188 | throw new Exception("Parsing of cmdline '" + cmdLine + "' failed!\n" |
nloodin@3681 | 189 | + searchName + " parsed as " + parsedValue |
nloodin@3681 | 190 | + "! Expected: " + expectedValue); |
nloodin@3681 | 191 | } |
nloodin@3681 | 192 | } |
nloodin@3681 | 193 | } |
nloodin@3681 | 194 | throw new Exception(searchName + " not found as a parsed Argument!"); |
nloodin@3681 | 195 | } |
nloodin@3681 | 196 | |
nloodin@3681 | 197 | private void shouldFail(String argument, DiagnosticCommand[] argumentTypes) throws Exception { |
nloodin@3681 | 198 | try { |
nloodin@3681 | 199 | wb.parseCommandLine(argument, argumentTypes); |
nloodin@3681 | 200 | throw new Exception("Parser accepted argument: " + argument); |
nloodin@3681 | 201 | } catch (IllegalArgumentException e) { |
nloodin@3681 | 202 | //expected |
nloodin@3681 | 203 | } |
nloodin@3681 | 204 | } |
nloodin@3681 | 205 | } |