Browse Source

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.
pull/29130/head
Sam Brannen 3 years ago
parent
commit
1688becd73
  1. 14
      spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java
  2. 19
      spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java
  3. 27
      spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java

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

@ -22,8 +22,6 @@ import java.io.InputStream; @@ -22,8 +22,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@ -173,7 +171,7 @@ class XmlBeanFactoryTests { @@ -173,7 +171,7 @@ class XmlBeanFactoryTests {
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
try (InputStream inputStream = getClass().getResourceAsStream(REFTYPES_CONTEXT.getPath())) {
try (InputStream inputStream = REFTYPES_CONTEXT.getInputStream()) {
reader.loadBeanDefinitions(new InputSource(inputStream));
}
@ -1172,10 +1170,9 @@ class XmlBeanFactoryTests { @@ -1172,10 +1170,9 @@ class XmlBeanFactoryTests {
}
@Test
void urlResourceWithImport() {
URL url = getClass().getResource(RESOURCE_CONTEXT.getPath());
void urlResourceWithImport() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(url));
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new UrlResource(RESOURCE_CONTEXT.getURL()));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"
@ -1183,10 +1180,9 @@ class XmlBeanFactoryTests { @@ -1183,10 +1180,9 @@ class XmlBeanFactoryTests {
}
@Test
void fileSystemResourceWithImport() throws URISyntaxException {
String file = getClass().getResource(RESOURCE_CONTEXT.getPath()).toURI().getPath();
void fileSystemResourceWithImport() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(file));
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new FileSystemResource(RESOURCE_CONTEXT.getFile()));
// comes from "resourceImport.xml"
xbf.getBean("resource1", ResourceTestBean.class);
// comes from "resource.xml"

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

@ -77,15 +77,19 @@ class ResourceWebHandlerTests { @@ -77,15 +77,19 @@ class ResourceWebHandlerTests {
private static final Duration TIMEOUT = Duration.ofSeconds(1);
private final ClassPathResource testResource = new ClassPathResource("test/", getClass());
private final ClassPathResource testAlternatePathResource = new ClassPathResource("testalternatepath/", getClass());
private final ClassPathResource webjarsResource = new ClassPathResource("META-INF/resources/webjars/");
private ResourceWebHandler handler;
@BeforeEach
void setup() throws Exception {
List<Resource> locations = List.of(
new ClassPathResource("test/", getClass()),
new ClassPathResource("testalternatepath/", getClass()),
new ClassPathResource("META-INF/resources/webjars/"));
this.testResource,
this.testAlternatePathResource,
this.webjarsResource);
this.handler = new ResourceWebHandler();
this.handler.setLocations(locations);
@ -420,10 +424,7 @@ class ResourceWebHandlerTests { @@ -420,10 +424,7 @@ class ResourceWebHandlerTests {
PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0);
Resource[] locations = resolver.getAllowedLocations();
assertThat(locations.length).isEqualTo(3);
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
assertThat(((ClassPathResource) locations[1]).getPath()).isEqualTo("testalternatepath/");
assertThat(((ClassPathResource) locations[2]).getPath()).isEqualTo("META-INF/resources/webjars/");
assertThat(locations).containsExactly(this.testResource, this.testAlternatePathResource, this.webjarsResource);
}
@Test
@ -439,9 +440,7 @@ class ResourceWebHandlerTests { @@ -439,9 +440,7 @@ class ResourceWebHandlerTests {
handler.setLocations(Arrays.asList(location1, location2));
handler.afterPropertiesSet();
Resource[] locations = pathResolver.getAllowedLocations();
assertThat(locations.length).isEqualTo(1);
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
assertThat(pathResolver.getAllowedLocations()).containsExactly(location1);
}
@Test

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 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.
@ -17,7 +17,6 @@ @@ -17,7 +17,6 @@
package org.springframework.web.servlet.resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -59,10 +58,15 @@ import static org.mockito.Mockito.mock; @@ -59,10 +58,15 @@ import static org.mockito.Mockito.mock;
* @author Jeremy Grelle
* @author Rossen Stoyanchev
* @author Brian Clozel
* @author Sam Brannen
*/
@ExtendWith(GzipSupport.class)
public class ResourceHttpRequestHandlerTests {
private final ClassPathResource testResource = new ClassPathResource("test/", getClass());
private final ClassPathResource testAlternatePathResource = new ClassPathResource("testalternatepath/", getClass());
private final ClassPathResource webjarsResource = new ClassPathResource("META-INF/resources/webjars/");
private ResourceHttpRequestHandler handler;
private MockHttpServletRequest request;
@ -72,15 +76,15 @@ public class ResourceHttpRequestHandlerTests { @@ -72,15 +76,15 @@ public class ResourceHttpRequestHandlerTests {
@BeforeEach
public void setup() throws Exception {
List<Resource> paths = new ArrayList<>(2);
paths.add(new ClassPathResource("test/", getClass()));
paths.add(new ClassPathResource("testalternatepath/", getClass()));
paths.add(new ClassPathResource("META-INF/resources/webjars/"));
List<Resource> locations = List.of(
this.testResource,
this.testAlternatePathResource,
this.webjarsResource);
TestServletContext servletContext = new TestServletContext();
this.handler = new ResourceHttpRequestHandler();
this.handler.setLocations(paths);
this.handler.setLocations(locations);
this.handler.setCacheSeconds(3600);
this.handler.setServletContext(servletContext);
this.handler.afterPropertiesSet();
@ -467,10 +471,7 @@ public class ResourceHttpRequestHandlerTests { @@ -467,10 +471,7 @@ public class ResourceHttpRequestHandlerTests {
PathResourceResolver resolver = (PathResourceResolver) this.handler.getResourceResolvers().get(0);
Resource[] locations = resolver.getAllowedLocations();
assertThat(locations.length).isEqualTo(3);
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
assertThat(((ClassPathResource) locations[1]).getPath()).isEqualTo("testalternatepath/");
assertThat(((ClassPathResource) locations[2]).getPath()).isEqualTo("META-INF/resources/webjars/");
assertThat(locations).containsExactly(this.testResource, this.testAlternatePathResource, this.webjarsResource);
}
@Test
@ -487,9 +488,7 @@ public class ResourceHttpRequestHandlerTests { @@ -487,9 +488,7 @@ public class ResourceHttpRequestHandlerTests {
handler.setLocations(Arrays.asList(location1, location2));
handler.afterPropertiesSet();
Resource[] locations = pathResolver.getAllowedLocations();
assertThat(locations.length).isEqualTo(1);
assertThat(((ClassPathResource) locations[0]).getPath()).isEqualTo("test/");
assertThat(pathResolver.getAllowedLocations()).containsExactly(location1);
}
@Test

Loading…
Cancel
Save