jdk/test/javax/management/query/QuerySubstringTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 5506 202f599c92aa
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 5506
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main QuerySubstringTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.management.ManagementFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.Query;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.QueryExp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class QuerySubstringTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public static interface SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        public String getString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static class Simple implements SimpleMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        public Simple(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        public String getString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static String[][] data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        { "a*b?c\\d[e-f]",   "OK", "OK", "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        { "a*b?c\\d[e-f]g",  "OK", "OK", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        { "za*b?c\\d[e-f]",  "KO", "OK", "OK" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        { "za*b?c\\d[e-f]g", "KO", "OK", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        { "a*b?c\\de",       "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        { "a*b?c\\deg",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        { "za*b?c\\de",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        { "za*b?c\\deg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        { "a*b?c\\df",       "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        { "a*b?c\\dfg",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        { "za*b?c\\df",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        { "za*b?c\\dfg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        { "axxbxc\\de",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        { "axxbxc\\deg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        { "zaxxbxc\\de",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        { "zaxxbxc\\deg",    "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        { "axxbxc\\df",      "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        { "axxbxc\\dfg",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        { "zaxxbxc\\df",     "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        { "zaxxbxc\\dfg",    "KO", "KO", "KO" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static int query(MBeanServer mbs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                             int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                             String substring,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                             String[][] data) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        String querySubString = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                querySubString = "InitialSubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                querySubString = "AnySubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                querySubString = "FinalSubString";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        System.out.println("\n" + querySubString + " = " + substring + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        for (int i = 0; i < data.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            ObjectName on = new ObjectName("test:type=Simple,query=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                           querySubString + ",name=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Simple s = new Simple(data[i][0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            mbs.registerMBean(s, on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            QueryExp q = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    q = Query.initialSubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                               Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    q = Query.anySubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                           Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    q = Query.finalSubString(Query.attr("String"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                             Query.value(substring));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            q.setMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            boolean r = q.apply(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            System.out.print("Attribute Value = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                mbs.getAttribute(on, "String"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (r && "OK".equals(data[i][type])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.println(" OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else if (!r && "KO".equals(data[i][type])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println(" KO");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                System.out.println(" Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                error++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return error;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        String pattern = "a*b?c\\d[e-f]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
          "\n--- Test javax.management.Query.{initial|any|final}SubString ---");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        error += query(mbs, 1, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        error += query(mbs, 2, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        error += query(mbs, 3, pattern, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.println("\nTest failed! " + error + " errors.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new IllegalArgumentException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.out.println("\nTest passed!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
}