0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4886033
0N/A * @summary Query.{initial,any,final}SubString fail if the
0N/A * matching constraint string contains wildcards.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean QuerySubstringTest
0N/A * @run build QuerySubstringTest
0N/A * @run main QuerySubstringTest
0N/A */
0N/A
0N/Aimport java.lang.management.ManagementFactory;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.Query;
0N/Aimport javax.management.QueryExp;
0N/A
0N/Apublic class QuerySubstringTest {
0N/A
0N/A public static interface SimpleMBean {
0N/A public String getString();
0N/A }
0N/A
0N/A public static class Simple implements SimpleMBean {
0N/A public Simple(String value) {
0N/A this.value = value;
0N/A }
0N/A public String getString() {
0N/A return value;
0N/A }
0N/A private String value;
0N/A }
0N/A
0N/A private static String[][] data = {
0N/A { "a*b?c\\d[e-f]", "OK", "OK", "OK" },
0N/A { "a*b?c\\d[e-f]g", "OK", "OK", "KO" },
0N/A { "za*b?c\\d[e-f]", "KO", "OK", "OK" },
0N/A { "za*b?c\\d[e-f]g", "KO", "OK", "KO" },
0N/A { "a*b?c\\de", "KO", "KO", "KO" },
0N/A { "a*b?c\\deg", "KO", "KO", "KO" },
0N/A { "za*b?c\\de", "KO", "KO", "KO" },
0N/A { "za*b?c\\deg", "KO", "KO", "KO" },
0N/A { "a*b?c\\df", "KO", "KO", "KO" },
0N/A { "a*b?c\\dfg", "KO", "KO", "KO" },
0N/A { "za*b?c\\df", "KO", "KO", "KO" },
0N/A { "za*b?c\\dfg", "KO", "KO", "KO" },
0N/A { "axxbxc\\de", "KO", "KO", "KO" },
0N/A { "axxbxc\\deg", "KO", "KO", "KO" },
0N/A { "zaxxbxc\\de", "KO", "KO", "KO" },
0N/A { "zaxxbxc\\deg", "KO", "KO", "KO" },
0N/A { "axxbxc\\df", "KO", "KO", "KO" },
0N/A { "axxbxc\\dfg", "KO", "KO", "KO" },
0N/A { "zaxxbxc\\df", "KO", "KO", "KO" },
0N/A { "zaxxbxc\\dfg", "KO", "KO", "KO" },
0N/A };
0N/A
0N/A private static int query(MBeanServer mbs,
0N/A int type,
0N/A String substring,
0N/A String[][] data) throws Exception {
0N/A
0N/A int error = 0;
0N/A
0N/A String querySubString = null;
0N/A switch (type) {
0N/A case 1:
0N/A querySubString = "InitialSubString";
0N/A break;
0N/A case 2:
0N/A querySubString = "AnySubString";
0N/A break;
0N/A case 3:
0N/A querySubString = "FinalSubString";
0N/A break;
0N/A }
0N/A
0N/A System.out.println("\n" + querySubString + " = " + substring + "\n");
0N/A
0N/A for (int i = 0; i < data.length; i++) {
0N/A ObjectName on = new ObjectName("test:type=Simple,query=" +
0N/A querySubString + ",name=" + i);
0N/A Simple s = new Simple(data[i][0]);
0N/A mbs.registerMBean(s, on);
0N/A QueryExp q = null;
0N/A switch (type) {
0N/A case 1:
0N/A q = Query.initialSubString(Query.attr("String"),
0N/A Query.value(substring));
0N/A break;
0N/A case 2:
0N/A q = Query.anySubString(Query.attr("String"),
0N/A Query.value(substring));
0N/A break;
0N/A case 3:
0N/A q = Query.finalSubString(Query.attr("String"),
0N/A Query.value(substring));
0N/A break;
0N/A }
0N/A q.setMBeanServer(mbs);
0N/A boolean r = q.apply(on);
0N/A System.out.print("Attribute Value = " +
0N/A mbs.getAttribute(on, "String"));
0N/A if (r && "OK".equals(data[i][type])) {
0N/A System.out.println(" OK");
0N/A } else if (!r && "KO".equals(data[i][type])) {
0N/A System.out.println(" KO");
0N/A } else {
0N/A System.out.println(" Error");
0N/A error++;
0N/A }
0N/A }
0N/A
0N/A return error;
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A int error = 0;
0N/A
0N/A String pattern = "a*b?c\\d[e-f]";
0N/A
0N/A System.out.println(
0N/A "\n--- Test javax.management.Query.{initial|any|final}SubString ---");
0N/A
0N/A MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
0N/A
0N/A error += query(mbs, 1, pattern, data);
0N/A
0N/A error += query(mbs, 2, pattern, data);
0N/A
0N/A error += query(mbs, 3, pattern, data);
0N/A
0N/A if (error > 0) {
0N/A System.out.println("\nTest failed! " + error + " errors.\n");
0N/A throw new IllegalArgumentException("Test failed");
0N/A } else {
0N/A System.out.println("\nTest passed!\n");
0N/A }
0N/A }
0N/A}