Browse Source

Fix handling of file: paths to non-existent files

For setAsText, if the text argument is a file: URL for a path that does not exist, Paths.get(text) is called where text is a file: URL, which doesn't work - the result is an InvalidPathException.

To fix this issue, also check that the resource isn't a file before calling Paths.get(). That way, resources that are files skip to the other branch.
pull/26612/head
Craig Andrews 5 years ago committed by Juergen Hoeller
parent
commit
ebf6fff312
  1. 2
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java
  2. 24
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java

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

@ -97,7 +97,7 @@ public class PathEditor extends PropertyEditorSupport { @@ -97,7 +97,7 @@ public class PathEditor extends PropertyEditorSupport {
if (resource == null) {
setValue(null);
}
else if (!resource.exists() && nioPathCandidate) {
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
setValue(Paths.get(text).normalize());
}
else {

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

@ -76,6 +76,30 @@ public class PathEditorTests { @@ -76,6 +76,30 @@ public class PathEditorTests {
assertThat(condition).isTrue();
}
@Test
public void testWindowsAbsolutePath() throws Exception {
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();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
}
@Test
public void testWindowsAbsoluteFilePath() throws Exception {
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();
}
@Test
public void testUnqualifiedPathNameFound() throws Exception {
PropertyEditor pathEditor = new PathEditor();

Loading…
Cancel
Save