Path: blob/master/test/jdk/javax/management/modelmbean/ModelMBeanInfoSupport/GetAllDescriptorsTest.java
41153 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 633706126* @summary Test that ModelMBeanInfoSupport.getDescriptors(null) also27* returns the MBean's descriptor.28* @author Eamonn McManus, Daniel Fuchs29*30* @run clean GetAllDescriptorsTest31* @run build GetAllDescriptorsTest32* @run main/othervm/java.security.policy=policy GetAllDescriptorsTest33*/3435import java.lang.reflect.*;36import java.util.*;37import javax.management.*;38import javax.management.modelmbean.*;3940public class GetAllDescriptorsTest {4142public static class Resource {43public int getNumber() {44return number;45}4647public void setNumber(int n) {48number = n;49}5051public int addOne(int x) {52return x + 1;53}5455public Object[] getArray() {56return (Object[]) array.clone();57}5859// doesn't look like an attribute so not seen by caching logic60public void tweakArray(Object[] array) {61this.array = (Object[]) array.clone();62}6364private int number = 1234;65private Object[] array = {"hello", "world"};66}6768public static void main(String[] args) {69int errorCount = 0;70for (int i = 0; i < NTESTS; i++) {71try {72System.out.println("Test " + i + ":");73test(i);74} catch (Throwable e) {75errorCount++;76boolean first = true;77do {78System.err.println(first ? "Exception:" : "Caused by:");79first = false;80e.printStackTrace();81Throwable nexte;82nexte = e.getCause();83if (nexte == null) { // old JMX84if (e instanceof MBeanException)85nexte = ((MBeanException) e).getTargetException();86}87e = nexte;88} while (e != null);89}90}91if (errorCount == 0) {92System.out.println("All ModelMBean tests successfuly passed");93System.out.println("Bye! Bye!");94// JTReg doesn't like System.exit(0);95return;96} else {97System.err.println("ERROR: " + errorCount + " tests failed");98System.exit(errorCount);99}100101}102103private static void test(int testno) throws Exception {104// com.sun.jmx.trace.TraceImplementation.init(2);105Resource resource = new Resource();106Class resourceClass = Resource.class;107Class rmmbClass = RequiredModelMBean.class;108Method setManagedResource =109rmmbClass.getMethod("setManagedResource",110new Class[] {Object.class,111String.class});112Method sendNotification =113rmmbClass.getMethod("sendNotification",114new Class[] {Notification.class});115Method addAttributeChangeNL =116rmmbClass.getMethod("addAttributeChangeNotificationListener",117new Class[] {NotificationListener.class,118String.class,119Object.class});120Method getArray = resourceClass.getMethod("getArray", new Class[0]);121Method getNumber = resourceClass.getMethod("getNumber", new Class[0]);122Method setNumber =123resourceClass.getMethod("setNumber", new Class[] {Integer.TYPE});124Method tweakArray =125resourceClass.getMethod("tweakArray",126new Class[] {Object[].class});127Method addOne =128resourceClass.getMethod("addOne", new Class[] {Integer.TYPE});129MBeanServer mbs = MBeanServerFactory.newMBeanServer();130ObjectName on = new ObjectName("a:b=c");131Descriptor attrDescr = new DescriptorSupport();132attrDescr.setField("name", "Array");133attrDescr.setField("descriptorType", "attribute");134attrDescr.setField("getMethod", "getArray");135ModelMBeanAttributeInfo attrInfo =136new ModelMBeanAttributeInfo("Array", "array attr", getArray,137null, attrDescr);138Descriptor attrDescr2 = new DescriptorSupport();139attrDescr2.setField("name", "Number");140attrDescr2.setField("descriptorType", "attribute");141attrDescr2.setField("getMethod", "getNumber");142attrDescr2.setField("setMethod", "setNumber");143ModelMBeanAttributeInfo attrInfo2 =144new ModelMBeanAttributeInfo("Number", "number attr", getNumber,145setNumber, attrDescr2);146Descriptor attrDescr3 = new DescriptorSupport();147attrDescr3.setField("name", "Local");148attrDescr3.setField("descriptorType", "attribute");149attrDescr3.setField("currencyTimeLimit", "" + Integer.MAX_VALUE);150ModelMBeanAttributeInfo attrInfo3 =151new ModelMBeanAttributeInfo("Local", "java.lang.String",152"local attr", true, true, false,153attrDescr3);154Descriptor attrDescr4 = new DescriptorSupport();155attrDescr4.setField("name", "Local2");156attrDescr4.setField("descriptorType", "attribute");157ModelMBeanAttributeInfo attrInfo4 =158new ModelMBeanAttributeInfo("Local2", "java.lang.String",159"local attr 2", true, true, false,160attrDescr4);161ModelMBeanAttributeInfo[] attrs =162new ModelMBeanAttributeInfo[] {attrInfo, attrInfo2, attrInfo3,163attrInfo4};164ModelMBeanOperationInfo operInfo =165new ModelMBeanOperationInfo("getArray descr", getArray);166ModelMBeanOperationInfo operInfo2 =167new ModelMBeanOperationInfo("getNumber descr", getNumber);168ModelMBeanOperationInfo operInfo3 =169new ModelMBeanOperationInfo("addOne descr", addOne);170ModelMBeanOperationInfo operInfo4 =171new ModelMBeanOperationInfo("setNumber descr", setNumber);172ModelMBeanOperationInfo operInfo5 =173new ModelMBeanOperationInfo("tweakArray descr", tweakArray);174ModelMBeanOperationInfo operInfoSetManagedResource =175new ModelMBeanOperationInfo("setManagedResource descr",176setManagedResource);177ModelMBeanOperationInfo operInfoSendNotification =178new ModelMBeanOperationInfo("sendNotification descr",179sendNotification);180ModelMBeanOperationInfo operInfoAddAttributeChangeNL =181new ModelMBeanOperationInfo("AddAttributeChangeNL descr",182addAttributeChangeNL);183ModelMBeanOperationInfo[] opers =184new ModelMBeanOperationInfo[] {operInfo, operInfo2, operInfo3,185operInfo4, operInfo5,186operInfoSetManagedResource,187operInfoSendNotification,188operInfoAddAttributeChangeNL};189ModelMBeanInfo info =190new ModelMBeanInfoSupport(Resource.class.getName(),191"Resourcish resource",192attrs, null, opers, null,193null);194mbs.createMBean(RequiredModelMBean.class.getName(),195on,196new Object[] {info},197new String[] {ModelMBeanInfo.class.getName()});198mbs.invoke(on, "setManagedResource",199new Object[] {resource, "objectReference"},200new String[] {"java.lang.Object", "java.lang.String"});201switch (testno) {202case 0: {203/* Check getDescriptors("") on original MBeanInfo */204final Descriptor[] desc = info.getDescriptors("");205checkDescriptors(info,desc,"info.getDescriptors(\"\")");206break;207}208case 1: {209/* Check getDescriptors(null) on original MBeanInfo */210final Descriptor[] desc = info.getDescriptors(null);211checkDescriptors(info,desc,"info.getDescriptors(null)");212break;213}214case 2: {215/* Check getDescriptors("") on retrieved MBeanInfo */216final MBeanInfo mbi = mbs.getMBeanInfo(on);217final ModelMBeanInfo model = (ModelMBeanInfo)mbi;218final Descriptor[] desc = model.getDescriptors("");219checkDescriptors(info,desc,"model.getDescriptors(\"\")");220break;221}222case 3: {223/* Check getDescriptors(null) on retrieved MBeanInfo */224final MBeanInfo mbi = mbs.getMBeanInfo(on);225final ModelMBeanInfo model = (ModelMBeanInfo)mbi;226final Descriptor[] desc = model.getDescriptors(null);227checkDescriptors(info,desc,"model.getDescriptors(null)");228break;229}230default:231System.err.println("UNKNOWN TEST NUMBER " + testno);232break;233}234}235236/* Removes descriptor from the list and returns it. Returns {@code null}237if descriptor is not found */238private static Descriptor remove(ArrayList<Descriptor> list,239Descriptor item) {240if (list.remove(item)) return item;241else return null;242}243244/* Check that all descriptors have been returned */245private static void checkDescriptors(ModelMBeanInfo modelMBeanInfo,246Descriptor[] descriptors, String string) {247int errCount = 0;248final ArrayList<Descriptor> list =249new ArrayList<Descriptor>(descriptors.length);250list.addAll(Arrays.asList(descriptors));251System.out.println("Got " + list.size() + " descriptors for "+string);252253// checks that MBean's descriptor is returned.254//255final Descriptor mbd = ((MBeanInfo)modelMBeanInfo).getDescriptor();256if (!mbd.equals(remove(list,mbd))) {257System.err.println("modelMBeanInfo.getDescriptor(): not found");258errCount++;259}260261// checks that MBean's attributes descriptors are returned.262//263final MBeanAttributeInfo[] attrs = modelMBeanInfo.getAttributes();264for (MBeanAttributeInfo att : attrs) {265final Descriptor ad = att.getDescriptor();266final String name = att.getName();267if (!ad.equals(remove(list,ad))) {268System.err.println("attInfo.getDescriptor(): not found for "+269name);270errCount++;271}272}273274// checks that MBean's operations descriptors are returned.275//276final MBeanOperationInfo[] ops = modelMBeanInfo.getOperations();277for (MBeanOperationInfo op : ops) {278final Descriptor od = op.getDescriptor();279final String name = op.getName();280if (!od.equals(remove(list,od))) {281System.err.println("opInfo.getDescriptor(): not found for "+282name);283errCount++;284}285}286287// checks that MBean's notifications descriptors are returned.288//289final MBeanNotificationInfo[] ntfs = modelMBeanInfo.getNotifications();290for (MBeanNotificationInfo ntf : ntfs) {291final Descriptor nd = ntf.getDescriptor();292final String name = ntf.getName();293if (!nd.equals(remove(list,nd))) {294System.err.println("notifInfo.getDescriptor(): not found for "+295name);296errCount++;297}298}299if (errCount > 0) {300throw new RuntimeException(string+": failed with "+errCount+301" errors");302} else if (list.size() != 0) {303// Check that there are no additional descriptors304//305throw new RuntimeException(string+306": Unexpected remaining descriptors: "+list);307} else System.out.println(string+": PASSED");308}309310private static final int NTESTS = 4;311312}313314315