Path: blob/master/test/jdk/javax/management/query/QuerySubstringTest.java
41152 views
/*1* Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 488603326* @summary Query.{initial,any,final}SubString fail if the27* matching constraint string contains wildcards.28* @author Luis-Miguel Alventosa29*30* @run clean QuerySubstringTest31* @run build QuerySubstringTest32* @run main QuerySubstringTest33*/3435import java.lang.management.ManagementFactory;36import javax.management.MBeanServer;37import javax.management.ObjectName;38import javax.management.Query;39import javax.management.QueryExp;4041public class QuerySubstringTest {4243public static interface SimpleMBean {44public String getString();45}4647public static class Simple implements SimpleMBean {48public Simple(String value) {49this.value = value;50}51public String getString() {52return value;53}54private String value;55}5657private static String[][] data = {58{ "a*b?c\\d[e-f]", "OK", "OK", "OK" },59{ "a*b?c\\d[e-f]g", "OK", "OK", "KO" },60{ "za*b?c\\d[e-f]", "KO", "OK", "OK" },61{ "za*b?c\\d[e-f]g", "KO", "OK", "KO" },62{ "a*b?c\\de", "KO", "KO", "KO" },63{ "a*b?c\\deg", "KO", "KO", "KO" },64{ "za*b?c\\de", "KO", "KO", "KO" },65{ "za*b?c\\deg", "KO", "KO", "KO" },66{ "a*b?c\\df", "KO", "KO", "KO" },67{ "a*b?c\\dfg", "KO", "KO", "KO" },68{ "za*b?c\\df", "KO", "KO", "KO" },69{ "za*b?c\\dfg", "KO", "KO", "KO" },70{ "axxbxc\\de", "KO", "KO", "KO" },71{ "axxbxc\\deg", "KO", "KO", "KO" },72{ "zaxxbxc\\de", "KO", "KO", "KO" },73{ "zaxxbxc\\deg", "KO", "KO", "KO" },74{ "axxbxc\\df", "KO", "KO", "KO" },75{ "axxbxc\\dfg", "KO", "KO", "KO" },76{ "zaxxbxc\\df", "KO", "KO", "KO" },77{ "zaxxbxc\\dfg", "KO", "KO", "KO" },78};7980private static int query(MBeanServer mbs,81int type,82String substring,83String[][] data) throws Exception {8485int error = 0;8687String querySubString = null;88switch (type) {89case 1:90querySubString = "InitialSubString";91break;92case 2:93querySubString = "AnySubString";94break;95case 3:96querySubString = "FinalSubString";97break;98}99100System.out.println("\n" + querySubString + " = " + substring + "\n");101102for (int i = 0; i < data.length; i++) {103ObjectName on = new ObjectName("test:type=Simple,query=" +104querySubString + ",name=" + i);105Simple s = new Simple(data[i][0]);106mbs.registerMBean(s, on);107QueryExp q = null;108switch (type) {109case 1:110q = Query.initialSubString(Query.attr("String"),111Query.value(substring));112break;113case 2:114q = Query.anySubString(Query.attr("String"),115Query.value(substring));116break;117case 3:118q = Query.finalSubString(Query.attr("String"),119Query.value(substring));120break;121}122q.setMBeanServer(mbs);123boolean r = q.apply(on);124System.out.print("Attribute Value = " +125mbs.getAttribute(on, "String"));126if (r && "OK".equals(data[i][type])) {127System.out.println(" OK");128} else if (!r && "KO".equals(data[i][type])) {129System.out.println(" KO");130} else {131System.out.println(" Error");132error++;133}134}135136return error;137}138139public static void main(String[] args) throws Exception {140141int error = 0;142143String pattern = "a*b?c\\d[e-f]";144145System.out.println(146"\n--- Test javax.management.Query.{initial|any|final}SubString ---");147148MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();149150error += query(mbs, 1, pattern, data);151152error += query(mbs, 2, pattern, data);153154error += query(mbs, 3, pattern, data);155156if (error > 0) {157System.out.println("\nTest failed! " + error + " errors.\n");158throw new IllegalArgumentException("Test failed");159} else {160System.out.println("\nTest passed!\n");161}162}163}164165166