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
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)));

View File

@ -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);
}
}