Skip to content

Commit fdafd38

Browse files
committed
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. Closes gh-26575
1 parent 74b248a commit fdafd38

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,7 +97,8 @@ public void setAsText(String text) throws IllegalArgumentException {
9797
if (resource == null) {
9898
setValue(null);
9999
}
100-
else if (!resource.exists() && nioPathCandidate) {
100+
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
101+
// Prefer getFile().toPath() below for non-existent file handles
101102
setValue(Paths.get(text).normalize());
102103
}
103104
else {

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@
3434
public class PathEditorTests {
3535

3636
@Test
37-
public void testClasspathPathName() throws Exception {
37+
public void testClasspathPathName() {
3838
PropertyEditor pathEditor = new PathEditor();
3939
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
4040
ClassUtils.getShortName(getClass()) + ".class");
@@ -46,14 +46,14 @@ public void testClasspathPathName() throws Exception {
4646
}
4747

4848
@Test
49-
public void testWithNonExistentResource() throws Exception {
49+
public void testWithNonExistentResource() {
5050
PropertyEditor propertyEditor = new PathEditor();
5151
assertThatIllegalArgumentException().isThrownBy(() ->
5252
propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc"));
5353
}
5454

5555
@Test
56-
public void testWithNonExistentPath() throws Exception {
56+
public void testWithNonExistentPath() {
5757
PropertyEditor pathEditor = new PathEditor();
5858
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
5959
Object value = pathEditor.getValue();
@@ -65,7 +65,7 @@ public void testWithNonExistentPath() throws Exception {
6565
}
6666

6767
@Test
68-
public void testAbsolutePath() throws Exception {
68+
public void testAbsolutePath() {
6969
PropertyEditor pathEditor = new PathEditor();
7070
pathEditor.setAsText("/no_way_this_file_is_found.doc");
7171
Object value = pathEditor.getValue();
@@ -77,7 +77,31 @@ public void testAbsolutePath() throws Exception {
7777
}
7878

7979
@Test
80-
public void testUnqualifiedPathNameFound() throws Exception {
80+
public void testWindowsAbsolutePath() {
81+
PropertyEditor pathEditor = new PathEditor();
82+
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
83+
Object value = pathEditor.getValue();
84+
boolean condition1 = value instanceof Path;
85+
assertThat(condition1).isTrue();
86+
Path path = (Path) value;
87+
boolean condition = !path.toFile().exists();
88+
assertThat(condition).isTrue();
89+
}
90+
91+
@Test
92+
public void testWindowsAbsoluteFilePath() {
93+
PropertyEditor pathEditor = new PathEditor();
94+
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
95+
Object value = pathEditor.getValue();
96+
boolean condition1 = value instanceof Path;
97+
assertThat(condition1).isTrue();
98+
Path path = (Path) value;
99+
boolean condition = !path.toFile().exists();
100+
assertThat(condition).isTrue();
101+
}
102+
103+
@Test
104+
public void testUnqualifiedPathNameFound() {
81105
PropertyEditor pathEditor = new PathEditor();
82106
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
83107
ClassUtils.getShortName(getClass()) + ".class";
@@ -96,7 +120,7 @@ public void testUnqualifiedPathNameFound() throws Exception {
96120
}
97121

98122
@Test
99-
public void testUnqualifiedPathNameNotFound() throws Exception {
123+
public void testUnqualifiedPathNameNotFound() {
100124
PropertyEditor pathEditor = new PathEditor();
101125
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
102126
ClassUtils.getShortName(getClass()) + ".clazz";

0 commit comments

Comments
 (0)