@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2019 the original author or authors .
* Copyright 2002 - 202 1 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -44,6 +44,7 @@ import static org.mockito.Mockito.mock;
@@ -44,6 +44,7 @@ import static org.mockito.Mockito.mock;
*
* @author Jeremy Grelle
* @author Rossen Stoyanchev
* @author Brian Clozel
* /
public class ResourceUrlProviderTests {
@ -57,7 +58,7 @@ public class ResourceUrlProviderTests {
@@ -57,7 +58,7 @@ public class ResourceUrlProviderTests {
@BeforeEach
public void setUp ( ) throws Exception {
void setUp ( ) throws Exception {
this . locations . add ( new ClassPathResource ( "test/" , getClass ( ) ) ) ;
this . locations . add ( new ClassPathResource ( "testalternatepath/" , getClass ( ) ) ) ;
this . handler . setServletContext ( new MockServletContext ( ) ) ;
@ -69,13 +70,13 @@ public class ResourceUrlProviderTests {
@@ -69,13 +70,13 @@ public class ResourceUrlProviderTests {
@Test
public void getStaticResourceUrl ( ) {
void getStaticResourceUrl ( ) {
String url = this . urlProvider . getForLookupPath ( "/resources/foo.css" ) ;
assertThat ( url ) . isEqualTo ( "/resources/foo.css" ) ;
}
@Test // SPR-13374
public void getStaticResourceUrlRequestWithQueryOrHash ( ) {
void getStaticResourceUrlRequestWithQueryOrHash ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . setContextPath ( "/" ) ;
request . setRequestURI ( "/" ) ;
@ -90,7 +91,7 @@ public class ResourceUrlProviderTests {
@@ -90,7 +91,7 @@ public class ResourceUrlProviderTests {
}
@Test // SPR-16526
public void getStaticResourceWithMissingContextPath ( ) {
void getStaticResourceWithMissingContextPath ( ) {
MockHttpServletRequest request = new MockHttpServletRequest ( ) ;
request . setContextPath ( "/contextpath-longer-than-request-path" ) ;
request . setRequestURI ( "/contextpath-longer-than-request-path/style.css" ) ;
@ -100,7 +101,7 @@ public class ResourceUrlProviderTests {
@@ -100,7 +101,7 @@ public class ResourceUrlProviderTests {
}
@Test
public void getFingerprintedResourceUrl ( ) {
void getFingerprintedResourceUrl ( ) {
Map < String , VersionStrategy > versionStrategyMap = new HashMap < > ( ) ;
versionStrategyMap . put ( "/**" , new ContentVersionStrategy ( ) ) ;
VersionResourceResolver versionResolver = new VersionResourceResolver ( ) ;
@ -116,7 +117,7 @@ public class ResourceUrlProviderTests {
@@ -116,7 +117,7 @@ public class ResourceUrlProviderTests {
}
@Test // SPR-12647
public void bestPatternMatch ( ) throws Exception {
void bestPatternMatch ( ) throws Exception {
ResourceHttpRequestHandler otherHandler = new ResourceHttpRequestHandler ( ) ;
otherHandler . setLocations ( this . locations ) ;
Map < String , VersionStrategy > versionStrategyMap = new HashMap < > ( ) ;
@ -138,7 +139,7 @@ public class ResourceUrlProviderTests {
@@ -138,7 +139,7 @@ public class ResourceUrlProviderTests {
@Test // SPR-12592
@SuppressWarnings ( "resource" )
public void initializeOnce ( ) throws Exception {
void initializeOnce ( ) throws Exception {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
context . setServletContext ( new MockServletContext ( ) ) ;
context . register ( HandlerMappingConfiguration . class ) ;
@ -149,8 +150,30 @@ public class ResourceUrlProviderTests {
@@ -149,8 +150,30 @@ public class ResourceUrlProviderTests {
assertThat ( urlProviderBean . isAutodetect ( ) ) . isFalse ( ) ;
}
@Test
void initializeOnCurrentContext ( ) {
AnnotationConfigWebApplicationContext parentContext = new AnnotationConfigWebApplicationContext ( ) ;
parentContext . setServletContext ( new MockServletContext ( ) ) ;
parentContext . register ( ParentHandlerMappingConfiguration . class ) ;
AnnotationConfigWebApplicationContext childContext = new AnnotationConfigWebApplicationContext ( ) ;
childContext . setParent ( parentContext ) ;
childContext . setServletContext ( new MockServletContext ( ) ) ;
childContext . register ( HandlerMappingConfiguration . class ) ;
parentContext . refresh ( ) ;
childContext . refresh ( ) ;
ResourceUrlProvider parentUrlProvider = parentContext . getBean ( ResourceUrlProvider . class ) ;
assertThat ( parentUrlProvider . getHandlerMap ( ) ) . isEmpty ( ) ;
assertThat ( parentUrlProvider . isAutodetect ( ) ) . isTrue ( ) ;
ResourceUrlProvider childUrlProvider = childContext . getBean ( ResourceUrlProvider . class ) ;
assertThat ( childUrlProvider . getHandlerMap ( ) ) . containsOnlyKeys ( "/resources/**" ) ;
assertThat ( childUrlProvider . isAutodetect ( ) ) . isFalse ( ) ;
}
@Test // SPR-16296
public void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes ( ) {
void getForLookupPathShouldNotFailIfPathContainsDoubleSlashes ( ) {
// given
ResourceResolver mockResourceResolver = mock ( ResourceResolver . class ) ;
given ( mockResourceResolver . resolveUrlPath ( any ( ) , any ( ) , any ( ) ) ) . willReturn ( "some-path" ) ;
@ -185,4 +208,14 @@ public class ResourceUrlProviderTests {
@@ -185,4 +208,14 @@ public class ResourceUrlProviderTests {
}
}
@Configuration
@SuppressWarnings ( { "unused" , "WeakerAccess" } )
static class ParentHandlerMappingConfiguration {
@Bean
public ResourceUrlProvider resourceUrlProvider ( ) {
return new ResourceUrlProvider ( ) ;
}
}
}