1 22 package org.jboss.test.jbossmx.compliance.modelmbean; 23 24 import javax.management.Attribute ; 25 import javax.management.Descriptor ; 26 import javax.management.MBeanOperationInfo ; 27 import javax.management.MBeanServer ; 28 import javax.management.MBeanServerFactory ; 29 import javax.management.ObjectName ; 30 import javax.management.modelmbean.DescriptorSupport ; 31 import javax.management.modelmbean.ModelMBean ; 32 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 33 import javax.management.modelmbean.ModelMBeanInfo ; 34 import javax.management.modelmbean.ModelMBeanInfoSupport ; 35 import javax.management.modelmbean.ModelMBeanOperationInfo ; 36 import javax.management.modelmbean.RequiredModelMBean ; 37 38 import org.jboss.test.jbossmx.compliance.TestCase; 39 import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource; 40 import org.jboss.test.jbossmx.compliance.modelmbean.support.Resource2; 41 42 47 public class ModelMBeanTestCase extends TestCase 48 { 49 public ModelMBeanTestCase(String s) 50 { 51 super(s); 52 } 53 54 public void testRequiredModelMBeanConstructors() 55 { 56 try 57 { 58 new RequiredModelMBean (); 59 } 60 catch (Throwable t) 61 { 62 log.debug("failed", t); 63 fail("Creating Required ModelMBean instance with default constructor failed: " + t.toString()); 64 } 65 } 66 67 public void testRMMSetManagedResource() 68 { 69 try 70 { 71 ModelMBean modelmbean = new RequiredModelMBean (); 72 Resource resource = new Resource(); 73 modelmbean.setManagedResource(resource, "ObjectReference"); 74 } 75 catch (Throwable t) 76 { 77 log.debug("failed", t); 78 fail("Setting resource object with 'ObjectReference' type failed: " + t.toString()); 79 } 80 } 81 82 public void testRMMSetModelMBeanInfo() 83 { 84 try 85 { 86 ModelMBean modelmbean = new RequiredModelMBean (); 87 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 88 } 89 catch (Throwable t) 90 { 91 log.debug("failed", t); 92 fail("Unable to set model mbean info for resource object: " + t.toString()); 93 } 94 95 } 96 97 public void testRMMInvocation() 98 { 99 try 100 { 101 MBeanServer server = MBeanServerFactory.createMBeanServer(); 102 103 Resource resource = new Resource(); 104 ModelMBean modelmbean = new RequiredModelMBean (); 105 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 106 modelmbean.setManagedResource(resource, "ObjectReference"); 107 108 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 109 server.registerMBean(modelmbean, name); 110 111 assertTrue(((Boolean )server.invoke(name, "isActive", new Object [] {}, new String [] {})).booleanValue()); 112 } 113 catch (Throwable t) 114 { 115 log.debug("failed", t); 116 fail("RMMInvocation: " + t.toString()); 117 } 118 } 119 120 128 public void testRMMResourceImplementsMBean() 129 { 130 try 131 { 132 MBeanServer server = MBeanServerFactory.createMBeanServer(); 133 134 Resource2 resource = new Resource2(); 135 ModelMBean modelmbean = new RequiredModelMBean (); 136 modelmbean.setModelMBeanInfo(getModelMBeanInfo2()); 137 modelmbean.setManagedResource(resource, "ObjectReference"); 138 139 ObjectName name = new ObjectName ("rmm:resourceImplementsMBean=true"); 140 server.registerMBean(modelmbean, name); 141 142 server.setAttribute(name, new Attribute ("pojoAttribute", new Integer (111))); 143 144 assertEquals((Integer )server.getAttribute(name, "pojoAttribute"), new Integer (111)); 145 assertTrue(((Boolean )server.invoke(name, "pojoOperation", new Object [] {}, new String [] {})).booleanValue()); 146 } 147 catch (Throwable t) 148 { 149 log.debug("failed", t); 150 fail("testRMMResourceImplementsMBean: " + t.toString()); 151 } 152 } 153 154 private ModelMBeanInfo getModelMBeanInfo() 155 { 156 final boolean READABLE = true; 157 final boolean WRITABLE = true; 158 final boolean BOOLEAN = true; 159 160 Descriptor descr1 = new DescriptorSupport (); 162 descr1.setField("name", "Room"); 163 descr1.setField("descriptorType", "attribute"); 164 descr1.setField("displayName", "Room Number"); 165 descr1.setField("default", "D325"); 166 167 ModelMBeanAttributeInfo roomNameInfo = 168 new ModelMBeanAttributeInfo ( 169 "Room", String .class.getName(), "Room name or number.", READABLE, WRITABLE, !BOOLEAN, descr1 ); 175 176 177 Descriptor descr2 = new DescriptorSupport (); 179 descr2.setField("name", "Active"); 180 descr2.setField("descriptorType", "attribute"); 181 descr2.setField("getMethod", "isActive"); 182 descr2.setField("currencyTimeLimit", "10"); 183 184 ModelMBeanAttributeInfo activeInfo = 185 new ModelMBeanAttributeInfo ( 186 "Active", 187 boolean.class.getName(), 188 "Printer state.", 189 READABLE, !WRITABLE, !BOOLEAN, 190 descr2 191 ); 192 193 Descriptor descr3 = new DescriptorSupport (); 195 descr3.setField("name", "isActive"); 196 descr3.setField("descriptorType", "operation"); 197 descr3.setField("role", "getter"); 198 199 ModelMBeanOperationInfo isActiveInfo = 200 new ModelMBeanOperationInfo ( 201 "isActive", "Checks if the printer is currently active.", 203 null, boolean.class.getName(), MBeanOperationInfo.INFO, descr3 ); 208 209 Descriptor descr4 = new DescriptorSupport (); 211 descr4.setField("name", RequiredModelMBean .class.getName()); 212 descr4.setField("descriptorType", "MBean"); 213 214 ModelMBeanInfo info = new ModelMBeanInfoSupport ( 216 RequiredModelMBean .class.getName(), "Printer", new ModelMBeanAttributeInfo [] { roomNameInfo, 220 activeInfo 221 }, 222 null, new ModelMBeanOperationInfo [] { isActiveInfo 225 }, 226 null, descr4 ); 229 230 return info; 231 } 232 233 private ModelMBeanInfo getModelMBeanInfo2() 234 { 235 final boolean READABLE = true; 236 final boolean WRITABLE = true; 237 final boolean BOOLEAN = true; 238 239 Descriptor descr2 = new DescriptorSupport (); 241 descr2.setField("name", "pojoAttribute"); 242 descr2.setField("descriptorType", "attribute"); 243 descr2.setField("getMethod", "getpojoAttribute"); 244 descr2.setField("setMethod", "setpojoAttribute"); 245 246 ModelMBeanAttributeInfo pojoAttributeInfo = 247 new ModelMBeanAttributeInfo ( 248 "pojoAttribute", 249 int.class.getName(), 250 "A simple integer attribute.", 251 READABLE, WRITABLE, !BOOLEAN, 252 descr2 253 ); 254 255 Descriptor descr3 = new DescriptorSupport (); 257 descr3.setField("name", "pojoOperation"); 258 descr3.setField("descriptorType", "operation"); 259 260 ModelMBeanOperationInfo pojoOperationInfo = 261 new ModelMBeanOperationInfo ( 262 "pojoOperation", "A simple operation.", 264 null, boolean.class.getName(), MBeanOperationInfo.ACTION, descr3 ); 269 270 Descriptor descr4 = new DescriptorSupport (); 272 descr4.setField("name", RequiredModelMBean .class.getName()); 273 descr4.setField("descriptorType", "MBean"); 274 275 ModelMBeanInfo info = new ModelMBeanInfoSupport ( 277 RequiredModelMBean .class.getName(), "POJO", new ModelMBeanAttributeInfo [] { pojoAttributeInfo 281 }, 282 null, new ModelMBeanOperationInfo [] { pojoOperationInfo 285 }, 286 null, descr4 ); 289 290 return info; 291 } 292 } 293 | Popular Tags |