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
This commit is contained in:
Sam Brannen
2022-10-04 15:50:33 +02:00
parent 5c447bb7a4
commit dd814b6d30

View File

@@ -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<String> 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);
}
}
}