Skip to content

Commit 384d0e4

Browse files
committed
Merge branch '6.1.x'
2 parents 1ea4eb1 + daea3f0 commit 384d0e4

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
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

+12-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ void testClasspathFileName() {
4646

4747
@Test
4848
void testWithNonExistentResource() {
49-
PropertyEditor propertyEditor = new FileEditor();
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
@@ -71,6 +71,16 @@ void testAbsoluteFileName() {
7171
assertThat(file).doesNotExist();
7272
}
7373

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);
80+
File file = (File) value;
81+
assertThat(file).isEqualTo(new File("."));
82+
}
83+
7484
@Test
7585
void testUnqualifiedFileNameFound() {
7686
PropertyEditor fileEditor = new FileEditor();

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -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

@@ -46,9 +47,9 @@ void testClasspathPathName() {
4647

4748
@Test
4849
void testWithNonExistentResource() {
49-
PropertyEditor propertyEditor = new PathEditor();
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
@@ -98,6 +99,16 @@ void testWindowsAbsoluteFilePath() {
9899
}
99100
}
100101

102+
@Test
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+
101112
@Test
102113
void testUnqualifiedPathNameFound() {
103114
PropertyEditor pathEditor = new PathEditor();

0 commit comments

Comments
 (0)