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