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.
This commit is contained in:
Sam Brannen
2022-09-07 14:59:05 +02:00
parent 4cc91e46b2
commit 1688becd73
3 changed files with 27 additions and 33 deletions

View File

@@ -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 @@
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;
* @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 {
@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 {
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 {
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