Skip to content

Commit 9abf9e6

Browse files
committed
Modify more tests for static resource location filtering
Fix `WebFluxAutoConfigurationTests` following upstream Spring Framework changes. Also refine `WebMvcAutoConfigurationTests` to check the locations are set even if they are filtered. See gh-28223
1 parent 3d21ac7 commit 9abf9e6

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,6 @@
5050
import org.springframework.core.Ordered;
5151
import org.springframework.core.annotation.Order;
5252
import org.springframework.core.convert.ConversionService;
53-
import org.springframework.core.io.ClassPathResource;
5453
import org.springframework.format.Parser;
5554
import org.springframework.format.Printer;
5655
import org.springframework.format.support.FormattingConversionService;
@@ -151,12 +150,13 @@ void shouldRegisterResourceHandlerMapping() {
151150
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
152151
assertThat(hm.getUrlMap().get("/**")).isInstanceOf(ResourceWebHandler.class);
153152
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/**");
154-
assertThat(staticHandler.getLocations()).hasSize(4);
153+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
154+
assertThat(staticHandler.getLocations()).hasSize(1);
155+
assertThat(staticHandler.getLocations().get(0)).hasToString("class path resource [public/]");
155156
assertThat(hm.getUrlMap().get("/webjars/**")).isInstanceOf(ResourceWebHandler.class);
156157
ResourceWebHandler webjarsHandler = (ResourceWebHandler) hm.getUrlMap().get("/webjars/**");
157-
assertThat(webjarsHandler.getLocations()).hasSize(1);
158-
assertThat(webjarsHandler.getLocations().get(0))
159-
.isEqualTo(new ClassPathResource("/META-INF/resources/webjars/"));
158+
assertThat(webjarsHandler).extracting("locationValues").asList()
159+
.containsExactly("classpath:/META-INF/resources/webjars/");
160160
});
161161
}
162162

@@ -166,7 +166,7 @@ void shouldMapResourcesToCustomPath() {
166166
SimpleUrlHandlerMapping hm = context.getBean("resourceHandlerMapping", SimpleUrlHandlerMapping.class);
167167
assertThat(hm.getUrlMap().get("/static/**")).isInstanceOf(ResourceWebHandler.class);
168168
ResourceWebHandler staticHandler = (ResourceWebHandler) hm.getUrlMap().get("/static/**");
169-
assertThat(staticHandler.getLocations()).hasSize(4);
169+
assertThat(staticHandler).extracting("locationValues").asList().hasSize(4);
170170
});
171171
}
172172

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.time.ZonedDateTime;
2323
import java.time.format.DateTimeFormatter;
2424
import java.time.format.FormatStyle;
25+
import java.util.ArrayList;
2526
import java.util.Collections;
2627
import java.util.Date;
2728
import java.util.LinkedHashMap;
@@ -177,8 +178,10 @@ void handlerMappingsCreated() {
177178
void resourceHandlerMapping() {
178179
this.contextRunner.run((context) -> {
179180
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/"));
182185
assertThat(getResourceResolvers(context, "/webjars/**")).hasSize(1);
183186
assertThat(getResourceTransformers(context, "/webjars/**")).hasSize(0);
184187
assertThat(getResourceResolvers(context, "/**")).hasSize(1);
@@ -190,17 +193,17 @@ void resourceHandlerMapping() {
190193
void customResourceHandlerMapping() {
191194
this.contextRunner.withPropertyValues("spring.mvc.static-path-pattern:/static/**").run((context) -> {
192195
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
193-
assertThat(locations.get("/static/**")).hasSize(2);
196+
assertThat(locations.get("/static/**")).hasSize(5);
194197
assertThat(getResourceResolvers(context, "/static/**")).hasSize(1);
195198
});
196199
}
197200

198201
@Test
199202
void resourceHandlerMappingOverrideWebjars() {
200-
this.contextRunner.withUserConfiguration(WebJarsResources.class).run((context) -> {
203+
this.contextRunner.withUserConfiguration(WebJars.class).run((context) -> {
201204
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
202205
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/"));
204207
});
205208
}
206209

@@ -209,7 +212,7 @@ void resourceHandlerMappingOverrideAll() {
209212
this.contextRunner.withUserConfiguration(AllResources.class).run((context) -> {
210213
Map<String, List<Resource>> locations = getResourceMappingLocations(context);
211214
assertThat(locations.get("/**")).hasSize(1);
212-
assertThat(locations.get("/**").get(0).getFilename()).isEqualTo("test");
215+
assertThat(locations.get("/**").get(0)).isEqualTo(new ClassPathResource("/foo/"));
213216
});
214217
}
215218

@@ -1040,7 +1043,7 @@ private void assertResourceHttpRequestHandler(AssertableWebApplicationContext co
10401043
protected Map<String, List<Resource>> getResourceMappingLocations(ApplicationContext context) {
10411044
Object bean = context.getBean("resourceHandlerMapping");
10421045
if (bean instanceof HandlerMapping) {
1043-
return getMappingLocations((HandlerMapping) bean);
1046+
return getMappingLocations(context, (HandlerMapping) bean);
10441047
}
10451048
assertThat(bean.toString()).isEqualTo("null");
10461049
return Collections.emptyMap();
@@ -1059,11 +1062,18 @@ protected List<ResourceTransformer> getResourceTransformers(ApplicationContext c
10591062
}
10601063

10611064
@SuppressWarnings("unchecked")
1062-
protected Map<String, List<Resource>> getMappingLocations(HandlerMapping mapping) {
1065+
private Map<String, List<Resource>> getMappingLocations(ApplicationContext context, HandlerMapping mapping) {
10631066
Map<String, List<Resource>> mappingLocations = new LinkedHashMap<>();
10641067
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);
10671077
});
10681078
return mappingLocations;
10691079
}
@@ -1094,11 +1104,11 @@ protected void renderMergedOutputModel(Map<String, Object> model, HttpServletReq
10941104
}
10951105

10961106
@Configuration(proxyBeanMethods = false)
1097-
static class WebJarsResources implements WebMvcConfigurer {
1107+
static class WebJars implements WebMvcConfigurer {
10981108

10991109
@Override
11001110
public void addResourceHandlers(ResourceHandlerRegistry registry) {
1101-
registry.addResourceHandler("/webjars/**").addResourceLocations(new ClassPathResource("/test", getClass()));
1111+
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/foo/");
11021112
}
11031113

11041114
}
@@ -1108,7 +1118,7 @@ static class AllResources implements WebMvcConfigurer {
11081118

11091119
@Override
11101120
public void addResourceHandlers(ResourceHandlerRegistry registry) {
1111-
registry.addResourceHandler("/**").addResourceLocations(new ClassPathResource("/test", getClass()));
1121+
registry.addResourceHandler("/**").addResourceLocations("classpath:/foo/");
11121122
}
11131123

11141124
}

0 commit comments

Comments
 (0)