Browse Source

Bypass root path resolution for "file:" prefix only

See gh-26702
pull/27107/head
Juergen Hoeller 5 years ago
parent
commit
e4e2212817
  1. 17
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java
  2. 46
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java

17
spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java

@ -26,8 +26,8 @@ import java.nio.file.Paths; @@ -26,8 +26,8 @@ import java.nio.file.Paths;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
/**
* Editor for {@code java.nio.file.Path}, to directly populate a Path
@ -74,7 +74,7 @@ public class PathEditor extends PropertyEditorSupport { @@ -74,7 +74,7 @@ public class PathEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
boolean nioPathCandidate = !text.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX);
boolean nioPathCandidate = !text.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX);
if (nioPathCandidate && !text.startsWith("/")) {
try {
URI uri = new URI(text);
@ -85,9 +85,13 @@ public class PathEditor extends PropertyEditorSupport { @@ -85,9 +85,13 @@ public class PathEditor extends PropertyEditorSupport {
return;
}
}
catch (URISyntaxException | FileSystemNotFoundException ex) {
// Not a valid URI (let's try as Spring resource location),
// or a URI scheme not registered for NIO (let's try URL
catch (URISyntaxException ex) {
// Not a valid URI; potentially a Windows-style path after
// a file prefix (let's try as Spring resource location)
nioPathCandidate = !text.startsWith(ResourceUtils.FILE_URL_PREFIX);
}
catch (FileSystemNotFoundException ex) {
// URI scheme not registered for NIO (let's try URL
// protocol handlers via Spring's resource mechanism).
}
}
@ -97,8 +101,7 @@ public class PathEditor extends PropertyEditorSupport { @@ -97,8 +101,7 @@ public class PathEditor extends PropertyEditorSupport {
if (resource == null) {
setValue(null);
}
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
// Prefer getFile().toPath() below for non-existent file handles
else if (nioPathCandidate && !resource.exists()) {
setValue(Paths.get(text).normalize());
}
else {

46
spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java

@ -39,8 +39,7 @@ public class PathEditorTests { @@ -39,8 +39,7 @@ public class PathEditorTests {
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class");
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
assertThat(path.toFile().exists()).isTrue();
}
@ -57,11 +56,9 @@ public class PathEditorTests { @@ -57,11 +56,9 @@ public class PathEditorTests {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
assertThat(!path.toFile().exists()).isTrue();
}
@Test
@ -69,11 +66,9 @@ public class PathEditorTests { @@ -69,11 +66,9 @@ public class PathEditorTests {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
assertThat(!path.toFile().exists()).isTrue();
}
@Test
@ -81,23 +76,26 @@ public class PathEditorTests { @@ -81,23 +76,26 @@ public class PathEditorTests {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
assertThat(!path.toFile().exists()).isTrue();
}
@Test
public void testWindowsAbsoluteFilePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
try {
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
}
catch (IllegalArgumentException ex) {
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
throw ex;
}
}
}
@Test
@ -107,8 +105,7 @@ public class PathEditorTests { @@ -107,8 +105,7 @@ public class PathEditorTests {
ClassUtils.getShortName(getClass()) + ".class";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isTrue();
@ -126,8 +123,7 @@ public class PathEditorTests { @@ -126,8 +123,7 @@ public class PathEditorTests {
ClassUtils.getShortName(getClass()) + ".clazz";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isFalse();

Loading…
Cancel
Save