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 ;
@@ -177,8 +178,10 @@ void handlerMappingsCreated() {
177
178
void resourceHandlerMapping () {
178
179
this .contextRunner .run ((context ) -> {
179
180
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
180
- assertThat (locations .get ("/**" )).hasSize (2 );
181
- assertThat (locations .get ("/webjars/**" )).hasSize (0 );
181
+ assertThat (locations .get ("/**" )).hasSize (5 );
182
+ assertThat (locations .get ("/webjars/**" )).hasSize (1 );
183
+ assertThat (locations .get ("/webjars/**" ).get (0 ))
184
+ .isEqualTo (new ClassPathResource ("/META-INF/resources/webjars/" ));
182
185
assertThat (getResourceResolvers (context , "/webjars/**" )).hasSize (1 );
183
186
assertThat (getResourceTransformers (context , "/webjars/**" )).hasSize (0 );
184
187
assertThat (getResourceResolvers (context , "/**" )).hasSize (1 );
@@ -190,17 +193,17 @@ void resourceHandlerMapping() {
190
193
void customResourceHandlerMapping () {
191
194
this .contextRunner .withPropertyValues ("spring.mvc.static-path-pattern:/static/**" ).run ((context ) -> {
192
195
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
193
- assertThat (locations .get ("/static/**" )).hasSize (2 );
196
+ assertThat (locations .get ("/static/**" )).hasSize (5 );
194
197
assertThat (getResourceResolvers (context , "/static/**" )).hasSize (1 );
195
198
});
196
199
}
197
200
198
201
@ Test
199
202
void resourceHandlerMappingOverrideWebjars () {
200
- this .contextRunner .withUserConfiguration (WebJarsResources .class ).run ((context ) -> {
203
+ this .contextRunner .withUserConfiguration (WebJars .class ).run ((context ) -> {
201
204
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
202
205
assertThat (locations .get ("/webjars/**" )).hasSize (1 );
203
- assertThat (locations .get ("/webjars/**" ).get (0 ). getFilename ()) .isEqualTo ("test" );
206
+ assertThat (locations .get ("/webjars/**" ).get (0 )) .isEqualTo (new ClassPathResource ( "/foo/" ) );
204
207
});
205
208
}
206
209
@@ -209,7 +212,7 @@ void resourceHandlerMappingOverrideAll() {
209
212
this .contextRunner .withUserConfiguration (AllResources .class ).run ((context ) -> {
210
213
Map <String , List <Resource >> locations = getResourceMappingLocations (context );
211
214
assertThat (locations .get ("/**" )).hasSize (1 );
212
- assertThat (locations .get ("/**" ).get (0 ). getFilename ()) .isEqualTo ("test" );
215
+ assertThat (locations .get ("/**" ).get (0 )) .isEqualTo (new ClassPathResource ( "/foo/" ) );
213
216
});
214
217
}
215
218
@@ -1040,7 +1043,7 @@ private void assertResourceHttpRequestHandler(AssertableWebApplicationContext co
1040
1043
protected Map <String , List <Resource >> getResourceMappingLocations (ApplicationContext context ) {
1041
1044
Object bean = context .getBean ("resourceHandlerMapping" );
1042
1045
if (bean instanceof HandlerMapping ) {
1043
- return getMappingLocations ((HandlerMapping ) bean );
1046
+ return getMappingLocations (context , (HandlerMapping ) bean );
1044
1047
}
1045
1048
assertThat (bean .toString ()).isEqualTo ("null" );
1046
1049
return Collections .emptyMap ();
@@ -1059,11 +1062,18 @@ protected List<ResourceTransformer> getResourceTransformers(ApplicationContext c
1059
1062
}
1060
1063
1061
1064
@ SuppressWarnings ("unchecked" )
1062
- protected Map <String , List <Resource >> getMappingLocations (HandlerMapping mapping ) {
1065
+ private Map <String , List <Resource >> getMappingLocations (ApplicationContext context , HandlerMapping mapping ) {
1063
1066
Map <String , List <Resource >> mappingLocations = new LinkedHashMap <>();
1064
1067
getHandlerMap (mapping ).forEach ((key , value ) -> {
1065
- Object locations = ReflectionTestUtils .getField (value , "locationsToUse" );
1066
- mappingLocations .put (key , (List <Resource >) locations );
1068
+ List <String > locationValues = (List <String >) ReflectionTestUtils .getField (value , "locationValues" );
1069
+ List <Resource > locationResources = (List <Resource >) ReflectionTestUtils .getField (value ,
1070
+ "locationResources" );
1071
+ List <Resource > resources = new ArrayList <>();
1072
+ for (String locationValue : locationValues ) {
1073
+ resources .add (context .getResource (locationValue ));
1074
+ }
1075
+ resources .addAll (locationResources );
1076
+ mappingLocations .put (key , resources );
1067
1077
});
1068
1078
return mappingLocations ;
1069
1079
}
@@ -1094,11 +1104,11 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
1094
1104
}
1095
1105
1096
1106
@ Configuration (proxyBeanMethods = false )
1097
- static class WebJarsResources implements WebMvcConfigurer {
1107
+ static class WebJars implements WebMvcConfigurer {
1098
1108
1099
1109
@ Override
1100
1110
public void addResourceHandlers (ResourceHandlerRegistry registry ) {
1101
- registry .addResourceHandler ("/webjars/**" ).addResourceLocations (new ClassPathResource ( "/test" , getClass ()) );
1111
+ registry .addResourceHandler ("/webjars/**" ).addResourceLocations ("classpath:/foo/" );
1102
1112
}
1103
1113
1104
1114
}
@@ -1108,7 +1118,7 @@ static class AllResources implements WebMvcConfigurer {
1108
1118
1109
1119
@ Override
1110
1120
public void addResourceHandlers (ResourceHandlerRegistry registry ) {
1111
- registry .addResourceHandler ("/**" ).addResourceLocations (new ClassPathResource ( "/test" , getClass ()) );
1121
+ registry .addResourceHandler ("/**" ).addResourceLocations ("classpath:/foo/" );
1112
1122
}
1113
1123
1114
1124
}
0 commit comments