1 22 package org.jboss.test.mx.mxbean.test; 23 24 import java.lang.reflect.Method ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.LinkedHashMap ; 30 import java.util.LinkedHashSet ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanServer ; 37 import javax.management.ObjectName ; 38 import javax.management.openmbean.OpenMBeanAttributeInfo ; 39 import javax.management.openmbean.OpenMBeanInfo ; 40 import javax.management.openmbean.OpenType ; 41 import javax.management.openmbean.SimpleType ; 42 43 import junit.framework.Test; 44 import junit.framework.TestSuite; 45 46 import org.jboss.mx.mxbean.MXBeanFactory; 47 import org.jboss.mx.mxbean.MXBeanUtils; 48 import org.jboss.test.mx.mxbean.support.CollectionsInterface; 49 import org.jboss.test.mx.mxbean.support.CollectionsMXBeanSupport; 50 import org.jboss.test.mx.mxbean.support.CollectionsMXBeanSupportMXBean; 51 import org.jboss.test.mx.mxbean.support.CompositeInterface; 52 import org.jboss.test.mx.mxbean.support.CompositeMXBeanSupport; 53 import org.jboss.test.mx.mxbean.support.CompositeMXBeanSupportMXBean; 54 import org.jboss.test.mx.mxbean.support.SimpleInterface; 55 import org.jboss.test.mx.mxbean.support.SimpleMXBeanSupport; 56 import org.jboss.test.mx.mxbean.support.SimpleMXBeanSupportMXBean; 57 import org.jboss.test.mx.mxbean.support.SimpleObject; 58 import org.jboss.test.mx.mxbean.support.TestEnum; 59 60 66 public class MXBeanInvocationHandlerUnitTestCase extends AbstractMXBeanTest 67 { 68 public static Test suite() 69 { 70 return new TestSuite(MXBeanInvocationHandlerUnitTestCase.class); 71 } 72 73 public MXBeanInvocationHandlerUnitTestCase(String name) 74 { 75 super(name); 76 } 77 78 public void testGetSimpleAttributes() throws Exception 79 { 80 MBeanServer server = createMBeanServer(); 81 SimpleMXBeanSupport support = new SimpleMXBeanSupport(); 82 ObjectName objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 83 server.registerMBean(support, objectName); 84 SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class); 85 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 86 OpenType [] types = new OpenType [SimpleInterface.TYPES.length]; 87 for (int i = 0; i < types.length; ++i) 88 types[i] = MXBeanUtils.getOpenType(SimpleInterface.TYPES[i]); 89 checkAttributes(SimpleMXBeanSupportMXBean.class, proxy, support, SimpleInterface.KEYS, types, SimpleInterface.VALUES, info); 90 } 91 92 public void testSetSimpleAttributes() throws Exception 93 { 94 MBeanServer server = createMBeanServer(); 95 SimpleMXBeanSupport support = new SimpleMXBeanSupport(); 96 ObjectName objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 97 server.registerMBean(support, objectName); 98 SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class); 99 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 100 OpenType [] types = new OpenType [SimpleInterface.TYPES.length]; 101 for (int i = 0; i < types.length; ++i) 102 types[i] = MXBeanUtils.getOpenType(SimpleInterface.TYPES[i]); 103 proxy.setBigDecimal(SimpleInterface.bigDecimalChangedValue); 104 proxy.setBigInteger(SimpleInterface.bigIntegerChangedValue); 105 proxy.setBoolean(SimpleInterface.booleanChangedValue); 106 proxy.setByte(SimpleInterface.byteChangedValue); 107 proxy.setCharacter(SimpleInterface.characterChangedValue); 108 proxy.setDate(SimpleInterface.dateChangedValue); 109 proxy.setDouble(SimpleInterface.doubleChangedValue); 110 proxy.setFloat(SimpleInterface.floatChangedValue); 111 proxy.setInteger(SimpleInterface.integerChangedValue); 112 proxy.setLong(SimpleInterface.longChangedValue); 113 proxy.setObjectName(SimpleInterface.objectNameChangedValue); 114 proxy.setPrimitiveBoolean(SimpleInterface.primitiveBooleanChangedValue); 115 proxy.setPrimitiveByte(SimpleInterface.primitiveByteChangedValue); 116 proxy.setPrimitiveChar(SimpleInterface.primitiveCharChangedValue); 117 proxy.setPrimitiveDouble(SimpleInterface.primitiveDoubleChangedValue); 118 proxy.setPrimitiveFloat(SimpleInterface.primitiveFloatChangedValue); 119 proxy.setPrimitiveInt(SimpleInterface.primitiveIntChangedValue); 120 proxy.setPrimitiveLong(SimpleInterface.primitiveLongChangedValue); 121 proxy.setPrimitiveShort(SimpleInterface.primitiveShortChangedValue); 122 proxy.setShort(SimpleInterface.shortChangedValue); 123 proxy.setString(SimpleInterface.stringChangedValue); 124 checkAttributes(SimpleInterface.class, proxy, support, SimpleInterface.KEYS, types, SimpleInterface.CHANGED_VALUES, info); 125 } 126 127 public void testSimpleInvoke() throws Exception 128 { 129 MBeanServer server = createMBeanServer(); 130 SimpleMXBeanSupport support = new SimpleMXBeanSupport(); 131 ObjectName objectName = SimpleMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 132 server.registerMBean(support, objectName); 133 SimpleMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, SimpleMXBeanSupportMXBean.class); 134 assertEquals("olleH", proxy.echoReverse("Hello")); 135 } 136 137 public void testGetCompositeAttributes() throws Exception 138 { 139 MBeanServer server = createMBeanServer(); 140 SimpleObject composite = new SimpleObject(); 141 CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite); 142 ObjectName objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 143 server.registerMBean(support, objectName); 144 CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class); 145 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 146 Object [] values = { "Simple", composite }; 147 checkAttributes(CompositeMXBeanSupportMXBean.class, proxy, support, CompositeInterface.KEYS, CompositeInterface.TYPES, values, info); 148 } 149 150 public void testSetCompositeAttributes() throws Exception 151 { 152 MBeanServer server = createMBeanServer(); 153 SimpleObject composite = new SimpleObject(); 154 CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite); 155 ObjectName objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 156 server.registerMBean(support, objectName); 157 CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class); 158 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 159 SimpleObject changed = new SimpleObject(); 160 changed.setBigDecimal(SimpleInterface.bigDecimalChangedValue); 161 changed.setBigInteger(SimpleInterface.bigIntegerChangedValue); 162 changed.setBoolean(SimpleInterface.booleanChangedValue); 163 changed.setByte(SimpleInterface.byteChangedValue); 164 changed.setCharacter(SimpleInterface.characterChangedValue); 165 changed.setDate(SimpleInterface.dateChangedValue); 166 changed.setDouble(SimpleInterface.doubleChangedValue); 167 changed.setFloat(SimpleInterface.floatChangedValue); 168 changed.setInteger(SimpleInterface.integerChangedValue); 169 changed.setLong(SimpleInterface.longChangedValue); 170 changed.setObjectName(SimpleInterface.objectNameChangedValue); 171 changed.setPrimitiveBoolean(SimpleInterface.primitiveBooleanChangedValue); 172 changed.setPrimitiveByte(SimpleInterface.primitiveByteChangedValue); 173 changed.setPrimitiveChar(SimpleInterface.primitiveCharChangedValue); 174 changed.setPrimitiveDouble(SimpleInterface.primitiveDoubleChangedValue); 175 changed.setPrimitiveFloat(SimpleInterface.primitiveFloatChangedValue); 176 changed.setPrimitiveInt(SimpleInterface.primitiveIntChangedValue); 177 changed.setPrimitiveLong(SimpleInterface.primitiveLongChangedValue); 178 changed.setPrimitiveShort(SimpleInterface.primitiveShortChangedValue); 179 changed.setShort(SimpleInterface.shortChangedValue); 180 changed.setString(SimpleInterface.stringChangedValue); 181 proxy.setSimple("Changed"); 182 proxy.setComposite(changed); 183 Object [] values = new Object [] { "Changed", changed }; 184 checkAttributes(CompositeMXBeanSupportMXBean.class, proxy, support, CompositeInterface.KEYS, CompositeInterface.TYPES, values, info); 185 } 186 187 public void testCompositeInvoke() throws Exception 188 { 189 MBeanServer server = createMBeanServer(); 190 SimpleObject composite = new SimpleObject(); 191 CompositeMXBeanSupport support = new CompositeMXBeanSupport("Simple", composite); 192 ObjectName objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 193 server.registerMBean(support, objectName); 194 CompositeMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CompositeMXBeanSupportMXBean.class); 195 composite = new SimpleObject(); 196 composite.setString("hello"); 197 SimpleInterface result = proxy.echoReverse(composite); 198 assertEquals("olleh", result.getString()); 199 } 200 201 public void testGetCollectionAttributes() throws Exception 202 { 203 MBeanServer server = createMBeanServer(); 204 String [] array = { "array" }; 205 Collection <String > collection = new ArrayList <String >(); 206 collection.add("collection"); 207 Set <String > set = new HashSet <String >(); 208 set.add("set"); 209 List <String > list = new ArrayList <String >(); 210 list.add("list"); 211 Map <String , Integer > map = new HashMap <String , Integer >(); 212 map.put("map", 1); 213 TestEnum enumeration = TestEnum.FIRST; 214 CollectionsMXBeanSupport support = new CollectionsMXBeanSupport(array, collection, set, list, map, enumeration); 215 ObjectName objectName = CollectionsMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 216 server.registerMBean(support, objectName); 217 CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class); 218 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 219 Object [] values = { array, collection, set, list, map, enumeration }; 220 checkAttributes(CollectionsMXBeanSupportMXBean.class, proxy, support, CollectionsInterface.KEYS, CollectionsInterface.TYPES, values, info); 221 } 222 223 public void testSetCollectionAttributes() throws Exception 224 { 225 MBeanServer server = createMBeanServer(); 226 String [] array = { "array" }; 227 Collection <String > collection = new ArrayList <String >(); 228 collection.add("collection"); 229 Set <String > set = new LinkedHashSet <String >(); 230 set.add("set"); 231 List <String > list = new ArrayList <String >(); 232 list.add("list"); 233 Map <String , Integer > map = new LinkedHashMap <String , Integer >(); 234 map.put("map", 1); 235 TestEnum enumeration = TestEnum.FIRST; 236 CollectionsMXBeanSupport support = new CollectionsMXBeanSupport(array, collection, set, list, map, enumeration); 237 ObjectName objectName = CollectionsMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 238 server.registerMBean(support, objectName); 239 CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class); 240 OpenMBeanInfo info = (OpenMBeanInfo ) server.getMBeanInfo(objectName); 241 242 String [] changedArray = { "arrayChanged" }; 243 Collection <String > changedCollection = new ArrayList <String >(); 244 changedCollection.add("collectionChanged"); 245 Set <String > changedSet = new LinkedHashSet <String >(); 246 changedSet.add("setChanged"); 247 List <String > changedList = new ArrayList <String >(); 248 changedList.add("listChanged"); 249 Map <String , Integer > changedMap = new LinkedHashMap <String , Integer >(); 250 changedMap.put("mapChanged", 2); 251 TestEnum changedEnumeration = TestEnum.SECOND; 252 253 proxy.setArray(changedArray); 254 proxy.setCollection(changedCollection); 255 proxy.setEnum(changedEnumeration); 256 proxy.setList(changedList); 257 proxy.setMap(changedMap); 258 proxy.setSet(changedSet); 259 Object [] changedValues = { changedArray, changedCollection, changedSet, changedList, changedMap, changedEnumeration }; 260 checkAttributes(CollectionsMXBeanSupportMXBean.class, proxy, support, CollectionsInterface.KEYS, CollectionsInterface.TYPES, changedValues, info); 261 } 262 263 public void testCollectionInvoke() throws Exception 264 { 265 MBeanServer server = createMBeanServer(); 266 CollectionsMXBeanSupport support = new CollectionsMXBeanSupport(); 267 ObjectName objectName = CompositeMXBeanSupportMXBean.REGISTERED_OBJECT_NAME; 268 server.registerMBean(support, objectName); 269 CollectionsMXBeanSupportMXBean proxy = MXBeanFactory.makeProxy(server, objectName, CollectionsMXBeanSupportMXBean.class); 270 List <String > list = new ArrayList <String >(); 271 list.add("one"); 272 list.add("two"); 273 list.add("three"); 274 List <String > expected = new ArrayList <String >(); 275 expected.add("three"); 276 expected.add("two"); 277 expected.add("one"); 278 List <String > result = proxy.echoReverse(list); 279 assertEquals(expected, result); 280 } 281 282 protected void checkAttributes(Class intf, Object proxy, Object mxbean, String [] keys, OpenType [] types, Object [] values, OpenMBeanInfo info) throws Exception 283 { 284 MBeanAttributeInfo [] attributes = info.getAttributes(); 285 286 assertEquals(keys.length, values.length); 287 assertEquals(keys.length, types.length); 288 assertEquals(keys.length, attributes.length); 289 290 Map <String , OpenMBeanAttributeInfo > mapping = new HashMap <String , OpenMBeanAttributeInfo >(attributes.length); 291 for (int i = 0; i < attributes.length; ++i) 292 { 293 OpenMBeanAttributeInfo attribute = (OpenMBeanAttributeInfo ) attributes[i]; 294 String name = attribute.getName(); 295 mapping.put(name, attribute); 296 } 297 298 for (int i = 0; i < keys.length; ++i) 299 { 300 String name = getUpperName(keys[i]); 301 OpenMBeanAttributeInfo attribute = mapping.get(name); 302 assertNotNull("Could not find key " + name + " in " + mapping.keySet(), attribute); 303 Method method = MXBeanUtils.getCompositeDataMethod(intf, keys[i], types[i] == SimpleType.BOOLEAN); 304 Object value = method.invoke(proxy, null); 305 checkValueEquals(values[i], value); 306 value = method.invoke(mxbean, null); 307 checkValueEquals(values[i], value); 308 assertEquals(types[i], attribute.getOpenType()); 309 } 310 } 311 } 312 | Popular Tags |