|
|
|
@ -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"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -19,6 +19,7 @@ package org.springframework.beans.propertyeditors; |
|
|
|
import java.beans.PropertyEditor; |
|
|
|
import java.beans.PropertyEditor; |
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.nio.file.Path; |
|
|
|
import java.nio.file.Path; |
|
|
|
|
|
|
|
import java.nio.file.Paths; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
@ -31,65 +32,65 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @since 4.3.2 |
|
|
|
* @since 4.3.2 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class PathEditorTests { |
|
|
|
class PathEditorTests { |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testClasspathPathName() { |
|
|
|
void testClasspathPathName() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
pathEditor.setAsText("classpath:" + ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
ClassUtils.getShortName(getClass()) + ".class"); |
|
|
|
ClassUtils.getShortName(getClass()) + ".class"); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
assertThat(path.toFile().exists()).isTrue(); |
|
|
|
assertThat(path.toFile()).exists(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testWithNonExistentResource() { |
|
|
|
void testWithNonExistentResource() { |
|
|
|
PropertyEditor propertyEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> |
|
|
|
assertThatIllegalArgumentException().isThrownBy(() -> |
|
|
|
propertyEditor.setAsText("classpath:/no_way_this_file_is_found.doc")); |
|
|
|
pathEditor.setAsText("classpath:/no_way_this_file_is_found.doc")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testWithNonExistentPath() { |
|
|
|
void testWithNonExistentPath() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); |
|
|
|
pathEditor.setAsText("file:/no_way_this_file_is_found.doc"); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
assertThat(!path.toFile().exists()).isTrue(); |
|
|
|
assertThat(path.toFile()).doesNotExist(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testAbsolutePath() { |
|
|
|
void testAbsolutePath() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
pathEditor.setAsText("/no_way_this_file_is_found.doc"); |
|
|
|
pathEditor.setAsText("/no_way_this_file_is_found.doc"); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
assertThat(!path.toFile().exists()).isTrue(); |
|
|
|
assertThat(path.toFile()).doesNotExist(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testWindowsAbsolutePath() { |
|
|
|
void testWindowsAbsolutePath() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc"); |
|
|
|
pathEditor.setAsText("C:\\no_way_this_file_is_found.doc"); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
assertThat(!path.toFile().exists()).isTrue(); |
|
|
|
assertThat(path.toFile()).doesNotExist(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testWindowsAbsoluteFilePath() { |
|
|
|
void testWindowsAbsoluteFilePath() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc"); |
|
|
|
pathEditor.setAsText("file://C:\\no_way_this_file_is_found.doc"); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
assertThat(!path.toFile().exists()).isTrue(); |
|
|
|
assertThat(path.toFile()).doesNotExist(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (IllegalArgumentException ex) { |
|
|
|
catch (IllegalArgumentException ex) { |
|
|
|
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
|
|
|
|
if (File.separatorChar == '\\') { // on Windows, otherwise silently ignore
|
|
|
|
@ -99,39 +100,49 @@ public class PathEditorTests { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@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(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
ClassUtils.getShortName(getClass()) + ".class"; |
|
|
|
ClassUtils.getShortName(getClass()) + ".class"; |
|
|
|
pathEditor.setAsText(fileName); |
|
|
|
pathEditor.setAsText(fileName); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
File file = path.toFile(); |
|
|
|
File file = path.toFile(); |
|
|
|
assertThat(file.exists()).isTrue(); |
|
|
|
assertThat(file).exists(); |
|
|
|
String absolutePath = file.getAbsolutePath(); |
|
|
|
String absolutePath = file.getAbsolutePath(); |
|
|
|
if (File.separatorChar == '\\') { |
|
|
|
if (File.separatorChar == '\\') { |
|
|
|
absolutePath = absolutePath.replace('\\', '/'); |
|
|
|
absolutePath = absolutePath.replace('\\', '/'); |
|
|
|
} |
|
|
|
} |
|
|
|
assertThat(absolutePath.endsWith(fileName)).isTrue(); |
|
|
|
assertThat(absolutePath).endsWith(fileName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testUnqualifiedPathNameNotFound() { |
|
|
|
void testUnqualifiedPathNameNotFound() { |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
PropertyEditor pathEditor = new PathEditor(); |
|
|
|
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
String fileName = ClassUtils.classPackageAsResourcePath(getClass()) + "/" + |
|
|
|
ClassUtils.getShortName(getClass()) + ".clazz"; |
|
|
|
ClassUtils.getShortName(getClass()) + ".clazz"; |
|
|
|
pathEditor.setAsText(fileName); |
|
|
|
pathEditor.setAsText(fileName); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
Object value = pathEditor.getValue(); |
|
|
|
assertThat(value instanceof Path).isTrue(); |
|
|
|
assertThat(value).isInstanceOf(Path.class); |
|
|
|
Path path = (Path) value; |
|
|
|
Path path = (Path) value; |
|
|
|
File file = path.toFile(); |
|
|
|
File file = path.toFile(); |
|
|
|
assertThat(file.exists()).isFalse(); |
|
|
|
assertThat(file).doesNotExist(); |
|
|
|
String absolutePath = file.getAbsolutePath(); |
|
|
|
String absolutePath = file.getAbsolutePath(); |
|
|
|
if (File.separatorChar == '\\') { |
|
|
|
if (File.separatorChar == '\\') { |
|
|
|
absolutePath = absolutePath.replace('\\', '/'); |
|
|
|
absolutePath = absolutePath.replace('\\', '/'); |
|
|
|
} |
|
|
|
} |
|
|
|
assertThat(absolutePath.endsWith(fileName)).isTrue(); |
|
|
|
assertThat(absolutePath).endsWith(fileName); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|