|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.io.UncheckedIOException;
|
22 | 22 | import java.nio.file.Path;
|
| 23 | +import java.nio.file.Paths; |
23 | 24 | import java.util.Arrays;
|
24 | 25 | import java.util.List;
|
| 26 | +import java.util.stream.Collectors; |
25 | 27 |
|
| 28 | +import org.junit.jupiter.api.Disabled; |
26 | 29 | import org.junit.jupiter.api.Nested;
|
27 | 30 | import org.junit.jupiter.api.Test;
|
28 | 31 |
|
@@ -97,19 +100,106 @@ void usingClasspathStarProtocol() {
|
97 | 100 | assertExactSubPaths(pattern, pathPrefix, "support/resource#test1.txt", "support/resource#test2.txt");
|
98 | 101 | }
|
99 | 102 |
|
| 103 | + @Disabled("Until gh-29333 is resolved") |
100 | 104 | @Test
|
101 |
| - void usingFileProtocol() { |
102 |
| - Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath(); |
103 |
| - String pattern = "file:%s/scanned-resources/**".formatted(testResourcesDir); |
104 |
| - String pathPrefix = ".+scanned-resources/"; |
| 105 | + void usingClasspathStarProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception { |
| 106 | + String pattern = "classpath*:org/springframework/core/io/sup*"; |
| 107 | + String pathPrefix = ".+org/springframework/core/io/"; |
| 108 | + |
| 109 | + List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix); |
| 110 | + |
| 111 | + // We DO find "support" if the pattern does NOT end with a slash. |
| 112 | + assertThat(actualSubPaths).containsExactly("support"); |
| 113 | + } |
| 114 | + |
| 115 | + @Disabled("Until gh-29333 is resolved") |
| 116 | + @Test |
| 117 | + void usingFileProtocolWithWildcardInPatternAndNotEndingInSlash() throws Exception { |
| 118 | + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); |
| 119 | + String pattern = String.format("file:%s/org/springframework/core/io/sup*", testResourcesDir); |
| 120 | + String pathPrefix = ".+org/springframework/core/io/"; |
| 121 | + |
| 122 | + List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix); |
| 123 | + |
| 124 | + // We DO find "support" if the pattern does NOT end with a slash. |
| 125 | + assertThat(actualSubPaths).containsExactly("support"); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void usingClasspathStarProtocolWithWildcardInPatternAndEndingInSlash() throws Exception { |
| 130 | + String pattern = "classpath*:org/springframework/core/io/sup*/"; |
| 131 | + String pathPrefix = ".+org/springframework/core/io/"; |
| 132 | + |
| 133 | + List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix); |
105 | 134 |
|
| 135 | + // We do NOT find "support" if the pattern ENDS with a slash. |
| 136 | + assertThat(actualSubPaths).isEmpty(); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void usingFileProtocolWithWildcardInPatternAndEndingInSlash() throws Exception { |
| 141 | + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); |
| 142 | + String pattern = String.format("file:%s/org/springframework/core/io/sup*/", testResourcesDir); |
| 143 | + String pathPrefix = ".+org/springframework/core/io/"; |
| 144 | + |
| 145 | + List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix); |
| 146 | + |
| 147 | + // We do NOT find "support" if the pattern ENDS with a slash. |
| 148 | + assertThat(actualSubPaths).isEmpty(); |
| 149 | + } |
| 150 | + |
| 151 | + @Disabled("Until gh-29333 is resolved") |
| 152 | + @Test |
| 153 | + void usingClasspathStarProtocolWithWildcardInPatternAndEndingWithSlashStarStar() throws Exception { |
| 154 | + String pattern = "classpath*:org/springframework/core/io/sup*/**"; |
| 155 | + String pathPrefix = ".+org/springframework/core/io/"; |
| 156 | + |
| 157 | + List<String> actualSubPaths = getSubPathsIgnoringClassFilesEtc(pattern, pathPrefix); |
| 158 | + |
| 159 | + // We DO find "support" if the pattern ENDS with "/**". |
| 160 | + assertThat(actualSubPaths) |
| 161 | + .containsExactlyInAnyOrder("support", "support/resource#test1.txt", "support/resource#test2.txt"); |
| 162 | + } |
| 163 | + |
| 164 | + private List<String> getSubPathsIgnoringClassFilesEtc(String pattern, String pathPrefix) throws IOException { |
| 165 | + return Arrays.stream(resolver.getResources(pattern)) |
| 166 | + .map(resource -> getPath(resource).replaceFirst(pathPrefix, "")) |
| 167 | + .filter(name -> !name.endsWith(".class")) |
| 168 | + .filter(name -> !name.endsWith(".kt")) |
| 169 | + .filter(name -> !name.endsWith(".factories")) |
| 170 | + .distinct() |
| 171 | + .sorted() |
| 172 | + .collect(Collectors.toList()); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + void usingFileProtocolWithoutWildcardInPatternAndEndingInSlashStarStar() throws Exception { |
| 177 | + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); |
| 178 | + String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir); |
| 179 | + String pathPrefix = ".+?resources/"; |
| 180 | + |
| 181 | + // We do NOT find "scanned-resources" if the pattern ENDS with "/**" AND does NOT otherwise contain a wildcard. |
106 | 182 | assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
|
107 |
| - assertExactSubPaths(pattern, pathPrefix, "resource#test1.txt", "resource#test2.txt"); |
| 183 | + assertExactSubPaths(pattern, pathPrefix, "scanned-resources/resource#test1.txt", |
| 184 | + "scanned-resources/resource#test2.txt"); |
| 185 | + } |
| 186 | + |
| 187 | + @Disabled("Until gh-29333 is resolved") |
| 188 | + @Test |
| 189 | + void usingFileProtocolWithWildcardInPatternAndEndingInSlashStarStar() throws Exception { |
| 190 | + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); |
| 191 | + String pattern = String.format("file:%s/scanned*resources/**", testResourcesDir); |
| 192 | + String pathPrefix = ".+?resources/"; |
| 193 | + |
| 194 | + // We DO find "scanned-resources" if the pattern ENDS with "/**" AND DOES otherwise contain a wildcard. |
| 195 | + assertExactFilenames(pattern, "scanned-resources", "resource#test1.txt", "resource#test2.txt"); |
| 196 | + assertExactSubPaths(pattern, pathPrefix, "scanned-resources", "scanned-resources/resource#test1.txt", |
| 197 | + "scanned-resources/resource#test2.txt"); |
108 | 198 | }
|
109 | 199 |
|
110 | 200 | @Test
|
111 | 201 | void usingFileProtocolAndAssertingUrlAndUriSyntax() throws Exception {
|
112 |
| - Path testResourcesDir = Path.of("src/test/resources").toAbsolutePath(); |
| 202 | + Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath(); |
113 | 203 | String pattern = "file:%s/scanned-resources/**/resource#test1.txt".formatted(testResourcesDir);
|
114 | 204 | Resource[] resources = resolver.getResources(pattern);
|
115 | 205 | assertThat(resources).hasSize(1);
|
|
0 commit comments