Skip to content

Commit 05a7b65

Browse files
committed
backported NotificationListenerTests fixes
1 parent cee6cb8 commit 05a7b65

File tree

1 file changed

+39
-34
lines changed

1 file changed

+39
-34
lines changed

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

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2008 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21-
2221
import javax.management.Attribute;
2322
import javax.management.AttributeChangeNotification;
2423
import javax.management.MalformedObjectNameException;
@@ -27,7 +26,6 @@
2726
import javax.management.NotificationListener;
2827
import javax.management.ObjectName;
2928

30-
import org.junit.Ignore;
3129
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3230
import org.springframework.jmx.AbstractMBeanServerTests;
3331
import org.springframework.jmx.JmxTestBean;
@@ -38,16 +36,17 @@
3836
/**
3937
* @author Rob Harrop
4038
* @author Mark Fisher
39+
* @author Sam Brannen
4140
*/
42-
@Ignore // Getting CCEs regarding ObjectName being cast to String
4341
public class NotificationListenerTests extends AbstractMBeanServerTests {
4442

43+
@SuppressWarnings({"rawtypes", "unchecked"})
4544
public void testRegisterNotificationListenerForMBean() throws Exception {
4645
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
4746
JmxTestBean bean = new JmxTestBean();
4847

49-
Map beans = new HashMap();
50-
beans.put(objectName, bean);
48+
Map<String, Object> beans = new HashMap<String, Object>();
49+
beans.put(objectName.getCanonicalName(), bean);
5150

5251
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
5352

@@ -66,12 +65,13 @@ public void testRegisterNotificationListenerForMBean() throws Exception {
6665
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
6766
}
6867

68+
@SuppressWarnings({ "rawtypes", "unchecked" })
6969
public void testRegisterNotificationListenerWithWildcard() throws Exception {
7070
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
7171
JmxTestBean bean = new JmxTestBean();
7272

73-
Map beans = new HashMap();
74-
beans.put(objectName, bean);
73+
Map<String, Object> beans = new HashMap<String, Object>();
74+
beans.put(objectName.getCanonicalName(), bean);
7575

7676
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
7777

@@ -94,7 +94,7 @@ public void testRegisterNotificationListenerWithHandback() throws Exception {
9494
String objectName = "spring:name=Test";
9595
JmxTestBean bean = new JmxTestBean();
9696

97-
Map beans = new HashMap();
97+
Map<String, Object> beans = new HashMap<String, Object>();
9898
beans.put(objectName, bean);
9999

100100
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
@@ -108,12 +108,13 @@ public void testRegisterNotificationListenerWithHandback() throws Exception {
108108
MBeanExporter exporter = new MBeanExporter();
109109
exporter.setServer(server);
110110
exporter.setBeans(beans);
111-
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
111+
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
112112
exporter.afterPropertiesSet();
113113

114114
// update the attribute
115115
String attributeName = "Name";
116-
server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName, "Rob Harrop"));
116+
server.setAttribute(ObjectNameManager.getInstance("spring:name=Test"), new Attribute(attributeName,
117+
"Rob Harrop"));
117118

118119
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
119120
assertEquals("Handback object not transmitted correctly", handback, listener.getLastHandback(attributeName));
@@ -123,8 +124,8 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
123124
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
124125
JmxTestBean bean = new JmxTestBean();
125126

126-
Map beans = new HashMap();
127-
beans.put(objectName, bean);
127+
Map<String, Object> beans = new HashMap<String, Object>();
128+
beans.put(objectName.getCanonicalName(), bean);
128129

129130
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
130131

@@ -134,7 +135,7 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
134135
MBeanExporter exporter = new MBeanExporter();
135136
exporter.setServer(server);
136137
exporter.setBeans(beans);
137-
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
138+
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
138139
exporter.afterPropertiesSet();
139140

140141
// update the attribute
@@ -144,12 +145,13 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
144145
assertEquals("Listener not notified", 1, listener.getCount(attributeName));
145146
}
146147

148+
@SuppressWarnings("serial")
147149
public void testRegisterNotificationListenerWithFilter() throws Exception {
148150
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
149151
JmxTestBean bean = new JmxTestBean();
150152

151-
Map beans = new HashMap();
152-
beans.put(objectName, bean);
153+
Map<String, Object> beans = new HashMap<String, Object>();
154+
beans.put(objectName.getCanonicalName(), bean);
153155

154156
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener();
155157

@@ -160,8 +162,7 @@ public boolean isNotificationEnabled(Notification notification) {
160162
if (notification instanceof AttributeChangeNotification) {
161163
AttributeChangeNotification changeNotification = (AttributeChangeNotification) notification;
162164
return "Name".equals(changeNotification.getAttributeName());
163-
}
164-
else {
165+
} else {
165166
return false;
166167
}
167168
}
@@ -170,7 +171,7 @@ public boolean isNotificationEnabled(Notification notification) {
170171
MBeanExporter exporter = new MBeanExporter();
171172
exporter.setServer(server);
172173
exporter.setBeans(beans);
173-
exporter.setNotificationListeners(new NotificationListenerBean[]{listenerBean});
174+
exporter.setNotificationListeners(new NotificationListenerBean[] { listenerBean });
174175
exporter.afterPropertiesSet();
175176

176177
// update the attributes
@@ -188,11 +189,11 @@ public void testCreationWithNoNotificationListenerSet() {
188189
try {
189190
new NotificationListenerBean().afterPropertiesSet();
190191
fail("Must have thrown an IllegalArgumentException (no NotificationListener supplied)");
191-
}
192-
catch (IllegalArgumentException expected) {
192+
} catch (IllegalArgumentException expected) {
193193
}
194194
}
195195

196+
@SuppressWarnings({ "rawtypes", "unchecked" })
196197
public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap() throws Exception {
197198
String beanName = "testBean";
198199
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@@ -203,7 +204,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap()
203204
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
204205
factory.registerSingleton(beanName, testBean);
205206

206-
Map beans = new HashMap();
207+
Map<String, Object> beans = new HashMap<String, Object>();
207208
beans.put(beanName, beanName);
208209

209210
Map listenerMappings = new HashMap();
@@ -222,6 +223,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap()
222223
assertEquals("Listener not notified", 1, listener.getCount("Age"));
223224
}
224225

226+
@SuppressWarnings({ "rawtypes", "unchecked" })
225227
public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMap() throws Exception {
226228
String beanName = "testBean";
227229
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@@ -232,7 +234,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMa
232234
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
233235
factory.registerSingleton(beanName, testBean);
234236

235-
Map beans = new HashMap();
237+
Map<String, Object> beans = new HashMap<String, Object>();
236238
beans.put(beanName, testBean);
237239

238240
Map listenerMappings = new HashMap();
@@ -251,6 +253,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMa
251253
assertEquals("Listener not notified", 1, listener.getCount("Age"));
252254
}
253255

256+
@SuppressWarnings({ "rawtypes", "unchecked" })
254257
public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance() throws Exception {
255258
String beanName = "testBean";
256259
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@@ -261,7 +264,7 @@ public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedTo
261264
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
262265
factory.registerSingleton(beanName, testBean);
263266

264-
Map beans = new HashMap();
267+
Map<String, Object> beans = new HashMap<String, Object>();
265268
beans.put(beanName, testBean);
266269

267270
Map listenerMappings = new HashMap();
@@ -281,6 +284,7 @@ public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedTo
281284
assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age"));
282285
}
283286

287+
@SuppressWarnings({ "rawtypes", "unchecked" })
284288
public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance() throws Exception {
285289
String beanName = "testBean";
286290
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
@@ -291,7 +295,7 @@ public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedTo
291295
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
292296
factory.registerSingleton(beanName, testBean);
293297

294-
Map beans = new HashMap();
298+
Map<String, Object> beans = new HashMap<String, Object>();
295299
beans.put(beanName, testBean);
296300

297301
Map listenerMappings = new HashMap();
@@ -311,6 +315,7 @@ public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedTo
311315
assertEquals("Listener should have been notified exactly once", 1, listener.getCount("Age"));
312316
}
313317

318+
@SuppressWarnings({ "rawtypes", "unchecked" })
314319
public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBeanInstances() throws Exception {
315320
String beanName1 = "testBean1";
316321
String beanName2 = "testBean2";
@@ -328,7 +333,7 @@ public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBea
328333
factory.registerSingleton(beanName1, testBean1);
329334
factory.registerSingleton(beanName2, testBean2);
330335

331-
Map beans = new HashMap();
336+
Map<String, Object> beans = new HashMap<String, Object>();
332337
beans.put(beanName1, testBean1);
333338
beans.put(beanName2, testBean2);
334339

@@ -357,8 +362,8 @@ public void testNotificationListenerRegistrar() throws Exception {
357362
ObjectName objectName = ObjectName.getInstance("spring:name=Test");
358363
JmxTestBean bean = new JmxTestBean();
359364

360-
Map beans = new HashMap();
361-
beans.put(objectName, bean);
365+
Map<String, Object> beans = new HashMap<String, Object>();
366+
beans.put(objectName.getCanonicalName(), bean);
362367

363368
MBeanExporter exporter = new MBeanExporter();
364369
exporter.setServer(server);
@@ -391,9 +396,9 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
391396
JmxTestBean bean = new JmxTestBean();
392397
JmxTestBean bean2 = new JmxTestBean();
393398

394-
Map beans = new HashMap();
395-
beans.put(objectName, bean);
396-
beans.put(objectName2, bean2);
399+
Map<String, Object> beans = new HashMap<String, Object>();
400+
beans.put(objectName.getCanonicalName(), bean);
401+
beans.put(objectName2.getCanonicalName(), bean2);
397402

398403
MBeanExporter exporter = new MBeanExporter();
399404
exporter.setServer(server);
@@ -406,7 +411,7 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
406411
registrar.setServer(server);
407412
registrar.setNotificationListener(listener);
408413
//registrar.setMappedObjectNames(new Object[] {objectName, objectName2});
409-
registrar.setMappedObjectNames(new String[] {"spring:name=Test", "spring:name=Test2"});
414+
registrar.setMappedObjectNames(new String[] { "spring:name=Test", "spring:name=Test2" });
410415
registrar.afterPropertiesSet();
411416

412417
// update the attribute
@@ -422,6 +427,7 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
422427
}
423428

424429

430+
@SuppressWarnings({"rawtypes", "unchecked"})
425431
private static class CountingAttributeChangeNotificationListener implements NotificationListener {
426432

427433
private Map attributeCounts = new HashMap();
@@ -438,8 +444,7 @@ public void handleNotification(Notification notification, Object handback) {
438444
if (currentCount != null) {
439445
int count = currentCount.intValue() + 1;
440446
this.attributeCounts.put(attributeName, new Integer(count));
441-
}
442-
else {
447+
} else {
443448
this.attributeCounts.put(attributeName, new Integer(1));
444449
}
445450

0 commit comments

Comments
 (0)