J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6332962 |
| 27 | * @summary Test that a RequiredModelMBean operation can have a targetObject |
| 28 | * that is not serializable |
| 29 | * @author Eamonn McManus |
| 30 | * @run clean UnserializableTargetObjectTest |
| 31 | * @run build UnserializableTargetObjectTest |
| 32 | * @run main UnserializableTargetObjectTest |
| 33 | */ |
| 34 | |
| 35 | /* This test and DescriptorSupportSerialTest basically cover the same thing. |
| 36 | I wrote them at different times and forgot that I had written the earlier |
| 37 | one. However the coverage is slightly different so I'm keeping both. */ |
| 38 | |
| 39 | import java.lang.reflect.Method; |
| 40 | import javax.management.Attribute; |
| 41 | import javax.management.Descriptor; |
| 42 | import javax.management.MBeanServer; |
| 43 | import javax.management.MBeanServerConnection; |
| 44 | import javax.management.MBeanServerFactory; |
| 45 | import javax.management.ObjectName; |
| 46 | import javax.management.modelmbean.DescriptorSupport; |
| 47 | import javax.management.modelmbean.ModelMBean; |
| 48 | import javax.management.modelmbean.ModelMBeanAttributeInfo; |
| 49 | import javax.management.modelmbean.ModelMBeanInfo; |
| 50 | import javax.management.modelmbean.ModelMBeanInfoSupport; |
| 51 | import javax.management.modelmbean.ModelMBeanOperationInfo; |
| 52 | import javax.management.modelmbean.RequiredModelMBean; |
| 53 | import javax.management.remote.JMXConnector; |
| 54 | import javax.management.remote.JMXConnectorFactory; |
| 55 | import javax.management.remote.JMXConnectorServer; |
| 56 | import javax.management.remote.JMXConnectorServerFactory; |
| 57 | import javax.management.remote.JMXServiceURL; |
| 58 | |
| 59 | public class UnserializableTargetObjectTest { |
| 60 | public static class Resource { // not serializable! |
| 61 | int count; |
| 62 | int operationCount; |
| 63 | |
| 64 | public void operation() { |
| 65 | operationCount++; |
| 66 | } |
| 67 | |
| 68 | public int getCount() { |
| 69 | return count; |
| 70 | } |
| 71 | |
| 72 | public void setCount(int count) { |
| 73 | this.count = count; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public static void main(String[] args) throws Exception { |
| 78 | MBeanServer mbs = MBeanServerFactory.newMBeanServer(); |
| 79 | ObjectName name = new ObjectName("a:b=c"); |
| 80 | Resource resource1 = new Resource(); |
| 81 | Resource resource2 = new Resource(); |
| 82 | Resource resource3 = new Resource(); |
| 83 | Method operationMethod = Resource.class.getMethod("operation"); |
| 84 | Method getCountMethod = Resource.class.getMethod("getCount"); |
| 85 | Method setCountMethod = Resource.class.getMethod("setCount", int.class); |
| 86 | Descriptor operationDescriptor = |
| 87 | new DescriptorSupport(new String[] { |
| 88 | "descriptorType", "name", "targetObject" |
| 89 | }, new Object[] { |
| 90 | "operation", "operation", resource1 |
| 91 | }); |
| 92 | Descriptor getCountDescriptor = |
| 93 | new DescriptorSupport(new String[] { |
| 94 | "descriptorType", "name", "targetObject" |
| 95 | }, new Object[] { |
| 96 | "operation", "getCount", resource2 |
| 97 | }); |
| 98 | Descriptor setCountDescriptor = |
| 99 | new DescriptorSupport(new String[] { |
| 100 | "descriptorType", "name", "targetObject" |
| 101 | }, new Object[] { |
| 102 | "operation", "setCount", resource2 |
| 103 | }); |
| 104 | Descriptor countDescriptor = |
| 105 | new DescriptorSupport(new String[] { |
| 106 | "descriptorType", "name", "getMethod", "setMethod" |
| 107 | }, new Object[] { |
| 108 | "attribute", "Count", "getCount", "setCount" |
| 109 | }); |
| 110 | ModelMBeanOperationInfo operationInfo = |
| 111 | new ModelMBeanOperationInfo("operation description", |
| 112 | operationMethod, operationDescriptor); |
| 113 | ModelMBeanOperationInfo getCountInfo = |
| 114 | new ModelMBeanOperationInfo("getCount description", |
| 115 | getCountMethod, getCountDescriptor); |
| 116 | ModelMBeanOperationInfo setCountInfo = |
| 117 | new ModelMBeanOperationInfo("setCount description", |
| 118 | setCountMethod, setCountDescriptor); |
| 119 | ModelMBeanAttributeInfo countInfo = |
| 120 | new ModelMBeanAttributeInfo("Count", "Count description", |
| 121 | getCountMethod, setCountMethod, |
| 122 | countDescriptor); |
| 123 | ModelMBeanInfo mmbi = |
| 124 | new ModelMBeanInfoSupport(Resource.class.getName(), |
| 125 | "ModelMBean to test targetObject", |
| 126 | new ModelMBeanAttributeInfo[] {countInfo}, |
| 127 | null, // no constructors |
| 128 | new ModelMBeanOperationInfo[] { |
| 129 | operationInfo, getCountInfo, setCountInfo |
| 130 | }, |
| 131 | null); // no notifications |
| 132 | ModelMBean mmb = new RequiredModelMBean(mmbi); |
| 133 | mmb.setManagedResource(resource3, "ObjectReference"); |
| 134 | mbs.registerMBean(mmb, name); |
| 135 | mbs.invoke(name, "operation", null, null); |
| 136 | mbs.setAttribute(name, new Attribute("Count", 53)); |
| 137 | if (resource1.operationCount != 1) |
| 138 | throw new Exception("operationCount: " + resource1.operationCount); |
| 139 | if (resource2.count != 53) |
| 140 | throw new Exception("count: " + resource2.count); |
| 141 | int got = (Integer) mbs.getAttribute(name, "Count"); |
| 142 | if (got != 53) |
| 143 | throw new Exception("got count: " + got); |
| 144 | |
| 145 | JMXServiceURL url = new JMXServiceURL("rmi", null, 0); |
| 146 | JMXConnectorServer cs = |
| 147 | JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs); |
| 148 | cs.start(); |
| 149 | JMXServiceURL addr = cs.getAddress(); |
| 150 | JMXConnector cc = JMXConnectorFactory.connect(addr); |
| 151 | MBeanServerConnection mbsc = cc.getMBeanServerConnection(); |
| 152 | ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name); |
| 153 | // Above gets NotSerializableException if resource included in |
| 154 | // serialized form |
| 155 | cc.close(); |
| 156 | cs.stop(); |
| 157 | System.out.println("TEST PASSED"); |
| 158 | } |
| 159 | } |