Browse Source

backported NotificationListenerTests fixes

3.0.x
Juergen Hoeller 15 years ago
parent
commit
05a7b65d43
  1. 73
      org.springframework.context/src/test/java/org/springframework/jmx/export/NotificationListenerTests.java

73
org.springframework.context/src/test/java/org/springframework/jmx/export/NotificationListenerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.jmx.export; @@ -18,7 +18,6 @@ package org.springframework.jmx.export;
import java.util.HashMap;
import java.util.Map;
import javax.management.Attribute;
import javax.management.AttributeChangeNotification;
import javax.management.MalformedObjectNameException;
@ -27,7 +26,6 @@ import javax.management.NotificationFilter; @@ -27,7 +26,6 @@ import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectName;
import org.junit.Ignore;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.jmx.AbstractMBeanServerTests;
import org.springframework.jmx.JmxTestBean;
@ -38,16 +36,17 @@ import org.springframework.jmx.support.ObjectNameManager; @@ -38,16 +36,17 @@ import org.springframework.jmx.support.ObjectNameManager;
/**
* @author Rob Harrop
* @author Mark Fisher
* @author Sam Brannen
*/
@Ignore // Getting CCEs regarding ObjectName being cast to String
public class NotificationListenerTests extends AbstractMBeanServerTests {
@SuppressWarnings({"rawtypes", "unchecked"})
public void testRegisterNotificationListenerForMBean() throws Exception {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@ -66,12 +65,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -66,12 +65,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRegisterNotificationListenerWithWildcard() throws Exception {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@ -94,7 +94,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -94,7 +94,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
String objectName = "spring:name=Test";
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName, bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@ -108,12 +108,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -108,12 +108,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
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"));
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));
@ -123,8 +124,8 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -123,8 +124,8 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@ -134,7 +135,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -134,7 +135,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
exporter.afterPropertiesSet();
// update the attribute
@ -144,12 +145,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -144,12 +145,13 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
}
@SuppressWarnings("serial")
public void testRegisterNotificationListenerWithFilter() throws Exception {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@ -160,8 +162,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -160,8 +162,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
if (notification instanceof AttributeChangeNotification) {
AttributeChangeNotification changeNotification = (AttributeChangeNotification) notification;
return "Name".equals(changeNotification.getAttributeName());
}
else {
} else {
return false;
}
}
@ -170,7 +171,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -170,7 +171,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
exporter.setBeans(beans);
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
exporter.afterPropertiesSet();
// update the attributes
@ -188,11 +189,11 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -188,11 +189,11 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
try {
new NotificationListenerBean().afterPropertiesSet();
fail("Must have thrown an IllegalArgumentException (no NotificationListener supplied)");
}
catch (IllegalArgumentException expected) {
} catch (IllegalArgumentException expected) {
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() throws Exception {
String beanName = "testBean";
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@ -203,7 +204,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -203,7 +204,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerSingleton(beanName, testBean);
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(beanName, beanName);
Map listenerMappings = new HashMap();
@ -222,6 +223,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -222,6 +223,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
assertEquals("Listener not notified", 1, listener.getCount("Age"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMap() throws Exception {
String beanName = "testBean";
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@ -232,7 +234,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -232,7 +234,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerSingleton(beanName, testBean);
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(beanName, testBean);
Map listenerMappings = new HashMap();
@ -251,6 +253,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -251,6 +253,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
assertEquals("Listener not notified", 1, listener.getCount("Age"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance() throws Exception {
String beanName = "testBean";
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@ -261,7 +264,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -261,7 +264,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerSingleton(beanName, testBean);
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(beanName, testBean);
Map listenerMappings = new HashMap();
@ -281,6 +284,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -281,6 +284,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age"));
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance() throws Exception {
String beanName = "testBean";
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@ -291,7 +295,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -291,7 +295,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
factory.registerSingleton(beanName, testBean);
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(beanName, testBean);
Map listenerMappings = new HashMap();
@ -311,6 +315,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -311,6 +315,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
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";
@ -328,7 +333,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -328,7 +333,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
factory.registerSingleton(beanName1, testBean1);
factory.registerSingleton(beanName2, testBean2);
Map beans = new HashMap();
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(beanName1, testBean1);
beans.put(beanName2, testBean2);
@ -357,8 +362,8 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -357,8 +362,8 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
JmxTestBean bean = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
@ -391,9 +396,9 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -391,9 +396,9 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
JmxTestBean bean = new JmxTestBean();
JmxTestBean bean2 = new JmxTestBean();
Map beans = new HashMap();
beans.put(objectName, bean);
beans.put(objectName2, bean2);
Map<String, Object> beans = new HashMap<String, Object>();
beans.put(objectName.getCanonicalName(), bean);
beans.put(objectName2.getCanonicalName(), bean2);
MBeanExporter exporter = new MBeanExporter();
exporter.setServer(server);
@ -406,7 +411,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -406,7 +411,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
registrar.setServer(server);
registrar.setNotificationListener(listener);
//registrar.setMappedObjectNames(new Object[] {objectName, objectName2});
registrar.setMappedObjectNames(new String[] {"spring:name=Test", "spring:name=Test2"});
registrar.setMappedObjectNames(new String[] { "spring:name=Test", "spring:name=Test2" });
registrar.afterPropertiesSet();
// update the attribute
@ -422,6 +427,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -422,6 +427,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static class CountingAttributeChangeNotificationListener implements NotificationListener {
private Map attributeCounts = new HashMap();
@ -438,8 +444,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests { @@ -438,8 +444,7 @@ public class NotificationListenerTests extends AbstractMBeanServerTests {
if (currentCount != null) {
int count = currentCount.intValue() + 1;
this.attributeCounts.put(attributeName, new Integer(count));
}
else {
} else {
this.attributeCounts.put(attributeName, new Integer(1));
}

Loading…
Cancel
Save