Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/RequiredModelMBeanGetAttributeTest.java
38840 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 504324526* @summary Test the following in RequiredModelMBean.getAttribute():27* The declared type of the attribute is the String returned by28* ModelMBeanAttributeInfo.getType(). A value is compatible29* with this type if one of the following is true:30* - the value is null;31* - the declared name is a primitive type name (such as "int")32* and the value is an instance of the corresponding wrapper33* type (such as java.lang.Integer);34* - the name of the value's class is identical to the declared name;35* - the declared name can be loaded by the value's class loader and36* produces a class to which the value can be assigned.37* @author Luis-Miguel Alventosa38* @run clean RequiredModelMBeanGetAttributeTest39* @run build RequiredModelMBeanGetAttributeTest40* @run main RequiredModelMBeanGetAttributeTest41*/4243import java.lang.reflect.Method;44import java.util.Hashtable;45import java.util.Map;46import javax.management.Descriptor;47import javax.management.MBeanServer;48import javax.management.MBeanServerFactory;49import javax.management.ObjectName;50import javax.management.modelmbean.DescriptorSupport;51import javax.management.modelmbean.ModelMBean;52import javax.management.modelmbean.ModelMBeanAttributeInfo;53import javax.management.modelmbean.ModelMBeanInfo;54import javax.management.modelmbean.ModelMBeanInfoSupport;55import javax.management.modelmbean.ModelMBeanOperationInfo;56import javax.management.modelmbean.RequiredModelMBean;5758public class RequiredModelMBeanGetAttributeTest {5960public static void main(String[] args) throws Exception {6162boolean ok = true;6364MBeanServer mbs = MBeanServerFactory.createMBeanServer();6566// Resource methods6768Method nullGetter =69Resource.class.getMethod("getNull", (Class[]) null);70Method integerGetter =71Resource.class.getMethod("getInteger", (Class[]) null);72Method hashtableGetter =73Resource.class.getMethod("getHashtable", (Class[]) null);74Method mapGetter =75Resource.class.getMethod("getMap", (Class[]) null);7677// ModelMBeanOperationInfo7879Descriptor nullOperationDescriptor =80new DescriptorSupport(new String[] {81"name=getNull",82"descriptorType=operation",83"role=getter"84});85ModelMBeanOperationInfo nullOperationInfo =86new ModelMBeanOperationInfo("Null attribute",87nullGetter,88nullOperationDescriptor);8990Descriptor integerOperationDescriptor =91new DescriptorSupport(new String[] {92"name=getInteger",93"descriptorType=operation",94"role=getter"95});96ModelMBeanOperationInfo integerOperationInfo =97new ModelMBeanOperationInfo("Integer attribute",98integerGetter,99integerOperationDescriptor);100101Descriptor hashtableOperationDescriptor =102new DescriptorSupport(new String[] {103"name=getHashtable",104"descriptorType=operation",105"role=getter"106});107ModelMBeanOperationInfo hashtableOperationInfo =108new ModelMBeanOperationInfo("Hashtable attribute",109hashtableGetter,110hashtableOperationDescriptor);111112Descriptor mapOperationDescriptor =113new DescriptorSupport(new String[] {114"name=getMap",115"descriptorType=operation",116"role=getter"117});118ModelMBeanOperationInfo mapOperationInfo =119new ModelMBeanOperationInfo("Map attribute",120mapGetter,121mapOperationDescriptor);122123// ModelMBeanAttributeInfo124125Descriptor nullAttributeDescriptor =126new DescriptorSupport(new String[] {127"name=Null",128"descriptorType=attribute",129"getMethod=getNull"130});131ModelMBeanAttributeInfo nullAttributeInfo =132new ModelMBeanAttributeInfo("Null",133"java.lang.Object",134"Null attribute",135true,136false,137false,138nullAttributeDescriptor);139140Descriptor integerAttributeDescriptor =141new DescriptorSupport(new String[] {142"name=Integer",143"descriptorType=attribute",144"getMethod=getInteger"145});146ModelMBeanAttributeInfo integerAttributeInfo =147new ModelMBeanAttributeInfo("Integer",148"int",149"Integer attribute",150true,151false,152false,153integerAttributeDescriptor);154155Descriptor hashtableAttributeDescriptor =156new DescriptorSupport(new String[] {157"name=Hashtable",158"descriptorType=attribute",159"getMethod=getHashtable"160});161ModelMBeanAttributeInfo hashtableAttributeInfo =162new ModelMBeanAttributeInfo("Hashtable",163"java.util.Hashtable",164"Hashtable attribute",165true,166false,167false,168hashtableAttributeDescriptor);169170Descriptor mapAttributeDescriptor =171new DescriptorSupport(new String[] {172"name=Map",173"descriptorType=attribute",174"getMethod=getMap"175});176ModelMBeanAttributeInfo mapAttributeInfo =177new ModelMBeanAttributeInfo("Map",178"java.util.Map",179"Map attribute",180true,181false,182false,183mapAttributeDescriptor);184185Descriptor null2AttributeDescriptor =186new DescriptorSupport(new String[] {187"name=Null2",188"descriptorType=attribute"189});190null2AttributeDescriptor.setField("default", null);191ModelMBeanAttributeInfo null2AttributeInfo =192new ModelMBeanAttributeInfo("Null2",193"java.lang.Object",194"Null2 attribute",195true,196false,197false,198null2AttributeDescriptor);199200Descriptor integer2AttributeDescriptor =201new DescriptorSupport(new String[] {202"name=Integer2",203"descriptorType=attribute"204});205integer2AttributeDescriptor.setField("default", 10);206ModelMBeanAttributeInfo integer2AttributeInfo =207new ModelMBeanAttributeInfo("Integer2",208"int",209"Integer2 attribute",210true,211false,212false,213integer2AttributeDescriptor);214215Descriptor hashtable2AttributeDescriptor =216new DescriptorSupport(new String[] {217"name=Hashtable2",218"descriptorType=attribute"219});220hashtable2AttributeDescriptor.setField("default", new Hashtable());221ModelMBeanAttributeInfo hashtable2AttributeInfo =222new ModelMBeanAttributeInfo("Hashtable2",223"java.util.Hashtable",224"Hashtable2 attribute",225true,226false,227false,228hashtable2AttributeDescriptor);229230Descriptor map2AttributeDescriptor =231new DescriptorSupport(new String[] {232"name=Map2",233"descriptorType=attribute"234});235map2AttributeDescriptor.setField("default", new Hashtable());236ModelMBeanAttributeInfo map2AttributeInfo =237new ModelMBeanAttributeInfo("Map2",238"java.util.Map",239"Map2 attribute",240true,241false,242false,243map2AttributeDescriptor);244245// ModelMBeanInfo246247ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(248Resource.class.getName(),249"Resource MBean",250new ModelMBeanAttributeInfo[] { nullAttributeInfo,251integerAttributeInfo,252hashtableAttributeInfo,253mapAttributeInfo,254null2AttributeInfo,255integer2AttributeInfo,256hashtable2AttributeInfo,257map2AttributeInfo },258null,259new ModelMBeanOperationInfo[] { nullOperationInfo,260integerOperationInfo,261hashtableOperationInfo,262mapOperationInfo },263null);264265// RequiredModelMBean266267ModelMBean mmb = new RequiredModelMBean(mmbi);268mmb.setManagedResource(resource, "ObjectReference");269ObjectName mmbName = new ObjectName(":type=ResourceMBean");270mbs.registerMBean(mmb, mmbName);271272// Run tests273274System.out.println("\nTesting that we can call getNull()... ");275try {276Object o = mbs.getAttribute(mmbName, "Null");277System.out.println("getNull() = " + o);278System.out.println("Attribute's declared type = java.lang.Object");279System.out.println("Returned value's type = null");280} catch (Exception e) {281System.out.println("TEST FAILED: Caught exception:");282e.printStackTrace(System.out);283ok = false;284}285286System.out.println("\nTesting that we can call getInteger()... ");287try {288Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");289System.out.println("getInteger() = " + i);290System.out.println("Attribute's declared type = int");291System.out.println("Returned value's type = " +292i.getClass().getName());293} catch (Exception e) {294System.out.println("TEST FAILED: Caught exception:");295e.printStackTrace(System.out);296ok = false;297}298299System.out.println("\nTesting that we can call getHashtable()... ");300try {301Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");302System.out.println("getHashtable() = " + h);303System.out.println("Attribute's declared type = " +304"java.util.Hashtable");305System.out.println("Returned value's type = " +306h.getClass().getName());307} catch (Exception e) {308System.out.println("TEST FAILED: Caught exception:");309e.printStackTrace(System.out);310ok = false;311}312313System.out.println("\nTesting that we can call getMap()... ");314try {315Map m = (Map) mbs.getAttribute(mmbName, "Map");316System.out.println("getMap() = " + m);317System.out.println("Attribute's declared type = " +318"java.util.Map");319System.out.println("Returned value's type = " +320m.getClass().getName());321} catch (Exception e) {322System.out.println("TEST FAILED: Caught exception:");323e.printStackTrace(System.out);324ok = false;325}326327System.out.println("\nTesting that we can call getNull2()... ");328try {329Object o = mbs.getAttribute(mmbName, "Null2");330System.out.println("getNull2() = " + o);331System.out.println("Attribute's declared type = java.lang.Object");332System.out.println("Returned value's type = null");333} catch (Exception e) {334System.out.println("TEST FAILED: Caught exception:");335e.printStackTrace(System.out);336ok = false;337}338339System.out.println("\nTesting that we can call getInteger2()... ");340try {341Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");342System.out.println("getInteger2() = " + i);343System.out.println("Attribute's declared type = int");344System.out.println("Returned value's type = " +345i.getClass().getName());346} catch (Exception e) {347System.out.println("TEST FAILED: Caught exception:");348e.printStackTrace(System.out);349ok = false;350}351352System.out.println("\nTesting that we can call getHashtable2()... ");353try {354Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");355System.out.println("getHashtable2() = " + h);356System.out.println("Attribute's declared type = " +357"java.util.Hashtable");358System.out.println("Returned value's type = " +359h.getClass().getName());360} catch (Exception e) {361System.out.println("TEST FAILED: Caught exception:");362e.printStackTrace(System.out);363ok = false;364}365366System.out.println("\nTesting that we can call getMap2()... ");367try {368Map m = (Map) mbs.getAttribute(mmbName, "Map2");369System.out.println("getMap2() = " + m);370System.out.println("Attribute's declared type = " +371"java.util.Map");372System.out.println("Returned value's type = " +373m.getClass().getName());374} catch (Exception e) {375System.out.println("TEST FAILED: Caught exception:");376e.printStackTrace(System.out);377ok = false;378}379380if (ok)381System.out.println("\nTest passed.\n");382else {383System.out.println("\nTest failed.\n");384System.exit(1);385}386}387388public static class Resource {389public Object getNull() {390return null;391}392public int getInteger() {393return 10;394}395public Hashtable getHashtable() {396return new Hashtable();397}398public Map getMap() {399return new Hashtable();400}401}402403private static Resource resource = new Resource();404}405406407