@ -16,21 +16,33 @@
@@ -16,21 +16,33 @@
package org.springframework.boot.servlet.autoconfigure.actuate.web ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.function.Consumer ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.extension.ExtendWith ;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration ;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration ;
import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration ;
import org.springframework.boot.autoconfigure.AutoConfigurations ;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration ;
import org.springframework.boot.env.PropertySourceInfo ;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner ;
import org.springframework.boot.test.system.CapturedOutput ;
import org.springframework.boot.test.system.OutputCaptureExtension ;
import org.springframework.boot.tomcat.autoconfigure.actuate.web.server.TomcatServletManagementContextAutoConfiguration ;
import org.springframework.boot.tomcat.autoconfigure.servlet.TomcatServletWebServerAutoConfiguration ;
import org.springframework.boot.web.server.servlet.context.AnnotationConfigServletWebServerApplicationContext ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.context.annotation.Configuration ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.core.env.PropertySource ;
import org.springframework.core.env.StandardEnvironment ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.util.ClassUtils ;
import org.springframework.util.StringUtils ;
import static org.assertj.core.api.Assertions.assertThat ;
@ -100,6 +112,31 @@ class ServletManagementContextAutoConfigurationIntegrationTests {
@@ -100,6 +112,31 @@ class ServletManagementContextAutoConfigurationIntegrationTests {
. hasMessageStartingWith ( "Management-specific server address cannot be configured" ) ) ;
}
@Test // gh-45858
void childEnvironmentShouldInheritPrefix ( ) throws Exception {
SpringApplication application = new SpringApplication ( ChildEnvironmentConfiguration . class ) ;
Map < String , Object > properties = new LinkedHashMap < > ( ) ;
properties . put ( "server.port" , "0" ) ;
properties . put ( "management.server.port" , "0" ) ;
application . setDefaultProperties ( properties ) ;
application . setEnvironmentPrefix ( "my" ) ;
try ( ConfigurableApplicationContext parentContext = application . run ( ) ) {
Class < ? > initializerClass = ClassUtils . forName (
"org.springframework.boot.actuate.autoconfigure.web.server.ChildManagementContextInitializer" ,
null ) ;
Object initializer = parentContext . getBean ( initializerClass ) ;
ConfigurableApplicationContext managementContext = ( ConfigurableApplicationContext ) ReflectionTestUtils
. getField ( initializer , "managementContext" ) ;
assertThat ( managementContext ) . isNotNull ( ) ;
ConfigurableEnvironment managementEnvironment = managementContext . getEnvironment ( ) ;
assertThat ( managementEnvironment ) . isNotNull ( ) ;
PropertySource < ? > systemEnvironmentPropertySource = managementEnvironment . getPropertySources ( )
. get ( StandardEnvironment . SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ) ;
assertThat ( systemEnvironmentPropertySource ) . isNotNull ( ) ;
assertThat ( ( ( PropertySourceInfo ) systemEnvironmentPropertySource ) . getPrefix ( ) ) . isEqualTo ( "my" ) ;
}
}
private < T extends CharSequence > Consumer < T > numberOfOccurrences ( String substring , int expectedCount ) {
return ( charSequence ) - > {
int count = StringUtils . countOccurrencesOf ( charSequence . toString ( ) , substring ) ;
@ -107,4 +144,12 @@ class ServletManagementContextAutoConfigurationIntegrationTests {
@@ -107,4 +144,12 @@ class ServletManagementContextAutoConfigurationIntegrationTests {
} ;
}
@Configuration ( proxyBeanMethods = false )
@ImportAutoConfiguration ( { ManagementContextAutoConfiguration . class , TomcatServletWebServerAutoConfiguration . class ,
TomcatServletManagementContextAutoConfiguration . class , ServletManagementContextAutoConfiguration . class ,
WebEndpointAutoConfiguration . class , EndpointAutoConfiguration . class } )
static class ChildEnvironmentConfiguration {
}
}