28
28
import org .junit .jupiter .api .Nested ;
29
29
import org .junit .jupiter .api .Test ;
30
30
31
+ import org .springframework .core .io .FileSystemResource ;
31
32
import org .springframework .core .io .Resource ;
32
33
import org .springframework .util .StringUtils ;
33
34
48
49
class PathMatchingResourcePatternResolverTests {
49
50
50
51
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" };
54
55
55
56
private static final String [] TEST_CLASSES_IN_CORE_IO_SUPPORT = { "PathMatchingResourcePatternResolverTests.class" };
56
57
@@ -67,7 +68,6 @@ class InvalidPatterns {
67
68
void invalidPrefixWithPatternElementInItThrowsException () {
68
69
assertThatExceptionOfType (FileNotFoundException .class ).isThrownBy (() -> resolver .getResources ("xx**:**/*.xy" ));
69
70
}
70
-
71
71
}
72
72
73
73
@@ -93,18 +93,22 @@ class WithHashtagsInTheirFileNames {
93
93
@ Test
94
94
void usingClasspathStarProtocol () {
95
95
String pattern = "classpath*:org/springframework/core/io/**/resource#test*.txt" ;
96
+ String pathPrefix = ".+org/springframework/core/io/" ;
97
+
96
98
assertExactFilenames (pattern , "resource#test1.txt" , "resource#test2.txt" );
99
+ assertExactSubPaths (pattern , pathPrefix , "support/resource#test1.txt" , "support/resource#test2.txt" );
97
100
}
98
101
99
102
@ Test
100
- void usingFilePrototol () {
103
+ void usingFileProtocol () {
101
104
Path testResourcesDir = Paths .get ("src/test/resources" ).toAbsolutePath ();
102
105
String pattern = String .format ("file:%s/scanned-resources/**" , testResourcesDir );
106
+ String pathPrefix = ".+scanned-resources/" ;
107
+
103
108
assertExactFilenames (pattern , "resource#test1.txt" , "resource#test2.txt" );
109
+ assertExactSubPaths (pattern , pathPrefix , "resource#test1.txt" , "resource#test2.txt" );
104
110
}
105
-
106
111
}
107
-
108
112
}
109
113
110
114
@@ -142,7 +146,6 @@ void rootPatternRetrievalInJarFiles() throws IOException {
142
146
.as ("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar" )
143
147
.containsExactly ("aspectj_1_5_0.dtd" );
144
148
}
145
-
146
149
}
147
150
148
151
@@ -181,4 +184,30 @@ private void assertFilenames(String pattern, boolean exactly, String... filename
181
184
}
182
185
}
183
186
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
+
184
213
}
0 commit comments