Polish SpringApplicationAdminJmxAutoConfigurationTests
See gh-26416
This commit is contained in:
parent
f042dcf0e0
commit
b263f126fa
|
@ -36,13 +36,13 @@ import org.springframework.jmx.export.MBeanExporter;
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Nguyen Bao Sach
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
* @see SpringApplicationAdminMXBean
|
* @see SpringApplicationAdminMXBean
|
||||||
*/
|
*/
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@AutoConfigureAfter(JmxAutoConfiguration.class)
|
@AutoConfigureAfter(JmxAutoConfiguration.class)
|
||||||
@ConditionalOnProperty(prefix = "spring.application.admin", value = "enabled", havingValue = "true",
|
@ConditionalOnProperty(prefix = "spring.application.admin", value = "enabled", havingValue = "true")
|
||||||
matchIfMissing = false)
|
|
||||||
public class SpringApplicationAdminJmxAutoConfiguration {
|
public class SpringApplicationAdminJmxAutoConfiguration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -61,11 +61,8 @@ public class SpringApplicationAdminJmxAutoConfiguration {
|
||||||
public SpringApplicationAdminMXBeanRegistrar springApplicationAdminRegistrar(
|
public SpringApplicationAdminMXBeanRegistrar springApplicationAdminRegistrar(
|
||||||
ObjectProvider<MBeanExporter> mbeanExporters, Environment environment) throws MalformedObjectNameException {
|
ObjectProvider<MBeanExporter> mbeanExporters, Environment environment) throws MalformedObjectNameException {
|
||||||
String jmxName = environment.getProperty(JMX_NAME_PROPERTY, DEFAULT_JMX_NAME);
|
String jmxName = environment.getProperty(JMX_NAME_PROPERTY, DEFAULT_JMX_NAME);
|
||||||
if (mbeanExporters != null) { // Make sure to not register that MBean twice
|
// Make sure to not register that MBean twice
|
||||||
for (MBeanExporter mbeanExporter : mbeanExporters) {
|
mbeanExporters.forEach((mbeanExporter) -> mbeanExporter.addExcludedBean(jmxName));
|
||||||
mbeanExporter.addExcludedBean(jmxName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new SpringApplicationAdminMXBeanRegistrar(jmxName);
|
return new SpringApplicationAdminMXBeanRegistrar(jmxName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Nguyen Bao Sach
|
||||||
*/
|
*/
|
||||||
class SpringApplicationAdminJmxAutoConfigurationTests {
|
class SpringApplicationAdminJmxAutoConfigurationTests {
|
||||||
|
|
||||||
|
@ -60,17 +61,10 @@ class SpringApplicationAdminJmxAutoConfigurationTests {
|
||||||
private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
private final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
|
||||||
|
|
||||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
.withConfiguration(AutoConfigurations.of(MultipleMBeanExportersConfiguration.class,
|
.withConfiguration(AutoConfigurations.of(SpringApplicationAdminJmxAutoConfiguration.class));
|
||||||
SpringApplicationAdminJmxAutoConfiguration.class));
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void notRegisteredByDefault() {
|
void WhenThereAreNotAnyMBeanExporters() {
|
||||||
this.contextRunner.run((context) -> assertThatExceptionOfType(InstanceNotFoundException.class)
|
|
||||||
.isThrownBy(() -> this.server.getObjectInstance(createDefaultObjectName())));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void registeredWithProperty() {
|
|
||||||
this.contextRunner.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
this.contextRunner.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
||||||
ObjectName objectName = createDefaultObjectName();
|
ObjectName objectName = createDefaultObjectName();
|
||||||
ObjectInstance objectInstance = this.server.getObjectInstance(objectName);
|
ObjectInstance objectInstance = this.server.getObjectInstance(objectName);
|
||||||
|
@ -79,9 +73,27 @@ class SpringApplicationAdminJmxAutoConfigurationTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void registerWithCustomJmxName() {
|
void notRegisteredByDefaultWhenThereAreMultipleMBeanExporters() {
|
||||||
|
this.contextRunner.withUserConfiguration(MultipleMBeanExportersConfiguration.class)
|
||||||
|
.run((context) -> assertThatExceptionOfType(InstanceNotFoundException.class)
|
||||||
|
.isThrownBy(() -> this.server.getObjectInstance(createDefaultObjectName())));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void registeredWithPropertyWhenThereAreMultipleMBeanExporters() {
|
||||||
|
this.contextRunner.withUserConfiguration(MultipleMBeanExportersConfiguration.class)
|
||||||
|
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
||||||
|
ObjectName objectName = createDefaultObjectName();
|
||||||
|
ObjectInstance objectInstance = this.server.getObjectInstance(objectName);
|
||||||
|
assertThat(objectInstance).as("Lifecycle bean should have been registered").isNotNull();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void registerWithCustomJmxNameWhenThereAreMultipleMBeanExporters() {
|
||||||
String customJmxName = "org.acme:name=FooBar";
|
String customJmxName = "org.acme:name=FooBar";
|
||||||
this.contextRunner.withSystemProperties("spring.application.admin.jmx-name=" + customJmxName)
|
this.contextRunner.withUserConfiguration(MultipleMBeanExportersConfiguration.class)
|
||||||
|
.withSystemProperties("spring.application.admin.jmx-name=" + customJmxName)
|
||||||
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
.withPropertyValues(ENABLE_ADMIN_PROP).run((context) -> {
|
||||||
try {
|
try {
|
||||||
this.server.getObjectInstance(createObjectName(customJmxName));
|
this.server.getObjectInstance(createObjectName(customJmxName));
|
||||||
|
|
Loading…
Reference in New Issue