Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/modelmbean/UnserializableTargetObjectTest.java
38840 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 633296226* @summary Test that a RequiredModelMBean operation can have a targetObject27* that is not serializable28* @author Eamonn McManus29* @run clean UnserializableTargetObjectTest30* @run build UnserializableTargetObjectTest31* @run main UnserializableTargetObjectTest32*/3334/* This test and DescriptorSupportSerialTest basically cover the same thing.35I wrote them at different times and forgot that I had written the earlier36one. However the coverage is slightly different so I'm keeping both. */3738import java.lang.reflect.Method;39import javax.management.Attribute;40import javax.management.Descriptor;41import javax.management.MBeanServer;42import javax.management.MBeanServerConnection;43import javax.management.MBeanServerFactory;44import javax.management.ObjectName;45import javax.management.modelmbean.DescriptorSupport;46import javax.management.modelmbean.ModelMBean;47import javax.management.modelmbean.ModelMBeanAttributeInfo;48import javax.management.modelmbean.ModelMBeanInfo;49import javax.management.modelmbean.ModelMBeanInfoSupport;50import javax.management.modelmbean.ModelMBeanOperationInfo;51import javax.management.modelmbean.RequiredModelMBean;52import javax.management.remote.JMXConnector;53import javax.management.remote.JMXConnectorFactory;54import javax.management.remote.JMXConnectorServer;55import javax.management.remote.JMXConnectorServerFactory;56import javax.management.remote.JMXServiceURL;5758public class UnserializableTargetObjectTest {59public static class Resource { // not serializable!60int count;61int operationCount;6263public void operation() {64operationCount++;65}6667public int getCount() {68return count;69}7071public void setCount(int count) {72this.count = count;73}74}7576public static void main(String[] args) throws Exception {77MBeanServer mbs = MBeanServerFactory.newMBeanServer();78ObjectName name = new ObjectName("a:b=c");79Resource resource1 = new Resource();80Resource resource2 = new Resource();81Resource resource3 = new Resource();82Method operationMethod = Resource.class.getMethod("operation");83Method getCountMethod = Resource.class.getMethod("getCount");84Method setCountMethod = Resource.class.getMethod("setCount", int.class);85Descriptor operationDescriptor =86new DescriptorSupport(new String[] {87"descriptorType", "name", "targetObject"88}, new Object[] {89"operation", "operation", resource190});91Descriptor getCountDescriptor =92new DescriptorSupport(new String[] {93"descriptorType", "name", "targetObject"94}, new Object[] {95"operation", "getCount", resource296});97Descriptor setCountDescriptor =98new DescriptorSupport(new String[] {99"descriptorType", "name", "targetObject"100}, new Object[] {101"operation", "setCount", resource2102});103Descriptor countDescriptor =104new DescriptorSupport(new String[] {105"descriptorType", "name", "getMethod", "setMethod"106}, new Object[] {107"attribute", "Count", "getCount", "setCount"108});109ModelMBeanOperationInfo operationInfo =110new ModelMBeanOperationInfo("operation description",111operationMethod, operationDescriptor);112ModelMBeanOperationInfo getCountInfo =113new ModelMBeanOperationInfo("getCount description",114getCountMethod, getCountDescriptor);115ModelMBeanOperationInfo setCountInfo =116new ModelMBeanOperationInfo("setCount description",117setCountMethod, setCountDescriptor);118ModelMBeanAttributeInfo countInfo =119new ModelMBeanAttributeInfo("Count", "Count description",120getCountMethod, setCountMethod,121countDescriptor);122ModelMBeanInfo mmbi =123new ModelMBeanInfoSupport(Resource.class.getName(),124"ModelMBean to test targetObject",125new ModelMBeanAttributeInfo[] {countInfo},126null, // no constructors127new ModelMBeanOperationInfo[] {128operationInfo, getCountInfo, setCountInfo129},130null); // no notifications131ModelMBean mmb = new RequiredModelMBean(mmbi);132mmb.setManagedResource(resource3, "ObjectReference");133mbs.registerMBean(mmb, name);134mbs.invoke(name, "operation", null, null);135mbs.setAttribute(name, new Attribute("Count", 53));136if (resource1.operationCount != 1)137throw new Exception("operationCount: " + resource1.operationCount);138if (resource2.count != 53)139throw new Exception("count: " + resource2.count);140int got = (Integer) mbs.getAttribute(name, "Count");141if (got != 53)142throw new Exception("got count: " + got);143144JMXServiceURL url = new JMXServiceURL("rmi", null, 0);145JMXConnectorServer cs =146JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);147cs.start();148JMXServiceURL addr = cs.getAddress();149JMXConnector cc = JMXConnectorFactory.connect(addr);150MBeanServerConnection mbsc = cc.getMBeanServerConnection();151ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);152// Above gets NotSerializableException if resource included in153// serialized form154cc.close();155cs.stop();156System.out.println("TEST PASSED");157}158}159160161