|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2015 the original author or authors. |
| 2 | + * Copyright 2012-2016 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.
|
|
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;
|
|
49 | 52 | * Tests for {@link SpringApplicationAdminJmxAutoConfiguration}.
|
50 | 53 | *
|
51 | 54 | * @author Stephane Nicoll
|
| 55 | + * @author Andy Wilkinson |
52 | 56 | */
|
53 | 57 | public class SpringApplicationAdminJmxAutoConfigurationTests {
|
54 | 58 |
|
@@ -131,6 +135,37 @@ public void registerWithSimpleWebApp() throws Exception {
|
131 | 135 | assertEquals(String.valueOf(expected), actual);
|
132 | 136 | }
|
133 | 137 |
|
| 138 | + @Test |
| 139 | + public void onlyRegisteredOnceWhenThereIsAChildContext() throws Exception { |
| 140 | + SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder().web(false) |
| 141 | + .sources(JmxAutoConfiguration.class, |
| 142 | + SpringApplicationAdminJmxAutoConfiguration.class); |
| 143 | + SpringApplicationBuilder childBuilder = parentBuilder |
| 144 | + .child(JmxAutoConfiguration.class, |
| 145 | + SpringApplicationAdminJmxAutoConfiguration.class) |
| 146 | + .web(false); |
| 147 | + ConfigurableApplicationContext parent = null; |
| 148 | + ConfigurableApplicationContext child = null; |
| 149 | + |
| 150 | + try { |
| 151 | + parent = parentBuilder.run("--" + ENABLE_ADMIN_PROP); |
| 152 | + child = childBuilder.run("--" + ENABLE_ADMIN_PROP); |
| 153 | + BeanFactoryUtils.beanOfType(parent.getBeanFactory(), |
| 154 | + SpringApplicationAdminMXBeanRegistrar.class); |
| 155 | + this.thrown.expect(NoSuchBeanDefinitionException.class); |
| 156 | + BeanFactoryUtils.beanOfType(child.getBeanFactory(), |
| 157 | + SpringApplicationAdminMXBeanRegistrar.class); |
| 158 | + } |
| 159 | + finally { |
| 160 | + if (parent != null) { |
| 161 | + parent.close(); |
| 162 | + } |
| 163 | + if (child != null) { |
| 164 | + child.close(); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
134 | 169 | private ObjectName createDefaultObjectName() {
|
135 | 170 | return createObjectName(DEFAULT_JMX_NAME);
|
136 | 171 | }
|
|
0 commit comments