diff --git a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 955edca5c56..b165abefaee 100644 --- a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -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 { 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 { } @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 { } @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" diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java index 5fa5f11a183..c395dfc3ed7 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java @@ -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 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 { 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 { 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 diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java index b6889bf2068..0c274e73d87 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerTests.java @@ -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 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 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