author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 686 | d0c74839e1bd |
child 4156 | acaa49a2768a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2003-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 |
* have any questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4886011 |
|
27 |
* @summary Test that QueryExp.toString() is reversible |
|
28 |
* @author Eamonn McManus |
|
29 |
* @run clean QueryExpStringTest |
|
30 |
* @run build QueryExpStringTest |
|
31 |
* @run main QueryExpStringTest |
|
32 |
*/ |
|
33 |
||
34 | 34 |
// This test is mostly obsolete, since we now have Query.fromString. |
35 |
// The test includes its own parser, from which Query.fromString was derived. |
|
36 |
// The parsers are not identical and the one here is no longer maintained. |
|
37 |
||
2 | 38 |
import java.util.*; |
39 |
import javax.management.*; |
|
40 |
||
41 |
public class QueryExpStringTest { |
|
42 |
||
43 |
private static final ValueExp |
|
44 |
attr = Query.attr("attr"), |
|
45 |
qattr = Query.attr("className", "attr"), |
|
34 | 46 |
aa = Query.attr("A"), |
47 |
bb = Query.attr("B"), |
|
48 |
cc = Query.attr("C"), |
|
49 |
dd = Query.attr("D"), |
|
50 |
zero = Query.value(0), |
|
2 | 51 |
classattr = Query.classattr(), |
52 |
simpleString = Query.value("simpleString"), |
|
53 |
complexString = Query.value("a'b\\'\""), |
|
54 |
intValue = Query.value(12345678), |
|
55 |
integerValue = Query.value(new Integer(12345678)), |
|
56 |
longValue = Query.value(12345678L), |
|
57 |
floatValue = Query.value(2.5f), |
|
58 |
doubleValue = Query.value(2.5d), |
|
59 |
booleanValue = Query.value(true), |
|
60 |
plusValue = Query.plus(intValue, integerValue), |
|
61 |
timesValue = Query.times(doubleValue, floatValue), |
|
62 |
minusValue = Query.minus(floatValue, doubleValue), |
|
63 |
divValue = Query.div(doubleValue, floatValue); |
|
64 |
||
65 |
private static final QueryExp |
|
66 |
gt = Query.gt(intValue, floatValue), |
|
67 |
geq = Query.geq(intValue, floatValue), |
|
68 |
leq = Query.leq(intValue, floatValue), |
|
69 |
lt = Query.lt(intValue, floatValue), |
|
70 |
eq = Query.eq(intValue, floatValue), |
|
71 |
between = Query.between(intValue, floatValue, doubleValue), |
|
72 |
match = Query.match((AttributeValueExp) attr, |
|
73 |
(StringValueExp) simpleString), |
|
74 |
initial = Query.initialSubString((AttributeValueExp) attr, |
|
75 |
(StringValueExp) simpleString), |
|
76 |
initialStar = Query.initialSubString((AttributeValueExp) attr, |
|
77 |
Query.value("*")), |
|
34 | 78 |
initialPercent = Query.initialSubString((AttributeValueExp) attr, |
79 |
Query.value("%")), |
|
2 | 80 |
any = Query.anySubString((AttributeValueExp) attr, |
81 |
(StringValueExp) simpleString), |
|
82 |
anyStar = Query.anySubString((AttributeValueExp) attr, |
|
83 |
Query.value("*")), |
|
34 | 84 |
anyPercent = Query.anySubString((AttributeValueExp) attr, |
85 |
Query.value("%")), |
|
2 | 86 |
ffinal = Query.finalSubString((AttributeValueExp) attr, |
87 |
(StringValueExp) simpleString), |
|
88 |
finalMagic = Query.finalSubString((AttributeValueExp) attr, |
|
89 |
Query.value("?*[\\")), |
|
90 |
in = Query.in(intValue, new ValueExp[] {intValue, floatValue}), |
|
91 |
and = Query.and(gt, lt), |
|
92 |
or = Query.or(gt, lt), |
|
34 | 93 |
not = Query.not(gt), |
94 |
aPlusB_PlusC = Query.gt(Query.plus(Query.plus(aa, bb), cc), zero), |
|
95 |
aPlus_BPlusC = Query.gt(Query.plus(aa, Query.plus(bb, cc)), zero); |
|
2 | 96 |
|
97 |
// Commented-out tests below require change to implementation |
|
98 |
||
99 |
private static final Object tests[] = { |
|
100 |
attr, "attr", |
|
34 | 101 |
// qattr, "className.attr", |
102 |
// Preceding form now appears as className#attr, an incompatible change |
|
103 |
// which we don't mind much because nobody uses the two-arg Query.attr. |
|
2 | 104 |
classattr, "Class", |
105 |
simpleString, "'simpleString'", |
|
34 | 106 |
complexString, "'a''b\\\''\"'", |
2 | 107 |
intValue, "12345678", |
108 |
integerValue, "12345678", |
|
109 |
longValue, "12345678", |
|
110 |
floatValue, "2.5", |
|
111 |
doubleValue, "2.5", |
|
112 |
booleanValue, "true", |
|
113 |
plusValue, "12345678 + 12345678", |
|
114 |
timesValue, "2.5 * 2.5", |
|
115 |
minusValue, "2.5 - 2.5", |
|
116 |
divValue, "2.5 / 2.5", |
|
117 |
gt, "(12345678) > (2.5)", |
|
118 |
geq, "(12345678) >= (2.5)", |
|
119 |
leq, "(12345678) <= (2.5)", |
|
120 |
lt, "(12345678) < (2.5)", |
|
121 |
eq, "(12345678) = (2.5)", |
|
122 |
between, "(12345678) between (2.5) and (2.5)", |
|
123 |
match, "attr like 'simpleString'", |
|
686
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
124 |
initial, "attr like 'simpleString*'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
125 |
initialStar, "attr like '\\**'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
126 |
initialPercent, "attr like '%*'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
127 |
any, "attr like '*simpleString*'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
128 |
anyStar, "attr like '*\\**'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
129 |
anyPercent, "attr like '*%*'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
130 |
ffinal, "attr like '*simpleString'", |
d0c74839e1bd
6701498: Change JMX query language to use * and ? as wildcards rather than % and _
emcmanus
parents:
34
diff
changeset
|
131 |
finalMagic, "attr like '*\\?\\*\\[\\\\'", |
2 | 132 |
in, "12345678 in (12345678, 2.5)", |
133 |
and, "((12345678) > (2.5)) and ((12345678) < (2.5))", |
|
134 |
or, "((12345678) > (2.5)) or ((12345678) < (2.5))", |
|
135 |
not, "not ((12345678) > (2.5))", |
|
34 | 136 |
aPlusB_PlusC, "(A + B + C) > (0)", |
137 |
// aPlus_BPlusC, "(A + (B + C)) > (0)", |
|
2 | 138 |
}; |
139 |
||
140 |
public static void main(String[] args) throws Exception { |
|
141 |
System.out.println("Testing QueryExp.toString()"); |
|
142 |
||
143 |
boolean ok = true; |
|
144 |
||
145 |
for (int i = 0; i < tests.length; i += 2) { |
|
146 |
String testString = tests[i].toString(); |
|
147 |
String expected = (String) tests[i + 1]; |
|
148 |
if (expected.equals(testString)) |
|
149 |
System.out.println("OK: " + expected); |
|
150 |
else { |
|
151 |
System.err.println("Expected: {" + expected + "}; got: {" + |
|
152 |
testString + "}"); |
|
153 |
ok = false; |
|
154 |
} |
|
155 |
||
156 |
try { |
|
157 |
Object parsed; |
|
158 |
String[] expectedref = new String[] {expected}; |
|
159 |
if (tests[i] instanceof ValueExp) |
|
160 |
parsed = parseExp(expectedref); |
|
161 |
else |
|
162 |
parsed = parseQuery(expectedref); |
|
163 |
if (expectedref[0].length() > 0) |
|
164 |
throw new Exception("Junk after parse: " + expectedref[0]); |
|
165 |
String parsedString = parsed.toString(); |
|
166 |
if (parsedString.equals(expected)) |
|
167 |
System.out.println("OK: parsed " + parsedString); |
|
168 |
else { |
|
169 |
System.err.println("Parse differs: expected: {" + |
|
170 |
expected + "}; got: {" + |
|
171 |
parsedString + "}"); |
|
172 |
ok = false; |
|
173 |
} |
|
174 |
} catch (Exception e) { |
|
175 |
System.err.println("Parse got exception: {" + expected + |
|
176 |
"}: " + e); |
|
177 |
ok = false; |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
if (ok) |
|
182 |
System.out.println("Test passed"); |
|
183 |
else { |
|
184 |
System.out.println("TEST FAILED"); |
|
185 |
System.exit(1); |
|
186 |
} |
|
187 |
} |
|
188 |
||
189 |
private static QueryExp parseQuery(String[] ss) throws Exception { |
|
190 |
if (skip(ss, "(")) |
|
191 |
return parseQueryAfterParen(ss); |
|
192 |
||
193 |
if (skip(ss, "not (")) { |
|
194 |
QueryExp not = parseQuery(ss); |
|
195 |
if (!skip(ss, ")")) |
|
196 |
throw new Exception("Expected ) after not (..."); |
|
197 |
return Query.not(not); |
|
198 |
} |
|
199 |
||
200 |
ValueExp exp = parseExp(ss); |
|
201 |
||
202 |
if (skip(ss, " like ")) { |
|
203 |
ValueExp pat = parseExp(ss); |
|
204 |
if (!(exp instanceof AttributeValueExp && |
|
205 |
pat instanceof StringValueExp)) { |
|
206 |
throw new Exception("Expected types `attr like string': " + |
|
207 |
exp + " like " + pat); |
|
208 |
} |
|
34 | 209 |
StringValueExp spat = (StringValueExp) pat; |
210 |
return Query.match((AttributeValueExp) exp, spat); |
|
2 | 211 |
} |
212 |
||
213 |
if (skip(ss, " in (")) { |
|
214 |
List values = new ArrayList(); |
|
215 |
if (!skip(ss, ")")) { |
|
216 |
do { |
|
217 |
values.add(parseExp(ss)); |
|
218 |
} while (skip(ss, ", ")); |
|
219 |
if (!skip(ss, ")")) |
|
220 |
throw new Exception("Expected ) after in (..."); |
|
221 |
} |
|
222 |
return Query.in(exp, (ValueExp[]) values.toArray(new ValueExp[0])); |
|
223 |
} |
|
224 |
||
225 |
throw new Exception("Expected in or like after expression"); |
|
226 |
} |
|
227 |
||
228 |
private static QueryExp parseQueryAfterParen(String[] ss) |
|
229 |
throws Exception { |
|
230 |
/* This is very ugly. We might have "(q1) and (q2)" here, or |
|
231 |
we might have "(e1) < (e2)". Since the syntax for a query |
|
232 |
(q1) is not the same as for an expression (e1), but can |
|
233 |
begin with one, we try to parse the query, and if we get an |
|
234 |
exception we then try to parse an expression. It's a hacky |
|
235 |
kind of look-ahead. */ |
|
236 |
String start = ss[0]; |
|
237 |
try { |
|
238 |
QueryExp lhs = parseQuery(ss); |
|
239 |
QueryExp result; |
|
240 |
||
241 |
if (skip(ss, ") and (")) |
|
242 |
result = Query.and(lhs, parseQuery(ss)); |
|
243 |
else if (skip(ss, ") or (")) |
|
244 |
result = Query.or(lhs, parseQuery(ss)); |
|
245 |
else |
|
246 |
throw new Exception("Expected `) and/or ('"); |
|
247 |
if (!skip(ss, ")")) |
|
248 |
throw new Exception("Expected `)' after subquery"); |
|
249 |
return result; |
|
250 |
} catch (Exception e) { |
|
251 |
ss[0] = start; |
|
252 |
ValueExp lhs = parseExp(ss); |
|
253 |
if (!skip(ss, ") ")) |
|
34 | 254 |
throw new Exception("Expected `) ' after subexpression: " + ss[0]); |
2 | 255 |
String op = scanWord(ss); |
256 |
if (!skip(ss, " (")) |
|
257 |
throw new Exception("Expected ` (' after `" + op + "'"); |
|
258 |
ValueExp rhs = parseExp(ss); |
|
259 |
if (!skip(ss, ")")) |
|
260 |
throw new Exception("Expected `)' after subexpression"); |
|
261 |
if (op.equals("=")) |
|
262 |
return Query.eq(lhs, rhs); |
|
263 |
if (op.equals("<")) |
|
264 |
return Query.lt(lhs, rhs); |
|
265 |
if (op.equals(">")) |
|
266 |
return Query.gt(lhs, rhs); |
|
267 |
if (op.equals("<=")) |
|
268 |
return Query.leq(lhs, rhs); |
|
269 |
if (op.equals(">=")) |
|
270 |
return Query.geq(lhs, rhs); |
|
271 |
if (!op.equals("between")) |
|
272 |
throw new Exception("Unknown operator `" + op + "'"); |
|
273 |
if (!skip(ss, " and (")) |
|
274 |
throw new Exception("Expected ` and (' after between"); |
|
275 |
ValueExp high = parseExp(ss); |
|
276 |
if (!skip(ss, ")")) |
|
277 |
throw new Exception("Expected `)' after subexpression"); |
|
278 |
return Query.between(lhs, rhs, high); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
private static ValueExp parseExp(String[] ss) throws Exception { |
|
34 | 283 |
ValueExp lhs = parsePrimary(ss); |
2 | 284 |
|
34 | 285 |
while (true) { |
2 | 286 |
/* Look ahead to see if we have an arithmetic operator. */ |
287 |
String back = ss[0]; |
|
288 |
if (!skip(ss, " ")) |
|
34 | 289 |
return lhs; |
2 | 290 |
if (ss[0].equals("") || "+-*/".indexOf(ss[0].charAt(0)) < 0) { |
291 |
ss[0] = back; |
|
34 | 292 |
return lhs; |
2 | 293 |
} |
294 |
||
295 |
final String op = scanWord(ss); |
|
296 |
if (op.length() != 1) |
|
297 |
throw new Exception("Expected arithmetic operator after space"); |
|
298 |
if ("+-*/".indexOf(op) < 0) |
|
299 |
throw new Exception("Unknown arithmetic operator: " + op); |
|
300 |
if (!skip(ss, " ")) |
|
301 |
throw new Exception("Expected space after arithmetic operator"); |
|
34 | 302 |
ValueExp rhs = parsePrimary(ss); |
2 | 303 |
switch (op.charAt(0)) { |
34 | 304 |
case '+': lhs = Query.plus(lhs, rhs); break; |
305 |
case '-': lhs = Query.minus(lhs, rhs); break; |
|
306 |
case '*': lhs = Query.times(lhs, rhs); break; |
|
307 |
case '/': lhs = Query.div(lhs, rhs); break; |
|
2 | 308 |
default: throw new Exception("Can't happen: " + op.charAt(0)); |
309 |
} |
|
310 |
} |
|
34 | 311 |
} |
2 | 312 |
|
313 |
private static ValueExp parsePrimary(String[] ss) throws Exception { |
|
314 |
String s = ss[0]; |
|
315 |
||
316 |
if (s.length() == 0) |
|
317 |
throw new Exception("Empty string found, expression expected"); |
|
318 |
||
319 |
char first = s.charAt(0); |
|
320 |
||
321 |
if (first == ' ') |
|
322 |
throw new Exception("Space found, expression expected"); |
|
323 |
||
324 |
if (first == '-' || Character.isDigit(first)) |
|
325 |
return parseNumberExp(ss); |
|
326 |
||
327 |
if (first == '\'') |
|
328 |
return parseString(ss); |
|
329 |
||
330 |
if (matchWord(ss, "true")) |
|
331 |
return Query.value(true); |
|
332 |
||
333 |
if (matchWord(ss, "false")) |
|
334 |
return Query.value(false); |
|
335 |
||
336 |
if (matchWord(ss, "Class")) |
|
337 |
return Query.classattr(); |
|
338 |
||
339 |
String word = scanWord(ss); |
|
340 |
int lastDot = word.lastIndexOf('.'); |
|
341 |
if (lastDot < 0) |
|
342 |
return Query.attr(word); |
|
343 |
else |
|
344 |
return Query.attr(word.substring(0, lastDot), |
|
345 |
word.substring(lastDot + 1)); |
|
346 |
} |
|
347 |
||
348 |
private static String scanWord(String[] ss) throws Exception { |
|
349 |
String s = ss[0]; |
|
350 |
int space = s.indexOf(' '); |
|
34 | 351 |
int rpar = s.indexOf(')'); |
352 |
if (space < 0 && rpar < 0) { |
|
2 | 353 |
ss[0] = ""; |
354 |
return s; |
|
355 |
} |
|
34 | 356 |
int stop; |
357 |
if (space >= 0 && rpar >= 0) // string has both space and ), stop at first |
|
358 |
stop = Math.min(space, rpar); |
|
359 |
else // string has only one, stop at it |
|
360 |
stop = Math.max(space, rpar); |
|
361 |
String word = s.substring(0, stop); |
|
362 |
ss[0] = s.substring(stop); |
|
363 |
return word; |
|
2 | 364 |
} |
365 |
||
366 |
private static boolean matchWord(String[] ss, String word) |
|
367 |
throws Exception { |
|
368 |
String s = ss[0]; |
|
369 |
if (s.startsWith(word)) { |
|
370 |
int len = word.length(); |
|
371 |
if (s.length() == len || s.charAt(len) == ' ' |
|
372 |
|| s.charAt(len) == ')') { |
|
373 |
ss[0] = s.substring(len); |
|
374 |
return true; |
|
375 |
} |
|
376 |
} |
|
377 |
return false; |
|
378 |
} |
|
379 |
||
380 |
private static ValueExp parseNumberExp(String[] ss) throws Exception { |
|
381 |
String s = ss[0]; |
|
382 |
int len = s.length(); |
|
383 |
boolean isFloat = false; |
|
384 |
int i; |
|
385 |
for (i = 0; i < len; i++) { |
|
386 |
char c = s.charAt(i); |
|
387 |
if (Character.isDigit(c) || c == '-' || c == '+') |
|
388 |
continue; |
|
389 |
if (c == '.' || c == 'e' || c == 'E') { |
|
390 |
isFloat = true; |
|
391 |
continue; |
|
392 |
} |
|
393 |
break; |
|
394 |
} |
|
395 |
ss[0] = s.substring(i); |
|
396 |
s = s.substring(0, i); |
|
397 |
if (isFloat) |
|
398 |
return Query.value(Double.parseDouble(s)); |
|
399 |
else |
|
400 |
return Query.value(Long.parseLong(s)); |
|
401 |
} |
|
402 |
||
403 |
private static ValueExp parseString(String[] ss) throws Exception { |
|
404 |
if (!skip(ss, "'")) |
|
405 |
throw new Exception("Expected ' at start of string"); |
|
406 |
String s = ss[0]; |
|
407 |
int len = s.length(); |
|
408 |
StringBuffer buf = new StringBuffer(); |
|
409 |
int i; |
|
410 |
for (i = 0; i < len; i++) { |
|
411 |
char c = s.charAt(i); |
|
412 |
if (c == '\'') { |
|
34 | 413 |
++i; |
414 |
if (i >= len || s.charAt(i) != '\'') { |
|
415 |
ss[0] = s.substring(i); |
|
2 | 416 |
return Query.value(buf.toString()); |
417 |
} |
|
418 |
} |
|
419 |
buf.append(c); |
|
420 |
} |
|
421 |
throw new Exception("No closing ' at end of string"); |
|
422 |
} |
|
423 |
||
424 |
private static boolean skip(String[] ss, String skip) { |
|
425 |
if (ss[0].startsWith(skip)) { |
|
426 |
ss[0] = ss[0].substring(skip.length()); |
|
427 |
return true; |
|
428 |
} else |
|
429 |
return false; |
|
430 |
} |
|
431 |
} |