Path: blob/master/test/jdk/javax/management/modelmbean/SimpleModelMBean/SimpleModelMBeanCommand.java
41155 views
/*1* Copyright (c) 2003, 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 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*31* @run clean SimpleModelMBeanCommand32* @run build SimpleModelMBeanCommand33* @run main/othervm/java.security.policy=policy SimpleModelMBeanCommand34*/3536import java.lang.reflect.*;37import java.util.*;38import javax.management.*;39import javax.management.modelmbean.*;4041public class SimpleModelMBeanCommand {4243public static class Resource {44public int getNumber() {45return number;46}4748public void setNumber(int n) {49number = n;50}5152public int addOne(int x) {53return x + 1;54}5556public Object[] getArray() {57return (Object[]) array.clone();58}5960// doesn't look like an attribute so not seen by caching logic61public void tweakArray(Object[] array) {62this.array = (Object[]) array.clone();63}6465private int number = 1234;66private Object[] array = {"hello", "world"};67}6869public static void main(String[] args) {70int errorCount = 0;71for (int i = 0; i < NTESTS; i++) {72try {73System.out.println("Test " + i + ":");74test(i);75} catch (Throwable e) {76errorCount++;77boolean first = true;78do {79System.err.println(first ? "Exception:" : "Caused by:");80first = false;81e.printStackTrace();82Throwable nexte;83nexte = e.getCause();84if (nexte == null) { // old JMX85if (e instanceof MBeanException)86nexte = ((MBeanException) e).getTargetException();87}88e = nexte;89} while (e != null);90}91}92if (errorCount == 0) {93System.out.println("All ModelMBean tests successfuly passed");94System.out.println("Bye! Bye!");95// JTReg doesn't like System.exit(0);96return;97} else {98System.err.println("ERROR: " + errorCount + " tests failed");99System.exit(errorCount);100}101102}103104private static void test(int testno) throws Exception {105// com.sun.jmx.trace.TraceImplementation.init(2);106Resource resource = new Resource();107Class resourceClass = Resource.class;108Class rmmbClass = RequiredModelMBean.class;109Method setManagedResource =110rmmbClass.getMethod("setManagedResource",111new Class[] {Object.class,112String.class});113Method sendNotification =114rmmbClass.getMethod("sendNotification",115new Class[] {Notification.class});116Method addAttributeChangeNL =117rmmbClass.getMethod("addAttributeChangeNotificationListener",118new Class[] {NotificationListener.class,119String.class,120Object.class});121Method getArray = resourceClass.getMethod("getArray", new Class[0]);122Method getNumber = resourceClass.getMethod("getNumber", new Class[0]);123Method setNumber =124resourceClass.getMethod("setNumber", new Class[] {Integer.TYPE});125Method tweakArray =126resourceClass.getMethod("tweakArray",127new Class[] {Object[].class});128Method addOne =129resourceClass.getMethod("addOne", new Class[] {Integer.TYPE});130MBeanServer mbs = MBeanServerFactory.newMBeanServer();131ObjectName on = new ObjectName("a:b=c");132Descriptor attrDescr = new DescriptorSupport();133attrDescr.setField("name", "Array");134attrDescr.setField("descriptorType", "attribute");135attrDescr.setField("getMethod", "getArray");136ModelMBeanAttributeInfo attrInfo =137new ModelMBeanAttributeInfo("Array", "array attr", getArray,138null, attrDescr);139Descriptor attrDescr2 = new DescriptorSupport();140attrDescr2.setField("name", "Number");141attrDescr2.setField("descriptorType", "attribute");142attrDescr2.setField("getMethod", "getNumber");143attrDescr2.setField("setMethod", "setNumber");144ModelMBeanAttributeInfo attrInfo2 =145new ModelMBeanAttributeInfo("Number", "number attr", getNumber,146setNumber, attrDescr2);147Descriptor attrDescr3 = new DescriptorSupport();148attrDescr3.setField("name", "Local");149attrDescr3.setField("descriptorType", "attribute");150attrDescr3.setField("currencyTimeLimit", "" + Integer.MAX_VALUE);151ModelMBeanAttributeInfo attrInfo3 =152new ModelMBeanAttributeInfo("Local", "java.lang.String",153"local attr", true, true, false,154attrDescr3);155Descriptor attrDescr4 = new DescriptorSupport();156attrDescr4.setField("name", "Local2");157attrDescr4.setField("descriptorType", "attribute");158ModelMBeanAttributeInfo attrInfo4 =159new ModelMBeanAttributeInfo("Local2", "java.lang.String",160"local attr 2", true, true, false,161attrDescr4);162ModelMBeanAttributeInfo[] attrs =163new ModelMBeanAttributeInfo[] {attrInfo, attrInfo2, attrInfo3,164attrInfo4};165ModelMBeanOperationInfo operInfo =166new ModelMBeanOperationInfo("getArray descr", getArray);167ModelMBeanOperationInfo operInfo2 =168new ModelMBeanOperationInfo("getNumber descr", getNumber);169ModelMBeanOperationInfo operInfo3 =170new ModelMBeanOperationInfo("addOne descr", addOne);171ModelMBeanOperationInfo operInfo4 =172new ModelMBeanOperationInfo("setNumber descr", setNumber);173ModelMBeanOperationInfo operInfo5 =174new ModelMBeanOperationInfo("tweakArray descr", tweakArray);175ModelMBeanOperationInfo operInfoSetManagedResource =176new ModelMBeanOperationInfo("setManagedResource descr",177setManagedResource);178ModelMBeanOperationInfo operInfoSendNotification =179new ModelMBeanOperationInfo("sendNotification descr",180sendNotification);181ModelMBeanOperationInfo operInfoAddAttributeChangeNL =182new ModelMBeanOperationInfo("AddAttributeChangeNL descr",183addAttributeChangeNL);184ModelMBeanOperationInfo[] opers =185new ModelMBeanOperationInfo[] {operInfo, operInfo2, operInfo3,186operInfo4, operInfo5,187operInfoSetManagedResource,188operInfoSendNotification,189operInfoAddAttributeChangeNL};190ModelMBeanInfo info =191new ModelMBeanInfoSupport(Resource.class.getName(),192"Resourcish resource",193attrs, null, opers, null,194null);195mbs.createMBean(RequiredModelMBean.class.getName(),196on,197new Object[] {info},198new String[] {ModelMBeanInfo.class.getName()});199mbs.invoke(on, "setManagedResource",200new Object[] {resource, "objectReference"},201new String[] {"java.lang.Object", "java.lang.String"});202switch (testno) {203case 0:204/* Check that we can get an attribute of type Object[] */205Object[] objs = (Object[]) mbs.getAttribute(on, "Array");206for (int i = 0; i < objs.length; i++)207System.out.println(objs[i]);208break;209case 1:210/* Check that we can get an attribute of type int */211Integer n = (Integer) mbs.getAttribute(on, "Number");212System.out.println(n);213break;214case 2:215/* Check that we can call an operation that returns int */216Integer n1 =217(Integer) mbs.invoke(on, "addOne",218new Integer[] {new Integer(1233)},219new String[] {"int"});220System.out.println(n1);221break;222case 3:223/* Check that we don't get an exception if you sendNotification224without any listeners. */225Notification notif = new Notification("type", "source", 123L);226mbs.invoke(on, "sendNotification", new Object[] {notif},227new String[] {"javax.management.Notification"});228System.out.println("Successfully sent notification");229break;230case 4:231/* Check that we can call addAttributeChangeNotificationListener232with null attribute. */233NotificationListener listener = new NotificationListener() {234public void handleNotification(Notification notif,235Object handback) {236System.out.println("Got notif: " + notif +237" with handback: " + handback);238}239};240mbs.invoke(on, "addAttributeChangeNotificationListener",241new Object[] {listener, null, "the-handback"},242new String[] {243"javax.management.NotificationListener",244"java.lang.String",245"java.lang.Object",246});247mbs.setAttribute(on, new Attribute("Number", new Integer(4321)));248System.out.println("Attribute value now: " +249mbs.getAttribute(on, "Number"));250break;251case 5:252/* Check that the default caching behaviour is not to cache. */253Object[] firstGot = (Object[]) mbs.getAttribute(on, "Array");254System.out.println("First got: " + Arrays.asList(firstGot));255ModelMBeanInfo mmbi = (ModelMBeanInfo) mbs.getMBeanInfo(on);256System.out.println(mmbi.getDescriptor("Array", "attribute"));257mbs.invoke(on, "tweakArray", new Object[] {new Object[] {"x"}},258new String[] {Object[].class.getName()});259Object[] secondGot = (Object[]) mbs.getAttribute(on, "Array");260System.out.println("Second got: " + Arrays.asList(secondGot));261if (secondGot.length != 1)262throw new Exception("Got value: " + Arrays.asList(secondGot));263break;264case 6:265/* Check that attributes without getters or setters work.266The value is stored in the descriptor. This test includes267an explicit currencyTimeLimit attribute. */268mbs.setAttribute(on, new Attribute("Local", "string value"));269ModelMBeanInfo mmbi2 = (ModelMBeanInfo) mbs.getMBeanInfo(on);270System.out.println(mmbi2.getDescriptor("Local", "attribute"));271Object gotback = mbs.getAttribute(on, "Local");272if (!"string value".equals(gotback))273throw new Exception("Got value: " + gotback);274break;275case 7:276/* Check that attributes without getters or setters work.277The value is stored in the descriptor. This test does278not have an explicit currencyTimeLimit attribute. */279mbs.setAttribute(on, new Attribute("Local2", "thing value"));280ModelMBeanInfo mmbi3 = (ModelMBeanInfo) mbs.getMBeanInfo(on);281System.out.println(mmbi3.getDescriptor("Local2", "attribute"));282Object gotback2 = mbs.getAttribute(on, "Local2");283if (!"thing value".equals(gotback2))284throw new Exception("Got value: " + gotback2);285break;286default:287System.err.println("UNKNOWN TEST NUMBER " + testno);288break;289}290}291292private static final int NTESTS = 8;293294}295296297