Skip to content

Commit 1688bec

Browse files
committed
Avoid questionable use of ClassPathResource#getPath() in tests
Prior to this commit, several tests used ClassPathResource#getPath() based on the knowledge that the ClassPathResource had been created using the ClassPathResource(String,Class) constructor. However, making such an assumption seems ill advised in light of the abstraction that ClassPathResource provides. In light of that, this commit avoids questionable use of ClassPathResource#getPath() in tests by refactoring those tests to use the proper abstractions provided by ClassPathResource.
1 parent 4cc91e4 commit 1688bec

3 files changed

Lines changed: 27 additions & 33 deletions

File tree

spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.io.InputStreamReader;
2323
import java.io.StringWriter;
2424
import java.lang.reflect.Method;
25-
import java.net.URISyntaxException;
26-
import java.net.URL;
2725
import java.util.Map;
2826
import java.util.concurrent.atomic.AtomicInteger;
2927

@@ -173,7 +171,7 @@ void innerBeans() throws IOException {
173171
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
174172

175173
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
176-
try (InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath())) {
174+
try (InputStream inputStream = REFTYPES_CONTEXT.getInputStream()) {
177175
reader.loadBeanDefinitions(new InputSource(inputStream));
178176
}
179177

@@ -1172,21 +1170,19 @@ void classPathResourceWithImport() {
11721170
}
11731171

11741172
@Test
1175-
void urlResourceWithImport() {
1176-
URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
1173+
void urlResourceWithImport() throws Exception {
11771174
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
1178-
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
1175+
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(RESOURCE_CONTEXT.getURL()));
11791176
// comes from "resourceImport.xml"
11801177
xbf.getBean("resource1", ResourceTestBean.class);
11811178
// comes from "resource.xml"
11821179
xbf.getBean("resource2", ResourceTestBean.class);
11831180
}
11841181

11851182
@Test
1186-
void fileSystemResourceWithImport() throws URISyntaxException {
1187-
String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
1183+
void fileSystemResourceWithImport() throws Exception {
11881184
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
1189-
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
1185+
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(RESOURCE_CONTEXT.getFile()));
11901186
// comes from "resourceImport.xml"
11911187
xbf.getBean("resource1", ResourceTestBean.class);
11921188
// comes from "resource.xml"

spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,19 @@ class ResourceWebHandlerTests {
7777

7878
private static final Duration TIMEOUT = Duration.ofSeconds(1);
7979

80+
private final ClassPathResource testResource = new ClassPathResource("test/", getClass());
81+
private final ClassPathResource testAlternatePathResource = new ClassPathResource("testalternatepath/", getClass());
82+
private final ClassPathResource webjarsResource = new ClassPathResource("META-INF/resources/webjars/");
83+
8084
private ResourceWebHandler handler;
8185

8286

8387
@BeforeEach
8488
void setup() throws Exception {
8589
List<Resource> locations = List.of(
86-
new ClassPathResource("test/", getClass()),
87-
new ClassPathResource("testalternatepath/", getClass()),
88-
new ClassPathResource("META-INF/resources/webjars/"));
90+
this.testResource,
91+
this.testAlternatePathResource,
92+
this.webjarsResource);
8993

9094
this.handler = new ResourceWebHandler();
9195
this.handler.setLocations(locations);
@@ -420,10 +424,7 @@ void initAllowedLocations() {
420424
PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0);
421425
Resource[] locations = resolver.getAllowedLocations();
422426

423-
assertThat(locations.length).isEqualTo(3);
424-
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
425-
assertThat(((ClassPathResource) locations[1]).getPath()).isEqualTo("testalternatepath/");
426-
assertThat(((ClassPathResource) locations[2]).getPath()).isEqualTo("META-INF/resources/webjars/");
427+
assertThat(locations).containsExactly(this.testResource, this.testAlternatePathResource, this.webjarsResource);
427428
}
428429

429430
@Test
@@ -439,9 +440,7 @@ void initAllowedLocationsWithExplicitConfiguration() throws Exception {
439440
handler.setLocations(Arrays.asList(location1, location2));
440441
handler.afterPropertiesSet();
441442

442-
Resource[] locations = pathResolver.getAllowedLocations();
443-
assertThat(locations.length).isEqualTo(1);
444-
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
443+
assertThat(pathResolver.getAllowedLocations()).containsExactly(location1);
445444
}
446445

447446
@Test

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -17,7 +17,6 @@
1717
package org.springframework.web.servlet.resource;
1818

1919
import java.io.IOException;
20-
import java.util.ArrayList;
2120
import java.util.Arrays;
2221
import java.util.Collections;
2322
import java.util.List;
@@ -59,10 +58,15 @@
5958
* @author Jeremy Grelle
6059
* @author Rossen Stoyanchev
6160
* @author Brian Clozel
61+
* @author Sam Brannen
6262
*/
6363
@ExtendWith(GzipSupport.class)
6464
public class ResourceHttpRequestHandlerTests {
6565

66+
private final ClassPathResource testResource = new ClassPathResource("test/", getClass());
67+
private final ClassPathResource testAlternatePathResource = new ClassPathResource("testalternatepath/", getClass());
68+
private final ClassPathResource webjarsResource = new ClassPathResource("META-INF/resources/webjars/");
69+
6670
private ResourceHttpRequestHandler handler;
6771

6872
private MockHttpServletRequest request;
@@ -72,15 +76,15 @@ public class ResourceHttpRequestHandlerTests {
7276

7377
@BeforeEach
7478
public void setup() throws Exception {
75-
List<Resource> paths = new ArrayList<>(2);
76-
paths.add(new ClassPathResource("test/", getClass()));
77-
paths.add(new ClassPathResource("testalternatepath/", getClass()));
78-
paths.add(new ClassPathResource("META-INF/resources/webjars/"));
79+
List<Resource> locations = List.of(
80+
this.testResource,
81+
this.testAlternatePathResource,
82+
this.webjarsResource);
7983

8084
TestServletContext servletContext = new TestServletContext();
8185

8286
this.handler = new ResourceHttpRequestHandler();
83-
this.handler.setLocations(paths);
87+
this.handler.setLocations(locations);
8488
this.handler.setCacheSeconds(3600);
8589
this.handler.setServletContext(servletContext);
8690
this.handler.afterPropertiesSet();
@@ -467,10 +471,7 @@ public void initAllowedLocations() {
467471
PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0);
468472
Resource[] locations = resolver.getAllowedLocations();
469473

470-
assertThat(locations.length).isEqualTo(3);
471-
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
472-
assertThat(((ClassPathResource) locations[1]).getPath()).isEqualTo("testalternatepath/");
473-
assertThat(((ClassPathResource) locations[2]).getPath()).isEqualTo("META-INF/resources/webjars/");
474+
assertThat(locations).containsExactly(this.testResource, this.testAlternatePathResource, this.webjarsResource);
474475
}
475476

476477
@Test
@@ -487,9 +488,7 @@ public void initAllowedLocationsWithExplicitConfiguration() throws Exception {
487488
handler.setLocations(Arrays.asList(location1, location2));
488489
handler.afterPropertiesSet();
489490

490-
Resource[] locations = pathResolver.getAllowedLocations();
491-
assertThat(locations.length).isEqualTo(1);
492-
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
491+
assertThat(pathResolver.getAllowedLocations()).containsExactly(location1);
493492
}
494493

495494
@Test

0 commit comments

Comments
 (0)