1 4 package test.mx4j.server; 5 6 import java.util.List ; 7 import java.util.ArrayList ; 8 import java.util.Map ; 9 import java.util.Iterator ; 10 11 import javax.management.NotificationListener ; 12 import javax.management.Notification ; 13 import javax.management.ObjectName ; 14 import javax.management.MBeanInfo ; 15 import javax.management.ObjectInstance ; 16 17 import test.MX4JTestCase; 18 import mx4j.server.interceptor.NotificationListenerMBeanServerInterceptor; 19 import mx4j.server.interceptor.MBeanServerInterceptor; 20 import mx4j.server.MBeanMetaData; 21 import mx4j.server.MBeanInvoker; 22 23 26 public class NotificationListenerMBeanServerInterceptorTest extends MX4JTestCase 27 { 28 public NotificationListenerMBeanServerInterceptorTest(String name) 29 { 30 super(name); 31 } 32 33 36 public void testAddRemove1() throws Exception 37 { 38 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 39 interceptor.setEnabled(true); 40 List chain = new ArrayList (); 41 chain.add(interceptor); 42 chain.add(new NoOpMBeanServerInterceptor()); 43 interceptor.setChain(chain); 44 45 ObjectName objectName = ObjectName.getInstance(":type=test"); 46 MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, objectName); 47 NotificationListener listener = new NotificationListener () 48 { 49 public void handleNotification(Notification notification, Object handback) 50 { 51 } 52 }; 53 54 interceptor.addNotificationListener(metadata, listener, null, null); 55 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 56 Object wrapper = ((Map.Entry )interceptor.getNotificationListenerWrappers().entrySet().iterator().next()).getValue(); 57 assertEquals(1, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 58 assertEquals(1, interceptor.getObjectNames().size()); 59 assertEquals(objectName, interceptor.getObjectNames().keySet().iterator().next()); 60 61 interceptor.removeNotificationListener(metadata, listener, null, null); 62 assertEquals(0, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 63 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 64 assertEquals(0, interceptor.getObjectNames().size()); 65 } 66 67 70 public void testAddRemove2() throws Exception 71 { 72 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 73 interceptor.setEnabled(true); 74 List chain = new ArrayList (); 75 chain.add(interceptor); 76 chain.add(new NoOpMBeanServerInterceptor()); 77 interceptor.setChain(chain); 78 79 ObjectName objectName = ObjectName.getInstance(":type=test"); 80 MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, objectName); 81 NotificationListener listener = new NotificationListener () 82 { 83 public void handleNotification(Notification notification, Object handback) 84 { 85 } 86 }; 87 88 interceptor.addNotificationListener(metadata, listener, null, null); 89 interceptor.addNotificationListener(metadata, listener, null, listener); 90 Object handback = new Object (); 91 interceptor.addNotificationListener(metadata, listener, null, handback); 92 93 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 94 Object wrapper = ((Map.Entry )interceptor.getNotificationListenerWrappers().entrySet().iterator().next()).getValue(); 95 assertEquals(3, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 96 assertEquals(1, interceptor.getObjectNames().size()); 97 assertEquals(objectName, interceptor.getObjectNames().keySet().iterator().next()); 98 99 interceptor.removeNotificationListener(metadata, listener, null, handback); 100 assertEquals(2, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 101 assertEquals(1, interceptor.getObjectNames().size()); 102 103 interceptor.removeNotificationListener(metadata, listener); 104 assertEquals(0, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 105 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 106 assertEquals(0, interceptor.getObjectNames().size()); 107 } 108 109 112 public void testAddRemove3() throws Exception 113 { 114 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 115 interceptor.setEnabled(true); 116 List chain = new ArrayList (); 117 chain.add(interceptor); 118 chain.add(new NoOpMBeanServerInterceptor()); 119 interceptor.setChain(chain); 120 121 MBeanMetaData metadata1 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 122 MBeanMetaData metadata2 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test2")); 123 NotificationListener listener = new NotificationListener () 124 { 125 public void handleNotification(Notification notification, Object handback) 126 { 127 } 128 }; 129 130 interceptor.addNotificationListener(metadata1, listener, null, null); 131 interceptor.addNotificationListener(metadata2, listener, null, null); 132 133 assertEquals(2, interceptor.getNotificationListenerWrappers().size()); 134 assertEquals(2, interceptor.getObjectNames().size()); 135 136 interceptor.removeNotificationListener(metadata1, listener, null, null); 137 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 138 assertEquals(1, interceptor.getObjectNames().size()); 139 140 interceptor.removeNotificationListener(metadata2, listener, null, null); 141 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 142 assertEquals(0, interceptor.getObjectNames().size()); 143 } 144 145 148 public void testAddRemove4() throws Exception 149 { 150 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 151 interceptor.setEnabled(true); 152 List chain = new ArrayList (); 153 chain.add(interceptor); 154 chain.add(new NoOpMBeanServerInterceptor()); 155 interceptor.setChain(chain); 156 157 MBeanMetaData metadata1 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 158 MBeanMetaData metadata2 = new NotificationListenerMBeanMetaData(interceptor, ObjectName.getInstance(":type=test2")); 159 NotificationListener listener = new NotificationListener () 160 { 161 public void handleNotification(Notification notification, Object handback) 162 { 163 } 164 }; 165 166 interceptor.addNotificationListener(metadata1, listener, null, null); 167 interceptor.addNotificationListener(metadata2, listener, null, null); 168 169 assertEquals(2, interceptor.getNotificationListenerWrappers().size()); 170 assertEquals(2, interceptor.getObjectNames().size()); 171 172 interceptor.removeNotificationListener(metadata1, listener, null, null); 173 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 174 assertEquals(1, interceptor.getObjectNames().size()); 175 176 interceptor.removeNotificationListener(metadata2, listener, null, null); 177 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 178 assertEquals(0, interceptor.getObjectNames().size()); 179 } 180 181 184 public void testAddRemove5() throws Exception 185 { 186 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 187 interceptor.setEnabled(true); 188 List chain = new ArrayList (); 189 chain.add(interceptor); 190 chain.add(new NoOpMBeanServerInterceptor()); 191 interceptor.setChain(chain); 192 193 ObjectName objectName = ObjectName.getInstance(":type=test1"); 194 MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, objectName); 195 NotificationListener listener1 = new NotificationListener () 196 { 197 public void handleNotification(Notification notification, Object handback) 198 { 199 } 200 }; 201 NotificationListener listener2 = new NotificationListener () 202 { 203 public void handleNotification(Notification notification, Object handback) 204 { 205 } 206 }; 207 208 interceptor.addNotificationListener(metadata, listener1, null, null); 209 interceptor.addNotificationListener(metadata, listener2, null, null); 210 211 assertEquals(2, interceptor.getNotificationListenerWrappers().size()); 212 for (Iterator iterator = interceptor.getNotificationListenerWrappers().values().iterator(); iterator.hasNext();) 213 { 214 Object wrapper = iterator.next(); 215 assertEquals(1, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 216 } 217 assertEquals(1, interceptor.getObjectNames().size()); 218 assertEquals(objectName, interceptor.getObjectNames().keySet().iterator().next()); 219 220 interceptor.removeNotificationListener(metadata, listener1); 221 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 222 assertEquals(1, interceptor.getObjectNames().size()); 223 224 interceptor.removeNotificationListener(metadata, listener2); 225 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 226 assertEquals(0, interceptor.getObjectNames().size()); 227 } 228 229 232 public void testAddRemove6() throws Exception 233 { 234 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 235 interceptor.setEnabled(true); 236 List chain = new ArrayList (); 237 chain.add(interceptor); 238 chain.add(new NoOpMBeanServerInterceptor()); 239 interceptor.setChain(chain); 240 241 MBeanMetaData metadata1 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 242 MBeanMetaData metadata2 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test2")); 243 NotificationListener listener1 = new NotificationListener () 244 { 245 public void handleNotification(Notification notification, Object handback) 246 { 247 } 248 }; 249 NotificationListener listener2 = new NotificationListener () 250 { 251 public void handleNotification(Notification notification, Object handback) 252 { 253 } 254 }; 255 256 interceptor.addNotificationListener(metadata1, listener1, null, null); 257 interceptor.addNotificationListener(metadata2, listener2, null, null); 258 259 assertEquals(2, interceptor.getNotificationListenerWrappers().size()); 260 for (Iterator iterator = interceptor.getNotificationListenerWrappers().values().iterator(); iterator.hasNext();) 261 { 262 Object wrapper = iterator.next(); 263 assertEquals(1, interceptor.getNotificationListenerWrapperReferenceCount(wrapper)); 264 } 265 assertEquals(2, interceptor.getObjectNames().size()); 266 267 interceptor.removeNotificationListener(metadata1, listener1); 268 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 269 assertEquals(1, interceptor.getObjectNames().size()); 270 271 interceptor.removeNotificationListener(metadata2, listener2); 272 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 273 assertEquals(0, interceptor.getObjectNames().size()); 274 } 275 276 279 public void testAddUnregister1() throws Exception 280 { 281 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 282 interceptor.setEnabled(true); 283 List chain = new ArrayList (); 284 chain.add(interceptor); 285 chain.add(new NoOpMBeanServerInterceptor()); 286 interceptor.setChain(chain); 287 288 MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 289 NotificationListener listener = new NotificationListener () 290 { 291 public void handleNotification(Notification notification, Object handback) 292 { 293 } 294 }; 295 296 interceptor.addNotificationListener(metadata, listener, null, null); 297 interceptor.registration(metadata, MBeanServerInterceptor.POST_DEREGISTER); 298 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 299 assertEquals(0, interceptor.getObjectNames().size()); 300 } 301 302 305 public void testAddUnregister2() throws Exception 306 { 307 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 308 interceptor.setEnabled(true); 309 List chain = new ArrayList (); 310 chain.add(interceptor); 311 chain.add(new NoOpMBeanServerInterceptor()); 312 interceptor.setChain(chain); 313 314 MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 315 NotificationListener listener = new NotificationListener () 316 { 317 public void handleNotification(Notification notification, Object handback) 318 { 319 } 320 }; 321 322 interceptor.addNotificationListener(metadata, listener, null, null); 323 interceptor.addNotificationListener(metadata, listener, null, listener); 324 interceptor.registration(metadata, MBeanServerInterceptor.POST_DEREGISTER); 325 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 326 assertEquals(0, interceptor.getObjectNames().size()); 327 } 328 329 332 public void testAddUnregister3() throws Exception 333 { 334 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 335 interceptor.setEnabled(true); 336 List chain = new ArrayList (); 337 chain.add(interceptor); 338 chain.add(new NoOpMBeanServerInterceptor()); 339 interceptor.setChain(chain); 340 341 MBeanMetaData metadata1 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 342 MBeanMetaData metadata2 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test2")); 343 NotificationListener listener = new NotificationListener () 344 { 345 public void handleNotification(Notification notification, Object handback) 346 { 347 } 348 }; 349 350 interceptor.addNotificationListener(metadata1, listener, null, null); 351 interceptor.addNotificationListener(metadata2, listener, null, null); 352 interceptor.registration(metadata1, MBeanServerInterceptor.POST_DEREGISTER); 353 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 354 assertEquals(1, interceptor.getObjectNames().size()); 355 356 interceptor.registration(metadata2, MBeanServerInterceptor.POST_DEREGISTER); 357 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 358 assertEquals(0, interceptor.getObjectNames().size()); 359 } 360 361 364 public void testAddUnregister4() throws Exception 365 { 366 NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor(); 367 interceptor.setEnabled(true); 368 List chain = new ArrayList (); 369 chain.add(interceptor); 370 chain.add(new NoOpMBeanServerInterceptor()); 371 interceptor.setChain(chain); 372 373 MBeanMetaData metadata1 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test1")); 374 MBeanMetaData metadata2 = new NotificationListenerMBeanMetaData(this, ObjectName.getInstance(":type=test2")); 375 NotificationListener listener1 = new NotificationListener () 376 { 377 public void handleNotification(Notification notification, Object handback) 378 { 379 } 380 }; 381 NotificationListener listener2 = new NotificationListener () 382 { 383 public void handleNotification(Notification notification, Object handback) 384 { 385 } 386 }; 387 388 interceptor.addNotificationListener(metadata1, listener1, null, null); 389 interceptor.addNotificationListener(metadata2, listener2, null, null); 390 interceptor.registration(metadata1, MBeanServerInterceptor.POST_DEREGISTER); 391 assertEquals(1, interceptor.getNotificationListenerWrappers().size()); 392 assertEquals(1, interceptor.getObjectNames().size()); 393 394 interceptor.registration(metadata2, MBeanServerInterceptor.POST_DEREGISTER); 395 assertEquals(0, interceptor.getNotificationListenerWrappers().size()); 396 assertEquals(0, interceptor.getObjectNames().size()); 397 } 398 399 private class NotificationListenerMBeanMetaData implements MBeanMetaData 400 { 401 private final Object mbean; 402 private final ObjectName objectName; 403 404 public NotificationListenerMBeanMetaData(Object mbean, ObjectName objectName) 405 { 406 this.mbean = mbean; 407 this.objectName = objectName; 408 } 409 410 public Object getMBean() 411 { 412 return mbean; 413 } 414 415 public ObjectName getObjectName() 416 { 417 return objectName; 418 } 419 420 public void setMBean(Object mbean) 421 { 422 } 423 424 public void setClassLoader(ClassLoader loader) 425 { 426 } 427 428 public ClassLoader getClassLoader() 429 { 430 return null; 431 } 432 433 public void setObjectName(ObjectName name) 434 { 435 } 436 437 public void setMBeanInfo(MBeanInfo info) 438 { 439 } 440 441 public MBeanInfo getMBeanInfo() 442 { 443 return null; 444 } 445 446 public void setMBeanInterface(Class management) 447 { 448 } 449 450 public Class getMBeanInterface() 451 { 452 return null; 453 } 454 455 public void setMBeanStandard(boolean value) 456 { 457 } 458 459 public boolean isMBeanStandard() 460 { 461 return false; 462 } 463 464 public void setMBeanDynamic(boolean value) 465 { 466 } 467 468 public boolean isMBeanDynamic() 469 { 470 return false; 471 } 472 473 public void setMBeanInvoker(MBeanInvoker invoker) 474 { 475 } 476 477 public MBeanInvoker getMBeanInvoker() 478 { 479 return null; 480 } 481 482 public ObjectInstance getObjectInstance() 483 { 484 return null; 485 } 486 } 487 } 488 | Popular Tags |