Make URL path tests compatible with Windows

See gh-26775
This commit is contained in:
Juergen Hoeller 2021-04-13 09:52:23 +02:00
parent 74f7eb11be
commit 04ce8e0ac4
2 changed files with 8 additions and 14 deletions

View File

@ -237,7 +237,7 @@ public class ResourceWebHandlerTests {
@Test @Test
public void getResourceFromFileSystem() throws Exception { public void getResourceFromFileSystem() throws Exception {
String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath() String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath()
.replace("classes/java", "resources") + "/"; .replace('\\', '/').replace("classes/java", "resources") + "/";
ResourceWebHandler handler = new ResourceWebHandler(); ResourceWebHandler handler = new ResourceWebHandler();
handler.setLocations(Collections.singletonList(new FileSystemResource(path))); handler.setLocations(Collections.singletonList(new FileSystemResource(path)));

View File

@ -18,8 +18,6 @@ package org.springframework.web.servlet.resource;
import java.io.IOException; import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -137,7 +135,7 @@ public class ResourceHttpRequestHandlerIntegrationTests {
registerClasspathLocation("/cp/**", classPathLocation, registry); registerClasspathLocation("/cp/**", classPathLocation, registry);
registerFileSystemLocation("/fs/**", path, 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) { 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) { 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 { 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); throw new IllegalStateException(ex);
} }
} }
private URL toURL(String path) { private String getPath(ClassPathResource resource) {
try { 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); throw new IllegalStateException(ex);
} }
} }