Skip to content

Commit 04ce8e0

Browse files
committed
Make URL path tests compatible with Windows
See gh-26775
1 parent 74f7eb1 commit 04ce8e0

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceWebHandlerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public void getResourceWithRegisteredMediaType() throws Exception {
237237
@Test
238238
public void getResourceFromFileSystem() throws Exception {
239239
String path = new ClassPathResource("", getClass()).getFile().getCanonicalPath()
240-
.replace("classes/java", "resources") + "/";
240+
.replace('\\', '/').replace("classes/java", "resources") + "/";
241241

242242
ResourceWebHandler handler = new ResourceWebHandler();
243243
handler.setLocations(Collections.singletonList(new FileSystemResource(path)));

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandlerIntegrationTests.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.io.IOException;
2020
import java.net.MalformedURLException;
21-
import java.net.URI;
22-
import java.net.URL;
2321
import java.nio.charset.StandardCharsets;
2422
import java.util.stream.Stream;
2523

@@ -137,7 +135,7 @@ public void addResourceHandlers(ResourceHandlerRegistry registry) {
137135

138136
registerClasspathLocation("/cp/**", classPathLocation, registry);
139137
registerFileSystemLocation("/fs/**", path, registry);
140-
registerUrlLocation("/url/**", "file://" + path.replace('\\', '/'), registry);
138+
registerUrlLocation("/url/**", "file:" + path, registry);
141139
}
142140

143141
protected void registerClasspathLocation(String pattern, ClassPathResource resource, ResourceHandlerRegistry registry) {
@@ -150,24 +148,20 @@ protected void registerFileSystemLocation(String pattern, String path, ResourceH
150148
}
151149

152150
protected void registerUrlLocation(String pattern, String path, ResourceHandlerRegistry registry) {
153-
UrlResource urlLocation = new UrlResource(toURL(path));
154-
registry.addResourceHandler(pattern).addResourceLocations(urlLocation);
155-
}
156-
157-
private String getPath(ClassPathResource resource) {
158151
try {
159-
return resource.getFile().getCanonicalPath().replace("classes/java", "resources") + "/";
152+
UrlResource urlLocation = new UrlResource(path);
153+
registry.addResourceHandler(pattern).addResourceLocations(urlLocation);
160154
}
161-
catch (IOException ex) {
155+
catch (MalformedURLException ex) {
162156
throw new IllegalStateException(ex);
163157
}
164158
}
165159

166-
private URL toURL(String path) {
160+
private String getPath(ClassPathResource resource) {
167161
try {
168-
return URI.create(path).toURL();
162+
return resource.getFile().getCanonicalPath().replace('\\', '/').replace("classes/java", "resources") + "/";
169163
}
170-
catch (MalformedURLException ex) {
164+
catch (IOException ex) {
171165
throw new IllegalStateException(ex);
172166
}
173167
}

0 commit comments

Comments
 (0)