Commit ee083acf authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #26416 from nguyensach

* pr/26416:
  Polish "Polish SpringApplicationAdminJmxAutoConfigurationTests"
  Polish SpringApplicationAdminJmxAutoConfigurationTests

Closes gh-26416
parents f042dcf0 0e2bb5f1
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2021 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -50,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.fail; ...@@ -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 { ...@@ -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 notRegisteredWhenThereAreNoMBeanExporter() {
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 { ...@@ -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));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment