Expose getFilePath() on Resource interface for consistent NIO support

Closes gh-35435
This commit is contained in:
Juergen Hoeller
2025-09-08 17:03:07 +02:00
parent 114c3f7c9c
commit 27221581a1
12 changed files with 94 additions and 32 deletions
@@ -186,10 +186,11 @@ class PathResourceTests {
}
@Test
void getFile() throws IOException {
void getFile() {
PathResource resource = new PathResource(TEST_FILE);
File file = new File(TEST_FILE);
assertThat(resource.getFile().getAbsoluteFile()).isEqualTo(file.getAbsoluteFile());
assertThat(resource.getFilePath()).isEqualTo(file.toPath());
}
@Test
@@ -198,7 +199,7 @@ class PathResourceTests {
given(path.normalize()).willReturn(path);
given(path.toFile()).willThrow(new UnsupportedOperationException());
PathResource resource = new PathResource(path);
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(resource::getFile);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(resource::getFile);
}
@Test
@@ -236,13 +237,13 @@ class PathResourceTests {
@Test
void filename() {
Resource resource = new PathResource(TEST_FILE);
PathResource resource = new PathResource(TEST_FILE);
assertThat(resource.getFilename()).isEqualTo("example.properties");
}
@Test
void description() {
Resource resource = new PathResource(TEST_FILE);
PathResource resource = new PathResource(TEST_FILE);
assertThat(resource.getDescription()).contains("path [");
assertThat(resource.getDescription()).contains(TEST_FILE);
}
@@ -261,9 +262,9 @@ class PathResourceTests {
@Test
void equalsAndHashCode() {
Resource resource1 = new PathResource(TEST_FILE);
Resource resource2 = new PathResource(TEST_FILE);
Resource resource3 = new PathResource(TEST_DIR);
PathResource resource1 = new PathResource(TEST_FILE);
PathResource resource2 = new PathResource(TEST_FILE);
PathResource resource3 = new PathResource(TEST_DIR);
assertThat(resource1).isEqualTo(resource1);
assertThat(resource1).isEqualTo(resource2);
assertThat(resource2).isEqualTo(resource1);
@@ -55,6 +55,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for various {@link Resource} implementations.
@@ -268,6 +270,16 @@ class ResourceTests {
assertThat(relative).isEqualTo(new FileSystemResource("dir/subdir"));
}
@Test
void getFilePath() throws Exception {
Path path = mock();
given(path.normalize()).willReturn(path);
given(path.toFile()).willThrow(new UnsupportedOperationException());
Resource resource = new FileSystemResource(path);
assertThat(resource.getFilePath()).isSameAs(path);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(resource::getFile);
}
@Test
void readableChannelProvidesContent() throws Exception {
Resource resource = new FileSystemResource(getClass().getResource("ResourceTests.class").getFile());