Skip to content

Commit 069d081

Browse files
committed
Apply fallback resolution for non-hierarchical URIs such as "file:."
Includes meaningful exception message for file system resolution. Closes gh-33124 (cherry picked from commit daea3f0)
1 parent a352d88 commit 069d081

File tree

3 files changed

+59
-33
lines changed

3 files changed

+59
-33
lines changed

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -92,9 +92,9 @@ public void setAsText(String text) throws IllegalArgumentException {
9292
// a file prefix (let's try as Spring resource location)
9393
nioPathCandidate = !text.startsWith(ResourceUtils.FILE_URL_PREFIX);
9494
}
95-
catch (FileSystemNotFoundException ex) {
96-
// URI scheme not registered for NIO (let's try URL
97-
// protocol handlers via Spring's resource mechanism).
95+
catch (FileSystemNotFoundException | IllegalArgumentException ex) {
96+
// URI scheme not registered for NIO or not meeting Paths requirements:
97+
// let's try URL protocol handlers via Spring's resource mechanism.
9898
}
9999
}
100100

@@ -111,8 +111,13 @@ else if (nioPathCandidate && !resource.exists()) {
111111
setValue(resource.getFile().toPath());
112112
}
113113
catch (IOException ex) {
114-
throw new IllegalArgumentException(
115-
"Could not retrieve file for " + resource + ": " + ex.getMessage());
114+
String msg = "Could not resolve \"" + text + "\" to 'java.nio.file.Path' for " + resource + ": " +
115+
ex.getMessage();
116+
if (nioPathCandidate) {
117+
msg += " - In case of ambiguity, consider adding the 'file:' prefix for an explicit reference " +
118+
"to a file system resource of the same name: \"file:" + text + "\"";
119+
}
120+
throw new IllegalArgumentException(msg);
116121
}
117122
}
118123
}

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

