public void testRegisterNotificationListenerWithHandback() throws Exception {
    String objectName = "spring:name=Test";
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName, bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();
    Object handback = new Object();

    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setMappedObjectName("spring:name=Test");
    listenerBean.setHandback(handback);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] {listenerBean});
    exporter.afterPropertiesSet();

    // update the attribute
    String attributeName = "Name";
    server.setAttribute(
        ObjectNameManager.getInstance("spring:name=Test"),
        new Attribute(attributeName, "Rob Harrop"));

    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
    assertEquals(
        "Handback object not transmitted correctly",
        handback,
        listener.getLastHandback(attributeName));
  }
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void testRegisterNotificationListenerWithWildcard() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName.getCanonicalName(), bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();

    Map notificationListeners = new HashMap();
    notificationListeners.put("*", listener);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListenerMappings(notificationListeners);
    exporter.afterPropertiesSet();

    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));
  }
  public void testNotificationListenerRegistrar() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName.getCanonicalName(), bean);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.afterPropertiesSet();

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();

    NotificationListenerRegistrar registrar = new NotificationListenerRegistrar();
    registrar.setServer(server);
    registrar.setNotificationListener(listener);
    registrar.setMappedObjectName(objectName);
    registrar.afterPropertiesSet();

    // update the attribute
    String attributeName = "Name";
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener not notified", 1, listener.getCount(attributeName));

    registrar.destroy();

    // try to update the attribute again
    server.setAttribute(objectName, new Attribute(attributeName, "Rob Harrop"));
    assertEquals("Listener notified after destruction", 1, listener.getCount(attributeName));
  }
  @SuppressWarnings("serial")
  public void testRegisterNotificationListenerWithFilter() throws Exception {
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");
    JmxTestBean bean = new JmxTestBean();

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(objectName.getCanonicalName(), bean);

    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();

    NotificationListenerBean listenerBean = new NotificationListenerBean();
    listenerBean.setNotificationListener(listener);
    listenerBean.setNotificationFilter(
        new NotificationFilter() {
          public boolean isNotificationEnabled(Notification notification) {
            if (notification instanceof AttributeChangeNotification) {
              AttributeChangeNotification changeNotification =
                  (AttributeChangeNotification) notification;
              return "Name".equals(changeNotification.getAttributeName());
            } else {
              return false;
            }
          }
        });

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListeners(new NotificationListenerBean[] {listenerBean});
    exporter.afterPropertiesSet();

    // update the attributes
    String nameAttribute = "Name";
    String ageAttribute = "Age";

    server.setAttribute(objectName, new Attribute(nameAttribute, "Rob Harrop"));
    server.setAttribute(objectName, new Attribute(ageAttribute, new Integer(90)));

    assertEquals("Listener not notified for Name", 1, listener.getCount(nameAttribute));
    assertEquals("Listener incorrectly notified for Age", 0, listener.getCount(ageAttribute));
  }
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance()
      throws Exception {
    String beanName = "testBean";
    ObjectName objectName = ObjectName.getInstance("spring:name=Test");

    SelfNamingTestBean testBean = new SelfNamingTestBean();
    testBean.setObjectName(objectName);

    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerSingleton(beanName, testBean);

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(beanName, testBean);

    Map listenerMappings = new HashMap();
    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();
    listenerMappings.put(objectName, listener);
    listenerMappings.put(beanName, listener);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListenerMappings(listenerMappings);
    exporter.setBeanFactory(factory);
    exporter.afterPropertiesSet();
    assertIsRegistered("Should have registered MBean", objectName);

    server.setAttribute(objectName, new Attribute("Age", new Integer(77)));
    assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age"));
  }
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBeanInstances()
      throws Exception {
    String beanName1 = "testBean1";
    String beanName2 = "testBean2";

    ObjectName objectName1 = ObjectName.getInstance("spring:name=Test1");
    ObjectName objectName2 = ObjectName.getInstance("spring:name=Test2");

    SelfNamingTestBean testBean1 = new SelfNamingTestBean();
    testBean1.setObjectName(objectName1);

    SelfNamingTestBean testBean2 = new SelfNamingTestBean();
    testBean2.setObjectName(objectName2);

    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerSingleton(beanName1, testBean1);
    factory.registerSingleton(beanName2, testBean2);

    Map<String, Object> beans = new HashMap<String, Object>();
    beans.put(beanName1, testBean1);
    beans.put(beanName2, testBean2);

    Map listenerMappings = new HashMap();
    CountingAttributeChangeNotificationListener listener =
        new CountingAttributeChangeNotificationListener();
    listenerMappings.put(beanName1, listener);
    listenerMappings.put(beanName2, listener);

    MBeanExporter exporter = new MBeanExporter();
    exporter.setServer(server);
    exporter.setBeans(beans);
    exporter.setNotificationListenerMappings(listenerMappings);
    exporter.setBeanFactory(factory);
    exporter.afterPropertiesSet();
    assertIsRegistered("Should have registered MBean", objectName1);
    assertIsRegistered("Should have registered MBean", objectName2);

    server.setAttribute(
        ObjectNameManager.getInstance(objectName1), new Attribute("Age", new Integer(77)));
    assertEquals("Listener not notified for testBean1", 1, listener.getCount("Age"));

    server.setAttribute(
        ObjectNameManager.getInstance(objectName2), new Attribute("Age", new Integer(33)));
    assertEquals("Listener not notified for testBean2", 2, listener.getCount("Age"));
  }