Skip to content

Commit 0048756

Browse files
committed
Merge branch '5.3.x'
# Conflicts: # spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java
2 parents eadb003 + 652781c commit 0048756

File tree

1 file changed

+96
-6
lines changed

1 file changed

+96
-6
lines changed

Diff for: spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java

+96-6
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import java.io.IOException;
2121
import java.io.UncheckedIOException;
2222
import java.nio.file.Path;
23+
import java.nio.file.Paths;
2324
import java.util.Arrays;
2425
import java.util.List;
26+
import java.util.stream.Collectors;
2527

28+
import org.junit.jupiter.api.Disabled;
2629
import org.junit.jupiter.api.Nested;
2730
import org.junit.jupiter.api.Test;
2831

@@ -97,19 +100,106 @@ void usingClasspathStarProtocol() {
97100
assertExactSubPaths(pattern, pathPrefix, "support/resource#test1.txt", "support/resource#test2.txt");
98101
}
99102

103+
@Disabled("Until gh-29333 is resolved")
100104
@Test
101-
void usingFileProtocol() {
102-
Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();
103-
String pattern = "file:%s/scanned-resources/**".formatted(testResourcesDir);
104-
String pathPrefix = ".+scanned-resources/";
105+
void usingClasspathStarProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception {
106+
String pattern = "classpath*:org/springframework/core/io/sup*";
107+
String pathPrefix = ".+org/springframework/core/io/";
108+
109+
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
110+
111+
// We DO find "support" if the pattern does NOT end with a slash.
112+
assertThat(actualSubPaths).containsExactly("support");
113+
}
114+
115+
@Disabled("Until gh-29333 is resolved")
116+
@Test
117+
void usingFileProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception {
118+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
119+
String pattern = String.format("file:%s/org/springframework/core/io/sup*", testResourcesDir);
120+
String pathPrefix = ".+org/springframework/core/io/";
121+
122+
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
123+
124+
// We DO find "support" if the pattern does NOT end with a slash.
125+
assertThat(actualSubPaths).containsExactly("support");
126+
}
127+
128+
@Test
129+
void usingClasspathStarProtocolWithWildcardInPatternAndEndingInSlash() throws Exception {
130+
String pattern = "classpath*:org/springframework/core/io/sup*/";
131+
String pathPrefix = ".+org/springframework/core/io/";
132+
133+
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
105134

135+
// We do NOT find "support" if the pattern ENDS with a slash.
136+
assertThat(actualSubPaths).isEmpty();
137+
}
138+
139+
@Test
140+
void usingFileProtocolWithWildcardInPatternAndEndingInSlash() throws Exception {
141+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
142+
String pattern = String.format("file:%s/org/springframework/core/io/sup*/", testResourcesDir);
143+
String pathPrefix = ".+org/springframework/core/io/";
144+
145+
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
146+
147+
// We do NOT find "support" if the pattern ENDS with a slash.
148+
assertThat(actualSubPaths).isEmpty();
149+
}
150+
151+
@Disabled("Until gh-29333 is resolved")
152+
@Test
153+
void usingClasspathStarProtocolWithWildcardInPatternAndEndingWithSlashStarStar() throws Exception {
154+
String pattern = "classpath*:org/springframework/core/io/sup*/**";
155+
String pathPrefix = ".+org/springframework/core/io/";
156+
157+
List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix);
158+
159+
// We DO find "support" if the pattern ENDS with "/**".
160+
assertThat(actualSubPaths)
161+
.containsExactlyInAnyOrder("support", "support/resource#test1.txt", "support/resource#test2.txt");
162+
}
163+
164+
private List<String> getSubPathsIgnoringClassFilesEtc(String pattern, String pathPrefix) throws IOException {
165+
return Arrays.stream(resolver.getResources(pattern))
166+
.map(resource -> getPath(resource).replaceFirst(pathPrefix, ""))
167+
.filter(name -> !name.endsWith(".class"))
168+
.filter(name -> !name.endsWith(".kt"))
169+
.filter(name -> !name.endsWith(".factories"))
170+
.distinct()
171+
.sorted()
172+
.collect(Collectors.toList());
173+
}
174+
175+
@Test
176+
void usingFileProtocolWithoutWildcardInPatternAndEndingInSlashStarStar() throws Exception {
177+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
178+
String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir);
179+
String pathPrefix = ".+?resources/";
180+
181+
// We do NOT find "scanned-resources" if the pattern ENDS with "/**" AND does NOT otherwise contain a wildcard.
106182
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
107-
assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt");
183+
assertExactSubPaths(pattern, pathPrefix, "scanned-resources/resource#test1.txt",
184+
"scanned-resources/resource#test2.txt");
185+
}
186+
187+
@Disabled("Until gh-29333 is resolved")
188+
@Test
189+
void usingFileProtocolWithWildcardInPatternAndEndingInSlashStarStar() throws Exception {
190+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
191+
String pattern = String.format("file:%s/scanned*resources/**", testResourcesDir);
192+
String pathPrefix = ".+?resources/";
193+
194+
// We DO find "scanned-resources" if the pattern ENDS with "/**" AND DOES otherwise contain a wildcard.
195+
assertExactFilenames(pattern, "scanned-resources", "resource#test1.txt", "resource#test2.txt");
196+
assertExactSubPaths(pattern, pathPrefix, "scanned-resources", "scanned-resources/resource#test1.txt",
197+
"scanned-resources/resource#test2.txt");
108198
}
109199

110200
@Test
111201
void usingFileProtocolAndAssertingUrlAndUriSyntax() throws Exception {
112-
Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath();
202+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
113203
String pattern = "file:%s/scanned-resources/**/resource#test1.txt".formatted(testResourcesDir);
114204
Resource[] resources = resolver.getResources(pattern);
115205
assertThat(resources).hasSize(1);

0 commit comments

Comments
 (0)