|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.core.io.support;
|
18 | 18 |
|
19 |
| -import java.io.File; |
20 | 19 | import java.io.FileNotFoundException;
|
21 | 20 | 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; |
23 | 24 | import java.util.Arrays;
|
24 | 25 | import java.util.List;
|
| 26 | +import java.util.stream.Collectors; |
25 | 27 |
|
26 |
| -import org.junit.jupiter.api.Disabled; |
| 28 | +import org.junit.jupiter.api.Nested; |
27 | 29 | import org.junit.jupiter.api.Test;
|
28 | 30 |
|
29 | 31 | import org.springframework.core.io.Resource;
|
|
33 | 35 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
34 | 36 |
|
35 | 37 | /**
|
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...)}. |
38 | 41 | *
|
39 | 42 | * @author Oliver Hutchison
|
40 | 43 | * @author Juergen Hoeller
|
|
44 | 47 | */
|
45 | 48 | class PathMatchingResourcePatternResolverTests {
|
46 | 49 |
|
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" }; |
52 | 54 |
|
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" }; |
55 | 56 |
|
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" }; |
58 | 58 |
|
59 |
| - private PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
60 | 59 |
|
| 60 | + private final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); |
61 | 61 |
|
62 |
| - @Test |
63 |
| - void invalidPrefixWithPatternElementInIt() throws IOException { |
64 |
| - assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> |
65 |
| - resolver.getResources("xx**:**/*.xy")); |
66 |
| - } |
67 | 62 |
|
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 | + } |
75 | 70 |
|
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"); |
81 | 71 | }
|
82 | 72 |
|
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"); |
94 | 81 | }
|
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 |
| - } |
99 | 82 |
|
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 | + } |
106 | 89 |
|
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 { |
112 | 92 |
|
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 | + } |
118 | 98 |
|
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"); |
127 | 104 | }
|
| 105 | + |
128 | 106 | }
|
129 |
| - assertThat(found).as("Could not find aspectj_1_5_0.dtd in the root of the aspectjweaver jar").isTrue(); |
| 107 | + |
130 | 108 | }
|
131 | 109 |
|
132 | 110 |
|
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"); |
159 | 118 | }
|
| 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 | + |
160 | 146 | }
|
161 | 147 |
|
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 | + } |
165 | 182 | }
|
166 | 183 |
|
167 | 184 | }
|
0 commit comments