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