1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4886033 27 * @summary Query.{initial,any,final}SubString fail if the 28 * matching constraint string contains wildcards. 29 * @author Luis-Miguel Alventosa 30 * @run clean QuerySubstringTest 31 * @run build QuerySubstringTest 32 * @run main QuerySubstringTest 33 */ 34 35 import java.lang.management.ManagementFactory; 36 import javax.management.MBeanServer; 37 import javax.management.ObjectName; 38 import javax.management.Query; 39 import javax.management.QueryExp; 40 41 public class QuerySubstringTest { 42 43 public static interface SimpleMBean { 44 public String getString(); 45 } 46 47 public static class Simple implements SimpleMBean { 48 public Simple(String value) { 49 this.value = value; 50 } 51 public String getString() { 52 return value; 53 } 54 private String value; 55 } 56 57 private 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 }; 79 80 private static int query(MBeanServer mbs, 81 int type, 82 String substring, 83 String[][] data) throws Exception { 84 85 int error = 0; 86 87 String querySubString = null; 88 switch (type) { 89 case 1: 90 querySubString = "InitialSubString"; 91 break; 92 case 2: 93 querySubString = "AnySubString"; 94 break; 95 case 3: 96 querySubString = "FinalSubString"; 97 break; 98 } 99 100 System.out.println("\n" + querySubString + " = " + substring + "\n"); 101 102 for (int i = 0; i < data.length; i++) { 103 ObjectName on = new ObjectName("test:type=Simple,query=" + 104 querySubString + ",name=" + i); 105 Simple s = new Simple(data[i][0]); 106 mbs.registerMBean(s, on); 107 QueryExp q = null; 108 switch (type) { 109 case 1: 110 q = Query.initialSubString(Query.attr("String"), 111 Query.value(substring)); 112 break; 113 case 2: 114 q = Query.anySubString(Query.attr("String"), 115 Query.value(substring)); 116 break; 117 case 3: 118 q = Query.finalSubString(Query.attr("String"), 119 Query.value(substring)); 120 break; 121 } 122 q.setMBeanServer(mbs); 123 boolean r = q.apply(on); 124 System.out.print("Attribute Value = " + 125 mbs.getAttribute(on, "String")); 126 if (r && "OK".equals(data[i][type])) { 127 System.out.println(" OK"); 128 } else if (!r && "KO".equals(data[i][type])) { 129 System.out.println(" KO"); 130 } else { 131 System.out.println(" Error"); 132 error++; 133 } 134 } 135 136 return error; 137 } 138 139 public static void main(String[] args) throws Exception { 140 141 int error = 0; 142 143 String pattern = "a*b?c\\d[e-f]"; 144 145 System.out.println( 146 "\n--- Test javax.management.Query.{initial|any|final}SubString ---"); 147 148 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 149 150 error += query(mbs, 1, pattern, data); 151 152 error += query(mbs, 2, pattern, data); 153 154 error += query(mbs, 3, pattern, data); 155 156 if (error > 0) { 157 System.out.println("\nTest failed! " + error + " errors.\n"); 158 throw new IllegalArgumentException("Test failed"); 159 } else { 160 System.out.println("\nTest passed!\n"); 161 } 162 } 163 }