jdk/test/javax/management/query/QuerySubstringTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
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 4886033
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Query.{initial,any,final}SubString fail if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          matching constraint string contains wildcards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.QueryExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class QuerySubstringTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static interface SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        public String getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static class Simple implements SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        public Simple(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        private String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static String[][] data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        { "a*b?c\\d[e-f]",   "OK", "OK", "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        { "a*b?c\\d[e-f]g",  "OK", "OK", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        { "za*b?c\\d[e-f]",  "KO", "OK", "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        { "za*b?c\\d[e-f]g", "KO", "OK", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        { "a*b?c\\de",       "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        { "a*b?c\\deg",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        { "za*b?c\\de",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        { "za*b?c\\deg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        { "a*b?c\\df",       "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        { "a*b?c\\dfg",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        { "za*b?c\\df",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        { "za*b?c\\dfg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        { "axxbxc\\de",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        { "axxbxc\\deg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        { "zaxxbxc\\de",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        { "zaxxbxc\\deg",    "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        { "axxbxc\\df",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        { "axxbxc\\dfg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        { "zaxxbxc\\df",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        { "zaxxbxc\\dfg",    "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static int query(MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                             int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                             String substring,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                             String[][] data) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        String querySubString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                querySubString = "InitialSubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                querySubString = "AnySubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                querySubString = "FinalSubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        System.out.println("\n" + querySubString + " = " + substring + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (int i = 0; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            ObjectName on = new ObjectName("test:type=Simple,query=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                           querySubString + ",name=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            Simple s = new Simple(data[i][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            mbs.registerMBean(s, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            QueryExp q = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    q = Query.initialSubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                               Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    q = Query.anySubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                           Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    q = Query.finalSubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                             Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            q.setMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            boolean r = q.apply(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            System.out.print("Attribute Value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                mbs.getAttribute(on, "String"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (r && "OK".equals(data[i][type])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                System.out.println(" OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            } else if (!r && "KO".equals(data[i][type])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                System.out.println(" KO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println(" Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        String pattern = "a*b?c\\d[e-f]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
          "\n--- Test javax.management.Query.{initial|any|final}SubString ---");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        error += query(mbs, 1, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        error += query(mbs, 2, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        error += query(mbs, 3, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            System.out.println("\nTest failed! " + error + " errors.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            throw new IllegalArgumentException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.out.println("\nTest passed!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}