25
25
import org .junit .After ;
26
26
import org .junit .Test ;
27
27
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
28
+ import org .springframework .boot .actuate .endpoint .AbstractEndpoint ;
29
+ import org .springframework .boot .actuate .endpoint .Endpoint ;
28
30
import org .springframework .boot .actuate .endpoint .jmx .EndpointMBeanExporter ;
31
+ import org .springframework .boot .autoconfigure .PropertyPlaceholderAutoConfiguration ;
29
32
import org .springframework .boot .autoconfigure .jmx .JmxAutoConfiguration ;
30
33
import org .springframework .context .ApplicationContext ;
31
34
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
35
+ import org .springframework .context .annotation .Bean ;
32
36
import org .springframework .context .annotation .Configuration ;
33
37
import org .springframework .context .annotation .EnableMBeanExport ;
34
38
import org .springframework .jmx .export .MBeanExporter ;
39
+ import org .springframework .jmx .export .annotation .ManagedResource ;
35
40
import org .springframework .jmx .support .ObjectNameManager ;
36
41
import org .springframework .mock .env .MockEnvironment ;
42
+ import org .springframework .stereotype .Component ;
37
43
import org .springframework .util .ObjectUtils ;
38
44
45
+ import static org .junit .Assert .assertFalse ;
39
46
import static org .junit .Assert .assertNotNull ;
47
+ import static org .junit .Assert .assertTrue ;
40
48
import static org .junit .Assert .fail ;
41
49
42
50
/**
@@ -55,13 +63,51 @@ public void close() {
55
63
}
56
64
57
65
@ Test
58
- public void testEndpointMBeanExporterIsInstalled () {
66
+ public void testEndpointMBeanExporterIsInstalled () throws Exception {
59
67
this .context = new AnnotationConfigApplicationContext ();
60
68
this .context .register (TestConfiguration .class , JmxAutoConfiguration .class ,
61
69
EndpointAutoConfiguration .class ,
62
- EndpointMBeanExportAutoConfiguration .class );
70
+ EndpointMBeanExportAutoConfiguration .class ,
71
+ PropertyPlaceholderAutoConfiguration .class );
72
+ this .context .refresh ();
73
+ assertNotNull (this .context .getBean (EndpointMBeanExporter .class ));
74
+ MBeanExporter mbeanExporter = this .context .getBean (EndpointMBeanExporter .class );
75
+
76
+ assertFalse (mbeanExporter .getServer ()
77
+ .queryNames (getObjectName ("*" , "*,*" , this .context ), null ).isEmpty ());
78
+ }
79
+
80
+ @ Test
81
+ public void testEndpointMBeanExporterIsNotInstalledIfManagedResource ()
82
+ throws Exception {
83
+ this .context = new AnnotationConfigApplicationContext ();
84
+ this .context .register (TestConfiguration .class , JmxAutoConfiguration .class ,
85
+ ManagedEndpoint .class , EndpointMBeanExportAutoConfiguration .class ,
86
+ PropertyPlaceholderAutoConfiguration .class );
87
+ this .context .refresh ();
88
+ assertNotNull (this .context .getBean (EndpointMBeanExporter .class ));
89
+
90
+ MBeanExporter mbeanExporter = this .context .getBean (EndpointMBeanExporter .class );
91
+
92
+ assertTrue (mbeanExporter .getServer ()
93
+ .queryNames (getObjectName ("*" , "*,*" , this .context ), null ).isEmpty ());
94
+ }
95
+
96
+ @ Test
97
+ public void testEndpointMBeanExporterIsNotInstalledIfNestedInManagedResource ()
98
+ throws Exception {
99
+ this .context = new AnnotationConfigApplicationContext ();
100
+ this .context .register (TestConfiguration .class , JmxAutoConfiguration .class ,
101
+ NestedInManagedEndpoint .class ,
102
+ EndpointMBeanExportAutoConfiguration .class ,
103
+ PropertyPlaceholderAutoConfiguration .class );
63
104
this .context .refresh ();
64
105
assertNotNull (this .context .getBean (EndpointMBeanExporter .class ));
106
+
107
+ MBeanExporter mbeanExporter = this .context .getBean (EndpointMBeanExporter .class );
108
+
109
+ assertTrue (mbeanExporter .getServer ()
110
+ .queryNames (getObjectName ("*" , "*,*" , this .context ), null ).isEmpty ());
65
111
}
66
112
67
113
@ Test (expected = NoSuchBeanDefinitionException .class )
@@ -125,21 +171,23 @@ public void testEndpointMBeanExporterInParentChild() throws IntrospectionExcepti
125
171
126
172
private ObjectName getObjectName (String domain , String beanKey ,
127
173
ApplicationContext applicationContext ) throws MalformedObjectNameException {
174
+ String name = "%s:type=Endpoint,name=%s" ;
175
+ if (applicationContext .getParent () != null ) {
176
+ name = name + ",context=%s" ;
177
+ }
178
+ if (applicationContext .getEnvironment ().getProperty ("endpoints.jmx.unique_names" ,
179
+ Boolean .class , false )) {
180
+ name = name
181
+ + ",identity="
182
+ + ObjectUtils .getIdentityHexString (applicationContext
183
+ .getBean (beanKey ));
184
+ }
128
185
if (applicationContext .getParent () != null ) {
129
- return ObjectNameManager
130
- .getInstance (String .format (
131
- "%s:type=Endpoint,name=%s,context=%s,identity=%s" , domain ,
132
- beanKey ,
133
- ObjectUtils .getIdentityHexString (applicationContext ),
134
- ObjectUtils .getIdentityHexString (applicationContext
135
- .getBean (beanKey ))));
186
+ return ObjectNameManager .getInstance (String .format (name , domain , beanKey ,
187
+ ObjectUtils .getIdentityHexString (applicationContext )));
136
188
}
137
189
else {
138
- return ObjectNameManager
139
- .getInstance (String .format ("%s:type=Endpoint,name=%s,identity=%s" ,
140
- domain , beanKey , ObjectUtils
141
- .getIdentityHexString (applicationContext
142
- .getBean (beanKey ))));
190
+ return ObjectNameManager .getInstance (String .format (name , domain , beanKey ));
143
191
}
144
192
}
145
193
@@ -148,4 +196,43 @@ private ObjectName getObjectName(String domain, String beanKey,
148
196
public static class TestConfiguration {
149
197
150
198
}
199
+
200
+ @ Component
201
+ @ ManagedResource
202
+ protected static class ManagedEndpoint extends AbstractEndpoint <Boolean > {
203
+
204
+ public ManagedEndpoint () {
205
+ super ("managed" , true , true );
206
+ }
207
+
208
+ @ Override
209
+ public Boolean invoke () {
210
+ return true ;
211
+ }
212
+
213
+ }
214
+
215
+ @ Configuration
216
+ @ ManagedResource
217
+ protected static class NestedInManagedEndpoint {
218
+
219
+ @ Bean
220
+ public Endpoint <Boolean > nested () {
221
+ return new Nested ();
222
+ }
223
+
224
+ class Nested extends AbstractEndpoint <Boolean > {
225
+
226
+ public Nested () {
227
+ super ("managed" , true , true );
228
+ }
229
+
230
+ @ Override
231
+ public Boolean invoke () {
232
+ return true ;
233
+ }
234
+ }
235
+
236
+ }
237
+
151
238
}
0 commit comments