Path: blob/master/test/jdk/javax/management/modelmbean/RequiredModelMBeanGetAttributeTest.java
41149 views
/*1* Copyright (c) 2005, 2015, 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*39* @run clean RequiredModelMBeanGetAttributeTest40* @run build RequiredModelMBeanGetAttributeTest41* @run main RequiredModelMBeanGetAttributeTest42*/4344import java.lang.reflect.Method;45import java.util.Hashtable;46import java.util.Map;47import javax.management.Descriptor;48import javax.management.MBeanServer;49import javax.management.MBeanServerFactory;50import javax.management.ObjectName;51import javax.management.modelmbean.DescriptorSupport;52import javax.management.modelmbean.ModelMBean;53import javax.management.modelmbean.ModelMBeanAttributeInfo;54import javax.management.modelmbean.ModelMBeanInfo;55import javax.management.modelmbean.ModelMBeanInfoSupport;56import javax.management.modelmbean.ModelMBeanOperationInfo;57import javax.management.modelmbean.RequiredModelMBean;5859public class RequiredModelMBeanGetAttributeTest {6061public static void main(String[] args) throws Exception {6263boolean ok = true;6465MBeanServer mbs = MBeanServerFactory.createMBeanServer();6667// Resource methods6869Method nullGetter =70Resource.class.getMethod("getNull", (Class[]) null);71Method integerGetter =72Resource.class.getMethod("getInteger", (Class[]) null);73Method hashtableGetter =74Resource.class.getMethod("getHashtable", (Class[]) null);75Method mapGetter =76Resource.class.getMethod("getMap", (Class[]) null);7778// ModelMBeanOperationInfo7980Descriptor nullOperationDescriptor =81new DescriptorSupport(new String[] {82"name=getNull",83"descriptorType=operation",84"role=getter"85});86ModelMBeanOperationInfo nullOperationInfo =87new ModelMBeanOperationInfo("Null attribute",88nullGetter,89nullOperationDescriptor);9091Descriptor integerOperationDescriptor =92new DescriptorSupport(new String[] {93"name=getInteger",94"descriptorType=operation",95"role=getter"96});97ModelMBeanOperationInfo integerOperationInfo =98new ModelMBeanOperationInfo("Integer attribute",99integerGetter,100integerOperationDescriptor);101102Descriptor hashtableOperationDescriptor =103new DescriptorSupport(new String[] {104"name=getHashtable",105"descriptorType=operation",106"role=getter"107});108ModelMBeanOperationInfo hashtableOperationInfo =109new ModelMBeanOperationInfo("Hashtable attribute",110hashtableGetter,111hashtableOperationDescriptor);112113Descriptor mapOperationDescriptor =114new DescriptorSupport(new String[] {115"name=getMap",116"descriptorType=operation",117"role=getter"118});119ModelMBeanOperationInfo mapOperationInfo =120new ModelMBeanOperationInfo("Map attribute",121mapGetter,122mapOperationDescriptor);123124// ModelMBeanAttributeInfo125126Descriptor nullAttributeDescriptor =127new DescriptorSupport(new String[] {128"name=Null",129"descriptorType=attribute",130"getMethod=getNull"131});132ModelMBeanAttributeInfo nullAttributeInfo =133new ModelMBeanAttributeInfo("Null",134"java.lang.Object",135"Null attribute",136true,137false,138false,139nullAttributeDescriptor);140141Descriptor integerAttributeDescriptor =142new DescriptorSupport(new String[] {143"name=Integer",144"descriptorType=attribute",145"getMethod=getInteger"146});147ModelMBeanAttributeInfo integerAttributeInfo =148new ModelMBeanAttributeInfo("Integer",149"int",150"Integer attribute",151true,152false,153false,154integerAttributeDescriptor);155156Descriptor hashtableAttributeDescriptor =157new DescriptorSupport(new String[] {158"name=Hashtable",159"descriptorType=attribute",160"getMethod=getHashtable"161});162ModelMBeanAttributeInfo hashtableAttributeInfo =163new ModelMBeanAttributeInfo("Hashtable",164"java.util.Hashtable",165"Hashtable attribute",166true,167false,168false,169hashtableAttributeDescriptor);170171Descriptor mapAttributeDescriptor =172new DescriptorSupport(new String[] {173"name=Map",174"descriptorType=attribute",175"getMethod=getMap"176});177ModelMBeanAttributeInfo mapAttributeInfo =178new ModelMBeanAttributeInfo("Map",179"java.util.Map",180"Map attribute",181true,182false,183false,184mapAttributeDescriptor);185186Descriptor null2AttributeDescriptor =187new DescriptorSupport(new String[] {188"name=Null2",189"descriptorType=attribute"190});191null2AttributeDescriptor.setField("default", null);192ModelMBeanAttributeInfo null2AttributeInfo =193new ModelMBeanAttributeInfo("Null2",194"java.lang.Object",195"Null2 attribute",196true,197false,198false,199null2AttributeDescriptor);200201Descriptor integer2AttributeDescriptor =202new DescriptorSupport(new String[] {203"name=Integer2",204"descriptorType=attribute"205});206integer2AttributeDescriptor.setField("default", 10);207ModelMBeanAttributeInfo integer2AttributeInfo =208new ModelMBeanAttributeInfo("Integer2",209"int",210"Integer2 attribute",211true,212false,213false,214integer2AttributeDescriptor);215216Descriptor hashtable2AttributeDescriptor =217new DescriptorSupport(new String[] {218"name=Hashtable2",219"descriptorType=attribute"220});221hashtable2AttributeDescriptor.setField("default", new Hashtable());222ModelMBeanAttributeInfo hashtable2AttributeInfo =223new ModelMBeanAttributeInfo("Hashtable2",224"java.util.Hashtable",225"Hashtable2 attribute",226true,227false,228false,229hashtable2AttributeDescriptor);230231Descriptor map2AttributeDescriptor =232new DescriptorSupport(new String[] {233"name=Map2",234"descriptorType=attribute"235});236map2AttributeDescriptor.setField("default", new Hashtable());237ModelMBeanAttributeInfo map2AttributeInfo =238new ModelMBeanAttributeInfo("Map2",239"java.util.Map",240"Map2 attribute",241true,242false,243false,244map2AttributeDescriptor);245246// ModelMBeanInfo247248ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(249Resource.class.getName(),250"Resource MBean",251new ModelMBeanAttributeInfo[] { nullAttributeInfo,252integerAttributeInfo,253hashtableAttributeInfo,254mapAttributeInfo,255null2AttributeInfo,256integer2AttributeInfo,257hashtable2AttributeInfo,258map2AttributeInfo },259null,260new ModelMBeanOperationInfo[] { nullOperationInfo,261integerOperationInfo,262hashtableOperationInfo,263mapOperationInfo },264null);265266// RequiredModelMBean267268ModelMBean mmb = new RequiredModelMBean(mmbi);269mmb.setManagedResource(resource, "ObjectReference");270ObjectName mmbName = new ObjectName(":type=ResourceMBean");271mbs.registerMBean(mmb, mmbName);272273// Run tests274275System.out.println("\nTesting that we can call getNull()... ");276try {277Object o = mbs.getAttribute(mmbName, "Null");278System.out.println("getNull() = " + o);279System.out.println("Attribute's declared type = java.lang.Object");280System.out.println("Returned value's type = null");281} catch (Exception e) {282System.out.println("TEST FAILED: Caught exception:");283e.printStackTrace(System.out);284ok = false;285}286287System.out.println("\nTesting that we can call getInteger()... ");288try {289Integer i = (Integer) mbs.getAttribute(mmbName, "Integer");290System.out.println("getInteger() = " + i);291System.out.println("Attribute's declared type = int");292System.out.println("Returned value's type = " +293i.getClass().getName());294} catch (Exception e) {295System.out.println("TEST FAILED: Caught exception:");296e.printStackTrace(System.out);297ok = false;298}299300System.out.println("\nTesting that we can call getHashtable()... ");301try {302Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable");303System.out.println("getHashtable() = " + h);304System.out.println("Attribute's declared type = " +305"java.util.Hashtable");306System.out.println("Returned value's type = " +307h.getClass().getName());308} catch (Exception e) {309System.out.println("TEST FAILED: Caught exception:");310e.printStackTrace(System.out);311ok = false;312}313314System.out.println("\nTesting that we can call getMap()... ");315try {316Map m = (Map) mbs.getAttribute(mmbName, "Map");317System.out.println("getMap() = " + m);318System.out.println("Attribute's declared type = " +319"java.util.Map");320System.out.println("Returned value's type = " +321m.getClass().getName());322} catch (Exception e) {323System.out.println("TEST FAILED: Caught exception:");324e.printStackTrace(System.out);325ok = false;326}327328System.out.println("\nTesting that we can call getNull2()... ");329try {330Object o = mbs.getAttribute(mmbName, "Null2");331System.out.println("getNull2() = " + o);332System.out.println("Attribute's declared type = java.lang.Object");333System.out.println("Returned value's type = null");334} catch (Exception e) {335System.out.println("TEST FAILED: Caught exception:");336e.printStackTrace(System.out);337ok = false;338}339340System.out.println("\nTesting that we can call getInteger2()... ");341try {342Integer i = (Integer) mbs.getAttribute(mmbName, "Integer2");343System.out.println("getInteger2() = " + i);344System.out.println("Attribute's declared type = int");345System.out.println("Returned value's type = " +346i.getClass().getName());347} catch (Exception e) {348System.out.println("TEST FAILED: Caught exception:");349e.printStackTrace(System.out);350ok = false;351}352353System.out.println("\nTesting that we can call getHashtable2()... ");354try {355Hashtable h = (Hashtable) mbs.getAttribute(mmbName, "Hashtable2");356System.out.println("getHashtable2() = " + h);357System.out.println("Attribute's declared type = " +358"java.util.Hashtable");359System.out.println("Returned value's type = " +360h.getClass().getName());361} catch (Exception e) {362System.out.println("TEST FAILED: Caught exception:");363e.printStackTrace(System.out);364ok = false;365}366367System.out.println("\nTesting that we can call getMap2()... ");368try {369Map m = (Map) mbs.getAttribute(mmbName, "Map2");370System.out.println("getMap2() = " + m);371System.out.println("Attribute's declared type = " +372"java.util.Map");373System.out.println("Returned value's type = " +374m.getClass().getName());375} catch (Exception e) {376System.out.println("TEST FAILED: Caught exception:");377e.printStackTrace(System.out);378ok = false;379}380381if (ok)382System.out.println("\nTest passed.\n");383else {384System.out.println("\nTest failed.\n");385System.exit(1);386}387}388389public static class Resource {390public Object getNull() {391return null;392}393public int getInteger() {394return 10;395}396public Hashtable getHashtable() {397return new Hashtable();398}399public Map getMap() {400return new Hashtable();401}402}403404private static Resource resource = new Resource();405}406407408