From 66989434b41c06a610be1b32f4ebcabbc707bfa3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 7 Oct 2022 14:02:11 +0200 Subject: [PATCH] Introduce unit tests for gh-29275 --- .../core/io/ResourceTests.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java b/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java index 7549db9a63..0744ece766 100644 --- a/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/ResourceTests.java @@ -252,6 +252,27 @@ class ResourceTests { } } + @Test + void urlAndUriAreNormalizedWhenCreatedFromFile() throws Exception { + Path path = Path.of("src/test/resources/scanned-resources/resource#test1.txt").toAbsolutePath(); + assertUrlAndUriBehavior(new FileSystemResource(path.toFile())); + } + + @Test + void urlAndUriAreNormalizedWhenCreatedFromPath() throws Exception { + Path path = Path.of("src/test/resources/scanned-resources/resource#test1.txt").toAbsolutePath(); + assertUrlAndUriBehavior(new FileSystemResource(path)); + } + + /** + * The following assertions serve as regression tests for the lack of the + * "authority component" (//) in the returned URI/URL. For example, we are + * expecting file:/my/path (or file:/C:/My/Path) instead of file:///my/path. + */ + private void assertUrlAndUriBehavior(Resource resource) throws IOException { + assertThat(resource.getURL().toString()).matches("^file:\\/[^\\/].+test1\\.txt$"); + assertThat(resource.getURI().toString()).matches("^file:\\/[^\\/].+test1\\.txt$"); + } } @Nested