Skip to content

Commit 2302b30

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 96e3aa6 commit 2302b30

File tree

3 files changed

+82
-63
lines changed

3 files changed

+82
-63
lines changed

Diff for: 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
}
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-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,79 +31,82 @@
3131
* @author Chris Beams
3232
* @author Juergen Hoeller
3333
*/
34-
public class FileEditorTests {
34+
class FileEditorTests {
3535

3636
@Test
37-
public void testClasspathFileName() throws Exception {
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-
boolean condition = value instanceof File;
43-
assertThat(condition).isTrue();
42+
assertThat(value).isInstanceOf(File.class);
4443
File file = (File) value;
45-
assertThat(file.exists()).isTrue();
44+
assertThat(file).exists();
4645
}
4746

4847
@Test
49-
public void testWithNonExistentResource() throws Exception {
50-
PropertyEditor propertyEditor = new FileEditor();
48+
void testWithNonExistentResource() {
49+
PropertyEditor fileEditor = new FileEditor();
5150
assertThatIllegalArgumentException().isThrownBy(() ->
52-
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
51+
fileEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
5352
}
5453

5554
@Test
56-
public void testWithNonExistentFile() throws Exception {
55+
void testWithNonExistentFile() {
5756
PropertyEditor fileEditor = new FileEditor();
5857
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
5958
Object value = fileEditor.getValue();
60-
boolean condition1 = value instanceof File;
61-
assertThat(condition1).isTrue();
59+
assertThat(value).isInstanceOf(File.class);
6260
File file = (File) value;
63-
boolean condition = !file.exists();
64-
assertThat(condition).isTrue();
61+
assertThat(file).doesNotExist();
6562
}
6663

6764
@Test
68-
public void testAbsoluteFileName() throws Exception {
65+
void testAbsoluteFileName() {
6966
PropertyEditor fileEditor = new FileEditor();
7067
fileEditor.setAsText("/no_way_this_file_is_found.doc");
7168
Object value = fileEditor.getValue();
72-
boolean condition1 = value instanceof File;
73-
assertThat(condition1).isTrue();
69+
assertThat(value).isInstanceOf(File.class);
70+
File file = (File) value;
71+
assertThat(file).doesNotExist();
72+
}
73+
74+
@Test
75+
void testCurrentDirectory() {
76+
PropertyEditor fileEditor = new FileEditor();
77+
fileEditor.setAsText("file:.");
78+
Object value = fileEditor.getValue();
79+
assertThat(value).isInstanceOf(File.class);
7480
File file = (File) value;
75-
boolean condition = !file.exists();
76-
assertThat(condition).isTrue();
81+
assertThat(file).isEqualTo(new File("."));
7782
}
7883

7984
@Test
80-
public void testUnqualifiedFileNameFound() throws Exception {
85+
void testUnqualifiedFileNameFound() {
8186
PropertyEditor fileEditor = new FileEditor();
8287
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
8388
ClassUtils.getShortName(getClass()) + ".class";
8489
fileEditor.setAsText(fileName);
8590
Object value = fileEditor.getValue();
86-
boolean condition = value instanceof File;
87-
assertThat(condition).isTrue();
91+
assertThat(value).isInstanceOf(File.class);
8892
File file = (File) value;
89-
assertThat(file.exists()).isTrue();
93+
assertThat(file).exists();
9094
String absolutePath = file.getAbsolutePath().replace('\\', '/');
91-
assertThat(absolutePath.endsWith(fileName)).isTrue();
95+
assertThat(absolutePath).endsWith(fileName);
9296
}
9397

9498
@Test
95-
public void testUnqualifiedFileNameNotFound() throws Exception {
99+
void testUnqualifiedFileNameNotFound() {
96100
PropertyEditor fileEditor = new FileEditor();
97101
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
98102
ClassUtils.getShortName(getClass()) + ".clazz";
99103
fileEditor.setAsText(fileName);
100104
Object value = fileEditor.getValue();
101-
boolean condition = value instanceof File;
102-
assertThat(condition).isTrue();
105+
assertThat(value).isInstanceOf(File.class);
103106
File file = (File) value;
104-
assertThat(file.exists()).isFalse();
107+
assertThat(file).doesNotExist();
105108
String absolutePath = file.getAbsolutePath().replace('\\', '/');
106-
assertThat(absolutePath.endsWith(fileName)).isTrue();
109+
assertThat(absolutePath).endsWith(fileName);
107110
}
108111

109112
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 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,65 +32,65 @@
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");
4142
Object value = pathEditor.getValue();
42-
assertThat(value instanceof Path).isTrue();
43+
assertThat(value).isInstanceOf(Path.class);
4344
Path path = (Path) value;
44-
assertThat(path.toFile().exists()).isTrue();
45+
assertThat(path.toFile()).exists();
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();
59-
assertThat(value instanceof Path).isTrue();
60+
assertThat(value).isInstanceOf(Path.class);
6061
Path path = (Path) value;
61-
assertThat(!path.toFile().exists()).isTrue();
62+
assertThat(path.toFile()).doesNotExist();
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();
69-
assertThat(value instanceof Path).isTrue();
70+
assertThat(value).isInstanceOf(Path.class);
7071
Path path = (Path) value;
71-
assertThat(!path.toFile().exists()).isTrue();
72+
assertThat(path.toFile()).doesNotExist();
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();
79-
assertThat(value instanceof Path).isTrue();
80+
assertThat(value).isInstanceOf(Path.class);
8081
Path path = (Path) value;
81-
assertThat(!path.toFile().exists()).isTrue();
82+
assertThat(path.toFile()).doesNotExist();
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");
8990
Object value = pathEditor.getValue();
90-
assertThat(value instanceof Path).isTrue();
91+
assertThat(value).isInstanceOf(Path.class);
9192
Path path = (Path) value;
92-
assertThat(!path.toFile().exists()).isTrue();
93+
assertThat(path.toFile()).doesNotExist();
9394
}
9495
catch (IllegalArgumentException ex) {
9596
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
@@ -99,39 +100,49 @@ 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";
106117
pathEditor.setAsText(fileName);
107118
Object value = pathEditor.getValue();
108-
assertThat(value instanceof Path).isTrue();
119+
assertThat(value).isInstanceOf(Path.class);
109120
Path path = (Path) value;
110121
File file = path.toFile();
111-
assertThat(file.exists()).isTrue();
122+
assertThat(file).exists();
112123
String absolutePath = file.getAbsolutePath();
113124
if (File.separatorChar == '\\') {
114125
absolutePath = absolutePath.replace('\\', '/');
115126
}
116-
assertThat(absolutePath.endsWith(fileName)).isTrue();
127+
assertThat(absolutePath).endsWith(fileName);
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";
124135
pathEditor.setAsText(fileName);
125136
Object value = pathEditor.getValue();
126-
assertThat(value instanceof Path).isTrue();
137+
assertThat(value).isInstanceOf(Path.class);
127138
Path path = (Path) value;
128139
File file = path.toFile();
129-
assertThat(file.exists()).isFalse();
140+
assertThat(file).doesNotExist();
130141
String absolutePath = file.getAbsolutePath();
131142
if (File.separatorChar == '\\') {
132143
absolutePath = absolutePath.replace('\\', '/');
133144
}
134-
assertThat(absolutePath.endsWith(fileName)).isTrue();
145+
assertThat(absolutePath).endsWith(fileName);
135146
}
136147

137148
}

0 commit comments

Comments
 (0)