Browse Source

Restore directory matching (explicitly excluding root path itself)

Closes gh-29333
pull/29310/head
Juergen Hoeller 3 years ago
parent
commit
d17cdf98bb
  1. 3
      spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java
  2. 16
      spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

3
spring-core/src/main/java/org/springframework/core/io/support/PathMatchingResourcePatternResolver.java

@ -781,8 +781,9 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol @@ -781,8 +781,9 @@ public class PathMatchingResourcePatternResolver implements ResourcePatternResol
rootDir += "/";
}
Path rootPathForPattern = rootPath;
String resourcePattern = rootDir + StringUtils.cleanPath(subPattern);
Predicate<Path> isMatchingFile = path -> (Files.isRegularFile(path) &&
Predicate<Path> isMatchingFile = path -> (!path.equals(rootPathForPattern) &&
getPathMatcher().match(resourcePattern, StringUtils.cleanPath(path.toString())));
if (logger.isTraceEnabled()) {

16
spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

@ -25,7 +25,6 @@ import java.util.Arrays; @@ -25,7 +25,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@ -100,7 +99,6 @@ class PathMatchingResourcePatternResolverTests { @@ -100,7 +99,6 @@ class PathMatchingResourcePatternResolverTests {
assertExactSubPaths(pattern, pathPrefix, "support/resource#test1.txt", "support/resource#test2.txt");
}
@Disabled("Until gh-29333 is resolved")
@Test
void usingClasspathStarProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception {
String pattern = "classpath*:org/springframework/core/io/sup*";
@ -112,7 +110,6 @@ class PathMatchingResourcePatternResolverTests { @@ -112,7 +110,6 @@ class PathMatchingResourcePatternResolverTests {
assertThat(actualSubPaths).containsExactly("support");
}
@Disabled("Until gh-29333 is resolved")
@Test
void usingFileProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception {
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
@ -148,17 +145,15 @@ class PathMatchingResourcePatternResolverTests { @@ -148,17 +145,15 @@ class PathMatchingResourcePatternResolverTests {
assertThat(actualSubPaths).isEmpty();
}
@Disabled("Until gh-29333 is resolved")
@Test
void usingClasspathStarProtocolWithWildcardInPatternAndEndingWithSlashStarStar() throws Exception {
String pattern = "classpath*:org/springframework/core/io/sup*/**";
void usingClasspathStarProtocolWithWildcardInPatternAndEndingWithSuffixPattern() throws Exception {
String pattern = "classpath*:org/springframework/core/io/sup*/*.txt";
String pathPrefix = ".+org/springframework/core/io/";
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
// We DO find "support" if the pattern ENDS with "/**".
assertThat(actualSubPaths)
.containsExactlyInAnyOrder("support", "support/resource#test1.txt", "support/resource#test2.txt");
.containsExactlyInAnyOrder("support/resource#test1.txt", "support/resource#test2.txt");
}
private List<String> getSubPathsIgnoringClassFilesEtc(String pattern, String pathPrefix) throws IOException {
@ -173,7 +168,7 @@ class PathMatchingResourcePatternResolverTests { @@ -173,7 +168,7 @@ class PathMatchingResourcePatternResolverTests {
}
@Test
void usingFileProtocolWithoutWildcardInPatternAndEndingInSlashStarStar() throws Exception {
void usingFileProtocolWithoutWildcardInPatternAndEndingInSlashStarStar() {
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir);
String pathPrefix = ".+?resources/";
@ -184,9 +179,8 @@ class PathMatchingResourcePatternResolverTests { @@ -184,9 +179,8 @@ class PathMatchingResourcePatternResolverTests {
"scanned-resources/resource#test2.txt");
}
@Disabled("Until gh-29333 is resolved")
@Test
void usingFileProtocolWithWildcardInPatternAndEndingInSlashStarStar() throws Exception {
void usingFileProtocolWithWildcardInPatternAndEndingInSlashStarStar() {
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
String pattern = String.format("file:%s/scanned*resources/**", testResourcesDir);
String pathPrefix = ".+?resources/";

Loading…
Cancel
Save