1 16 17 18 package org.apache.commons.modeler; 19 20 21 import java.io.FileInputStream ; 22 import java.util.List ; 23 24 import javax.management.Descriptor ; 25 import javax.management.MBeanConstructorInfo ; 26 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 27 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 28 import javax.management.modelmbean.ModelMBeanInfo ; 29 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 30 import javax.management.modelmbean.ModelMBeanOperationInfo ; 31 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 37 43 44 public class RegistryTestCase extends TestCase { 45 46 47 49 50 53 protected Registry registry = null; 54 55 56 58 59 64 public RegistryTestCase(String name) { 65 66 super(name); 67 68 } 69 70 71 73 74 77 public void setUp() throws Exception { 78 79 registry = Registry.getRegistry(); 80 String names[] = registry.findManagedBeans(); 81 if (names.length == 0) { 82 FileInputStream stream = new FileInputStream 83 ("src/test/org/apache/commons/modeler/mbeans-descriptors.xml"); 84 Registry.loadRegistry(stream); 85 stream.close(); 86 } 87 88 } 89 90 91 94 public static Test suite() { 95 96 return (new TestSuite(RegistryTestCase.class)); 97 98 } 99 100 101 104 public void tearDown() { 105 106 registry = null; 107 108 } 109 110 111 113 114 117 public void testModelMBeanAttributeInfo() throws Exception { 118 119 ManagedBean http = registry.findManagedBean("HttpConnector"); 121 assertNotNull("Found HttpConnector managed bean"); 122 123 ModelMBeanInfo info = http.createMBeanInfo(); 125 assertNotNull("Found HttpConnector ModelMBeanInfo", info); 126 127 ModelMBeanAttributeInfo mmainfo = info.getAttribute("acceptCount"); 129 assertNotNull("Found HttpConnector acceptCount info", mmainfo); 130 131 Descriptor desc = mmainfo.getDescriptor(); 133 assertNotNull("Found HttpConnector acceptCount descriptor", desc); 134 135 checkDescriptor(desc, "field1", "HttpConnector.acceptCount/field1"); 137 checkDescriptor(desc, "field2", "HttpConnector.acceptCount/field2"); 138 139 } 140 141 142 145 public void testModelMBeanConstructorInfo() throws Exception { 146 147 ManagedBean http = registry.findManagedBean("HttpConnector"); 149 assertNotNull("Found HttpConnector managed bean"); 150 151 ModelMBeanInfo info = http.createMBeanInfo(); 153 assertNotNull("Found HttpConnector ModelMBeanInfo", info); 154 155 MBeanConstructorInfo mcinfo[] = info.getConstructors(); 157 assertNotNull("Found HttpConnector MBeanConstructorInfo array", mcinfo); 158 assertEquals("Found HttpConnector MBeanConstructorInfo entry", 159 1, mcinfo.length); 160 161 ModelMBeanConstructorInfo mmcinfo = 163 (ModelMBeanConstructorInfo ) mcinfo[0]; 164 165 Descriptor desc = mmcinfo.getDescriptor(); 167 assertNotNull("Found HttpConnector constructor descriptor", desc); 168 169 checkDescriptor(desc, "role", "constructor"); 171 checkDescriptor(desc, "field1", "HttpConnector.constructor/field1"); 172 checkDescriptor(desc, "field2", "HttpConnector.constructor/field2"); 173 174 } 175 176 177 180 public void testDescriptorEntries() { 181 182 ManagedBean http = registry.findManagedBean("HttpConnector"); 184 assertNotNull("Found HttpConnector managed bean"); 185 186 List beanFields = http.getFields(); 188 assertNotNull("Found HttpConnector fields"); 189 checkField(beanFields, "field1", "HttpConnector/field1"); 190 checkField(beanFields, "field2", "HttpConnector/field2"); 191 192 AttributeInfo attrs[] = http.getAttributes(); 194 AttributeInfo attr = null; 195 for (int i = 0; i < attrs.length; i++) { 196 if ("acceptCount".equals(attrs[i].getName())) { 197 attr = attrs[i]; 198 break; 199 } 200 } 201 assertNotNull("Found attribute"); 202 203 List attrFields = attr.getFields(); 205 assertNotNull("Found attribute fields"); 206 checkField(attrFields, "field1", "HttpConnector.acceptCount/field1"); 207 checkField(attrFields, "field2", "HttpConnector.acceptCount/field2"); 208 209 ConstructorInfo constrs[] = http.getConstructors(); 211 ConstructorInfo constr = null; 212 for (int i = 0; i < constrs.length; i++) { 213 if ("HttpConnector".equals(constrs[i].getName())) { 214 constr = constrs[i]; 215 break; 216 } 217 } 218 assertNotNull("Found constructor"); 219 220 List constrFields = constr.getFields(); 222 assertNotNull("Found constructor fields"); 223 checkField(constrFields, "field1", "HttpConnector.constructor/field1"); 224 checkField(constrFields, "field2", "HttpConnector.constructor/field2"); 225 226 NotificationInfo notifs[] = http.getNotifications(); 228 NotificationInfo notif = null; 229 for (int i = 0; i < notifs.length; i++) { 230 if ("Problem".equals(notifs[i].getName())) { 231 notif = notifs[i]; 232 break; 233 } 234 } 235 assertNotNull("Found notification"); 236 237 List notifFields = notif.getFields(); 239 assertNotNull("Found notification fields"); 240 checkField(notifFields, "field1", "HttpConnector.problem/field1"); 241 checkField(notifFields, "field2", "HttpConnector.problem/field2"); 242 243 OperationInfo opers[] = http.getOperations(); 245 OperationInfo oper = null; 246 for (int i = 0; i < opers.length; i++) { 247 if ("initialize".equals(opers[i].getName())) { 248 oper = opers[i]; 249 break; 250 } 251 } 252 assertNotNull("Found operation"); 253 254 List operFields = oper.getFields(); 256 assertNotNull("Found operation fields"); 257 checkField(operFields, "field1", "HttpConnector.initialize/field1"); 258 checkField(operFields, "field2", "HttpConnector.initialize/field2"); 259 260 } 261 262 263 266 public void testModelMBeanInfo() throws Exception { 267 268 ManagedBean http = registry.findManagedBean("HttpConnector"); 270 assertNotNull("Found HttpConnector managed bean"); 271 272 ModelMBeanInfo info = http.createMBeanInfo(); 274 assertNotNull("Found HttpConnector ModelMBeanInfo", info); 275 276 assertEquals("Correct className", 278 "org.apache.catalina.mbeans.HttpConnectorModelMBean", 279 info.getClassName()); 280 assertEquals("Correct description", 281 "HTTP/1.1 Connector for Tomcat Standalone", 282 info.getDescription()); 283 284 Descriptor desc = info.getMBeanDescriptor(); 286 assertNotNull("Found HttpConnector MBeanDescriptor", desc); 287 288 checkDescriptor(desc, "field1", "HttpConnector/field1"); 290 checkDescriptor(desc, "field2", "HttpConnector/field2"); 291 292 } 293 294 295 298 public void testModelMBeanNotificationInfo() throws Exception { 299 300 ManagedBean http = registry.findManagedBean("HttpConnector"); 302 assertNotNull("Found HttpConnector managed bean"); 303 304 ModelMBeanInfo info = http.createMBeanInfo(); 306 assertNotNull("Found HttpConnector ModelMBeanInfo", info); 307 308 ModelMBeanNotificationInfo mmninfo = info.getNotification("Problem"); 310 assertNotNull("Found HttpConnector problem info", mmninfo); 311 312 Descriptor desc = mmninfo.getDescriptor(); 314 assertNotNull("Found HttpConnector problem descriptor", desc); 315 316 checkDescriptor(desc, "field1", "HttpConnector.problem/field1"); 318 checkDescriptor(desc, "field2", "HttpConnector.problem/field2"); 319 320 } 321 322 323 326 public void testModelMBeanOperationInfo() throws Exception { 327 328 ManagedBean http = registry.findManagedBean("HttpConnector"); 330 assertNotNull("Found HttpConnector managed bean"); 331 332 ModelMBeanInfo info = http.createMBeanInfo(); 334 assertNotNull("Found HttpConnector ModelMBeanInfo", info); 335 336 ModelMBeanOperationInfo mmoinfo = info.getOperation("initialize"); 338 assertNotNull("Found HttpConnector initialize info", mmoinfo); 339 340 Descriptor desc = mmoinfo.getDescriptor(); 342 assertNotNull("Found HttpConnector initialize descriptor", desc); 343 344 checkDescriptor(desc, "field1", "HttpConnector.initialize/field1"); 346 checkDescriptor(desc, "field2", "HttpConnector.initialize/field2"); 347 348 } 349 350 351 354 public void testRegistryCreation() { 355 356 String names[] = null; 357 358 System.out.println("Registered managed beans:"); 359 names = registry.findManagedBeans(); 360 for (int i = 0; i < names.length; i++) 361 System.out.println(" " + names[i]); 362 System.out.println("-------------------------"); 363 364 System.out.println("Registered managed beans for Containers:"); 365 names = registry.findManagedBeans("org.apache.catalina.Container"); 366 for (int i = 0; i < names.length; i++) 367 System.out.println(" " + names[i]); 368 System.out.println("-------------------------"); 369 370 } 371 372 373 375 376 protected void checkDescriptor(Descriptor desc, String name, 378 Object value) { 379 380 String names[] = desc.getFieldNames(); 381 boolean found = false; 382 for (int i = 0; i < names.length; i++) { 383 if (name.equals(names[i])) { 384 found = true; 385 break; 386 } 387 } 388 assertTrue("Found name " + name, found); 389 assertEquals("Correct name " + name + " value", 390 value, 391 desc.getFieldValue(name)); 392 393 } 394 395 396 protected void checkField(List fields, String name, Object value) { 398 399 int n = fields.size(); 400 for (int i = 0; i < n; i++) { 401 FieldInfo field = (FieldInfo) fields.get(i); 402 if (name.equals(field.getName())) { 403 assertEquals("name=" + name + " value", 404 value, 405 field.getValue()); 406 return; 407 } 408 } 409 fail("Cannot find field name=" + name + " and value=" + value); 410 411 } 412 413 414 } 415 | Popular Tags |