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