@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")); }