From dd814b6d305ab119cae4c04af2f12112d6a8e316 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 4 Oct 2022 15:50:33 +0200 Subject: [PATCH] Test sub-paths in PathMatchingResourcePatternResolverTests This commit introduces assertions that verify that the sub-path is properly URL-decoded when a path contains `#`. The additional assertions are necessary since the existing assertions use Resource#getFilename which already implicitly decodes the path. See gh-29243 --- ...hMatchingResourcePatternResolverTests.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index 3870b65dd8..b091476f61 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -91,14 +91,20 @@ class PathMatchingResourcePatternResolverTests { @Test void usingClasspathStarProtocol() { String pattern = "classpath*:org/springframework/core/io/**/resource#test*.txt"; + String pathPrefix = ".+org/springframework/core/io/"; + assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt"); + assertExactSubPaths(pattern, pathPrefix, "support/resource#test1.txt", "support/resource#test2.txt"); } @Test void usingFilePrototol() { Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath(); String pattern = "file:%s/scanned-resources/**".formatted(testResourcesDir); + String pathPrefix = ".+scanned-resources/"; + assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt"); + assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt"); } } @@ -179,4 +185,30 @@ class PathMatchingResourcePatternResolverTests { } } + private void assertExactSubPaths(String pattern, String pathPrefix, String... subPaths) { + try { + Resource[] resources = resolver.getResources(pattern); + List actualSubPaths = Arrays.stream(resources) + .map(resource -> getPath(resource).replaceFirst(pathPrefix, "")) + .sorted() + .toList(); + assertThat(actualSubPaths).containsExactlyInAnyOrder(subPaths); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } + } + + private String getPath(Resource resource) { + try { + // Tests fail if we use getURL(). They would also fail on Mac OS when using getURI() + // if the resource paths are not Unicode normalized. + // See: https://github.com/spring-projects/spring-framework/issues/29243 + return resource.getFile().getPath(); + } + catch (IOException ex) { + throw new UncheckedIOException(ex); + } + } + }