1 22 package org.jboss.test.jmx.compliance.modelmbean; 23 24 import javax.management.Descriptor ; 25 import javax.management.MBeanOperationInfo ; 26 import javax.management.MBeanServer ; 27 import javax.management.MBeanServerFactory ; 28 import javax.management.ObjectName ; 29 import javax.management.MBeanInfo ; 30 import javax.management.NotificationListener ; 31 import javax.management.Notification ; 32 import javax.management.modelmbean.DescriptorSupport ; 33 import javax.management.modelmbean.ModelMBean ; 34 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 35 import javax.management.modelmbean.ModelMBeanInfo ; 36 import javax.management.modelmbean.ModelMBeanInfoSupport ; 37 import javax.management.modelmbean.ModelMBeanOperationInfo ; 38 import javax.management.modelmbean.RequiredModelMBean ; 39 40 import org.jboss.test.jmx.compliance.modelmbean.support.Resource; 41 42 import junit.framework.TestCase; 43 44 public class ModelMBeanTEST extends TestCase 45 { 46 static class RMMListener implements NotificationListener 47 { 48 public void handleNotification(Notification msg, Object handback) 49 { 50 System.out.println("handleNotification, msg="+msg); 51 } 52 } 53 54 public ModelMBeanTEST(String s) 55 { 56 super(s); 57 } 58 59 public void testRequiredModelMBeanConstructors() 60 { 61 try 62 { 63 new RequiredModelMBean (); 64 } 65 catch (Throwable t) 66 { 67 t.printStackTrace(); 68 fail("Creating Required ModelMBean instance with default constructor failed: " + t.toString()); 69 } 70 } 71 72 public void testRMMSetManagedResource() 73 { 74 try 75 { 76 ModelMBean modelmbean = new RequiredModelMBean (); 77 Resource resource = new Resource(); 78 modelmbean.setManagedResource(resource, "ObjectReference"); 79 } 80 catch (Throwable t) 81 { 82 t.printStackTrace(); 83 fail("Setting resource object with 'ObjectReference' type failed: " + t.toString()); 84 } 85 } 86 87 public void testRMMSetManagedResourceAfterRegister() 88 throws Exception 89 { 90 MBeanServer server = MBeanServerFactory.createMBeanServer(); 91 92 Resource resource = new Resource(); 93 ModelMBean modelmbean = new RequiredModelMBean (); 94 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 95 96 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 97 server.registerMBean(modelmbean, name); 98 99 modelmbean.setManagedResource(resource, "ObjectReference"); 100 101 MBeanInfo info = server.getMBeanInfo(name); 102 assertTrue("MBeanInfo != null", info != null); 103 104 Object [] args = {}; 105 String [] sig = {}; 106 Boolean flag = (Boolean ) server.invoke(name, "isActive", args, sig); 107 assertTrue("isActive", flag.booleanValue()); 108 } 109 110 public void testRMMSetManagedResourceAfterRegisterViaServer() 111 throws Exception 112 { 113 MBeanServer server = MBeanServerFactory.createMBeanServer(); 114 115 Resource resource = new Resource(); 116 ModelMBean modelmbean = new RequiredModelMBean (); 117 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 118 119 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 120 server.registerMBean(modelmbean, name); 121 122 Object [] args = {resource, "ObjectReference"}; 123 String [] sig = {"java.lang.Object", "java.lang.String"}; 124 server.invoke(name, "setManagedResource", args, sig); 125 126 MBeanInfo info = server.getMBeanInfo(name); 127 assertTrue("MBeanInfo != null", info != null); 128 129 args = new Object []{}; 130 sig = new String []{}; 131 Boolean flag = (Boolean ) server.invoke(name, "isActive", args, sig); 132 assertTrue("isActive", flag.booleanValue()); 133 } 134 135 public void testCreatedRMMSetManagedResourceAfterRegisterViaServer() 136 throws Exception 137 { 138 MBeanServer server = MBeanServerFactory.createMBeanServer(); 139 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 140 ModelMBeanInfo info = getModelMBeanInfo(); 141 Object [] ctorArgs = { info }; 142 String [] ctorSig = { "javax.management.modelmbean.ModelMBeanInfo" }; 143 server.createMBean("javax.management.modelmbean.RequiredModelMBean", 144 name, ctorArgs, ctorSig); 145 146 Resource resource = new Resource(); 147 148 Object [] args = {resource, "ObjectReference"}; 149 String [] sig = {"java.lang.Object", "java.lang.String"}; 150 server.invoke(name, "setManagedResource", args, sig); 151 152 info = (ModelMBeanInfo ) server.getMBeanInfo(name); 153 assertTrue("MBeanInfo != null", info != null); 154 155 args = new Object []{}; 156 sig = new String []{}; 157 Boolean flag = (Boolean ) server.invoke(name, "isActive", args, sig); 158 assertTrue("isActive", flag.booleanValue()); 159 } 160 161 public void testCreatedRMMSetManagedResourceAfterRegisterViaServerListener() 162 throws Exception 163 { 164 MBeanServer server = MBeanServerFactory.createMBeanServer(); 165 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 166 ModelMBeanInfo info = getModelMBeanInfo(); 167 Object [] ctorArgs = { info }; 168 String [] ctorSig = { "javax.management.modelmbean.ModelMBeanInfo" }; 169 server.createMBean("javax.management.modelmbean.RequiredModelMBean", 170 name, ctorArgs, ctorSig); 171 172 Resource resource = new Resource(); 173 174 Object [] args = {resource, "objectReference"}; 175 String [] sig = {"java.lang.Object", "java.lang.String"}; 176 server.invoke(name, "setManagedResource", args, sig); 177 178 RMMListener listener = new RMMListener(); 179 server.addNotificationListener(name, listener, null, null); 180 } 181 182 public void testRMMSetModelMBeanInfo() 183 throws Exception 184 { 185 ModelMBean modelmbean = new RequiredModelMBean (); 186 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 187 } 188 189 public void testRMMInvocation() 190 throws Exception 191 { 192 MBeanServer server = MBeanServerFactory.createMBeanServer(); 193 194 Resource resource = new Resource(); 195 ModelMBean modelmbean = new RequiredModelMBean (); 196 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 197 modelmbean.setManagedResource(resource, "ObjectReference"); 198 199 ObjectName name = new ObjectName ("rmm:invocationTest=true"); 200 server.registerMBean(modelmbean, name); 201 202 MBeanInfo info = server.getMBeanInfo(name); 203 assertTrue("MBeanInfo != null", info != null); 204 205 assertTrue(((Boolean )server.invoke(name, "isActive", new Object [] {}, new String [] {})).booleanValue()); 206 } 207 208 public void testRMMDefault() 209 throws Exception 210 { 211 MBeanServer server = MBeanServerFactory.newMBeanServer(); 212 213 Resource resource = new Resource(); 214 ModelMBean modelmbean = new RequiredModelMBean (); 215 modelmbean.setManagedResource(resource, "ObjectReference"); 216 modelmbean.setModelMBeanInfo(getModelMBeanInfo()); 217 218 ObjectName name = new ObjectName ("rmm:test=default"); 219 server.registerMBean(modelmbean, name); 220 221 assertTrue("Should be the default room", server.getAttribute(name, "Room").equals("D325")); 222 } 223 224 public ModelMBeanInfo getModelMBeanInfo() 225 { 226 final boolean READABLE = true; 227 final boolean WRITABLE = true; 228 final boolean BOOLEAN = true; 229 230 Descriptor descr1 = new DescriptorSupport (); 232 descr1.setField("name", "Room"); 233 descr1.setField("descriptorType", "attribute"); 234 descr1.setField("displayName", "Room Number"); 235 descr1.setField("default", "D325"); 236 237 ModelMBeanAttributeInfo roomNameInfo = 238 new ModelMBeanAttributeInfo ( 239 "Room", String .class.getName(), "Room name or number.", READABLE, WRITABLE, !BOOLEAN, descr1 ); 245 246 Descriptor descr2 = new DescriptorSupport (); 248 descr2.setField("name", "Active"); 249 descr2.setField("descriptorType", "attribute"); 250 descr2.setField("getMethod", "isActive"); 251 descr2.setField("currencyTimeLimit", "10"); 252 253 ModelMBeanAttributeInfo activeInfo = 254 new ModelMBeanAttributeInfo ( 255 "Active", 256 boolean.class.getName(), 257 "Printer state.", 258 READABLE, !WRITABLE, !BOOLEAN, 259 descr2 260 ); 261 262 Descriptor descr3 = new DescriptorSupport (); 264 descr3.setField("name", "isActive"); 265 descr3.setField("descriptorType", "operation"); 266 descr3.setField("role", "getter"); 267 268 ModelMBeanOperationInfo isActiveInfo = 269 new ModelMBeanOperationInfo ( 270 "isActive", "Checks if the printer is currently active.", 272 null, boolean.class.getName(), MBeanOperationInfo.INFO, descr3 ); 277 278 Descriptor descr4 = new DescriptorSupport (); 280 descr4.setField("name", RequiredModelMBean .class.getName()); 281 descr4.setField("descriptorType", "MBean"); 282 283 ModelMBeanInfo info = new ModelMBeanInfoSupport ( 285 RequiredModelMBean .class.getName(), "Printer", new ModelMBeanAttributeInfo [] { roomNameInfo, 289 activeInfo 290 }, 291 null, new ModelMBeanOperationInfo [] { isActiveInfo 294 }, 295 null, descr4 ); 298 299 return info; 300 } 301 302 } 303 | Popular Tags |