/**
* One listener thrice, one MBean, one ObjectName
*/
public void testAddRemove2() throws Exception
{
NotificationListenerMBeanServerInterceptor interceptor = new NotificationListenerMBeanServerInterceptor();
interceptor.setEnabled(true);
List chain = new ArrayList();
chain.add(interceptor);
chain.add(new NoOpMBeanServerInterceptor());
interceptor.setChain(chain);
ObjectName objectName = ObjectName.getInstance(":type=test");
MBeanMetaData metadata = new NotificationListenerMBeanMetaData(this, objectName);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
}
};
interceptor.addNotificationListener(metadata, listener, null, null);
interceptor.addNotificationListener(metadata, listener, null, listener);
Object handback = new Object();
interceptor.addNotificationListener(metadata, listener, null, handback);
assertEquals(1, interceptor.getNotificationListenerWrappers().size());
Object wrapper = ((Map.Entry)interceptor.getNotificationListenerWrappers().entrySet().iterator().next()).getValue();
assertEquals(3, interceptor.getNotificationListenerWrapperReferenceCount(wrapper));
assertEquals(1, interceptor.getObjectNames().size());
assertEquals(objectName, interceptor.getObjectNames().keySet().iterator().next());
interceptor.removeNotificationListener(metadata, listener, null, handback);
assertEquals(2, interceptor.getNotificationListenerWrapperReferenceCount(wrapper));
assertEquals(1, interceptor.getObjectNames().size());
interceptor.removeNotificationListener(metadata, listener);
assertEquals(0, interceptor.getNotificationListenerWrapperReferenceCount(wrapper));
assertEquals(0, interceptor.getNotificationListenerWrappers().size());
assertEquals(0, interceptor.getObjectNames().size());
}