0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5043245
0N/A * @summary Test the following in RequiredModelMBean.getAttribute():
0N/A * The declared type of the attribute is the String returned by
0N/A * ModelMBeanAttributeInfo.getType(). A value is compatible
0N/A * with this type if one of the following is true:
0N/A * - the value is null;
0N/A * - the declared name is a primitive type name (such as "int")
0N/A * and the value is an instance of the corresponding wrapper
0N/A * type (such as java.lang.Integer);
0N/A * - the name of the value's class is identical to the declared name;
0N/A * - the declared name can be loaded by the value's class loader and
0N/A * produces a class to which the value can be assigned.
0N/A * @author Luis-Miguel Alventosa
0N/A * @run clean RequiredModelMBeanGetAttributeTest
0N/A * @run build RequiredModelMBeanGetAttributeTest
0N/A * @run main RequiredModelMBeanGetAttributeTest
0N/A */
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.util.Hashtable;
0N/Aimport java.util.Map;
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/Aimport javax.management.modelmbean.ModelMBean;
0N/Aimport javax.management.modelmbean.ModelMBeanAttributeInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfoSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanOperationInfo;
0N/Aimport javax.management.modelmbean.RequiredModelMBean;
0N/A
0N/Apublic class RequiredModelMBeanGetAttributeTest {
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A boolean ok = true;
0N/A
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A
0N/A // Resource methods
0N/A
0N/A Method nullGetter =
0N/A Resource.class.getMethod("getNull", (Class[]) null);
0N/A Method integerGetter =
0N/A Resource.class.getMethod("getInteger", (Class[]) null);
0N/A Method hashtableGetter =
0N/A Resource.class.getMethod("getHashtable", (Class[]) null);
0N/A Method mapGetter =
0N/A Resource.class.getMethod("getMap", (Class[]) null);
0N/A
0N/A // ModelMBeanOperationInfo
0N/A
0N/A Descriptor nullOperationDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=getNull",
0N/A "descriptorType=operation",
0N/A "role=getter"
0N/A });
0N/A ModelMBeanOperationInfo nullOperationInfo =
0N/A new ModelMBeanOperationInfo("Null attribute",
0N/A nullGetter,
0N/A nullOperationDescriptor);
0N/A
0N/A Descriptor integerOperationDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=getInteger",
0N/A "descriptorType=operation",
0N/A "role=getter"
0N/A });
0N/A ModelMBeanOperationInfo integerOperationInfo =
0N/A new ModelMBeanOperationInfo("Integer attribute",
0N/A integerGetter,
0N/A integerOperationDescriptor);
0N/A
0N/A Descriptor hashtableOperationDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=getHashtable",
0N/A "descriptorType=operation",
0N/A "role=getter"
0N/A });
0N/A ModelMBeanOperationInfo hashtableOperationInfo =
0N/A new ModelMBeanOperationInfo("Hashtable attribute",
0N/A hashtableGetter,
0N/A hashtableOperationDescriptor);
0N/A
0N/A Descriptor mapOperationDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=getMap",
0N/A "descriptorType=operation",
0N/A "role=getter"
0N/A });
0N/A ModelMBeanOperationInfo mapOperationInfo =
0N/A new ModelMBeanOperationInfo("Map attribute",
0N/A mapGetter,
0N/A mapOperationDescriptor);
0N/A
0N/A // ModelMBeanAttributeInfo
0N/A
0N/A Descriptor nullAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Null",
0N/A "descriptorType=attribute",
0N/A "getMethod=getNull"
0N/A });
0N/A ModelMBeanAttributeInfo nullAttributeInfo =
0N/A new ModelMBeanAttributeInfo("Null",
0N/A "java.lang.Object",
0N/A "Null attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A nullAttributeDescriptor);
0N/A
0N/A Descriptor integerAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Integer",
0N/A "descriptorType=attribute",
0N/A "getMethod=getInteger"
0N/A });
0N/A ModelMBeanAttributeInfo integerAttributeInfo =
0N/A new ModelMBeanAttributeInfo("Integer",
0N/A "int",
0N/A "Integer attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A integerAttributeDescriptor);
0N/A
0N/A Descriptor hashtableAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Hashtable",
0N/A "descriptorType=attribute",
0N/A "getMethod=getHashtable"
0N/A });
0N/A ModelMBeanAttributeInfo hashtableAttributeInfo =
0N/A new ModelMBeanAttributeInfo("Hashtable",
0N/A "java.util.Hashtable",
0N/A "Hashtable attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A hashtableAttributeDescriptor);
0N/A
0N/A Descriptor mapAttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Map",
0N/A "descriptorType=attribute",
0N/A "getMethod=getMap"
0N/A });
0N/A ModelMBeanAttributeInfo mapAttributeInfo =
0N/A new ModelMBeanAttributeInfo("Map",
0N/A "java.util.Map",
0N/A "Map attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A mapAttributeDescriptor);
0N/A
0N/A Descriptor null2AttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Null2",
0N/A "descriptorType=attribute"
0N/A });
0N/A null2AttributeDescriptor.setField("default", null);
0N/A ModelMBeanAttributeInfo null2AttributeInfo =
0N/A new ModelMBeanAttributeInfo("Null2",
0N/A "java.lang.Object",
0N/A "Null2 attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A null2AttributeDescriptor);
0N/A
0N/A Descriptor integer2AttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Integer2",
0N/A "descriptorType=attribute"
0N/A });
0N/A integer2AttributeDescriptor.setField("default", 10);
0N/A ModelMBeanAttributeInfo integer2AttributeInfo =
0N/A new ModelMBeanAttributeInfo("Integer2",
0N/A "int",
0N/A "Integer2 attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A integer2AttributeDescriptor);
0N/A
0N/A Descriptor hashtable2AttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Hashtable2",
0N/A "descriptorType=attribute"
0N/A });
0N/A hashtable2AttributeDescriptor.setField("default", new Hashtable());
0N/A ModelMBeanAttributeInfo hashtable2AttributeInfo =
0N/A new ModelMBeanAttributeInfo("Hashtable2",
0N/A "java.util.Hashtable",
0N/A "Hashtable2 attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A hashtable2AttributeDescriptor);
0N/A
0N/A Descriptor map2AttributeDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "name=Map2",
0N/A "descriptorType=attribute"
0N/A });
0N/A map2AttributeDescriptor.setField("default", new Hashtable());
0N/A ModelMBeanAttributeInfo map2AttributeInfo =
0N/A new ModelMBeanAttributeInfo("Map2",
0N/A "java.util.Map",
0N/A "Map2 attribute",
0N/A true,
0N/A false,
0N/A false,
0N/A map2AttributeDescriptor);
0N/A
0N/A // ModelMBeanInfo
0N/A
0N/A ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(
0N/A Resource.class.getName(),
0N/A "Resource MBean",
0N/A new ModelMBeanAttributeInfo[] { nullAttributeInfo,
0N/A integerAttributeInfo,
0N/A hashtableAttributeInfo,
0N/A mapAttributeInfo,
0N/A null2AttributeInfo,
0N/A integer2AttributeInfo,
0N/A hashtable2AttributeInfo,
0N/A map2AttributeInfo },
0N/A null,
0N/A new ModelMBeanOperationInfo[] { nullOperationInfo,
0N/A integerOperationInfo,
0N/A hashtableOperationInfo,
0N/A mapOperationInfo },
0N/A null);
0N/A
0N/A // RequiredModelMBean
0N/A
0N/A ModelMBean mmb = new RequiredModelMBean(mmbi);
0N/A mmb.setManagedResource(resource, "ObjectReference");
0N/A ObjectName mmbName = new ObjectName(":type=ResourceMBean");
0N/A mbs.registerMBean(mmb, mmbName);
0N/A
0N/A // Run tests
0N/A
0N/A System.out.println("\nTesting that we can call getNull()... ");
0N/A try {
0N/A Object o = mbs.getAttribute(mmbName, "Null");
0N/A System.out.println("getNull() = " + o);
0N/A System.out.println("Attribute's declared type = java.lang.Object");
0N/A System.out.println("Returned value's type = null");
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getInteger()... ");
0N/A try {
0N/A Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");
0N/A System.out.println("getInteger() = " + i);
0N/A System.out.println("Attribute's declared type = int");
0N/A System.out.println("Returned value's type = " +
0N/A i.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getHashtable()... ");
0N/A try {
0N/A Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");
0N/A System.out.println("getHashtable() = " + h);
0N/A System.out.println("Attribute's declared type = " +
0N/A "java.util.Hashtable");
0N/A System.out.println("Returned value's type = " +
0N/A h.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getMap()... ");
0N/A try {
0N/A Map m = (Map) mbs.getAttribute(mmbName, "Map");
0N/A System.out.println("getMap() = " + m);
0N/A System.out.println("Attribute's declared type = " +
0N/A "java.util.Map");
0N/A System.out.println("Returned value's type = " +
0N/A m.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getNull2()... ");
0N/A try {
0N/A Object o = mbs.getAttribute(mmbName, "Null2");
0N/A System.out.println("getNull2() = " + o);
0N/A System.out.println("Attribute's declared type = java.lang.Object");
0N/A System.out.println("Returned value's type = null");
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getInteger2()... ");
0N/A try {
0N/A Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");
0N/A System.out.println("getInteger2() = " + i);
0N/A System.out.println("Attribute's declared type = int");
0N/A System.out.println("Returned value's type = " +
0N/A i.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getHashtable2()... ");
0N/A try {
0N/A Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");
0N/A System.out.println("getHashtable2() = " + h);
0N/A System.out.println("Attribute's declared type = " +
0N/A "java.util.Hashtable");
0N/A System.out.println("Returned value's type = " +
0N/A h.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A System.out.println("\nTesting that we can call getMap2()... ");
0N/A try {
0N/A Map m = (Map) mbs.getAttribute(mmbName, "Map2");
0N/A System.out.println("getMap2() = " + m);
0N/A System.out.println("Attribute's declared type = " +
0N/A "java.util.Map");
0N/A System.out.println("Returned value's type = " +
0N/A m.getClass().getName());
0N/A } catch (Exception e) {
0N/A System.out.println("TEST FAILED: Caught exception:");
0N/A e.printStackTrace(System.out);
0N/A ok = false;
0N/A }
0N/A
0N/A if (ok)
0N/A System.out.println("\nTest passed.\n");
0N/A else {
0N/A System.out.println("\nTest failed.\n");
0N/A System.exit(1);
0N/A }
0N/A }
0N/A
0N/A public static class Resource {
0N/A public Object getNull() {
0N/A return null;
0N/A }
0N/A public int getInteger() {
0N/A return 10;
0N/A }
0N/A public Hashtable getHashtable() {
0N/A return new Hashtable();
0N/A }
0N/A public Map getMap() {
0N/A return new Hashtable();
0N/A }
0N/A }
0N/A
0N/A private static Resource resource = new Resource();
0N/A}