+25-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -31,68 +31,78 @@
3131
* @author Chris Beams
3232
* @author Juergen Hoeller
3333
*/
34-
public class FileEditorTests {
34+
class FileEditorTests {
3535

3636
@Test
37-
public void testClasspathFileName() {
37+
void testClasspathFileName() {
3838
PropertyEditor fileEditor = new FileEditor();
3939
fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
4040
ClassUtils.getShortName(getClass()) + ".class");
4141
Object value = fileEditor.getValue();
42-
assertThat(value instanceof File).isTrue();
42+
assertThat(value).isInstanceOf(File.class);
4343
File file = (File) value;
4444
assertThat(file).exists();
4545
}
4646

4747
@Test
48-
public void testWithNonExistentResource() {
49-
PropertyEditor propertyEditor = new FileEditor();
48+
void testWithNonExistentResource() {
49+
PropertyEditor fileEditor = new FileEditor();
5050
assertThatIllegalArgumentException().isThrownBy(() ->
51-
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
51+
fileEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
5252
}
5353

5454
@Test
55-
public void testWithNonExistentFile() {
55+
void testWithNonExistentFile() {
5656
PropertyEditor fileEditor = new FileEditor();
5757
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
5858
Object value = fileEditor.getValue();
59-
assertThat(value instanceof File).isTrue();
59+
assertThat(value).isInstanceOf(File.class);
6060
File file = (File) value;
6161
assertThat(file).doesNotExist();
6262
}
6363

6464
@Test
65-
public void testAbsoluteFileName() {
65+
void testAbsoluteFileName() {
6666
PropertyEditor fileEditor = new FileEditor();
6767
fileEditor.setAsText("/no_way_this_file_is_found.doc");
6868
Object value = fileEditor.getValue();
69-
assertThat(value instanceof File).isTrue();
69+
assertThat(value).isInstanceOf(File.class);
7070
File file = (File) value;
7171
assertThat(file).doesNotExist();
7272
}
7373

7474
@Test
75-
public void testUnqualifiedFileNameFound() {
75+
void testCurrentDirectory() {
76+
PropertyEditor fileEditor = new FileEditor();
77+
fileEditor.setAsText("file:.");
78+
Object value = fileEditor.getValue();
79+
assertThat(value).isInstanceOf(File.class);
80+
File file = (File) value;
81+
assertThat(file).isEqualTo(new File("."));
82+
}
83+
84+
@Test
85+
void testUnqualifiedFileNameFound() {
7686
PropertyEditor fileEditor = new FileEditor();
7787
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
7888
ClassUtils.getShortName(getClass()) + ".class";
7989
fileEditor.setAsText(fileName);
8090
Object value = fileEditor.getValue();
81-
assertThat(value instanceof File).isTrue();
91+
assertThat(value).isInstanceOf(File.class);
8292
File file = (File) value;
8393
assertThat(file).exists();
8494
String absolutePath = file.getAbsolutePath().replace('\\', '/');
8595
assertThat(absolutePath).endsWith(fileName);
8696
}
8797

8898
@Test
89-
public void testUnqualifiedFileNameNotFound() {
99+
void testUnqualifiedFileNameNotFound() {
90100
PropertyEditor fileEditor = new FileEditor();
91101
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
92102
ClassUtils.getShortName(getClass()) + ".clazz";
93103
fileEditor.setAsText(fileName);
94104
Object value = fileEditor.getValue();
95-
assertThat(value instanceof File).isTrue();
105+
assertThat(value).isInstanceOf(File.class);
96106
File file = (File) value;
97107
assertThat(file).doesNotExist();
98108
String absolutePath = file.getAbsolutePath().replace('\\', '/');

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

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -19,6 +19,7 @@
1919
import java.beans.PropertyEditor;
2020
import java.io.File;
2121
import java.nio.file.Path;
22+
import java.nio.file.Paths;
2223

2324
import org.junit.jupiter.api.Test;
2425

@@ -31,10 +32,10 @@
3132
* @author Juergen Hoeller
3233
* @since 4.3.2
3334
*/
34-
public class PathEditorTests {
35+
class PathEditorTests {
3536

3637
@Test
37-
public void testClasspathPathName() {
38+
void testClasspathPathName() {
3839
PropertyEditor pathEditor = new PathEditor();
3940
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
4041
ClassUtils.getShortName(getClass()) + ".class");
@@ -45,14 +46,14 @@ public void testClasspathPathName() {
4546
}
4647

4748
@Test
48-
public void testWithNonExistentResource() {
49-
PropertyEditor propertyEditor = new PathEditor();
49+
void testWithNonExistentResource() {
50+
PropertyEditor pathEditor = new PathEditor();
5051
assertThatIllegalArgumentException().isThrownBy(() ->
51-
propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc"));
52+
pathEditor.setAsText("classpath:/no_way_this_file_is_found.doc"));
5253
}
5354

5455
@Test
55-
public void testWithNonExistentPath() {
56+
void testWithNonExistentPath() {
5657
PropertyEditor pathEditor = new PathEditor();
5758
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
5859
Object value = pathEditor.getValue();
@@ -62,7 +63,7 @@ public void testWithNonExistentPath() {
6263
}
6364

6465
@Test
65-
public void testAbsolutePath() {
66+
void testAbsolutePath() {
6667
PropertyEditor pathEditor = new PathEditor();
6768
pathEditor.setAsText("/no_way_this_file_is_found.doc");
6869
Object value = pathEditor.getValue();
@@ -72,7 +73,7 @@ public void testAbsolutePath() {
7273
}
7374

7475
@Test
75-
public void testWindowsAbsolutePath() {
76+
void testWindowsAbsolutePath() {
7677
PropertyEditor pathEditor = new PathEditor();
7778
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
7879
Object value = pathEditor.getValue();
@@ -82,7 +83,7 @@ public void testWindowsAbsolutePath() {
8283
}
8384

8485
@Test
85-
public void testWindowsAbsoluteFilePath() {
86+
void testWindowsAbsoluteFilePath() {
8687
PropertyEditor pathEditor = new PathEditor();
8788
try {
8889
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
@@ -99,7 +100,17 @@ public void testWindowsAbsoluteFilePath() {
99100
}
100101

101102
@Test
102-
public void testUnqualifiedPathNameFound() {
103+
void testCurrentDirectory() {
104+
PropertyEditor pathEditor = new PathEditor();
105+
pathEditor.setAsText("file:.");
106+
Object value = pathEditor.getValue();
107+
assertThat(value).isInstanceOf(Path.class);
108+
Path path = (Path) value;
109+
assertThat(path).isEqualTo(Paths.get("."));
110+
}
111+
112+
@Test
113+
void testUnqualifiedPathNameFound() {
103114
PropertyEditor pathEditor = new PathEditor();
104115
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
105116
ClassUtils.getShortName(getClass()) + ".class";
@@ -117,7 +128,7 @@ public void testUnqualifiedPathNameFound() {
117128
}
118129

119130
@Test
120-
public void testUnqualifiedPathNameNotFound() {
131+
void testUnqualifiedPathNameNotFound() {
121132
PropertyEditor pathEditor = new PathEditor();
122133
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
123134
ClassUtils.getShortName(getClass()) + ".clazz";

0 commit comments

Comments
 (0)