Skip to content

Commit 7241c30

Browse files
committed
Backport changes to PathMatchingResourcePatternResolverTests
1 parent 777caef commit 7241c30

File tree

3 files changed

+121
-102
lines changed

3 files changed

+121
-102
lines changed
Lines changed: 119 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,16 @@
1616

1717
package org.springframework.core.io.support;
1818

19-
import java.io.File;
2019
import java.io.FileNotFoundException;
2120
import java.io.IOException;
22-
import java.util.ArrayList;
21+
import java.io.UncheckedIOException;
22+
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

26-
import org.junit.jupiter.api.Disabled;
28+
import org.junit.jupiter.api.Nested;
2729
import org.junit.jupiter.api.Test;
2830

2931
import org.springframework.core.io.Resource;
@@ -33,8 +35,9 @@
3335
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3436

3537
/**
36-
* If this test case fails, uncomment diagnostics in the
37-
* {@link #assertProtocolAndFilenames} method.
38+
* Tests for {@link PathMatchingResourcePatternResolver}.
39+
*
40+
* <p>If tests fail, uncomment the diagnostics in {@link #assertFilenames(String, boolean, String...)}.
3841
*
3942
* @author Oliver Hutchison
4043
* @author Juergen Hoeller
@@ -44,124 +47,138 @@
4447
*/
4548
class PathMatchingResourcePatternResolverTests {
4649

47-
private static final String[] CLASSES_IN_CORE_IO_SUPPORT =
48-
new String[] {"EncodedResource.class", "LocalizedResourceHelper.class",
49-
"PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
50-
"PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class",
51-
"ResourcePatternResolver.class", "ResourcePatternUtils.class"};
50+
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" };
5254

53-
private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT =
54-
new String[] {"PathMatchingResourcePatternResolverTests.class"};
55+
private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT = { "PathMatchingResourcePatternResolverTests.class" };
5556

56-
private static final String[] CLASSES_IN_REACTOR_UTIL_ANNOTATIONS =
57-
new String[] {"NonNull.class", "NonNullApi.class", "Nullable.class"};
57+
private static final String[] CLASSES_IN_REACTOR_UTIL_ANNOTATION = { "NonNull.class", "NonNullApi.class", "Nullable.class" };
5858

59-
private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
6059

60+
private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
6161

62-
@Test
63-
void invalidPrefixWithPatternElementInIt() throws IOException {
64-
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() ->
65-
resolver.getResources("xx**:**/*.xy"));
66-
}
6762

68-
@Test
69-
void singleResourceOnFileSystem() throws IOException {
70-
Resource[] resources =
71-
resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class");
72-
assertThat(resources.length).isEqualTo(1);
73-
assertProtocolAndFilenames(resources, "file", "PathMatchingResourcePatternResolverTests.class");
74-
}
63+
@Nested
64+
class InvalidPatterns {
65+
66+
@Test
67+
void invalidPrefixWithPatternElementInItThrowsException() {
68+
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resolver.getResources("xx**:**/*.xy"));
69+
}
7570

76-
@Test
77-
void singleResourceInJar() throws IOException {
78-
Resource[] resources = resolver.getResources("org/reactivestreams/Publisher.class");
79-
assertThat(resources.length).isEqualTo(1);
80-
assertProtocolAndFilenames(resources, "jar", "Publisher.class");
8171
}
8272

