Skip to content

Commit

Permalink
Make URL path tests compatible with Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Apr 13, 2021
1 parent 74f7eb1 commit 04ce8e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Expand Up @@ -237,7 +237,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
@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)));
Expand Down
Expand Up @@ -18,8 +18,6 @@

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;

Expand Down Expand Up @@ -137,7 +135,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {

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) {
Expand All @@ -150,24 +148,20 @@ protected void registerFileSystemLocation(String pattern, String path, ResourceH
}

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);
}
}
Expand Down

0 comments on commit 04ce8e0

Please sign in to comment.