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