Path: blob/master/test/jdk/javax/management/modelmbean/UnserializableTargetObjectTest.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 633296226* @summary Test that a RequiredModelMBean operation can have a targetObject27* that is not serializable28* @author Eamonn McManus29* @modules java.management.rmi30* @run clean UnserializableTargetObjectTest31* @run build UnserializableTargetObjectTest32* @run main UnserializableTargetObjectTest33*/3435/* This test and DescriptorSupportSerialTest basically cover the same thing.36I wrote them at different times and forgot that I had written the earlier37one. However the coverage is slightly different so I'm keeping both. */3839import java.lang.reflect.Method;40import javax.management.Attribute;41import javax.management.Descriptor;42import javax.management.MBeanServer;43import javax.management.MBeanServerConnection;44import javax.management.MBeanServerFactory;45import javax.management.ObjectName;46import javax.management.modelmbean.DescriptorSupport;47import javax.management.modelmbean.ModelMBean;48import javax.management.modelmbean.ModelMBeanAttributeInfo;49import javax.management.modelmbean.ModelMBeanInfo;50import javax.management.modelmbean.ModelMBeanInfoSupport;51import javax.management.modelmbean.ModelMBeanOperationInfo;52import javax.management.modelmbean.RequiredModelMBean;53import javax.management.remote.JMXConnector;54import javax.management.remote.JMXConnectorFactory;55import javax.management.remote.JMXConnectorServer;56import javax.management.remote.JMXConnectorServerFactory;57import javax.management.remote.JMXServiceURL;5859public class UnserializableTargetObjectTest {60public static class Resource { // not serializable!61int count;62int operationCount;6364public void operation() {65operationCount++;66}6768public int getCount() {69return count;70}7172public void setCount(int count) {73this.count = count;74}75}7677public static void main(String[] args) throws Exception {78MBeanServer mbs = MBeanServerFactory.newMBeanServer();79ObjectName name = new ObjectName("a:b=c");80Resource resource1 = new Resource();81Resource resource2 = new Resource();82Resource resource3 = new Resource();83Method operationMethod = Resource.class.getMethod("operation");84Method getCountMethod = Resource.class.getMethod("getCount");85Method setCountMethod = Resource.class.getMethod("setCount", int.class);86Descriptor operationDescriptor =87new DescriptorSupport(new String[] {88"descriptorType", "name", "targetObject"89}, new Object[] {90"operation", "operation", resource191});92Descriptor getCountDescriptor =93new DescriptorSupport(new String[] {94"descriptorType", "name", "targetObject"95}, new Object[] {96"operation", "getCount", resource297});98Descriptor setCountDescriptor =99new DescriptorSupport(new String[] {100"descriptorType", "name", "targetObject"101}, new Object[] {102"operation", "setCount", resource2103});104Descriptor countDescriptor =105new DescriptorSupport(new String[] {106"descriptorType", "name", "getMethod", "setMethod"107}, new Object[] {108"attribute", "Count", "getCount", "setCount"109});110ModelMBeanOperationInfo operationInfo =111new ModelMBeanOperationInfo("operation description",112operationMethod, operationDescriptor);113ModelMBeanOperationInfo getCountInfo =114new ModelMBeanOperationInfo("getCount description",115getCountMethod, getCountDescriptor);116ModelMBeanOperationInfo setCountInfo =117new ModelMBeanOperationInfo("setCount description",118setCountMethod, setCountDescriptor);119ModelMBeanAttributeInfo countInfo =120new ModelMBeanAttributeInfo("Count", "Count description",121getCountMethod, setCountMethod,122countDescriptor);123ModelMBeanInfo mmbi =124new ModelMBeanInfoSupport(Resource.class.getName(),125"ModelMBean to test targetObject",126new ModelMBeanAttributeInfo[] {countInfo},127null, // no constructors128new ModelMBeanOperationInfo[] {129operationInfo, getCountInfo, setCountInfo130},131null); // no notifications132ModelMBean mmb = new RequiredModelMBean(mmbi);133mmb.setManagedResource(resource3, "ObjectReference");134mbs.registerMBean(mmb, name);135mbs.invoke(name, "operation", null, null);136mbs.setAttribute(name, new Attribute("Count", 53));137if (resource1.operationCount != 1)138throw new Exception("operationCount: " + resource1.operationCount);139if (resource2.count != 53)140throw new Exception("count: " + resource2.count);141int got = (Integer) mbs.getAttribute(name, "Count");142if (got != 53)143throw new Exception("got count: " + got);144145JMXServiceURL url = new JMXServiceURL("rmi", null, 0);146JMXConnectorServer cs =147JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);148cs.start();149JMXServiceURL addr = cs.getAddress();150JMXConnector cc = JMXConnectorFactory.connect(addr);151MBeanServerConnection mbsc = cc.getMBeanServerConnection();152ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);153// Above gets NotSerializableException if resource included in154// serialized form155cc.close();156cs.stop();157System.out.println("TEST PASSED");158}159}160161162