22
22
import java .time .ZonedDateTime ;
23
23
import java .time .format .DateTimeFormatter ;
24
24
import java .time .format .FormatStyle ;
25
+ import java .util .ArrayList ;
25
26
import java .util .Collections ;
26
27
import java .util .Date ;
27
28
import java .util .LinkedHashMap ;
@@ -175,8 +176,10 @@ void handlerMappingsCreated() {
175
176
void resourceHandlerMapping () {
176
177
this .contextRunner .run ((context ) -> {
177
178
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
178
- assertThat (locations .get ("/**" )).hasSize (2 );
179
- assertThat (locations .get ("/webjars/**" )).hasSize (0 );
179
+ assertThat (locations .get ("/**" )).hasSize (5 );
180
+ assertThat (locations .get ("/webjars/**" )).hasSize (1 );
181
+ assertThat (locations .get ("/webjars/**" ).get (0 ))
182
+ .isEqualTo (new ClassPathResource ("/META-INF/resources/webjars/" ));
180
183
assertThat (getResourceResolvers (context , "/webjars/**" )).hasSize (1 );
181
184
assertThat (getResourceTransformers (context , "/webjars/**" )).hasSize (0 );
182
185
assertThat (getResourceResolvers (context , "/**" )).hasSize (1 );
@@ -188,17 +191,17 @@ void resourceHandlerMapping() {
188
191
void customResourceHandlerMapping () {
189
192
this .contextRunner .withPropertyValues ("spring.mvc.static-path-pattern:/static/**" ).run ((context ) -> {
190
193
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
191
- assertThat (locations .get ("/static/**" )).hasSize (2 );
194
+ assertThat (locations .get ("/static/**" )).hasSize (5 );
192
195
assertThat (getResourceResolvers (context , "/static/**" )).hasSize (1 );
193
196
});
194
197
}
195
198
196
199
@ Test
197
200
void resourceHandlerMappingOverrideWebjars () {
198
- this .contextRunner .withUserConfiguration (WebJarsResources .class ).run ((context ) -> {
201
+ this .contextRunner .withUserConfiguration (WebJars .class ).run ((context ) -> {
199
202
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
200
203
assertThat (locations .get ("/webjars/**" )).hasSize (1 );
201
- assertThat (locations .get ("/webjars/**" ).get (0 ). getFilename ()) .isEqualTo ("test" );
204
+ assertThat (locations .get ("/webjars/**" ).get (0 )) .isEqualTo (new ClassPathResource ( "/foo/" ) );
202
205
});
203
206
}
204
207
@@ -207,7 +210,7 @@ void resourceHandlerMappingOverrideAll() {
207
210
this .contextRunner .withUserConfiguration (AllResources .class ).run ((context ) -> {
208
211
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
209
212
assertThat (locations .get ("/**" )).hasSize (1 );
210
- assertThat (locations .get ("/**" ).get (0 ). getFilename ()) .isEqualTo ("test" );
213
+ assertThat (locations .get ("/**" ).get (0 )) .isEqualTo (new ClassPathResource ( "/foo/" ) );
211
214
});
212
215
}
213
216
@@ -1024,7 +1027,7 @@ private void assertResourceHttpRequestHandler(AssertableWebApplicationContext co
1024
1027
protected Map <String , List <Resource >> getResourceMappingLocations (ApplicationContext context ) {
1025
1028
Object bean = context .getBean ("resourceHandlerMapping" );
1026
1029
if (bean instanceof HandlerMapping ) {
1027
- return getMappingLocations ((HandlerMapping ) bean );
1030
+ return getMappingLocations (context , (HandlerMapping ) bean );
1028
1031
}
1029
1032
assertThat (bean .toString ()).isEqualTo ("null" );
1030
1033
return Collections .emptyMap ();
@@ -1043,11 +1046,18 @@ protected List<ResourceTransformer> getResourceTransformers(ApplicationContext c
1043
1046
}
1044
1047
1045
1048
@ SuppressWarnings ("unchecked" )
1046
- protected Map <String , List <Resource >> getMappingLocations (HandlerMapping mapping ) {
1049
+ private Map <String , List <Resource >> getMappingLocations (ApplicationContext context , HandlerMapping mapping ) {
1047
1050
Map <String , List <Resource >> mappingLocations = new LinkedHashMap <>();
1048
1051
getHandlerMap (mapping ).forEach ((key , value ) -> {
1049
- Object locations = ReflectionTestUtils .getField (value , "locationsToUse" );
1050
- mappingLocations .put (key , (List <Resource >) locations );
1052
+ List <String > locationValues = (List <String >) ReflectionTestUtils .getField (value , "locationValues" );
1053
+ List <Resource > locationResources = (List <Resource >) ReflectionTestUtils .getField (value ,
1054
+ "locationResources" );
1055
+ List <Resource > resources = new ArrayList <>();
1056
+ for (String locationValue : locationValues ) {
1057
+ resources .add (context .getResource (locationValue ));
1058
+ }
1059
+ resources .addAll (locationResources );
1060
+ mappingLocations .put (key , resources );
1051
1061
});
1052
1062
return mappingLocations ;
1053
1063
}
@@ -1078,11 +1088,11 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
1078
1088
}
1079
1089
1080
1090
@ Configuration (proxyBeanMethods = false )
1081
- static class WebJarsResources implements WebMvcConfigurer {
1091
+ static class WebJars implements WebMvcConfigurer {
1082
1092
1083
1093
@ Override
1084
1094
public void addResourceHandlers (ResourceHandlerRegistry registry ) {
1085
- registry .addResourceHandler ("/webjars/**" ).addResourceLocations (new ClassPathResource ( "/test" , getClass ()) );
1095
+ registry .addResourceHandler ("/webjars/**" ).addResourceLocations ("classpath:/foo/" );
1086
1096
}
1087
1097
1088
1098
}
@@ -1092,7 +1102,7 @@ static class AllResources implements WebMvcConfigurer {
1092
1102
1093
1103
@ Override
1094
1104
public void addResourceHandlers (ResourceHandlerRegistry registry ) {
1095
- registry .addResourceHandler ("/**" ).addResourceLocations (new ClassPathResource ( "/test" , getClass ()) );
1105
+ registry .addResourceHandler ("/**" ).addResourceLocations ("classpath:/foo/" );
1096
1106
}
1097
1107
1098
1108
}
0 commit comments