1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 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.
21
21
import java .util .HashMap ;
22
22
import java .util .List ;
23
23
import java .util .Map ;
24
-
25
24
import javax .management .Attribute ;
26
25
import javax .management .InstanceNotFoundException ;
27
26
import javax .management .JMException ;
34
33
import javax .management .modelmbean .ModelMBeanInfo ;
35
34
36
35
import org .junit .Test ;
36
+
37
37
import org .springframework .aop .framework .ProxyFactory ;
38
38
import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
39
39
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
@@ -68,25 +68,10 @@ public final class MBeanExporterTests extends AbstractMBeanServerTests {
68
68
69
69
private static final String OBJECT_NAME = "spring:test=jmxMBeanAdaptor" ;
70
70
71
- @ SuppressWarnings ({ "rawtypes" , "unchecked" })
72
- @ Test
73
- public void testRegisterNonNotificationListenerType () throws Exception {
74
- Map listeners = new HashMap ();
75
- // put a non-NotificationListener instance in as a value...
76
- listeners .put ("*" , this );
77
- MBeanExporter exporter = new MBeanExporter ();
78
- try {
79
- exporter .setNotificationListenerMappings (listeners );
80
- fail ("Must have thrown a ClassCastException when registering a non-NotificationListener instance as a NotificationListener." );
81
- }
82
- catch (ClassCastException expected ) {
83
- }
84
- }
85
71
86
- @ SuppressWarnings ({ "rawtypes" , "unchecked" })
87
72
@ Test
88
73
public void testRegisterNullNotificationListenerType () throws Exception {
89
- Map listeners = new HashMap ();
74
+ Map < String , NotificationListener > listeners = new HashMap < String , NotificationListener > ();
90
75
// put null in as a value...
91
76
listeners .put ("*" , null );
92
77
MBeanExporter exporter = new MBeanExporter ();
@@ -98,10 +83,9 @@ public void testRegisterNullNotificationListenerType() throws Exception {
98
83
}
99
84
}
100
85
101
- @ SuppressWarnings ({ "rawtypes" , "unchecked" })
102
86
@ Test
103
87
public void testRegisterNotificationListenerForNonExistentMBean () throws Exception {
104
- Map listeners = new HashMap ();
88
+ Map < String , NotificationListener > listeners = new HashMap < String , NotificationListener > ();
105
89
NotificationListener dummyListener = new NotificationListener () {
106
90
@ Override
107
91
public void handleNotification (Notification notification , Object handback ) {
@@ -175,7 +159,8 @@ public void testAutodetectMBeans() throws Exception {
175
159
assertNotNull (instance );
176
160
instance = server .getObjectInstance (ObjectNameManager .getInstance ("spring:mbean3=true" ));
177
161
assertNotNull (instance );
178
- } finally {
162
+ }
163
+ finally {
179
164
bf .destroySingletons ();
180
165
}
181
166
}
@@ -193,9 +178,11 @@ public void testAutodetectWithExclude() throws Exception {
193
178
try {
194
179
server .getObjectInstance (ObjectNameManager .getInstance ("spring:mbean=false" ));
195
180
fail ("MBean with name spring:mbean=false should have been excluded" );
196
- } catch (InstanceNotFoundException expected ) {
197
181
}
198
- } finally {
182
+ catch (InstanceNotFoundException expected ) {
183
+ }
184
+ }
185
+ finally {
199
186
bf .destroySingletons ();
200
187
}
201
188
}
@@ -217,7 +204,8 @@ public void testAutodetectLazyMBeans() throws Exception {
217
204
assertNotNull (server .getObjectInstance (oname ));
218
205
name = (String ) server .getAttribute (oname , "Name" );
219
206
assertEquals ("Invalid name returned" , "Juergen Hoeller" , name );
220
- } finally {
207
+ }
208
+ finally {
221
209
bf .destroySingletons ();
222
210
}
223
211
}
@@ -228,7 +216,8 @@ public void testAutodetectNoMBeans() throws Exception {
228
216
new XmlBeanDefinitionReader (bf ).loadBeanDefinitions (new ClassPathResource ("autodetectNoMBeans.xml" , getClass ()));
229
217
try {
230
218
bf .getBean ("exporter" );
231
- } finally {
219
+ }
220
+ finally {
232
221
bf .destroySingletons ();
233
222
}
234
223
}
@@ -241,7 +230,7 @@ public void testWithMBeanExporterListeners() throws Exception {
241
230
MBeanExporter exporter = new MBeanExporter ();
242
231
exporter .setBeans (getBeanMap ());
243
232
exporter .setServer (server );
244
- exporter .setListeners (new MBeanExporterListener [] { listener1 , listener2 } );
233
+ exporter .setListeners (listener1 , listener2 );
245
234
exporter .afterPropertiesSet ();
246
235
exporter .destroy ();
247
236
@@ -257,7 +246,7 @@ public void testExportJdkProxy() throws Exception {
257
246
ProxyFactory factory = new ProxyFactory ();
258
247
factory .setTarget (bean );
259
248
factory .addAdvice (new NopInterceptor ());
260
- factory .setInterfaces (new Class <?>[] { IJmxTestBean .class } );
249
+ factory .setInterfaces (IJmxTestBean .class );
261
250
262
251
IJmxTestBean proxy = (IJmxTestBean ) factory .getProxy ();
263
252
String name = "bean:mmm=whatever" ;
@@ -377,8 +366,8 @@ public void testWithExposeClassLoader() throws Exception {
377
366
378
367
assertIsRegistered ("Bean instance not registered" , objectName );
379
368
380
- Object result = server .invoke (objectName , "add" , new Object [] { new Integer (2 ), new Integer (3 ) }, new String [] {
381
- int .class .getName (), int .class .getName () });
369
+ Object result = server .invoke (objectName , "add" , new Object [] {new Integer (2 ), new Integer (3 )}, new String [] {
370
+ int .class .getName (), int .class .getName ()});
382
371
383
372
assertEquals ("Incorrect result return from add" , result , new Integer (5 ));
384
373
assertEquals ("Incorrect attribute value" , name , server .getAttribute (objectName , "Name" ));
@@ -593,7 +582,7 @@ public void testMBeanIsNotUnregisteredSpuriouslyIfSomeExternalProcessHasUnregist
593
582
exporter .setBeans (getBeanMap ());
594
583
exporter .setServer (this .server );
595
584
MockMBeanExporterListener listener = new MockMBeanExporterListener ();
596
- exporter .setListeners (new MBeanExporterListener [] { listener } );
585
+ exporter .setListeners (listener );
597
586
exporter .afterPropertiesSet ();
598
587
assertIsRegistered ("The bean was not registered with the MBeanServer" ,
599
588
ObjectNameManager .getInstance (OBJECT_NAME ));
@@ -702,6 +691,7 @@ private void assertListener(MockMBeanExporterListener listener) throws Malformed
702
691
assertEquals ("Incorrect ObjectName in unregister" , desired , listener .getUnregistered ().get (0 ));
703
692
}
704
693
694
+
705
695
private static class InvokeDetectAssembler implements MBeanInfoAssembler {
706
696
707
697
private boolean invoked = false ;
@@ -713,6 +703,7 @@ public ModelMBeanInfo getMBeanInfo(Object managedResource, String beanKey) throw
713
703
}
714
704
}
715
705
706
+
716
707
private static class MockMBeanExporterListener implements MBeanExporterListener {
717
708
718
709
private List <ObjectName > registered = new ArrayList <ObjectName >();
@@ -738,6 +729,7 @@ public List<ObjectName> getUnregistered() {
738
729
}
739
730
}
740
731
732
+
741
733
private static class SelfNamingTestBean implements SelfNaming {
742
734
743
735
private ObjectName objectName ;
@@ -752,11 +744,13 @@ public ObjectName getObjectName() throws MalformedObjectNameException {
752
744
}
753
745
}
754
746
747
+
755
748
public static interface PersonMBean {
756
749
757
750
String getName ();
758
751
}
759
752
753
+
760
754
public static class Person implements PersonMBean {
761
755
762
756
private String name ;
@@ -771,6 +765,7 @@ public void setName(String name) {
771
765
}
772
766
}
773
767
768
+
774
769
public static final class StubNotificationListener implements NotificationListener {
775
770
776
771
private List <Notification > notifications = new ArrayList <Notification >();
@@ -785,6 +780,7 @@ public List<Notification> getNotifications() {
785
780
}
786
781
}
787
782
783
+
788
784
private static class RuntimeExceptionThrowingConstructorBean {
789
785
790
786
@ SuppressWarnings ("unused" )
@@ -793,6 +789,7 @@ public RuntimeExceptionThrowingConstructorBean() {
793
789
}
794
790
}
795
791
792
+
796
793
private static final class NamedBeanAutodetectCapableMBeanInfoAssemblerStub extends
797
794
SimpleReflectiveMBeanInfoAssembler implements AutodetectCapableMBeanInfoAssembler {
798
795
0 commit comments