From 652781c4a103b662df50d022086773ffe06c13d3 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 18 Oct 2022 17:51:29 +0200 Subject: [PATCH] Test status quo in 5.3.x for PathMatchingResourcePatternResolver See gh-29333 --- ...hMatchingResourcePatternResolverTests.java | 87 ++++++++++++++++++- 1 file changed, 84 insertions(+), 3 deletions(-) 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 ced415af9df..e18452d4044 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 @@ -100,13 +100,94 @@ class PathMatchingResourcePatternResolverTests { } @Test - void usingFileProtocol() { + void usingClasspathStarProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception { + String pattern = "classpath*:org/springframework/core/io/sup*"; + String pathPrefix = ".+org/springframework/core/io/"; + + List actualSubPaths = getSubPathsIgnoringClassFiles(pattern, pathPrefix); + + // We DO find "support" if the pattern does NOT end with a slash. + assertThat(actualSubPaths).containsExactly("support"); + } + + @Test + void usingFileProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception { + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); + String pattern = String.format("file:%s/org/springframework/core/io/sup*", testResourcesDir); + String pathPrefix = ".+org/springframework/core/io/"; + + List actualSubPaths = getSubPathsIgnoringClassFiles(pattern, pathPrefix); + + // We DO find "support" if the pattern does NOT end with a slash. + assertThat(actualSubPaths).containsExactly("support"); + } + + @Test + void usingClasspathStarProtocolWithWildcardInPatternAndEndingInSlash() throws Exception { + String pattern = "classpath*:org/springframework/core/io/sup*/"; + String pathPrefix = ".+org/springframework/core/io/"; + + List actualSubPaths = getSubPathsIgnoringClassFiles(pattern, pathPrefix); + + // We do NOT find "support" if the pattern ENDS with a slash. + assertThat(actualSubPaths).isEmpty(); + } + + @Test + void usingFileProtocolWithWildcardInPatternAndEndingInSlash() throws Exception { + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); + String pattern = String.format("file:%s/org/springframework/core/io/sup*/", testResourcesDir); + String pathPrefix = ".+org/springframework/core/io/"; + + List actualSubPaths = getSubPathsIgnoringClassFiles(pattern, pathPrefix); + + // We do NOT find "support" if the pattern ENDS with a slash. + assertThat(actualSubPaths).isEmpty(); + } + + @Test + void usingClasspathStarProtocolWithWildcardInPatternAndEndingWithSlashStarStar() throws Exception { + String pattern = "classpath*:org/springframework/core/io/sup*/**"; + String pathPrefix = ".+org/springframework/core/io/"; + + List actualSubPaths = getSubPathsIgnoringClassFiles(pattern, pathPrefix); + + // We DO find "support" if the pattern ENDS with "/**". + assertThat(actualSubPaths) + .containsExactlyInAnyOrder("support", "support/resource#test1.txt", "support/resource#test2.txt"); + } + + private List getSubPathsIgnoringClassFiles(String pattern, String pathPrefix) throws IOException { + return Arrays.stream(resolver.getResources(pattern)) + .map(resource -> getPath(resource).replaceFirst(pathPrefix, "")) + .filter(name -> !name.endsWith(".class")) + .distinct() + .sorted() + .collect(Collectors.toList()); + } + + @Test + void usingFileProtocolWithoutWildcardInPatternAndEndingInSlashStarStar() throws Exception { Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir); - String pathPrefix = ".+scanned-resources/"; + String pathPrefix = ".+?resources/"; + // We do NOT find "scanned-resources" if the pattern ENDS with "/**" AND does NOT otherwise contain a wildcard. assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt"); - assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt"); + assertExactSubPaths(pattern, pathPrefix, "scanned-resources/resource#test1.txt", + "scanned-resources/resource#test2.txt"); + } + + @Test + void usingFileProtocolWithWildcardInPatternAndEndingInSlashStarStar() throws Exception { + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); + String pattern = String.format("file:%s/scanned*resources/**", testResourcesDir); + String pathPrefix = ".+?resources/"; + + // We DO find "scanned-resources" if the pattern ENDS with "/**" AND DOES otherwise contain a wildcard. + assertExactFilenames(pattern, "scanned-resources", "resource#test1.txt", "resource#test2.txt"); + assertExactSubPaths(pattern, pathPrefix, "scanned-resources", "scanned-resources/resource#test1.txt", + "scanned-resources/resource#test2.txt"); } @Test