Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/SimpleModelMBean/SimpleModelMBeanCommand.java
38855 views
/*1* Copyright (c) 2003, 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 487481926* @summary Test that MBeanInfo classes no longer throw an27* IllegalArgumentException when attribute names, operation names, and28* Java type names do not strictly follow the expected Java syntax.29* @author Eamonn McManus, Daniel Fuchs30* @run clean SimpleModelMBeanCommand31* @run build SimpleModelMBeanCommand32* @run main/othervm/policy=policy SimpleModelMBeanCommand33*/3435import java.lang.reflect.*;36import java.util.*;37import javax.management.*;38import javax.management.modelmbean.*;3940public class SimpleModelMBeanCommand {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 that we can get an attribute of type Object[] */204Object[] objs = (Object[]) mbs.getAttribute(on, "Array");205for (int i = 0; i < objs.length; i++)206System.out.println(objs[i]);207break;208case 1:209/* Check that we can get an attribute of type int */210Integer n = (Integer) mbs.getAttribute(on, "Number");211System.out.println(n);212break;213case 2:214/* Check that we can call an operation that returns int */215Integer n1 =216(Integer) mbs.invoke(on, "addOne",217new Integer[] {new Integer(1233)},218new String[] {"int"});219System.out.println(n1);220break;221case 3:222/* Check that we don't get an exception if you sendNotification223without any listeners. */224Notification notif = new Notification("type", "source", 123L);225mbs.invoke(on, "sendNotification", new Object[] {notif},226new String[] {"javax.management.Notification"});227System.out.println("Successfully sent notification");228break;229case 4:230/* Check that we can call addAttributeChangeNotificationListener231with null attribute. */232NotificationListener listener = new NotificationListener() {233public void handleNotification(Notification notif,234Object handback) {235System.out.println("Got notif: " + notif +236" with handback: " + handback);237}238};239mbs.invoke(on, "addAttributeChangeNotificationListener",240new Object[] {listener, null, "the-handback"},241new String[] {242"javax.management.NotificationListener",243"java.lang.String",244"java.lang.Object",245});246mbs.setAttribute(on, new Attribute("Number", new Integer(4321)));247System.out.println("Attribute value now: " +248mbs.getAttribute(on, "Number"));249break;250case 5:251/* Check that the default caching behaviour is not to cache. */252Object[] firstGot = (Object[]) mbs.getAttribute(on, "Array");253System.out.println("First got: " + Arrays.asList(firstGot));254ModelMBeanInfo mmbi = (ModelMBeanInfo) mbs.getMBeanInfo(on);255System.out.println(mmbi.getDescriptor("Array", "attribute"));256mbs.invoke(on, "tweakArray", new Object[] {new Object[] {"x"}},257new String[] {Object[].class.getName()});258Object[] secondGot = (Object[]) mbs.getAttribute(on, "Array");259System.out.println("Second got: " + Arrays.asList(secondGot));260if (secondGot.length != 1)261throw new Exception("Got value: " + Arrays.asList(secondGot));262break;263case 6:264/* Check that attributes without getters or setters work.265The value is stored in the descriptor. This test includes266an explicit currencyTimeLimit attribute. */267mbs.setAttribute(on, new Attribute("Local", "string value"));268ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbs.getMBeanInfo(on);269System.out.println(mmbi2.getDescriptor("Local", "attribute"));270Object gotback = mbs.getAttribute(on, "Local");271if (!"string value".equals(gotback))272throw new Exception("Got value: " + gotback);273break;274case 7:275/* Check that attributes without getters or setters work.276The value is stored in the descriptor. This test does277not have an explicit currencyTimeLimit attribute. */278mbs.setAttribute(on, new Attribute("Local2", "thing value"));279ModelMBeanInfo mmbi3 = (ModelMBeanInfo) mbs.getMBeanInfo(on);280System.out.println(mmbi3.getDescriptor("Local2", "attribute"));281Object gotback2 = mbs.getAttribute(on, "Local2");282if (!"thing value".equals(gotback2))283throw new Exception("Got value: " + gotback2);284break;285default:286System.err.println("UNKNOWN TEST NUMBER " + testno);287break;288}289}290291private static final int NTESTS = 8;292293}294295296