|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.endpoint.jmx; |
18 | 18 |
|
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
19 | 22 | import java.util.function.Function; |
20 | 23 |
|
21 | 24 | import javax.management.MBeanServer; |
| 25 | +import javax.management.ObjectName; |
22 | 26 |
|
23 | 27 | import org.junit.jupiter.api.Test; |
24 | 28 | import org.mockito.ArgumentCaptor; |
|
35 | 39 | import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
36 | 40 |
|
37 | 41 | import static org.assertj.core.api.Assertions.assertThat; |
| 42 | +import static org.mockito.ArgumentMatchers.any; |
| 43 | +import static org.mockito.BDDMockito.given; |
38 | 44 | import static org.mockito.BDDMockito.then; |
39 | 45 | import static org.mockito.Mockito.mock; |
| 46 | +import static org.mockito.Mockito.times; |
40 | 47 |
|
41 | 48 | /** |
42 | 49 | * Tests for {@link JmxEndpointAutoConfiguration}. |
@@ -78,6 +85,26 @@ void jmxEndpointWithCustomEndpointObjectNameFactory() { |
78 | 85 | }); |
79 | 86 | } |
80 | 87 |
|
| 88 | + @Test |
| 89 | + void jmxEndpointWithContextHierarchyGeneratesUniqueNamesForEachEndpoint() throws Exception { |
| 90 | + given(this.mBeanServer.queryNames(any(), any())) |
| 91 | + .willReturn(new HashSet<>(Arrays.asList(new ObjectName("test:test=test")))); |
| 92 | + ArgumentCaptor<ObjectName> objectName = ArgumentCaptor.forClass(ObjectName.class); |
| 93 | + this.contextRunner.withPropertyValues("spring.jmx.enabled=true").with(mockMBeanServer()).run((parent) -> { |
| 94 | + this.contextRunner.withPropertyValues("spring.jmx.enabled=true").withParent(parent).run((child) -> { |
| 95 | + }); |
| 96 | + this.contextRunner.withPropertyValues("spring.jmx.enabled=true").withParent(parent).run((child) -> { |
| 97 | + }); |
| 98 | + }); |
| 99 | + then(this.mBeanServer).should(times(3)).registerMBean(any(Object.class), objectName.capture()); |
| 100 | + Set<ObjectName> uniqueValues = new HashSet<>(objectName.getAllValues()); |
| 101 | + assertThat(uniqueValues).hasSize(3); |
| 102 | + assertThat(uniqueValues).allMatch((name) -> name.getDomain().equals("org.springframework.boot")); |
| 103 | + assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("type").equals("Endpoint")); |
| 104 | + assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("name").equals("Test")); |
| 105 | + assertThat(uniqueValues).allMatch((name) -> name.getKeyProperty("context") != null); |
| 106 | + } |
| 107 | + |
81 | 108 | private Function<ApplicationContextRunner, ApplicationContextRunner> mockMBeanServer() { |
82 | 109 | return (ctxRunner) -> ctxRunner.withBean("mbeanServer", MBeanServer.class, () -> this.mBeanServer); |
83 | 110 | } |
|
0 commit comments