diff --git a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java index 41db4dcff58..31b3d6938cb 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +16,16 @@ package org.springframework.core.io.support; -import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; +import java.io.UncheckedIOException; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; -import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.core.io.Resource; @@ -33,8 +35,9 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** - * If this test case fails, uncomment diagnostics in the - * {@link #assertProtocolAndFilenames} method. + * Tests for {@link PathMatchingResourcePatternResolver}. + * + *
If tests fail, uncomment the diagnostics in {@link #assertFilenames(String, boolean, String...)}.
*
* @author Oliver Hutchison
* @author Juergen Hoeller
@@ -44,124 +47,138 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
class PathMatchingResourcePatternResolverTests {
- private static final String[] CLASSES_IN_CORE_IO_SUPPORT =
- new String[] {"EncodedResource.class", "LocalizedResourceHelper.class",
- "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
- "PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class",
- "ResourcePatternResolver.class", "ResourcePatternUtils.class"};
+ private static final String[] CLASSES_IN_CORE_IO_SUPPORT = { "EncodedResource.class",
+ "LocalizedResourceHelper.class", "PathMatchingResourcePatternResolver.class", "PropertiesLoaderSupport.class",
+ "PropertiesLoaderUtils.class", "ResourceArrayPropertyEditor.class", "ResourcePatternResolver.class",
+ "ResourcePatternUtils.class", "SpringFactoriesLoader.class" };
- private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT =
- new String[] {"PathMatchingResourcePatternResolverTests.class"};
+ private static final String[] TEST_CLASSES_IN_CORE_IO_SUPPORT = { "PathMatchingResourcePatternResolverTests.class" };
- private static final String[] CLASSES_IN_REACTOR_UTIL_ANNOTATIONS =
- new String[] {"NonNull.class", "NonNullApi.class", "Nullable.class"};
-
- private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
+ private static final String[] CLASSES_IN_REACTOR_UTIL_ANNOTATION = { "NonNull.class", "NonNullApi.class", "Nullable.class" };
- @Test
- void invalidPrefixWithPatternElementInIt() throws IOException {
- assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() ->
- resolver.getResources("xx**:**/*.xy"));
+ private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
+
+
+ @Nested
+ class InvalidPatterns {
+
+ @Test
+ void invalidPrefixWithPatternElementInItThrowsException() {
+ assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> resolver.getResources("xx**:**/*.xy"));
+ }
+
}
- @Test
- void singleResourceOnFileSystem() throws IOException {
- Resource[] resources =
- resolver.getResources("org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class");
- assertThat(resources.length).isEqualTo(1);
- assertProtocolAndFilenames(resources, "file", "PathMatchingResourcePatternResolverTests.class");
+
+ @Nested
+ class FileSystemResources {
+
+ @Test
+ void singleResourceOnFileSystem() {
+ String pattern = "org/springframework/core/io/support/PathMatchingResourcePatternResolverTests.class";
+ assertExactFilenames(pattern, "PathMatchingResourcePatternResolverTests.class");
+ }
+
+ @Test
+ void classpathStarWithPatternOnFileSystem() {
+ String pattern = "classpath*:org/springframework/core/io/sup*/*.class";
+ String[] expectedFilenames = StringUtils.concatenateStringArrays(CLASSES_IN_CORE_IO_SUPPORT, TEST_CLASSES_IN_CORE_IO_SUPPORT);
+ assertFilenames(pattern, expectedFilenames);
+ }
+
+ @Nested
+ class WithHashtagsInTheirFileNames {
+
+ @Test
+ void usingClasspathStarProtocol() {
+ String pattern = "classpath*:org/springframework/core/io/**/resource#test*.txt";
+ assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
+ }
+
+ @Test
+ void usingFilePrototol() {
+ Path testResourcesDir = Paths.get("src/test/resources").toAbsolutePath();
+ String pattern = String.format("file:%s/scanned-resources/**", testResourcesDir);
+ assertExactFilenames(pattern, "resource#test1.txt", "resource#test2.txt");
+ }
+
+ }
+
}
- @Test
- void singleResourceInJar() throws IOException {
- Resource[] resources = resolver.getResources("org/reactivestreams/Publisher.class");
- assertThat(resources.length).isEqualTo(1);
- assertProtocolAndFilenames(resources, "jar", "Publisher.class");
+
+ @Nested
+ class JarResources {
+
+ @Test
+ void singleResourceInJar() {
+ String pattern = "org/reactivestreams/Publisher.class";
+ assertExactFilenames(pattern, "Publisher.class");
+ }
+
+ @Test
+ void singleResourceInRootOfJar() {
+ String pattern = "aspectj_1_5_0.dtd";
+ assertExactFilenames(pattern, "aspectj_1_5_0.dtd");
+ }
+
+ @Test
+ void classpathWithPatternInJar() {
+ String pattern = "classpath:reactor/util/annotation/*.class";
+ assertExactFilenames(pattern, CLASSES_IN_REACTOR_UTIL_ANNOTATION);
+ }
+
+ @Test
+ void classpathStarWithPatternInJar() {
+ String pattern = "classpath*:reactor/util/annotation/*.class";
+ assertExactFilenames(pattern, CLASSES_IN_REACTOR_UTIL_ANNOTATION);
+ }
+
+ // Fails in a native image -- https://github.com/oracle/graal/issues/5020
+ @Test
+ void rootPatternRetrievalInJarFiles() throws IOException {
+ assertThat(resolver.getResources("classpath*:aspectj*.dtd")).extracting(Resource::getFilename)
+ .as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar")
+ .containsExactly("aspectj_1_5_0.dtd");
+ }
+
}
- @Disabled
- @Test
- void classpathStarWithPatternOnFileSystem() throws IOException {
- Resource[] resources = resolver.getResources("classpath*:org/springframework/core/io/sup*/*.class");
- // Have to exclude Clover-generated class files here,
- // as we might be running as part of a Clover test run.
- List