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 4716807
  27  * @summary Test wildcards in ObjectName key properties value part.
  28  * @author Luis-Miguel Alventosa
  29  * @modules java.management
  30  * @run clean ValueWildcardTest
  31  * @run build ValueWildcardTest
  32  * @run main ValueWildcardTest
  33  */
  34 
  35 import java.util.Hashtable;
  36 import javax.management.ObjectName;
  37 
  38 public class ValueWildcardTest {
  39 
  40     private static int createObjectName(int i,
  41                                         String s,
  42                                         String d,
  43                                         String k,
  44                                         String v,
  45                                         Hashtable<String,String> t,
  46                                         boolean plp,
  47                                         boolean pvp)
  48         throws Exception {
  49 
  50         System.out.println("----------------------------------------------");
  51         switch (i) {
  52         case 1:
  53             System.out.println("ObjectName = " + s);
  54             break;
  55         case 2:
  56             System.out.println("ObjectName.Domain = " + d);
  57             System.out.println("ObjectName.Key = " + k);
  58             System.out.println("ObjectName.Value = " + v);
  59             break;
  60         case 3:
  61             System.out.println("ObjectName.Domain = " + d);
  62             System.out.println("ObjectName.Hashtable = " + t);
  63             break;
  64         default:
  65             throw new Exception("Test incorrect: case: " + i);
  66         }
  67         int error = 0;
  68         ObjectName on = null;
  69         try {
  70             switch (i) {
  71             case 1:
  72                 on = new ObjectName(s);
  73                 break;
  74             case 2:
  75                 on = new ObjectName(d, k, v);
  76                 break;
  77             case 3:
  78                 on = new ObjectName(d, t);
  79                 break;
  80             default:
  81                 throw new Exception("Test incorrect: case: " + i);
  82             }
  83             System.out.println("Got Expected ObjectName = " +
  84                                on.getCanonicalName());
  85             boolean isPattern = on.isPattern();
  86             boolean isDomainPattern = on.isDomainPattern();
  87             boolean isPropertyPattern = on.isPropertyPattern();
  88             boolean isPropertyListPattern = on.isPropertyListPattern();
  89             boolean isPropertyValuePattern = on.isPropertyValuePattern();
  90             System.out.println("ObjectName.isPattern = " +
  91                                isPattern);
  92             System.out.println("ObjectName.isDomainPattern = " +
  93                                isDomainPattern);
  94             System.out.println("ObjectName.isPropertyPattern = " +
  95                                isPropertyPattern);
  96             System.out.println("ObjectName.isPropertyListPattern = " +
  97                                isPropertyListPattern);
  98             System.out.println("ObjectName.isPropertyValuePattern = " +
  99                                isPropertyValuePattern);
 100             int error2 = 0;
 101             if (isDomainPattern) {
 102                 error2++;
 103                 System.out.println("Error: Shouldn't be domain pattern!");
 104             }
 105             if (!plp && isPropertyListPattern) {
 106                 error2++;
 107                 System.out.println("Error: Shouldn't be property list pattern!");
 108             }
 109             if (!pvp && isPropertyValuePattern) {
 110                 error2++;
 111                 System.out.println("Error: Shouldn't be property value pattern!");
 112             }
 113             if (plp &&
 114                 !isPattern && !isPropertyPattern && !isPropertyListPattern) {
 115                 error2++;
 116                 System.out.println("Error: Should be property list pattern!");
 117             }
 118             if (pvp &&
 119                 !isPattern && !isPropertyPattern && !isPropertyValuePattern) {
 120                 error2++;
 121                 System.out.println("Error: Should be property value pattern!");
 122             }
 123             if (error2 > 0) {
 124                 error++;
 125                 System.out.println("Test failed!");
 126             } else {
 127                 System.out.println("Test passed!");
 128             }
 129         } catch (Exception e) {
 130             error++;
 131             System.out.println("Got Unexpected Exception = " + e.toString());
 132         }
 133         System.out.println("----------------------------------------------");
 134         return error;
 135     }
 136 
 137     private static int createObjectName1(String s,
 138                                          boolean plp,
 139                                          boolean pvp)
 140         throws Exception {
 141         return createObjectName(1, s, null, null, null, null, plp, pvp);
 142     }
 143 
 144     private static int createObjectName2(String d,
 145                                          String k,
 146                                          String v,
 147                                          boolean plp,
 148                                          boolean pvp)
 149         throws Exception {
 150         return createObjectName(2, null, d, k, v, null, plp, pvp);
 151     }
 152 
 153     private static int createObjectName3(String d,
 154                                          Hashtable<String,String> t,
 155                                          boolean plp,
 156                                          boolean pvp)
 157         throws Exception {
 158         return createObjectName(3, null, d, null, null, t, plp, pvp);
 159     }
 160 
 161     public static void main(String[] args) throws Exception {
 162 
 163         int error = 0;
 164 
 165         error += createObjectName1("d:k=*", false, true);
 166         error += createObjectName1("d:k=a*b", false, true);
 167         error += createObjectName1("d:k=a*b,*", true, true);
 168         error += createObjectName1("d:*,k=a*b", true, true);
 169 
 170         error += createObjectName1("d:k=?", false, true);
 171         error += createObjectName1("d:k=a?b", false, true);
 172         error += createObjectName1("d:k=a?b,*", true, true);
 173         error += createObjectName1("d:*,k=a?b", true, true);
 174 
 175         error += createObjectName1("d:k=?*", false, true);
 176         error += createObjectName1("d:k=a?bc*d", false, true);
 177         error += createObjectName1("d:k=a?bc*d,*", true, true);
 178         error += createObjectName1("d:*,k=a?bc*d", true, true);
 179 
 180         error += createObjectName1("d:k1=?,k2=*", false, true);
 181         error += createObjectName1("d:k1=a?b,k2=c*d", false, true);
 182         error += createObjectName1("d:k1=a?b,k2=c*d,*", true, true);
 183         error += createObjectName1("d:*,k1=a?b,k2=c*d", true, true);
 184 
 185         error += createObjectName1("d:k=\"*\"", false, true);
 186         error += createObjectName1("d:k=\"a*b\"", false, true);
 187         error += createObjectName1("d:k=\"a*b\",*", true, true);
 188         error += createObjectName1("d:*,k=\"a*b\"", true, true);
 189 
 190         error += createObjectName1("d:k=\"?\"", false, true);
 191         error += createObjectName1("d:k=\"a?b\"", false, true);
 192         error += createObjectName1("d:k=\"a?b\",*", true, true);
 193         error += createObjectName1("d:*,k=\"a?b\"", true, true);
 194 
 195         error += createObjectName1("d:k=\"?*\"", false, true);
 196         error += createObjectName1("d:k=\"a?bc*d\"", false, true);
 197         error += createObjectName1("d:k=\"a?bc*d\",*", true, true);
 198         error += createObjectName1("d:*,k=\"a?bc*d\"", true, true);
 199 
 200         error += createObjectName1("d:k1=\"?\",k2=\"*\"", false, true);
 201         error += createObjectName1("d:k1=\"a?b\",k2=\"c*d\"", false, true);
 202         error += createObjectName1("d:k1=\"a?b\",k2=\"c*d\",*", true, true);
 203         error += createObjectName1("d:*,k1=\"a?b\",k2=\"c*d\"", true, true);
 204 
 205         error += createObjectName2("d", "k", "*", false, true);
 206         error += createObjectName2("d", "k", "a*b", false, true);
 207         error += createObjectName2("d", "k", "?", false, true);
 208         error += createObjectName2("d", "k", "a?b", false, true);
 209         error += createObjectName2("d", "k", "?*", false, true);
 210         error += createObjectName2("d", "k", "a?bc*d", false, true);
 211 
 212         error += createObjectName2("d", "k", "\"*\"", false, true);
 213         error += createObjectName2("d", "k", "\"a*b\"", false, true);
 214         error += createObjectName2("d", "k", "\"?\"", false, true);
 215         error += createObjectName2("d", "k", "\"a?b\"", false, true);
 216         error += createObjectName2("d", "k", "\"?*\"", false, true);
 217         error += createObjectName2("d", "k", "\"a?bc*d\"", false, true);
 218 
 219         Hashtable<String,String> h1 = new Hashtable<String,String>();
 220         h1.put("k", "*");
 221         error += createObjectName3("d", h1, false, true);
 222         Hashtable<String,String> h2 = new Hashtable<String,String>();
 223         h2.put("k", "a*b");
 224         error += createObjectName3("d", h2, false, true);
 225 
 226         Hashtable<String,String> h3 = new Hashtable<String,String>();
 227         h3.put("k", "?");
 228         error += createObjectName3("d", h3, false, true);
 229         Hashtable<String,String> h4 = new Hashtable<String,String>();
 230         h4.put("k", "a?b");
 231         error += createObjectName3("d", h4, false, true);
 232 
 233         Hashtable<String,String> h5 = new Hashtable<String,String>();
 234         h5.put("k", "?*");
 235         error += createObjectName3("d", h5, false, true);
 236         Hashtable<String,String> h6 = new Hashtable<String,String>();
 237         h6.put("k", "a?bc*d");
 238         error += createObjectName3("d", h6, false, true);
 239 
 240         Hashtable<String,String> h7 = new Hashtable<String,String>();
 241         h7.put("k1", "?");
 242         h7.put("k2", "*");
 243         error += createObjectName3("d", h7, false, true);
 244         Hashtable<String,String> h8 = new Hashtable<String,String>();
 245         h8.put("k1", "a?b");
 246         h8.put("k2", "c*d");
 247         error += createObjectName3("d", h8, false, true);
 248 
 249         Hashtable<String,String> h9 = new Hashtable<String,String>();
 250         h9.put("k", "\"*\"");
 251         error += createObjectName3("d", h9, false, true);
 252         Hashtable<String,String> h10 = new Hashtable<String,String>();
 253         h10.put("k", "\"a*b\"");
 254         error += createObjectName3("d", h10, false, true);
 255 
 256         Hashtable<String,String> h11 = new Hashtable<String,String>();
 257         h11.put("k", "\"?\"");
 258         error += createObjectName3("d", h11, false, true);
 259         Hashtable<String,String> h12 = new Hashtable<String,String>();
 260         h12.put("k", "\"a?b\"");
 261         error += createObjectName3("d", h12, false, true);
 262 
 263         Hashtable<String,String> h13 = new Hashtable<String,String>();
 264         h13.put("k", "\"?*\"");
 265         error += createObjectName3("d", h13, false, true);
 266         Hashtable<String,String> h14 = new Hashtable<String,String>();
 267         h14.put("k", "\"a?bc*d\"");
 268         error += createObjectName3("d", h14, false, true);
 269 
 270         Hashtable<String,String> h15 = new Hashtable<String,String>();
 271         h15.put("k1", "\"?\"");
 272         h15.put("k2", "\"*\"");
 273         error += createObjectName3("d", h15, false, true);
 274         Hashtable<String,String> h16 = new Hashtable<String,String>();
 275         h16.put("k1", "\"a?b\"");
 276         h16.put("k2", "\"c*d\"");
 277         error += createObjectName3("d", h16, false, true);
 278 
 279         if (error > 0) {
 280             final String msg = "Test FAILED! Got " + error + " error(s)";
 281             System.out.println(msg);
 282             throw new IllegalArgumentException(msg);
 283         } else {
 284             System.out.println("Test PASSED!");
 285         }
 286     }
 287 }