Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jul 3, 2024
2 parents 1ea4eb1 + daea3f0 commit 384d0e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 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
Expand Up @@ -46,9 +46,9 @@ void testClasspathFileName() {

@Test
void testWithNonExistentResource() {
PropertyEditor propertyEditor = new FileEditor();
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
Expand All @@ -71,6 +71,16 @@ void testAbsoluteFileName() {
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;
assertThat(file).isEqualTo(new File("."));
}

@Test
void testUnqualifiedFileNameFound() {
PropertyEditor fileEditor = new FileEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -46,9 +47,9 @@ void testClasspathPathName() {

@Test
void testWithNonExistentResource() {
PropertyEditor propertyEditor = new PathEditor();
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
Expand Down Expand Up @@ -98,6 +99,16 @@ void testWindowsAbsoluteFilePath() {
}
}

@Test
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();
Expand Down

0 comments on commit 384d0e4

Please sign in to comment.