24
24
import javax .management .ObjectInstance ;
25
25
import javax .management .ObjectName ;
26
26
27
- import org .junit .After ;
28
- import org .junit .Before ;
29
27
import org .junit .Rule ;
30
28
import org .junit .Test ;
31
29
import org .junit .rules .ExpectedException ;
34
32
import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
35
33
import org .springframework .boot .WebApplicationType ;
36
34
import org .springframework .boot .admin .SpringApplicationAdminMXBeanRegistrar ;
35
+ import org .springframework .boot .autoconfigure .AutoConfigurations ;
37
36
import org .springframework .boot .autoconfigure .jmx .JmxAutoConfiguration ;
38
37
import org .springframework .boot .autoconfigure .web .servlet .DispatcherServletAutoConfiguration ;
39
38
import org .springframework .boot .autoconfigure .web .servlet .ServletWebServerFactoryAutoConfiguration ;
40
39
import org .springframework .boot .builder .SpringApplicationBuilder ;
41
- import org .springframework .boot .test .util . TestPropertyValues ;
40
+ import org .springframework .boot .test .context . runner . ApplicationContextRunner ;
42
41
import org .springframework .boot .web .servlet .context .ServletWebServerApplicationContext ;
43
42
import org .springframework .context .ConfigurableApplicationContext ;
44
- import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
45
43
46
44
import static org .assertj .core .api .Assertions .assertThat ;
47
45
import static org .junit .Assert .fail ;
@@ -56,52 +54,42 @@ public class SpringApplicationAdminJmxAutoConfigurationTests {
56
54
57
55
private static final String ENABLE_ADMIN_PROP = "spring.application.admin.enabled=true" ;
58
56
59
- private static final String JMX_NAME_PROPERTY = "spring.application.admin.jmx-name" ;
60
-
61
57
private static final String DEFAULT_JMX_NAME = "org.springframework.boot:type=Admin,name=SpringApplication" ;
62
58
63
59
@ Rule
64
60
public final ExpectedException thrown = ExpectedException .none ();
65
61
66
- private ConfigurableApplicationContext context ;
67
-
68
- private MBeanServer mBeanServer ;
62
+ private final MBeanServer mBeanServer = ManagementFactory .getPlatformMBeanServer ();
69
63
70
- @ Before
71
- public void setup () throws MalformedObjectNameException {
72
- this .mBeanServer = ManagementFactory .getPlatformMBeanServer ();
73
- }
74
-
75
- @ After
76
- public void tearDown () {
77
- if (this .context != null ) {
78
- this .context .close ();
79
- }
80
- }
64
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner ()
65
+ .withConfiguration (AutoConfigurations .of (JmxAutoConfiguration .class ,
66
+ SpringApplicationAdminJmxAutoConfiguration .class ));
81
67
82
68
@ Test
83
69
public void notRegisteredByDefault ()
84
70
throws MalformedObjectNameException , InstanceNotFoundException {
85
- load ();
86
- this .thrown .expect (InstanceNotFoundException .class );
87
- this .mBeanServer .getObjectInstance (createDefaultObjectName ());
71
+ this .contextRunner .run ((context ) -> {
72
+ this .thrown .expect (InstanceNotFoundException .class );
73
+ this .mBeanServer .getObjectInstance (createDefaultObjectName ());
74
+ });
88
75
}
89
76
90
77
@ Test
91
78
public void registeredWithProperty () throws Exception {
92
- load (ENABLE_ADMIN_PROP );
93
- ObjectName objectName = createDefaultObjectName ();
94
- ObjectInstance objectInstance = this .mBeanServer .getObjectInstance (objectName );
95
- assertThat (objectInstance ).as ("Lifecycle bean should have been registered" )
96
- .isNotNull ();
79
+ this .contextRunner .withPropertyValues (ENABLE_ADMIN_PROP ).run ((context ) -> {
80
+ ObjectName objectName = createDefaultObjectName ();
81
+ ObjectInstance objectInstance = this .mBeanServer .getObjectInstance (objectName );
82
+ assertThat (objectInstance ).as ("Lifecycle bean should have been registered" )
83
+ .isNotNull ();
84
+ });
97
85
}
98
86
99
87
@ Test
100
88
public void registerWithCustomJmxName () throws InstanceNotFoundException {
101
89
String customJmxName = "org.acme:name=FooBar" ;
102
- System . setProperty ( JMX_NAME_PROPERTY , customJmxName );
103
- try {
104
- load (ENABLE_ADMIN_PROP );
90
+ this . contextRunner . withSystemProperties (
91
+ "spring.application.admin.jmx-name=" + customJmxName )
92
+ . withPropertyValues (ENABLE_ADMIN_PROP ). run (( context ) -> {
105
93
try {
106
94
this .mBeanServer .getObjectInstance (createObjectName (customJmxName ));
107
95
}
@@ -110,27 +98,25 @@ public void registerWithCustomJmxName() throws InstanceNotFoundException {
110
98
}
111
99
this .thrown .expect (InstanceNotFoundException .class ); // Should not be exposed
112
100
this .mBeanServer .getObjectInstance (createDefaultObjectName ());
113
- }
114
- finally {
115
- System .clearProperty (JMX_NAME_PROPERTY );
116
- }
101
+ });
117
102
}
118
103
119
104
@ Test
120
105
public void registerWithSimpleWebApp () throws Exception {
121
- this . context = new SpringApplicationBuilder ()
106
+ try ( ConfigurableApplicationContext context = new SpringApplicationBuilder ()
122
107
.sources (ServletWebServerFactoryAutoConfiguration .class ,
123
108
DispatcherServletAutoConfiguration .class ,
124
109
JmxAutoConfiguration .class ,
125
110
SpringApplicationAdminJmxAutoConfiguration .class )
126
- .run ("--" + ENABLE_ADMIN_PROP , "--server.port=0" );
127
- assertThat (this .context ).isInstanceOf (ServletWebServerApplicationContext .class );
128
- assertThat (this .mBeanServer .getAttribute (createDefaultObjectName (),
129
- "EmbeddedWebApplication" )).isEqualTo (Boolean .TRUE );
130
- int expected = ((ServletWebServerApplicationContext ) this .context ).getWebServer ()
131
- .getPort ();
132
- String actual = getProperty (createDefaultObjectName (), "local.server.port" );
133
- assertThat (actual ).isEqualTo (String .valueOf (expected ));
111
+ .run ("--" + ENABLE_ADMIN_PROP , "--server.port=0" )) {
112
+ assertThat (context ).isInstanceOf (ServletWebServerApplicationContext .class );
113
+ assertThat (this .mBeanServer .getAttribute (createDefaultObjectName (),
114
+ "EmbeddedWebApplication" )).isEqualTo (Boolean .TRUE );
115
+ int expected = ((ServletWebServerApplicationContext ) context ).getWebServer ()
116
+ .getPort ();
117
+ String actual = getProperty (createDefaultObjectName (), "local.server.port" );
118
+ assertThat (actual ).isEqualTo (String .valueOf (expected ));
119
+ }
134
120
}
135
121
136
122
@ Test
@@ -145,8 +131,8 @@ public void onlyRegisteredOnceWhenThereIsAChildContext() throws Exception {
145
131
146
132
try (ConfigurableApplicationContext parent = parentBuilder
147
133
.run ("--" + ENABLE_ADMIN_PROP );
148
- ConfigurableApplicationContext child = childBuilder
149
- .run ("--" + ENABLE_ADMIN_PROP )) {
134
+ ConfigurableApplicationContext child = childBuilder
135
+ .run ("--" + ENABLE_ADMIN_PROP )) {
150
136
BeanFactoryUtils .beanOfType (parent .getBeanFactory (),
151
137
SpringApplicationAdminMXBeanRegistrar .class );
152
138
this .thrown .expect (NoSuchBeanDefinitionException .class );
@@ -173,13 +159,4 @@ private String getProperty(ObjectName objectName, String key) throws Exception {
173
159
new Object [] { key }, new String [] { String .class .getName () });
174
160
}
175
161
176
- private void load (String ... environment ) {
177
- AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext ();
178
- TestPropertyValues .of (environment ).applyTo (applicationContext );
179
- applicationContext .register (JmxAutoConfiguration .class ,
180
- SpringApplicationAdminJmxAutoConfiguration .class );
181
- applicationContext .refresh ();
182
- this .context = applicationContext ;
183
- }
184
-
185
162
}
0 commit comments