Skip to content

Commit

Permalink
Apply fallback resolution for non-hierarchical URIs such as "file:."
Browse files Browse the repository at this point in the history
Includes meaningful exception message for file system resolution.

Closes gh-33124

(cherry picked from commit daea3f0)
  • Loading branch information
jhoeller committed Jul 3, 2024
1 parent 96e3aa6 commit 2302b30
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -92,9 +92,9 @@ public void setAsText(String text) throws IllegalArgumentException {
// 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.
}
}

Expand All @@ -111,8 +111,13 @@ else if (nioPathCandidate && !resource.exists()) {
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -31,79 +31,82 @@
* @author Chris Beams
* @author Juergen Hoeller
*/
public class FileEditorTests {
class FileEditorTests {

@Test
public void testClasspathFileName() throws Exception {
void testClasspathFileName() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class");
Object value = fileEditor.getValue();
boolean condition = value instanceof File;
assertThat(condition).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file.exists()).isTrue();
assertThat(file).exists();
}

@Test
public void testWithNonExistentResource() throws Exception {
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() throws Exception {
void testWithNonExistentFile() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
boolean condition1 = value instanceof File;
assertThat(condition1).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
boolean condition = !file.exists();
assertThat(condition).isTrue();
assertThat(file).doesNotExist();
}

@Test
public void testAbsoluteFileName() throws Exception {
void testAbsoluteFileName() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = fileEditor.getValue();
boolean condition1 = value instanceof File;
assertThat(condition1).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file).doesNotExist();
}

@Test
void testCurrentDirectory() {
PropertyEditor fileEditor = new FileEditor();
fileEditor.setAsText("file:.");
Object value = fileEditor.getValue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
boolean condition = !file.exists();
assertThat(condition).isTrue();
assertThat(file).isEqualTo(new File("."));
}

@Test
public void testUnqualifiedFileNameFound() throws Exception {
void testUnqualifiedFileNameFound() {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
boolean condition = value instanceof File;
assertThat(condition).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file.exists()).isTrue();
assertThat(file).exists();
String absolutePath = file.getAbsolutePath().replace('\\', '/');
assertThat(absolutePath.endsWith(fileName)).isTrue();
assertThat(absolutePath).endsWith(fileName);
}

@Test
public void testUnqualifiedFileNameNotFound() throws Exception {
void testUnqualifiedFileNameNotFound() {
PropertyEditor fileEditor = new FileEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".clazz";
fileEditor.setAsText(fileName);
Object value = fileEditor.getValue();
boolean condition = value instanceof File;
assertThat(condition).isTrue();
assertThat(value).isInstanceOf(File.class);
File file = (File) value;
assertThat(file.exists()).isFalse();
assertThat(file).doesNotExist();
String absolutePath = file.getAbsolutePath().replace('\\', '/');
assertThat(absolutePath.endsWith(fileName)).isTrue();
assertThat(absolutePath).endsWith(fileName);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 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.
Expand All @@ -19,6 +19,7 @@
import java.beans.PropertyEditor;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

Expand All @@ -31,65 +32,65 @@
* @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");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(path.toFile().exists()).isTrue();
assertThat(path.toFile()).exists();
}

@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();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
assertThat(path.toFile()).doesNotExist();
}

@Test
public void testAbsolutePath() {
void testAbsolutePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
assertThat(path.toFile()).doesNotExist();
}

@Test
public void testWindowsAbsolutePath() {
void testWindowsAbsolutePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
assertThat(path.toFile()).doesNotExist();
}

@Test
public void testWindowsAbsoluteFilePath() {
void testWindowsAbsoluteFilePath() {
PropertyEditor pathEditor = new PathEditor();
try {
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
assertThat(path.toFile()).doesNotExist();
}
catch (IllegalArgumentException ex) {
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
Expand All @@ -99,39 +100,49 @@ public void testWindowsAbsoluteFilePath() {
}

@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";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isTrue();
assertThat(file).exists();
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertThat(absolutePath.endsWith(fileName)).isTrue();
assertThat(absolutePath).endsWith(fileName);
}

@Test
public void testUnqualifiedPathNameNotFound() {
void testUnqualifiedPathNameNotFound() {
PropertyEditor pathEditor = new PathEditor();
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".clazz";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
assertThat(value).isInstanceOf(Path.class);
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isFalse();
assertThat(file).doesNotExist();
String absolutePath = file.getAbsolutePath();
if (File.separatorChar == '\\') {
absolutePath = absolutePath.replace('\\', '/');
}
assertThat(absolutePath.endsWith(fileName)).isTrue();
assertThat(absolutePath).endsWith(fileName);
}

}

0 comments on commit 2302b30

Please sign in to comment.