@ -60,10 +60,8 @@ import org.springframework.boot.autoconfigure.web.EmbeddedServletContainerAutoCo
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration ;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration ;
import org.springframework.boot.autoconfigure.web.ServerProperties ;
import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfiguration ;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration ;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext ;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer ;
import org.springframework.boot.context.embedded.EmbeddedServletContainer ;
import org.springframework.boot.context.embedded.EmbeddedServletContainerException ;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory ;
@ -117,27 +115,20 @@ public class EndpointWebMvcAutoConfigurationTests {
@Rule
public ExpectedException thrown = ExpectedException . none ( ) ;
private final AnnotationConfigEmbeddedWebApplicationContext applicationContext = new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
private final AnnotationConfigEmbeddedWebApplicationContext applicationContext =
new AnnotationConfigEmbeddedWebApplicationContext ( ) ;
private static ThreadLocal < Ports > ports = new ThreadLocal < Ports > ( ) ;
private static ServerProperties server = new ServerProperties ( ) ;
private static ManagementServerProperties management = new ManagementServerProperties ( ) ;
@Before
public void defaultContextPath ( ) {
management . setContextPath ( "" ) ;
management . getSecurity ( ) . setEnabled ( false ) ;
server . setContextPath ( "" ) ;
}
@Before
public void grabPort s( ) {
public void setUp ( ) {
Ports values = new Ports ( ) ;
ports . set ( values ) ;
server . setPort ( values . server ) ;
management . setPort ( values . management ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.context-path=" ,
"management.security.enabled=false" ,
"server.context-path=" ,
"server.port=" + ports . get ( ) . server ) ;
}
@After
@ -149,10 +140,9 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onSamePort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.security.enabled : false") ;
"management.security.enabled = false") ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
BaseConfiguration . class , ServerPortConfig . class ,
EndpointWebMvcAutoConfiguration . class ) ;
BaseConfiguration . class , EndpointWebMvcAutoConfiguration . class ) ;
this . applicationContext . refresh ( ) ;
assertContent ( "/controller" , ports . get ( ) . server , "controlleroutput" ) ;
assertContent ( "/endpoint" , ports . get ( ) . server , "endpointoutput" ) ;
@ -169,8 +159,7 @@ public class EndpointWebMvcAutoConfigurationTests {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.add-application-context-header:false" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
BaseConfiguration . class , ServerPortConfig . class ,
EndpointWebMvcAutoConfiguration . class ) ;
BaseConfiguration . class , EndpointWebMvcAutoConfiguration . class ) ;
this . applicationContext . refresh ( ) ;
assertThat ( hasHeader ( "/endpoint" , ports . get ( ) . server , "X-Application-Context" ) )
. isFalse ( ) ;
@ -180,6 +169,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
@ -198,6 +189,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortWithSpecificContainer ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( SpecificContainerConfig . class , RootConfig . class ,
DifferentPortConfig . class , EndpointConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
@ -225,10 +218,12 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortAndContext ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ,
"management.context-path=/admin" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
management . setContextPath ( "/admin" ) ;
this . applicationContext . refresh ( ) ;
assertContent ( "/controller" , ports . get ( ) . server , "controlleroutput" ) ;
assertContent ( "/admin/endpoint" , ports . get ( ) . management , "endpointoutput" ) ;
@ -237,11 +232,13 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortAndMainContext ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"server.context-path=/spring" ,
"management.port=" + ports . get ( ) . management ,
"management.context-path=/admin" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
management . setContextPath ( "/admin" ) ;
server . setContextPath ( "/spring" ) ;
this . applicationContext . refresh ( ) ;
assertContent ( "/spring/controller" , ports . get ( ) . server , "controlleroutput" ) ;
assertContent ( "/admin/endpoint" , ports . get ( ) . management , "endpointoutput" ) ;
@ -250,6 +247,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortWithoutErrorMvcAutoConfiguration ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class ) ;
@ -259,6 +258,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortInServletContainer ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
@ -275,9 +276,11 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onRandomPort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=0" , "management.security.enabled=false" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
RandomPortConfig . class , BaseConfiguration . class ,
E ndpointWebMvcAutoConfiguration. class , E rrorMvcAutoConfiguration. class ) ;
BaseConfiguration. class , EndpointWebMvcAuto Configuration. class ,
E rrorMvcAutoConfiguration. class ) ;
GrabManagementPort grabManagementPort = new GrabManagementPort (
this . applicationContext ) ;
this . applicationContext . addApplicationListener ( grabManagementPort ) ;
@ -292,6 +295,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void onDifferentPortWithPrimaryFailure ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
@ -307,9 +312,10 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void disabled ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=-1" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
DisableConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class ) ;
BaseConfiguration . class , EndpointWebMvcAutoConfiguration . class ) ;
this . applicationContext . refresh ( ) ;
assertContent ( "/controller" , ports . get ( ) . server , "controlleroutput" ) ;
assertContent ( "/endpoint" , ports . get ( ) . server , null ) ;
@ -358,9 +364,8 @@ public class EndpointWebMvcAutoConfigurationTests {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.contextPath:/test" , "management.security.enabled:false" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
ServerPortConfig . class , PropertyPlaceholderAutoConfiguration . class ,
ManagementServerPropertiesAutoConfiguration . class ,
ServerPropertiesAutoConfiguration . class , JacksonAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
JacksonAutoConfiguration . class ,
EmbeddedServletContainerAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
DispatcherServletAutoConfiguration . class , WebMvcAutoConfiguration . class ,
@ -375,9 +380,8 @@ public class EndpointWebMvcAutoConfigurationTests {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"server.displayName:foo" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
ServerPortConfig . class , PropertyPlaceholderAutoConfiguration . class ,
ManagementServerPropertiesAutoConfiguration . class ,
ServerPropertiesAutoConfiguration . class , JacksonAutoConfiguration . class ,
PropertyPlaceholderAutoConfiguration . class ,
JacksonAutoConfiguration . class ,
EmbeddedServletContainerAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
DispatcherServletAutoConfiguration . class , WebMvcAutoConfiguration . class ,
@ -392,7 +396,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void portPropertiesOnSamePort ( ) throws Exception {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
new ServerPortInfoApplicationContextInitializer ( )
. initialize ( this . applicationContext ) ;
this . applicationContext . refresh ( ) ;
@ -407,6 +411,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void portPropertiesOnDifferentPort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
new ServerPortInfoApplicationContextInitializer ( )
. initialize ( this . applicationContext ) ;
this . applicationContext . register ( RootConfig . class , DifferentPortConfig . class ,
@ -420,14 +426,12 @@ public class EndpointWebMvcAutoConfigurationTests {
assertThat ( localServerPort ) . isNotNull ( ) ;
assertThat ( localManagementPort ) . isNotNull ( ) ;
assertThat ( localServerPort ) . isNotEqualTo ( localManagementPort ) ;
assertThat ( this . applicationContext . getBean ( ServerPortConfig . class ) . getCount ( ) )
. isEqualTo ( 2 ) ;
}
@Test
public void singleRequestMappingInfoHandlerMappingBean ( ) throws Exception {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
this . applicationContext . refresh ( ) ;
RequestMappingInfoHandlerMapping mapping = this . applicationContext
. getBean ( RequestMappingInfoHandlerMapping . class ) ;
@ -437,8 +441,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void endpointsDefaultConfiguration ( ) throws Exception {
this . applicationContext . register ( LoggingConfig . class , RootConfig . class ,
BaseConfiguration . class , ServerPortConfig . class ,
EndpointWebMvcAutoConfiguration . class ) ;
BaseConfiguration . class , EndpointWebMvcAutoConfiguration . class ) ;
this . applicationContext . refresh ( ) ;
// /health, /metrics, /loggers, /env, /actuator, /heapdump, /auditevents
// (/shutdown is disabled by default)
@ -448,7 +451,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void endpointsAllDisabled ( ) throws Exception {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"ENDPOINTS_ENABLED:false" ) ;
this . applicationContext . refresh ( ) ;
@ -498,7 +501,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void shutdownEndpointEnabled ( ) {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"endpoints.shutdown.enabled:true" ) ;
this . applicationContext . refresh ( ) ;
@ -509,7 +512,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void actuatorEndpointEnabledIndividually ( ) {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"endpoints.enabled:false" , "endpoints.actuator.enabled:true" ) ;
this . applicationContext . refresh ( ) ;
@ -520,6 +523,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void managementSpecificSslUsingDifferentPort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ,
"management.ssl.enabled=true" ,
"management.ssl.key-store=classpath:test.jks" ,
"management.ssl.key-password=password" ) ;
@ -551,7 +555,7 @@ public class EndpointWebMvcAutoConfigurationTests {
"management.ssl.key-password=password" ) ;
this . applicationContext . register ( RootConfig . class , EndpointConfig . class ,
BaseConfiguration . class , EndpointWebMvcAutoConfiguration . class ,
ErrorMvcAutoConfiguration . class , ServerPortConfig . class );
ErrorMvcAutoConfiguration . class );
this . thrown . expect ( IllegalStateException . class ) ;
this . thrown . expectMessage ( "Management-specific SSL cannot be configured as the "
+ "management server is not listening on a separate port" ) ;
@ -561,6 +565,7 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void managementServerCanDisableSslWhenUsingADifferentPort ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ,
"server.ssl.enabled=true" , "server.ssl.key-store=classpath:test.jks" ,
"server.ssl.key-password=password" , "management.ssl.enabled=false" ) ;
@ -586,6 +591,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void tomcatManagementAccessLogUsesCustomPrefix ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ) ;
this . applicationContext . register ( TomcatContainerConfig . class , RootConfig . class ,
EndpointConfig . class , DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
@ -606,11 +613,12 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void undertowManagementAccessLogUsesCustomPrefix ( ) throws Exception {
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"management.port=" + ports . get ( ) . management ,
"server.undertow.accesslog.enabled: true" ) ;
this . applicationContext . register ( UndertowContainerConfig . class , RootConfig . class ,
EndpointConfig . class , DifferentPortConfig . class , BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class , ErrorMvcAutoConfiguration . class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"server.undertow.accesslog.enabled: true" ) ;
this . applicationContext . refresh ( ) ;
ApplicationContext managementContext = this . applicationContext
. getBean ( ManagementContextResolver . class ) . getApplicationContext ( ) ;
@ -634,7 +642,7 @@ public class EndpointWebMvcAutoConfigurationTests {
private void endpointDisabled ( String name , Class < ? extends MvcEndpoint > type ) {
this . applicationContext . register ( RootConfig . class , BaseConfiguration . class ,
ServerPortConfig. class , EndpointWebMvcAutoConfiguration. class ) ;
EndpointWebMvcAutoConfiguration. class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
String . format ( "endpoints.%s.enabled:false" , name ) ) ;
this . applicationContext . refresh ( ) ;
@ -644,7 +652,7 @@ public class EndpointWebMvcAutoConfigurationTests {
private void endpointEnabledOverride ( String name , Class < ? extends MvcEndpoint > type )
throws Exception {
this . applicationContext . register ( LoggingConfig . class , RootConfig . class ,
BaseConfiguration . class , ServerPortConfig . class ,
BaseConfiguration . class ,
EndpointWebMvcAutoConfiguration . class ) ;
EnvironmentTestUtils . addEnvironment ( this . applicationContext ,
"endpoints.enabled:false" ,
@ -721,9 +729,9 @@ public class EndpointWebMvcAutoConfigurationTests {
private static class Ports {
int server = SocketUtils . findAvailableTcpPort ( ) ;
final int server = SocketUtils . findAvailableTcpPort ( ) ;
int management = SocketUtils . findAvailableTcpPort ( ) ;
final int management = SocketUtils . findAvailableTcpPort ( ) ;
}
@ -733,8 +741,7 @@ public class EndpointWebMvcAutoConfigurationTests {
JacksonAutoConfiguration . class , EndpointAutoConfiguration . class ,
HttpMessageConvertersAutoConfiguration . class ,
DispatcherServletAutoConfiguration . class , WebMvcAutoConfiguration . class ,
ManagementServerPropertiesAutoConfiguration . class ,
ServerPropertiesAutoConfiguration . class , AuditAutoConfiguration . class } )
AuditAutoConfiguration . class } )
protected static class BaseConfiguration {
}
@ -769,31 +776,6 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Configuration
public static class ServerPortConfig {
private int count = 0 ;
public int getCount ( ) {
return this . count ;
}
@Bean
public ServerProperties serverProperties ( ) {
ServerProperties properties = new ServerProperties ( ) {
@Override
public void customize ( ConfigurableEmbeddedServletContainer container ) {
ServerPortConfig . this . count + + ;
super . customize ( container ) ;
}
} ;
properties . setPort ( server . getPort ( ) ) ;
properties . setContextPath ( server . getContextPath ( ) ) ;
return properties ;
}
}
@Controller
public static class TestController {
@ -836,14 +818,8 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Configuration
@Import ( ServerPortConfig . class )
public static class DifferentPortConfig {
@Bean
public ManagementServerProperties managementServerProperties ( ) {
return management ;
}
@Bean
public EndpointHandlerMappingCustomizer mappingCustomizer ( ) {
return new EndpointHandlerMappingCustomizer ( ) {
@ -880,33 +856,6 @@ public class EndpointWebMvcAutoConfigurationTests {
}
@Configuration
@Import ( ServerPortConfig . class )
public static class RandomPortConfig {
@Bean
public ManagementServerProperties managementServerProperties ( ) {
ManagementServerProperties properties = new ManagementServerProperties ( ) ;
properties . setPort ( 0 ) ;
properties . getSecurity ( ) . setEnabled ( false ) ;
return properties ;
}
}
@Configuration
@Import ( ServerPortConfig . class )
public static class DisableConfig {
@Bean
public ManagementServerProperties managementServerProperties ( ) {
ManagementServerProperties properties = new ManagementServerProperties ( ) ;
properties . setPort ( - 1 ) ;
return properties ;
}
}
public static class TestEndpoint implements MvcEndpoint {
@RequestMapping