83-
@Disabled
84-
@Test
85-
void classpathStarWithPatternOnFileSystem() throws IOException {
86-
Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class");
87-
// Have to exclude Clover-generated class files here,
88-
// as we might be running as part of a Clover test run.
89-
List<Resource> noCloverResources = new ArrayList<>();
90-
for (Resource resource : resources) {
91-
if (!resource.getFilename().contains("$__CLOVER_")) {
92-
noCloverResources.add(resource);
93-
}
73+
74+
@Nested
75+
class FileSystemResources {
76+
77+
@Test
78+
void singleResourceOnFileSystem() {
79+
String pattern = "org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class";
80+
assertExactFilenames(pattern, "PathMatchingResourcePatternResolverTests.class");
9481
}
95-
resources = noCloverResources.toArray(new Resource[0]);
96-
assertProtocolAndFilenames(resources, "file",
97-
StringUtils.concatenateStringArrays(CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT));
98-
}
9982

100-
@Test
101-
void getResourcesOnFileSystemContainingHashtagsInTheirFileNames() throws IOException {
102-
Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/**/resource#test*.txt");
103-
assertThat(resources).extracting(Resource::getFile).extracting(File::getName)
104-
.containsExactlyInAnyOrder("resource#test1.txt", "resource#test2.txt");
105-
}
83+
@Test
84+
void classpathStarWithPatternOnFileSystem() {
85+
String pattern = "classpath*:org/springframework/core/io/sup*/*.class";
86+
String[] expectedFilenames = StringUtils.concatenateStringArrays(CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT);
87+
assertFilenames(pattern, expectedFilenames);
88+
}
10689

107-
@Test
108-
void classpathWithPatternInJar() throws IOException {
109-
Resource[] resources = resolver.getResources("classpath:reactor/util/annotation/*.class");
110-
assertProtocolAndFilenames(resources, "jar", CLASSES_IN_REACTOR_UTIL_ANNOTATIONS);
111-
}
90+
@Nested
91+
class WithHashtagsInTheirFileNames {
11292

113-
@Test
114-
void classpathStarWithPatternInJar() throws IOException {
115-
Resource[] resources = resolver.getResources("classpath*:reactor/util/annotation/*.class");
116-
assertProtocolAndFilenames(resources, "jar", CLASSES_IN_REACTOR_UTIL_ANNOTATIONS);
117-
}
93+
@Test
94+
void usingClasspathStarProtocol() {
95+
String pattern = "classpath*:org/springframework/core/io/**/resource#test*.txt";
96+
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
97+
}
11898

119-
@Test
120-
void rootPatternRetrievalInJarFiles() throws IOException {
121-
Resource[] resources = resolver.getResources("classpath*:*.dtd");
122-
boolean found = false;
123-
for (Resource resource : resources) {
124-
if (resource.getFilename().equals("aspectj_1_5_0.dtd")) {
125-
found = true;
126-
break;
99+
@Test
100+
void usingFilePrototol() {
101+
Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
102+
String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir);
103+
assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
127104
}
105+
128106
}
129-
assertThat(found).as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar").isTrue();
107+
130108
}
131109

132110

133-
private void assertProtocolAndFilenames(Resource[] resources, String protocol, String... filenames)
134-
throws IOException {
135-
136-
// Uncomment the following if you encounter problems with matching against the file system
137-
// It shows file locations.
138-
// String[] actualNames = new String[resources.length];
139-
// for (int i = 0; i < resources.length; i++) {
140-
// actualNames[i] = resources[i].getFilename();
141-
// }
142-
// List sortedActualNames = new LinkedList(Arrays.asList(actualNames));
143-
// List expectedNames = new LinkedList(Arrays.asList(fileNames));
144-
// Collections.sort(sortedActualNames);
145-
// Collections.sort(expectedNames);
146-
//
147-
// System.out.println("-----------");
148-
// System.out.println("Expected: " + StringUtils.collectionToCommaDelimitedString(expectedNames));
149-
// System.out.println("Actual: " + StringUtils.collectionToCommaDelimitedString(sortedActualNames));
150-
// for (int i = 0; i < resources.length; i++) {
151-
// System.out.println(resources[i]);
152-
// }
153-
154-
assertThat(resources.length).as("Correct number of files found").isEqualTo(filenames.length);
155-
for (Resource resource : resources) {
156-
String actualProtocol = resource.getURL().getProtocol();
157-
assertThat(actualProtocol).isEqualTo(protocol);
158-
assertFilenameIn(resource, filenames);
111+
@Nested
112+
class JarResources {
113+
114+
@Test
115+
void singleResourceInJar() {
116+
String pattern = "org/reactivestreams/Publisher.class";
117+
assertExactFilenames(pattern, "Publisher.class");
159118
}
119+
120+
@Test
121+
void singleResourceInRootOfJar() {
122+
String pattern = "aspectj_1_5_0.dtd";
123+
assertExactFilenames(pattern, "aspectj_1_5_0.dtd");
124+
}
125+
126+
@Test
127+
void classpathWithPatternInJar() {
128+
String pattern = "classpath:reactor/util/annotation/*.class";
129+
assertExactFilenames(pattern, CLASSES_IN_REACTOR_UTIL_ANNOTATION);
130+
}
131+
132+
@Test
133+
void classpathStarWithPatternInJar() {
134+
String pattern = "classpath*:reactor/util/annotation/*.class";
135+
assertExactFilenames(pattern, CLASSES_IN_REACTOR_UTIL_ANNOTATION);
136+
}
137+
138+
// Fails in a native image -- https://github.com/oracle/graal/issues/5020
139+
@Test
140+
void rootPatternRetrievalInJarFiles() throws IOException {
141+
assertThat(resolver.getResources("classpath*:aspectj*.dtd")).extracting(Resource::getFilename)
142+
.as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
143+
.containsExactly("aspectj_1_5_0.dtd");
144+
}
145+
160146
}
161147

162-
private void assertFilenameIn(Resource resource, String... filenames) {
163-
String filename = resource.getFilename();
164-
assertThat(Arrays.stream(filenames).anyMatch(filename::endsWith)).as(resource + " does not have a filename that matches any of the specified names").isTrue();
148+
149+
private void assertFilenames(String pattern, String... filenames) {
150+
assertFilenames(pattern, false, filenames);
151+
}
152+
153+
private void assertExactFilenames(String pattern, String... filenames) {
154+
assertFilenames(pattern, true, filenames);
155+
}
156+
157+
private void assertFilenames(String pattern, boolean exactly, String... filenames) {
158+
try {
159+
Resource[] resources = resolver.getResources(pattern);
160+
List<String> actualNames = Arrays.stream(resources)
161+
.map(Resource::getFilename)
162+
.sorted()
163+
.collect(Collectors.toList());
164+
165+
// Uncomment the following if you encounter problems with matching against the file system.
166+
// List<String> expectedNames = Arrays.stream(filenames).sorted().toList();
167+
// System.out.println("----------------------------------------------------------------------");
168+
// System.out.println("Expected: " + expectedNames);
169+
// System.out.println("Actual: " + actualNames);
170+
// Arrays.stream(resources).forEach(System.out::println);
171+
172+
if (exactly) {
173+
assertThat(actualNames).as("subset of files found").containsExactlyInAnyOrder(filenames);
174+
}
175+
else {
176+
assertThat(actualNames).as("subset of files found").contains(filenames);
177+
}
178+
}
179+
catch (IOException ex) {
180+
throw new UncheckedIOException(ex);
181+
}
165182
}
166183

167184
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test 2

0 commit comments

Comments
 (0)