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 5043245
  27  * @summary Test the following in RequiredModelMBean.getAttribute():
  28  * The declared type of the attribute is the String returned by
  29  * ModelMBeanAttributeInfo.getType(). A value is compatible
  30  * with this type if one of the following is true:
  31  * - the value is null;
  32  * - the declared name is a primitive type name (such as "int")
  33  *   and the value is an instance of the corresponding wrapper
  34  *   type (such as java.lang.Integer);
  35  * - the name of the value's class is identical to the declared name;
  36  * - the declared name can be loaded by the value's class loader and
  37  *   produces a class to which the value can be assigned.
  38  * @author Luis-Miguel Alventosa
  39  *
  40  * @run clean RequiredModelMBeanGetAttributeTest
  41  * @run build RequiredModelMBeanGetAttributeTest
  42  * @run main RequiredModelMBeanGetAttributeTest
  43  */
  44 
  45 import java.lang.reflect.Method;
  46 import java.util.Hashtable;
  47 import java.util.Map;
  48 import javax.management.Descriptor;
  49 import javax.management.MBeanServer;
  50 import javax.management.MBeanServerFactory;
  51 import javax.management.ObjectName;
  52 import javax.management.modelmbean.DescriptorSupport;
  53 import javax.management.modelmbean.ModelMBean;
  54 import javax.management.modelmbean.ModelMBeanAttributeInfo;
  55 import javax.management.modelmbean.ModelMBeanInfo;
  56 import javax.management.modelmbean.ModelMBeanInfoSupport;
  57 import javax.management.modelmbean.ModelMBeanOperationInfo;
  58 import javax.management.modelmbean.RequiredModelMBean;
  59 
  60 public class RequiredModelMBeanGetAttributeTest {
  61 
  62     public static void main(String[] args) throws Exception {
  63 
  64         boolean ok = true;
  65 
  66         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  67 
  68         // Resource methods
  69 
  70         Method nullGetter =
  71             Resource.class.getMethod("getNull", (Class[]) null);
  72         Method integerGetter =
  73             Resource.class.getMethod("getInteger", (Class[]) null);
  74         Method hashtableGetter =
  75             Resource.class.getMethod("getHashtable", (Class[]) null);
  76         Method mapGetter =
  77             Resource.class.getMethod("getMap", (Class[]) null);
  78 
  79         // ModelMBeanOperationInfo
  80 
  81         Descriptor nullOperationDescriptor =
  82             new DescriptorSupport(new String[] {
  83                 "name=getNull",
  84                 "descriptorType=operation",
  85                 "role=getter"
  86             });
  87         ModelMBeanOperationInfo nullOperationInfo =
  88             new ModelMBeanOperationInfo("Null attribute",
  89                                         nullGetter,
  90                                         nullOperationDescriptor);
  91 
  92         Descriptor integerOperationDescriptor =
  93             new DescriptorSupport(new String[] {
  94                 "name=getInteger",
  95                 "descriptorType=operation",
  96                 "role=getter"
  97             });
  98         ModelMBeanOperationInfo integerOperationInfo =
  99             new ModelMBeanOperationInfo("Integer attribute",
 100                                         integerGetter,
 101                                         integerOperationDescriptor);
 102 
 103         Descriptor hashtableOperationDescriptor =
 104             new DescriptorSupport(new String[] {
 105                 "name=getHashtable",
 106                 "descriptorType=operation",
 107                 "role=getter"
 108             });
 109         ModelMBeanOperationInfo hashtableOperationInfo =
 110             new ModelMBeanOperationInfo("Hashtable attribute",
 111                                         hashtableGetter,
 112                                         hashtableOperationDescriptor);
 113 
 114         Descriptor mapOperationDescriptor =
 115             new DescriptorSupport(new String[] {
 116                 "name=getMap",
 117                 "descriptorType=operation",
 118                 "role=getter"
 119             });
 120         ModelMBeanOperationInfo mapOperationInfo =
 121             new ModelMBeanOperationInfo("Map attribute",
 122                                         mapGetter,
 123                                         mapOperationDescriptor);
 124 
 125         // ModelMBeanAttributeInfo
 126 
 127         Descriptor nullAttributeDescriptor =
 128             new DescriptorSupport(new String[] {
 129                 "name=Null",
 130                 "descriptorType=attribute",
 131                 "getMethod=getNull"
 132             });
 133         ModelMBeanAttributeInfo nullAttributeInfo =
 134             new ModelMBeanAttributeInfo("Null",
 135                                         "java.lang.Object",
 136                                         "Null attribute",
 137                                         true,
 138                                         false,
 139                                         false,
 140                                         nullAttributeDescriptor);
 141 
 142         Descriptor integerAttributeDescriptor =
 143             new DescriptorSupport(new String[] {
 144                 "name=Integer",
 145                 "descriptorType=attribute",
 146                 "getMethod=getInteger"
 147             });
 148         ModelMBeanAttributeInfo integerAttributeInfo =
 149             new ModelMBeanAttributeInfo("Integer",
 150                                         "int",
 151                                         "Integer attribute",
 152                                         true,
 153                                         false,
 154                                         false,
 155                                         integerAttributeDescriptor);
 156 
 157         Descriptor hashtableAttributeDescriptor =
 158             new DescriptorSupport(new String[] {
 159                 "name=Hashtable",
 160                 "descriptorType=attribute",
 161                 "getMethod=getHashtable"
 162             });
 163         ModelMBeanAttributeInfo hashtableAttributeInfo =
 164             new ModelMBeanAttributeInfo("Hashtable",
 165                                         "java.util.Hashtable",
 166                                         "Hashtable attribute",
 167                                         true,
 168                                         false,
 169                                         false,
 170                                         hashtableAttributeDescriptor);
 171 
 172         Descriptor mapAttributeDescriptor =
 173             new DescriptorSupport(new String[] {
 174                 "name=Map",
 175                 "descriptorType=attribute",
 176                 "getMethod=getMap"
 177             });
 178         ModelMBeanAttributeInfo mapAttributeInfo =
 179             new ModelMBeanAttributeInfo("Map",
 180                                         "java.util.Map",
 181                                         "Map attribute",
 182                                         true,
 183                                         false,
 184                                         false,
 185                                         mapAttributeDescriptor);
 186 
 187         Descriptor null2AttributeDescriptor =
 188             new DescriptorSupport(new String[] {
 189                 "name=Null2",
 190                 "descriptorType=attribute"
 191             });
 192         null2AttributeDescriptor.setField("default", null);
 193         ModelMBeanAttributeInfo null2AttributeInfo =
 194             new ModelMBeanAttributeInfo("Null2",
 195                                         "java.lang.Object",
 196                                         "Null2 attribute",
 197                                         true,
 198                                         false,
 199                                         false,
 200                                         null2AttributeDescriptor);
 201 
 202         Descriptor integer2AttributeDescriptor =
 203             new DescriptorSupport(new String[] {
 204                 "name=Integer2",
 205                 "descriptorType=attribute"
 206             });
 207         integer2AttributeDescriptor.setField("default", 10);
 208         ModelMBeanAttributeInfo integer2AttributeInfo =
 209             new ModelMBeanAttributeInfo("Integer2",
 210                                         "int",
 211                                         "Integer2 attribute",
 212                                         true,
 213                                         false,
 214                                         false,
 215                                         integer2AttributeDescriptor);
 216 
 217         Descriptor hashtable2AttributeDescriptor =
 218             new DescriptorSupport(new String[] {
 219                 "name=Hashtable2",
 220                 "descriptorType=attribute"
 221             });
 222         hashtable2AttributeDescriptor.setField("default", new Hashtable());
 223         ModelMBeanAttributeInfo hashtable2AttributeInfo =
 224             new ModelMBeanAttributeInfo("Hashtable2",
 225                                         "java.util.Hashtable",
 226                                         "Hashtable2 attribute",
 227                                         true,
 228                                         false,
 229                                         false,
 230                                         hashtable2AttributeDescriptor);
 231 
 232         Descriptor map2AttributeDescriptor =
 233             new DescriptorSupport(new String[] {
 234                 "name=Map2",
 235                 "descriptorType=attribute"
 236             });
 237         map2AttributeDescriptor.setField("default", new Hashtable());
 238         ModelMBeanAttributeInfo map2AttributeInfo =
 239             new ModelMBeanAttributeInfo("Map2",
 240                                         "java.util.Map",
 241                                         "Map2 attribute",
 242                                         true,
 243                                         false,
 244                                         false,
 245                                         map2AttributeDescriptor);
 246 
 247         // ModelMBeanInfo
 248 
 249         ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(
 250             Resource.class.getName(),
 251             "Resource MBean",
 252             new ModelMBeanAttributeInfo[] { nullAttributeInfo,
 253                                             integerAttributeInfo,
 254                                             hashtableAttributeInfo,
 255                                             mapAttributeInfo,
 256                                             null2AttributeInfo,
 257                                             integer2AttributeInfo,
 258                                             hashtable2AttributeInfo,
 259                                             map2AttributeInfo },
 260             null,
 261             new ModelMBeanOperationInfo[] { nullOperationInfo,
 262                                             integerOperationInfo,
 263                                             hashtableOperationInfo,
 264                                             mapOperationInfo },
 265             null);
 266 
 267         // RequiredModelMBean
 268 
 269         ModelMBean mmb = new RequiredModelMBean(mmbi);
 270         mmb.setManagedResource(resource, "ObjectReference");
 271         ObjectName mmbName = new ObjectName(":type=ResourceMBean");
 272         mbs.registerMBean(mmb, mmbName);
 273 
 274         // Run tests
 275 
 276         System.out.println("\nTesting that we can call getNull()... ");
 277         try {
 278             Object o = mbs.getAttribute(mmbName, "Null");
 279             System.out.println("getNull() = " + o);
 280             System.out.println("Attribute's declared type = java.lang.Object");
 281             System.out.println("Returned value's type = null");
 282         } catch (Exception e) {
 283             System.out.println("TEST FAILED: Caught exception:");
 284             e.printStackTrace(System.out);
 285             ok = false;
 286         }
 287 
 288         System.out.println("\nTesting that we can call getInteger()... ");
 289         try {
 290             Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");
 291             System.out.println("getInteger() = " + i);
 292             System.out.println("Attribute's declared type = int");
 293             System.out.println("Returned value's type = " +
 294                                i.getClass().getName());
 295         } catch (Exception e) {
 296             System.out.println("TEST FAILED: Caught exception:");
 297             e.printStackTrace(System.out);
 298             ok = false;
 299         }
 300 
 301         System.out.println("\nTesting that we can call getHashtable()... ");
 302         try {
 303             Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");
 304             System.out.println("getHashtable() = " + h);
 305             System.out.println("Attribute's declared type = " +
 306                                "java.util.Hashtable");
 307             System.out.println("Returned value's type = " +
 308                                h.getClass().getName());
 309         } catch (Exception e) {
 310             System.out.println("TEST FAILED: Caught exception:");
 311             e.printStackTrace(System.out);
 312             ok = false;
 313         }
 314 
 315         System.out.println("\nTesting that we can call getMap()... ");
 316         try {
 317             Map m = (Map) mbs.getAttribute(mmbName, "Map");
 318             System.out.println("getMap() = " + m);
 319             System.out.println("Attribute's declared type = " +
 320                                "java.util.Map");
 321             System.out.println("Returned value's type = " +
 322                                m.getClass().getName());
 323         } catch (Exception e) {
 324             System.out.println("TEST FAILED: Caught exception:");
 325             e.printStackTrace(System.out);
 326             ok = false;
 327         }
 328 
 329         System.out.println("\nTesting that we can call getNull2()... ");
 330         try {
 331             Object o = mbs.getAttribute(mmbName, "Null2");
 332             System.out.println("getNull2() = " + o);
 333             System.out.println("Attribute's declared type = java.lang.Object");
 334             System.out.println("Returned value's type = null");
 335         } catch (Exception e) {
 336             System.out.println("TEST FAILED: Caught exception:");
 337             e.printStackTrace(System.out);
 338             ok = false;
 339         }
 340 
 341         System.out.println("\nTesting that we can call getInteger2()... ");
 342         try {
 343             Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");
 344             System.out.println("getInteger2() = " + i);
 345             System.out.println("Attribute's declared type = int");
 346             System.out.println("Returned value's type = " +
 347                                i.getClass().getName());
 348         } catch (Exception e) {
 349             System.out.println("TEST FAILED: Caught exception:");
 350             e.printStackTrace(System.out);
 351             ok = false;
 352         }
 353 
 354         System.out.println("\nTesting that we can call getHashtable2()... ");
 355         try {
 356             Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");
 357             System.out.println("getHashtable2() = " + h);
 358             System.out.println("Attribute's declared type = " +
 359                                "java.util.Hashtable");
 360             System.out.println("Returned value's type = " +
 361                                h.getClass().getName());
 362         } catch (Exception e) {
 363             System.out.println("TEST FAILED: Caught exception:");
 364             e.printStackTrace(System.out);
 365             ok = false;
 366         }
 367 
 368         System.out.println("\nTesting that we can call getMap2()... ");
 369         try {
 370             Map m = (Map) mbs.getAttribute(mmbName, "Map2");
 371             System.out.println("getMap2() = " + m);
 372             System.out.println("Attribute's declared type = " +
 373                                "java.util.Map");
 374             System.out.println("Returned value's type = " +
 375                                m.getClass().getName());
 376         } catch (Exception e) {
 377             System.out.println("TEST FAILED: Caught exception:");
 378             e.printStackTrace(System.out);
 379             ok = false;
 380         }
 381 
 382         if (ok)
 383             System.out.println("\nTest passed.\n");
 384         else {
 385             System.out.println("\nTest failed.\n");
 386             System.exit(1);
 387         }
 388     }
 389 
 390     public static class Resource {
 391         public Object getNull() {
 392             return null;
 393         }
 394         public int getInteger() {
 395             return 10;
 396         }
 397         public Hashtable getHashtable() {
 398             return new Hashtable();
 399         }
 400         public Map getMap() {
 401             return new Hashtable();
 402         }
 403     }
 404 
 405     private static Resource resource = new Resource();
 406 }