|
16 | 16 |
|
17 | 17 | package org.springframework.boot.servlet.autoconfigure.actuate.web; |
18 | 18 |
|
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
19 | 21 | import java.util.function.Consumer; |
20 | 22 |
|
21 | 23 | import org.junit.jupiter.api.Test; |
22 | 24 | import org.junit.jupiter.api.extension.ExtendWith; |
23 | 25 |
|
| 26 | +import org.springframework.boot.SpringApplication; |
24 | 27 | import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; |
25 | 28 | import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; |
26 | 29 | import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; |
27 | 30 | import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 31 | +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; |
| 32 | +import org.springframework.boot.env.PropertySourceInfo; |
28 | 33 | import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
29 | 34 | import org.springframework.boot.test.system.CapturedOutput; |
30 | 35 | import org.springframework.boot.test.system.OutputCaptureExtension; |
31 | 36 | import org.springframework.boot.tomcat.autoconfigure.actuate.web.server.TomcatServletManagementContextAutoConfiguration; |
32 | 37 | import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration; |
33 | 38 | import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext; |
| 39 | +import org.springframework.context.ConfigurableApplicationContext; |
| 40 | +import org.springframework.context.annotation.Configuration; |
| 41 | +import org.springframework.core.env.ConfigurableEnvironment; |
| 42 | +import org.springframework.core.env.PropertySource; |
| 43 | +import org.springframework.core.env.StandardEnvironment; |
| 44 | +import org.springframework.test.util.ReflectionTestUtils; |
| 45 | +import org.springframework.util.ClassUtils; |
34 | 46 | import org.springframework.util.StringUtils; |
35 | 47 |
|
36 | 48 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -100,11 +112,44 @@ void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenCon |
100 | 112 | .hasMessageStartingWith("Management-specific server address cannot be configured")); |
101 | 113 | } |
102 | 114 |
|
| 115 | + @Test // gh-45858 |
| 116 | + void childEnvironmentShouldInheritPrefix() throws Exception { |
| 117 | + SpringApplication application = new SpringApplication(ChildEnvironmentConfiguration.class); |
| 118 | + Map<String, Object> properties = new LinkedHashMap<>(); |
| 119 | + properties.put("server.port", "0"); |
| 120 | + properties.put("management.server.port", "0"); |
| 121 | + application.setDefaultProperties(properties); |
| 122 | + application.setEnvironmentPrefix("my"); |
| 123 | + try (ConfigurableApplicationContext parentContext = application.run()) { |
| 124 | + Class<?> initializerClass = ClassUtils.forName( |
| 125 | + "org.springframework.boot.actuate.autoconfigure.web.server.ChildManagementContextInitializer", |
| 126 | + null); |
| 127 | + Object initializer = parentContext.getBean(initializerClass); |
| 128 | + ConfigurableApplicationContext managementContext = (ConfigurableApplicationContext) ReflectionTestUtils |
| 129 | + .getField(initializer, "managementContext"); |
| 130 | + assertThat(managementContext).isNotNull(); |
| 131 | + ConfigurableEnvironment managementEnvironment = managementContext.getEnvironment(); |
| 132 | + assertThat(managementEnvironment).isNotNull(); |
| 133 | + PropertySource<?> systemEnvironmentPropertySource = managementEnvironment.getPropertySources() |
| 134 | + .get(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME); |
| 135 | + assertThat(systemEnvironmentPropertySource).isNotNull(); |
| 136 | + assertThat(((PropertySourceInfo) systemEnvironmentPropertySource).getPrefix()).isEqualTo("my"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
103 | 140 | private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) { |
104 | 141 | return (charSequence) -> { |
105 | 142 | int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring); |
106 | 143 | assertThat(count).isEqualTo(expectedCount); |
107 | 144 | }; |
108 | 145 | } |
109 | 146 |
|
| 147 | + @Configuration(proxyBeanMethods = false) |
| 148 | + @ImportAutoConfiguration({ ManagementContextAutoConfiguration.class, TomcatServletWebServerAutoConfiguration.class, |
| 149 | + TomcatServletManagementContextAutoConfiguration.class, ServletManagementContextAutoConfiguration.class, |
| 150 | + WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class }) |
| 151 | + static class ChildEnvironmentConfiguration { |
| 152 | + |
| 153 | + } |
| 154 | + |
110 | 155 | } |
0 commit comments