mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Merge branch '2.7.x'
Closes gh-31805
This commit is contained in:
commit
18d3d4de5d
@ -43,6 +43,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@ -88,7 +89,7 @@ public class JmxEndpointAutoConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(EndpointObjectNameFactory.class)
|
||||
@ConditionalOnMissingBean(value = EndpointObjectNameFactory.class, search = SearchStrategy.CURRENT)
|
||||
public DefaultEndpointObjectNameFactory endpointObjectNameFactory(MBeanServer mBeanServer) {
|
||||
String contextId = ObjectUtils.getIdentityHexString(this.applicationContext);
|
||||
return new DefaultEndpointObjectNameFactory(this.properties, this.jmxProperties, mBeanServer, contextId);
|
||||
|
@ -16,9 +16,13 @@
|
||||
|
||||
package org.springframework.boot.actuate.autoconfigure.endpoint.jmx;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import javax.management.MBeanServer;
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@ -35,8 +39,11 @@ import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
|
||||
/**
|
||||
* Tests for {@link JmxEndpointAutoConfiguration}.
|
||||
@ -78,6 +85,26 @@ class JmxEndpointAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void jmxEndpointWithContextHierarchyGeneratesUniqueNamesForEachEndpoint() throws Exception {
|
||||
given(this.mBeanServer.queryNames(any(), any()))
|
||||
.willReturn(new HashSet<>(Arrays.asList(new ObjectName("test:test=test"))));
|
||||
ArgumentCaptor<ObjectName> objectName = ArgumentCaptor.forClass(ObjectName.class);
|
||||
this.contextRunner.withPropertyValues("spring.jmx.enabled=true").with(mockMBeanServer()).run((parent) -> {
|
||||
this.contextRunner.withPropertyValues("spring.jmx.enabled=true").withParent(parent).run((child) -> {
|
||||
});
|
||||
this.contextRunner.withPropertyValues("spring.jmx.enabled=true").withParent(parent).run((child) -> {
|
||||
});
|
||||
});
|
||||
then(this.mBeanServer).should(times(3)).registerMBean(any(Object.class), objectName.capture());
|
||||
Set<ObjectName> uniqueValues = new HashSet<>(objectName.getAllValues());
|
||||
assertThat(uniqueValues).hasSize(3);
|
||||
assertThat(uniqueValues).allMatch((name) -> name.getDomain().equals("org.springframework.boot"));
|
||||
assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("type").equals("Endpoint"));
|
||||
assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("name").equals("Test"));
|
||||
assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("context") != null);
|
||||
}
|
||||
|
||||
private Function<ApplicationContextRunner, ApplicationContextRunner> mockMBeanServer() {
|
||||
return (ctxRunner) -> ctxRunner.withBean("mbeanServer", MBeanServer.class, () -> this.mBeanServer);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user