From 04ce8e0ac470e341366d150f6e36b0a5e5786799 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 13 Apr 2021 09:52:23 +0200 Subject: [PATCH] Make URL path tests compatible with Windows See gh-26775 --- .../resource/ResourceWebHandlerTests.java | 2 +- ...rceHttpRequestHandlerIntegrationTests.java | 20 +++++++------------ 2 files changed, 8 insertions(+), 14 deletions(-) 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 5a84861847a..691afde68ee 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 @@ -237,7 +237,7 @@ public class ResourceWebHandlerTests { @Test public void getResourceFromFileSystem() throws Exception { String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath() - .replace("classes/java", "resources") + "/"; + .replace('\\', '/').replace("classes/java", "resources") + "/"; ResourceWebHandler handler = new ResourceWebHandler(); handler.setLocations(Collections.singletonList(new FileSystemResource(path))); diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java index 51e1ddf5d3b..a55bd6f8434 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java @@ -18,8 +18,6 @@ package org.springframework.web.servlet.resource; import java.io.IOException; import java.net.MalformedURLException; -import java.net.URI; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.stream.Stream; @@ -137,7 +135,7 @@ public class ResourceHttpRequestHandlerIntegrationTests { registerClasspathLocation("/cp/**", classPathLocation, registry); registerFileSystemLocation("/fs/**", path, registry); - registerUrlLocation("/url/**", "file://" + path.replace('\\', '/'), registry); + registerUrlLocation("/url/**", "file:" + path, registry); } protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) { @@ -150,24 +148,20 @@ public class ResourceHttpRequestHandlerIntegrationTests { } protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) { - UrlResource urlLocation = new UrlResource(toURL(path)); - registry.addResourceHandler(pattern).addResourceLocations(urlLocation); - } - - private String getPath(ClassPathResource resource) { try { - return resource.getFile().getCanonicalPath().replace("classes/java", "resources") + "/"; + UrlResource urlLocation = new UrlResource(path); + registry.addResourceHandler(pattern).addResourceLocations(urlLocation); } - catch (IOException ex) { + catch (MalformedURLException ex) { throw new IllegalStateException(ex); } } - private URL toURL(String path) { + private String getPath(ClassPathResource resource) { try { - return URI.create(path).toURL(); + return resource.getFile().getCanonicalPath().replace('\\', '/').replace("classes/java", "resources") + "/"; } - catch (MalformedURLException ex) { + catch (IOException ex) { throw new IllegalStateException(ex); } }