|
30 | 30 | import org.junit.Test;
|
31 | 31 | import org.junit.rules.ExpectedException;
|
32 | 32 |
|
| 33 | +import org.springframework.beans.factory.BeanFactoryUtils; |
| 34 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 35 | +import org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar; |
33 | 36 | import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
34 | 37 | import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration;
|
35 | 38 | import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration;
|
|
47 | 50 | * Tests for {@link SpringApplicationAdminJmxAutoConfiguration}.
|
48 | 51 | *
|
49 | 52 | * @author Stephane Nicoll
|
| 53 | + * @author Andy Wilkinson |
50 | 54 | */
|
51 | 55 | public class SpringApplicationAdminJmxAutoConfigurationTests {
|
52 | 56 |
|
@@ -130,6 +134,37 @@ public void registerWithSimpleWebApp() throws Exception {
|
130 | 134 | assertThat(actual).isEqualTo(String.valueOf(expected));
|
131 | 135 | }
|
132 | 136 |
|
| 137 | + @Test |
| 138 | + public void onlyRegisteredOnceWhenThereIsAChildContext() throws Exception { |
| 139 | + SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder().web(false) |
| 140 | + .sources(JmxAutoConfiguration.class, |
| 141 | + SpringApplicationAdminJmxAutoConfiguration.class); |
| 142 | + SpringApplicationBuilder childBuilder = parentBuilder |
| 143 | + .child(JmxAutoConfiguration.class, |
| 144 | + SpringApplicationAdminJmxAutoConfiguration.class) |
| 145 | + .web(false); |
| 146 | + ConfigurableApplicationContext parent = null; |
| 147 | + ConfigurableApplicationContext child = null; |
| 148 | + |
| 149 | + try { |
| 150 | + parent = parentBuilder.run("--" + ENABLE_ADMIN_PROP); |
| 151 | + child = childBuilder.run("--" + ENABLE_ADMIN_PROP); |
| 152 | + BeanFactoryUtils.beanOfType(parent.getBeanFactory(), |
| 153 | + SpringApplicationAdminMXBeanRegistrar.class); |
| 154 | + this.thrown.expect(NoSuchBeanDefinitionException.class); |
| 155 | + BeanFactoryUtils.beanOfType(child.getBeanFactory(), |
| 156 | + SpringApplicationAdminMXBeanRegistrar.class); |
| 157 | + } |
| 158 | + finally { |
| 159 | + if (parent != null) { |
| 160 | + parent.close(); |
| 161 | + } |
| 162 | + if (child != null) { |
| 163 | + child.close(); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
133 | 168 | private ObjectName createDefaultObjectName() {
|
134 | 169 | return createObjectName(DEFAULT_JMX_NAME);
|
135 | 170 | }
|
|
0 commit comments