0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6332962
0N/A * @summary Test that a RequiredModelMBean operation can have a targetObject
0N/A * that is not serializable
0N/A * @author Eamonn McManus
0N/A * @run clean UnserializableTargetObjectTest
0N/A * @run build UnserializableTargetObjectTest
0N/A * @run main UnserializableTargetObjectTest
0N/A */
0N/A
0N/A/* This test and DescriptorSupportSerialTest basically cover the same thing.
0N/A I wrote them at different times and forgot that I had written the earlier
0N/A one. However the coverage is slightly different so I'm keeping both. */
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.Descriptor;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerConnection;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.modelmbean.DescriptorSupport;
0N/Aimport javax.management.modelmbean.ModelMBean;
0N/Aimport javax.management.modelmbean.ModelMBeanAttributeInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfo;
0N/Aimport javax.management.modelmbean.ModelMBeanInfoSupport;
0N/Aimport javax.management.modelmbean.ModelMBeanOperationInfo;
0N/Aimport javax.management.modelmbean.RequiredModelMBean;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnectorServer;
0N/Aimport javax.management.remote.JMXConnectorServerFactory;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/A
0N/Apublic class UnserializableTargetObjectTest {
0N/A public static class Resource { // not serializable!
0N/A int count;
0N/A int operationCount;
0N/A
0N/A public void operation() {
0N/A operationCount++;
0N/A }
0N/A
0N/A public int getCount() {
0N/A return count;
0N/A }
0N/A
0N/A public void setCount(int count) {
0N/A this.count = count;
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
0N/A ObjectName name = new ObjectName("a:b=c");
0N/A Resource resource1 = new Resource();
0N/A Resource resource2 = new Resource();
0N/A Resource resource3 = new Resource();
0N/A Method operationMethod = Resource.class.getMethod("operation");
0N/A Method getCountMethod = Resource.class.getMethod("getCount");
0N/A Method setCountMethod = Resource.class.getMethod("setCount", int.class);
0N/A Descriptor operationDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "descriptorType", "name", "targetObject"
0N/A }, new Object[] {
0N/A "operation", "operation", resource1
0N/A });
0N/A Descriptor getCountDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "descriptorType", "name", "targetObject"
0N/A }, new Object[] {
0N/A "operation", "getCount", resource2
0N/A });
0N/A Descriptor setCountDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "descriptorType", "name", "targetObject"
0N/A }, new Object[] {
0N/A "operation", "setCount", resource2
0N/A });
0N/A Descriptor countDescriptor =
0N/A new DescriptorSupport(new String[] {
0N/A "descriptorType", "name", "getMethod", "setMethod"
0N/A }, new Object[] {
0N/A "attribute", "Count", "getCount", "setCount"
0N/A });
0N/A ModelMBeanOperationInfo operationInfo =
0N/A new ModelMBeanOperationInfo("operation description",
0N/A operationMethod, operationDescriptor);
0N/A ModelMBeanOperationInfo getCountInfo =
0N/A new ModelMBeanOperationInfo("getCount description",
0N/A getCountMethod, getCountDescriptor);
0N/A ModelMBeanOperationInfo setCountInfo =
0N/A new ModelMBeanOperationInfo("setCount description",
0N/A setCountMethod, setCountDescriptor);
0N/A ModelMBeanAttributeInfo countInfo =
0N/A new ModelMBeanAttributeInfo("Count", "Count description",
0N/A getCountMethod, setCountMethod,
0N/A countDescriptor);
0N/A ModelMBeanInfo mmbi =
0N/A new ModelMBeanInfoSupport(Resource.class.getName(),
0N/A "ModelMBean to test targetObject",
0N/A new ModelMBeanAttributeInfo[] {countInfo},
0N/A null, // no constructors
0N/A new ModelMBeanOperationInfo[] {
0N/A operationInfo, getCountInfo, setCountInfo
0N/A },
0N/A null); // no notifications
0N/A ModelMBean mmb = new RequiredModelMBean(mmbi);
0N/A mmb.setManagedResource(resource3, "ObjectReference");
0N/A mbs.registerMBean(mmb, name);
0N/A mbs.invoke(name, "operation", null, null);
0N/A mbs.setAttribute(name, new Attribute("Count", 53));
0N/A if (resource1.operationCount != 1)
0N/A throw new Exception("operationCount: " + resource1.operationCount);
0N/A if (resource2.count != 53)
0N/A throw new Exception("count: " + resource2.count);
0N/A int got = (Integer) mbs.getAttribute(name, "Count");
0N/A if (got != 53)
0N/A throw new Exception("got count: " + got);
0N/A
0N/A JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
0N/A JMXConnectorServer cs =
0N/A JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
0N/A cs.start();
0N/A JMXServiceURL addr = cs.getAddress();
0N/A JMXConnector cc = JMXConnectorFactory.connect(addr);
0N/A MBeanServerConnection mbsc = cc.getMBeanServerConnection();
0N/A ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
0N/A // Above gets NotSerializableException if resource included in
0N/A // serialized form
0N/A cc.close();
0N/A cs.stop();
0N/A System.out.println("TEST PASSED");
0N/A }
0N/A}