1
1
/*
2
- * Copyright 2002-2008 the original author or authors.
2
+ * Copyright 2002-2011 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
19
19
import java .util .HashMap ;
20
20
import java .util .Map ;
21
-
22
21
import javax .management .Attribute ;
23
22
import javax .management .AttributeChangeNotification ;
24
23
import javax .management .MalformedObjectNameException ;
27
26
import javax .management .NotificationListener ;
28
27
import javax .management .ObjectName ;
29
28
30
- import org .junit .Ignore ;
31
29
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
32
30
import org .springframework .jmx .AbstractMBeanServerTests ;
33
31
import org .springframework .jmx .JmxTestBean ;
38
36
/**
39
37
* @author Rob Harrop
40
38
* @author Mark Fisher
39
+ * @author Sam Brannen
41
40
*/
42
- @ Ignore // Getting CCEs regarding ObjectName being cast to String
43
41
public class NotificationListenerTests extends AbstractMBeanServerTests {
44
42
43
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
45
44
public void testRegisterNotificationListenerForMBean () throws Exception {
46
45
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
47
46
JmxTestBean bean = new JmxTestBean ();
48
47
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 );
51
50
52
51
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener ();
53
52
@@ -66,12 +65,13 @@ public void testRegisterNotificationListenerForMBean() throws Exception {
66
65
assertEquals ("Listener not notified" , 1 , listener .getCount (attributeName ));
67
66
}
68
67
68
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
69
69
public void testRegisterNotificationListenerWithWildcard () throws Exception {
70
70
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
71
71
JmxTestBean bean = new JmxTestBean ();
72
72
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 );
75
75
76
76
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener ();
77
77
@@ -94,7 +94,7 @@ public void testRegisterNotificationListenerWithHandback() throws Exception {
94
94
String objectName = "spring:name=Test" ;
95
95
JmxTestBean bean = new JmxTestBean ();
96
96
97
- Map beans = new HashMap ();
97
+ Map < String , Object > beans = new HashMap < String , Object > ();
98
98
beans .put (objectName , bean );
99
99
100
100
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener ();
@@ -108,12 +108,13 @@ public void testRegisterNotificationListenerWithHandback() throws Exception {
108
108
MBeanExporter exporter = new MBeanExporter ();
109
109
exporter .setServer (server );
110
110
exporter .setBeans (beans );
111
- exporter .setNotificationListeners (new NotificationListenerBean []{ listenerBean });
111
+ exporter .setNotificationListeners (new NotificationListenerBean [] { listenerBean });
112
112
exporter .afterPropertiesSet ();
113
113
114
114
// update the attribute
115
115
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" ));
117
118
118
119
assertEquals ("Listener not notified" , 1 , listener .getCount (attributeName ));
119
120
assertEquals ("Handback object not transmitted correctly" , handback , listener .getLastHandback (attributeName ));
@@ -123,8 +124,8 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
123
124
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
124
125
JmxTestBean bean = new JmxTestBean ();
125
126
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 );
128
129
129
130
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener ();
130
131
@@ -134,7 +135,7 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
134
135
MBeanExporter exporter = new MBeanExporter ();
135
136
exporter .setServer (server );
136
137
exporter .setBeans (beans );
137
- exporter .setNotificationListeners (new NotificationListenerBean []{ listenerBean });
138
+ exporter .setNotificationListeners (new NotificationListenerBean [] { listenerBean });
138
139
exporter .afterPropertiesSet ();
139
140
140
141
// update the attribute
@@ -144,12 +145,13 @@ public void testRegisterNotificationListenerForAllMBeans() throws Exception {
144
145
assertEquals ("Listener not notified" , 1 , listener .getCount (attributeName ));
145
146
}
146
147
148
+ @ SuppressWarnings ("serial" )
147
149
public void testRegisterNotificationListenerWithFilter () throws Exception {
148
150
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
149
151
JmxTestBean bean = new JmxTestBean ();
150
152
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 );
153
155
154
156
CountingAttributeChangeNotificationListener listener = new CountingAttributeChangeNotificationListener ();
155
157
@@ -160,8 +162,7 @@ public boolean isNotificationEnabled(Notification notification) {
160
162
if (notification instanceof AttributeChangeNotification ) {
161
163
AttributeChangeNotification changeNotification = (AttributeChangeNotification ) notification ;
162
164
return "Name" .equals (changeNotification .getAttributeName ());
163
- }
164
- else {
165
+ } else {
165
166
return false ;
166
167
}
167
168
}
@@ -170,7 +171,7 @@ public boolean isNotificationEnabled(Notification notification) {
170
171
MBeanExporter exporter = new MBeanExporter ();
171
172
exporter .setServer (server );
172
173
exporter .setBeans (beans );
173
- exporter .setNotificationListeners (new NotificationListenerBean []{ listenerBean });
174
+ exporter .setNotificationListeners (new NotificationListenerBean [] { listenerBean });
174
175
exporter .afterPropertiesSet ();
175
176
176
177
// update the attributes
@@ -188,11 +189,11 @@ public void testCreationWithNoNotificationListenerSet() {
188
189
try {
189
190
new NotificationListenerBean ().afterPropertiesSet ();
190
191
fail ("Must have thrown an IllegalArgumentException (no NotificationListener supplied)" );
191
- }
192
- catch (IllegalArgumentException expected ) {
192
+ } catch (IllegalArgumentException expected ) {
193
193
}
194
194
}
195
195
196
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
196
197
public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap () throws Exception {
197
198
String beanName = "testBean" ;
198
199
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
@@ -203,7 +204,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap()
203
204
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
204
205
factory .registerSingleton (beanName , testBean );
205
206
206
- Map beans = new HashMap ();
207
+ Map < String , Object > beans = new HashMap < String , Object > ();
207
208
beans .put (beanName , beanName );
208
209
209
210
Map listenerMappings = new HashMap ();
@@ -222,6 +223,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanNameInBeansMap()
222
223
assertEquals ("Listener not notified" , 1 , listener .getCount ("Age" ));
223
224
}
224
225
226
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
225
227
public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMap () throws Exception {
226
228
String beanName = "testBean" ;
227
229
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
@@ -232,7 +234,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMa
232
234
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
233
235
factory .registerSingleton (beanName , testBean );
234
236
235
- Map beans = new HashMap ();
237
+ Map < String , Object > beans = new HashMap < String , Object > ();
236
238
beans .put (beanName , testBean );
237
239
238
240
Map listenerMappings = new HashMap ();
@@ -251,6 +253,7 @@ public void testRegisterNotificationListenerWithBeanNameAndBeanInstanceInBeansMa
251
253
assertEquals ("Listener not notified" , 1 , listener .getCount ("Age" ));
252
254
}
253
255
256
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
254
257
public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedToSameBeanInstance () throws Exception {
255
258
String beanName = "testBean" ;
256
259
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
@@ -261,7 +264,7 @@ public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedTo
261
264
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
262
265
factory .registerSingleton (beanName , testBean );
263
266
264
- Map beans = new HashMap ();
267
+ Map < String , Object > beans = new HashMap < String , Object > ();
265
268
beans .put (beanName , testBean );
266
269
267
270
Map listenerMappings = new HashMap ();
@@ -281,6 +284,7 @@ public void testRegisterNotificationListenerWithBeanNameBeforeObjectNameMappedTo
281
284
assertEquals ("Listener should have been notified exactly once" , 1 , listener .getCount ("Age" ));
282
285
}
283
286
287
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
284
288
public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedToSameBeanInstance () throws Exception {
285
289
String beanName = "testBean" ;
286
290
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
@@ -291,7 +295,7 @@ public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedTo
291
295
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
292
296
factory .registerSingleton (beanName , testBean );
293
297
294
- Map beans = new HashMap ();
298
+ Map < String , Object > beans = new HashMap < String , Object > ();
295
299
beans .put (beanName , testBean );
296
300
297
301
Map listenerMappings = new HashMap ();
@@ -311,6 +315,7 @@ public void testRegisterNotificationListenerWithObjectNameBeforeBeanNameMappedTo
311
315
assertEquals ("Listener should have been notified exactly once" , 1 , listener .getCount ("Age" ));
312
316
}
313
317
318
+ @ SuppressWarnings ({ "rawtypes" , "unchecked" })
314
319
public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBeanInstances () throws Exception {
315
320
String beanName1 = "testBean1" ;
316
321
String beanName2 = "testBean2" ;
@@ -328,7 +333,7 @@ public void testRegisterNotificationListenerWithTwoBeanNamesMappedToDifferentBea
328
333
factory .registerSingleton (beanName1 , testBean1 );
329
334
factory .registerSingleton (beanName2 , testBean2 );
330
335
331
- Map beans = new HashMap ();
336
+ Map < String , Object > beans = new HashMap < String , Object > ();
332
337
beans .put (beanName1 , testBean1 );
333
338
beans .put (beanName2 , testBean2 );
334
339
@@ -357,8 +362,8 @@ public void testNotificationListenerRegistrar() throws Exception {
357
362
ObjectName objectName = ObjectName .getInstance ("spring:name=Test" );
358
363
JmxTestBean bean = new JmxTestBean ();
359
364
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 );
362
367
363
368
MBeanExporter exporter = new MBeanExporter ();
364
369
exporter .setServer (server );
@@ -391,9 +396,9 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
391
396
JmxTestBean bean = new JmxTestBean ();
392
397
JmxTestBean bean2 = new JmxTestBean ();
393
398
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 );
397
402
398
403
MBeanExporter exporter = new MBeanExporter ();
399
404
exporter .setServer (server );
@@ -406,7 +411,7 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
406
411
registrar .setServer (server );
407
412
registrar .setNotificationListener (listener );
408
413
//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" });
410
415
registrar .afterPropertiesSet ();
411
416
412
417
// update the attribute
@@ -422,6 +427,7 @@ public void testNotificationListenerRegistrarWithMultipleNames() throws Exceptio
422
427
}
423
428
424
429
430
+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
425
431
private static class CountingAttributeChangeNotificationListener implements NotificationListener {
426
432
427
433
private Map attributeCounts = new HashMap ();
@@ -438,8 +444,7 @@ public void handleNotification(Notification notification, Object handback) {
438
444
if (currentCount != null ) {
439
445
int count = currentCount .intValue () + 1 ;
440
446
this .attributeCounts .put (attributeName , new Integer (count ));
441
- }
442
- else {
447
+ } else {
443
448
this .attributeCounts .put (attributeName , new Integer (1 ));
444
449
}
445
450
0 commit comments