@ -41,6 +41,7 @@ import org.springframework.boot.actuate.web.mappings.servlet.ServletRegistration
@@ -41,6 +41,7 @@ import org.springframework.boot.actuate.web.mappings.servlet.ServletRegistration
import org.springframework.boot.actuate.web.mappings.servlet.ServletsMappingDescriptionProvider ;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner ;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner ;
import org.springframework.boot.web.servlet.ServletRegistrationBean ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
@ -67,28 +68,14 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
@@ -67,28 +68,14 @@ import static org.springframework.web.reactive.function.server.RouterFunctions.r
* Tests for { @link MappingsEndpoint } .
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* /
public class MappingsEndpointTests {
@Test
@SuppressWarnings ( "unchecked" )
public void servletWebMappings ( ) {
ServletContext servletContext = mock ( ServletContext . class ) ;
given ( servletContext . getInitParameterNames ( ) )
. willReturn ( Collections . emptyEnumeration ( ) ) ;
given ( servletContext . getAttributeNames ( ) )
. willReturn ( Collections . emptyEnumeration ( ) ) ;
FilterRegistration filterRegistration = mock ( FilterRegistration . class ) ;
given ( ( Map < String , FilterRegistration > ) servletContext . getFilterRegistrations ( ) )
. willReturn ( Collections . singletonMap ( "testFilter" , filterRegistration ) ) ;
ServletRegistration servletRegistration = mock ( ServletRegistration . class ) ;
given ( ( Map < String , ServletRegistration > ) servletContext . getServletRegistrations ( ) )
. willReturn ( Collections . singletonMap ( "testServlet" , servletRegistration ) ) ;
Supplier < ConfigurableWebApplicationContext > contextSupplier = ( ) - > {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
context . setServletContext ( servletContext ) ;
return context ;
} ;
Supplier < ConfigurableWebApplicationContext > contextSupplier = prepareContextSupplier ( ) ;
new WebApplicationContextRunner ( contextSupplier )
. withUserConfiguration ( EndpointConfiguration . class ,
ServletWebConfiguration . class )
@ -112,6 +99,47 @@ public class MappingsEndpointTests {
@@ -112,6 +99,47 @@ public class MappingsEndpointTests {
} ) ;
}
@Test
@SuppressWarnings ( "unchecked" )
public void servletWebMappingsWithAdditionalDispatcherServlets ( ) {
Supplier < ConfigurableWebApplicationContext > contextSupplier = prepareContextSupplier ( ) ;
new WebApplicationContextRunner ( contextSupplier ) . withUserConfiguration (
EndpointConfiguration . class , ServletWebConfiguration . class ,
CustomDispatcherServletConfiguration . class ) . run ( ( context ) - > {
ContextMappings contextMappings = contextMappings ( context ) ;
Map < String , List < DispatcherServletMappingDescription > > dispatcherServlets = mappings (
contextMappings , "dispatcherServlets" ) ;
assertThat ( dispatcherServlets ) . containsOnlyKeys ( "dispatcherServlet" ,
"customDispatcherServletRegistration" ,
"anotherDispatcherServletRegistration" ) ;
assertThat ( dispatcherServlets . get ( "dispatcherServlet" ) ) . hasSize ( 1 ) ;
assertThat (
dispatcherServlets . get ( "customDispatcherServletRegistration" ) )
. hasSize ( 1 ) ;
assertThat ( dispatcherServlets
. get ( "anotherDispatcherServletRegistration" ) ) . hasSize ( 1 ) ;
} ) ;
}
private Supplier < ConfigurableWebApplicationContext > prepareContextSupplier ( ) {
ServletContext servletContext = mock ( ServletContext . class ) ;
given ( servletContext . getInitParameterNames ( ) )
. willReturn ( Collections . emptyEnumeration ( ) ) ;
given ( servletContext . getAttributeNames ( ) )
. willReturn ( Collections . emptyEnumeration ( ) ) ;
FilterRegistration filterRegistration = mock ( FilterRegistration . class ) ;
given ( ( Map < String , FilterRegistration > ) servletContext . getFilterRegistrations ( ) )
. willReturn ( Collections . singletonMap ( "testFilter" , filterRegistration ) ) ;
ServletRegistration servletRegistration = mock ( ServletRegistration . class ) ;
given ( ( Map < String , ServletRegistration > ) servletContext . getServletRegistrations ( ) )
. willReturn ( Collections . singletonMap ( "testServlet" , servletRegistration ) ) ;
return ( ) - > {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
context . setServletContext ( servletContext ) ;
return context ;
} ;
}
@Test
public void reactiveWebMappings ( ) {
new ReactiveWebApplicationContextRunner ( )
@ -213,4 +241,44 @@ public class MappingsEndpointTests {
@@ -213,4 +241,44 @@ public class MappingsEndpointTests {
}
@Configuration
static class CustomDispatcherServletConfiguration {
@Bean
public ServletRegistrationBean < DispatcherServlet > customDispatcherServletRegistration (
WebApplicationContext context ) {
ServletRegistrationBean < DispatcherServlet > registration = new ServletRegistrationBean < > (
createTestDispatcherServlet ( context ) ) ;
registration . setName ( "customDispatcherServletRegistration" ) ;
return registration ;
}
@Bean
public DispatcherServlet anotherDispatcherServlet ( WebApplicationContext context ) {
return createTestDispatcherServlet ( context ) ;
}
@Bean
public ServletRegistrationBean < DispatcherServlet > anotherDispatcherServletRegistration (
WebApplicationContext context ) {
ServletRegistrationBean < DispatcherServlet > registrationBean = new ServletRegistrationBean < > (
anotherDispatcherServlet ( context ) ) ;
registrationBean . setName ( "anotherDispatcherServletRegistration" ) ;
return registrationBean ;
}
private DispatcherServlet createTestDispatcherServlet (
WebApplicationContext context ) {
try {
DispatcherServlet dispatcherServlet = new DispatcherServlet ( context ) ;
dispatcherServlet . init ( new MockServletConfig ( ) ) ;
return dispatcherServlet ;
}
catch ( ServletException ex ) {
throw new IllegalStateException ( ex ) ;
}
}
}
}