Browse Source

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 daea3f0eae)
6.0.x
Juergen Hoeller 1 year ago
parent
commit
069d0819dd
  1. 17
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/PathEditor.java
  2. 40
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/FileEditorTests.java
  3. 35
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/PathEditorTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -92,9 +92,9 @@ public class PathEditor extends PropertyEditorSupport { @@ -92,9 +92,9 @@ public class PathEditor extends PropertyEditorSupport {
// a file prefix (let's try as Spring resource location)
nioPathCandidate = !text.startsWith(ResourceUtils.FILE_URL_PREFIX);
}
catch (FileSystemNotFoundException ex) {
// URI scheme not registered for NIO (let's try URL
// protocol handlers via Spring's resource mechanism).
catch (FileSystemNotFoundException | IllegalArgumentException ex) {
// URI scheme not registered for NIO or not meeting Paths requirements:
// let's try URL protocol handlers via Spring's resource mechanism.
}
}
@ -111,8 +111,13 @@ public class PathEditor extends PropertyEditorSupport { @@ -111,8 +111,13 @@ public class PathEditor extends PropertyEditorSupport {
setValue(resource.getFile().toPath());
}
catch (IOException ex) {
throw new IllegalArgumentException(
"Could not retrieve file for " + resource + ": " + ex.getMessage());
String msg = "Could not resolve \"" + text + "\" to 'java.nio.file.Path' for " + resource + ": " +
ex.getMessage();
if (nioPathCandidate) {
msg += " - In case of ambiguity, consider adding the 'file:' prefix for an explicit reference " +
"to a file system resource of the same name: \"file:" + text + "\"";
}
throw new IllegalArgumentException(msg);
}
}
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,54 +31,64 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException @@ -31,54 +31,64 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Chris Beams
* @author Juergen Hoeller
*/
public class FileEditorTests {
class FileEditorTests {
@Test
public void testClasspathFileName() {
void testClasspathFileName() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class");
Object value = fileEditor.getValue();
assertThat(value instanceof File).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).exists();
}
@Test
public void testWithNonExistentResource() {
PropertyEditor propertyEditor = new FileEditor();
void testWithNonExistentResource() {
PropertyEditor fileEditor = new FileEditor();
assertThatIllegalArgumentException().isThrownBy(() ->
propertyEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
fileEditor.setAsText("classpath:no_way_this_file_is_found.doc"));
}
@Test
public void testWithNonExistentFile() {
void testWithNonExistentFile() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertThat(value instanceof File).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).doesNotExist();
}
@Test
public void testAbsoluteFileName() {
void testAbsoluteFileName() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
assertThat(value instanceof File).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).doesNotExist();
}
@Test
public void testUnqualifiedFileNameFound() {
void testCurrentDirectory() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:.");
Object value = fileEditor.getValue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).isEqualTo(new File("."));
}
@Test
void testUnqualifiedFileNameFound() {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
assertThat(value instanceof File).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).exists();
String absolutePath = file.getAbsolutePath().replace('\\', '/');
@ -86,13 +96,13 @@ public class FileEditorTests { @@ -86,13 +96,13 @@ public class FileEditorTests {
}
@Test
public void testUnqualifiedFileNameNotFound() {
void testUnqualifiedFileNameNotFound() {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".clazz";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
assertThat(value instanceof File).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).doesNotExist();
String absolutePath = file.getAbsolutePath().replace('\\', '/');

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.beans.propertyeditors; @@ -19,6 +19,7 @@ package org.springframework.beans.propertyeditors;
import java.beans.PropertyEditor;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
@ -31,10 +32,10 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException @@ -31,10 +32,10 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* @author Juergen Hoeller
* @since 4.3.2
*/
public class PathEditorTests {
class PathEditorTests {
@Test
public void testClasspathPathName() {
void testClasspathPathName() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class");
@ -45,14 +46,14 @@ public class PathEditorTests { @@ -45,14 +46,14 @@ public class PathEditorTests {
}
@Test
public void testWithNonExistentResource() {
PropertyEditor propertyEditor = new PathEditor();
void testWithNonExistentResource() {
PropertyEditor pathEditor = new PathEditor();
assertThatIllegalArgumentException().isThrownBy(() ->
propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc"));
pathEditor.setAsText("classpath:/no_way_this_file_is_found.doc"));
}
@Test
public void testWithNonExistentPath() {
void testWithNonExistentPath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
@ -62,7 +63,7 @@ public class PathEditorTests { @@ -62,7 +63,7 @@ public class PathEditorTests {
}
@Test
public void testAbsolutePath() {
void testAbsolutePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
@ -72,7 +73,7 @@ public class PathEditorTests { @@ -72,7 +73,7 @@ public class PathEditorTests {
}
@Test
public void testWindowsAbsolutePath() {
void testWindowsAbsolutePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
@ -82,7 +83,7 @@ public class PathEditorTests { @@ -82,7 +83,7 @@ public class PathEditorTests {
}
@Test
public void testWindowsAbsoluteFilePath() {
void testWindowsAbsoluteFilePath() {
PropertyEditor pathEditor = new PathEditor();
try {
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
@ -99,7 +100,17 @@ public class PathEditorTests { @@ -99,7 +100,17 @@ public class PathEditorTests {
}
@Test
public void testUnqualifiedPathNameFound() {
void testCurrentDirectory() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file:.");
Object value = pathEditor.getValue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(path).isEqualTo(Paths.get("."));
}
@Test
void testUnqualifiedPathNameFound() {
PropertyEditor pathEditor = new PathEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class";
@ -117,7 +128,7 @@ public class PathEditorTests { @@ -117,7 +128,7 @@ public class PathEditorTests {
}
@Test
public void testUnqualifiedPathNameNotFound() {
void testUnqualifiedPathNameNotFound() {
PropertyEditor pathEditor = new PathEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".clazz";

Loading…
Cancel
Save