Skip to content

Commit eb2c1cb

Browse files
committed
Test encoding and Unicode normalization for scanned resources
This commit introduces tests which serve as "regression tests" for the behavior of PathMatchingResourcePatternResolver in Spring Framework 5.3.x with regard to URL-encoding and Unicode normalization of resource paths. Specifically, the new tests demonstrate that resource paths do NOT need to be decoded or normalized in 5.3.x. See gh-29243
1 parent 8d0ac21 commit eb2c1cb

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.japanese.バリューオブジェクト;
18+
19+
/**
20+
* This class and its package exist in order to test encoding and Unicode
21+
* normalization for resource paths discovered via classpath scanning.
22+
*
23+
* <p>"バリューオブジェクト" is Japanese for "value object".
24+
*/
25+
public class バリューオブジェクト {
26+
}

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,30 @@ void usingFilePrototol() {
105105

106106
}
107107

108+
@Nested
109+
class ContainingJapaneseCharactersInTheirPathsAndFileNames {
110+
111+
@Test
112+
void usingClasspathStarProtocol() {
113+
String pattern = "classpath*:example/japanese/バリューオブジェクト/**/*.class";
114+
String pathPrefix = ".+example/japanese/";
115+
116+
assertExactFilenames(pattern, "バリューオブジェクト.class");
117+
assertExactSubPaths(pattern, pathPrefix, "バリューオブジェクト/バリューオブジェクト.class");
118+
}
119+
120+
@Test
121+
void usingFilePrototol() {
122+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
123+
String pattern = String.format("file:%s/japanese-resources/バリューオブジェクト/**/*.text", testResourcesDir);
124+
String pathPrefix = ".+japanese-resources/";
125+
126+
assertExactFilenames(pattern, "バリューオブジェクト.text");
127+
assertExactSubPaths(pattern, pathPrefix, "バリューオブジェクト/バリューオブジェクト.text");
128+
}
129+
130+
}
131+
108132
}
109133

110134

@@ -181,4 +205,27 @@ private void assertFilenames(String pattern, boolean exactly, String... filename
181205
}
182206
}
183207

208+
private void assertExactSubPaths(String pattern, String pathPrefix, String... subPaths) {
209+
try {
210+
Resource[] resources = resolver.getResources(pattern);
211+
List<String> actualSubPaths = Arrays.stream(resources)
212+
.map(resource -> getPath(resource).replaceFirst(pathPrefix, ""))
213+
.sorted()
214+
.collect(Collectors.toList());
215+
assertThat(actualSubPaths).containsExactlyInAnyOrder(subPaths);
216+
}
217+
catch (IOException ex) {
218+
throw new UncheckedIOException(ex);
219+
}
220+
}
221+
222+
private String getPath(Resource resource) {
223+
try {
224+
return resource.getURL().getPath();
225+
}
226+
catch (IOException ex) {
227+
throw new UncheckedIOException(ex);
228+
}
229+
}
230+
184231
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value object

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
<suppress files="ResolvableType" checks="FinalClass"/>
3939
<suppress files="[\\/]src[\\/]testFixtures[\\/]java[\\/].+" checks="IllegalImport" id="bannedJUnitJupiterImports"/>
4040

41+
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]example[\\/]japanese[\\/].+" checks="TypeName" />
42+
4143
<!-- spring-expression -->
4244
<suppress files="ExpressionException" checks="MutableException"/>
4345
<suppress files="SpelMessage" checks="JavadocVariable|JavadocStyle"/>

0 commit comments

Comments
 (0)