Skip to content

Commit 3dc60f1

Browse files
committed
Backport changes to PathMatchingResourcePatternResolverTests
1 parent fee3817 commit 3dc60f1

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.junit.jupiter.api.Nested;
2929
import org.junit.jupiter.api.Test;
3030

31+
import org.springframework.core.io.FileSystemResource;
3132
import org.springframework.core.io.Resource;
3233
import org.springframework.util.StringUtils;
3334

@@ -48,9 +49,9 @@
4849
class PathMatchingResourcePatternResolverTests {
4950

5051
private static final String[] CLASSES_IN_CORE_IO_SUPPORT = { "EncodedResource.class",
51-
"LocalizedResourceHelper.class", "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
52-
"PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class",
53-
"ResourcePatternUtils.class", "SpringFactoriesLoader.class" };
52+
"LocalizedResourceHelper.class", "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
53+
"PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class",
54+
"ResourcePatternUtils.class", "SpringFactoriesLoader.class" };
5455

5556
private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT = { "PathMatchingResourcePatternResolverTests.class" };
5657

@@ -67,7 +68,6 @@ class InvalidPatterns {
6768
void invalidPrefixWithPatternElementInItThrowsException() {
6869
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resolver.getResources("xx**:**/*.xy"));
6970
}
70-
7171
}
7272

7373

@@ -93,18 +93,22 @@ class WithHashtagsInTheirFileNames {
9393
@Test
9494
void usingClasspathStarProtocol() {
9595
String pattern = "classpath*:org/springframework/core/io/**/resource#test*.txt";
96+
String pathPrefix = ".+org/springframework/core/io/";
97+
9698
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
99+
assertExactSubPaths(pattern, pathPrefix, "support/resource#test1.txt", "support/resource#test2.txt");
97100
}
98101

99102
@Test
100-
void usingFilePrototol() {
103+
void usingFileProtocol() {
101104
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
102105
String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir);
106+
String pathPrefix = ".+scanned-resources/";
107+
103108
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
109+
assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt");
104110
}
105-
106111
}
107-
108112
}
109113

110114

@@ -142,7 +146,6 @@ void rootPatternRetrievalInJarFiles() throws IOException {
142146
.as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
143147
.containsExactly("aspectj_1_5_0.dtd");
144148
}
145-
146149
}
147150

148151

@@ -181,4 +184,30 @@ private void assertFilenames(String pattern, boolean exactly, String... filename
181184
}
182185
}
183186

187+
private void assertExactSubPaths(String pattern, String pathPrefix, String... subPaths) {
188+
try {
189+
Resource[] resources = resolver.getResources(pattern);
190+
List<String> actualSubPaths = Arrays.stream(resources)
191+
.map(resource -> getPath(resource).replaceFirst(pathPrefix, ""))
192+
.sorted()
193+
.collect(Collectors.toList());
194+
assertThat(actualSubPaths).containsExactlyInAnyOrder(subPaths);
195+
}
196+
catch (IOException ex) {
197+
throw new UncheckedIOException(ex);
198+
}
199+
}
200+
201+
private String getPath(Resource resource) {
202+
// Tests fail if we use resouce.getURL().getPath(). They would also fail on Mac OS when
203+
// using resouce.getURI().getPath() if the resource paths are not Unicode normalized.
204+
//
205+
// On the JVM, all tests should pass when using resouce.getFile().getPath(); however,
206+
// we use FileSystemResource#getPath since this test class is sometimes run within a
207+
// GraalVM native image which cannot support Path#toFile.
208+
//
209+
// See: https://github.com/spring-projects/spring-framework/issues/29243
210+
return ((FileSystemResource) resource).getPath();
211+
}
212+
184213
}

0 commit comments

Comments
 (0)