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