Skip to content

Commit

Permalink
Bypass root path resolution for "file:" prefix only
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Apr 12, 2021
1 parent 34fcbfb commit e4e2212
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceEditor;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;

/**
* Editor for {@code java.nio.file.Path}, to directly populate a Path
Expand Down Expand Up @@ -74,7 +74,7 @@ public PathEditor(ResourceEditor resourceEditor) {

@Override
public void setAsText(String text) throws IllegalArgumentException {
boolean nioPathCandidate = !text.startsWith(ResourceLoader.CLASSPATH_URL_PREFIX);
boolean nioPathCandidate = !text.startsWith(ResourceUtils.CLASSPATH_URL_PREFIX);
if (nioPathCandidate && !text.startsWith("/")) {
try {
URI uri = new URI(text);
Expand All @@ -85,9 +85,13 @@ public void setAsText(String text) throws IllegalArgumentException {
return;
}
}
catch (URISyntaxException | FileSystemNotFoundException ex) {
// Not a valid URI (let's try as Spring resource location),
// or a URI scheme not registered for NIO (let's try URL
catch (URISyntaxException ex) {
// Not a valid URI; potentially a Windows-style path after
// 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).
}
}
Expand All @@ -97,8 +101,7 @@ public void setAsText(String text) throws IllegalArgumentException {
if (resource == null) {
setValue(null);
}
else if (!resource.isFile() && !resource.exists() && nioPathCandidate) {
// Prefer getFile().toPath() below for non-existent file handles
else if (nioPathCandidate && !resource.exists()) {
setValue(Paths.get(text).normalize());
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void testClasspathPathName() {
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" +
ClassUtils.getShortName(getClass()) + ".class");
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
assertThat(path.toFile().exists()).isTrue();
}
Expand All @@ -57,47 +56,46 @@ public void testWithNonExistentPath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file:/no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
assertThat(!path.toFile().exists()).isTrue();
}

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

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

@Test
public void testWindowsAbsoluteFilePath() {
PropertyEditor pathEditor = new PathEditor();
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
boolean condition1 = value instanceof Path;
assertThat(condition1).isTrue();
Path path = (Path) value;
boolean condition = !path.toFile().exists();
assertThat(condition).isTrue();
try {
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc");
Object value = pathEditor.getValue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
assertThat(!path.toFile().exists()).isTrue();
}
catch (IllegalArgumentException ex) {
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
throw ex;
}
}
}

@Test
Expand All @@ -107,8 +105,7 @@ public void testUnqualifiedPathNameFound() {
ClassUtils.getShortName(getClass()) + ".class";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isTrue();
Expand All @@ -126,8 +123,7 @@ public void testUnqualifiedPathNameNotFound() {
ClassUtils.getShortName(getClass()) + ".clazz";
pathEditor.setAsText(fileName);
Object value = pathEditor.getValue();
boolean condition = value instanceof Path;
assertThat(condition).isTrue();
assertThat(value instanceof Path).isTrue();
Path path = (Path) value;
File file = path.toFile();
assertThat(file.exists()).isFalse();
Expand Down

0 comments on commit e4e2212

Please sign in to comment.