|
24 | 24 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
25 | 25 | import org.springframework.context.ApplicationContext;
|
26 | 26 | import org.springframework.context.ApplicationContextAware;
|
| 27 | +import org.springframework.core.io.ByteArrayResource; |
| 28 | +import org.springframework.core.io.ClassPathResource; |
| 29 | +import org.springframework.core.io.FileSystemResource; |
| 30 | +import org.springframework.core.io.FileSystemResourceLoader; |
| 31 | +import org.springframework.core.io.FileUrlResource; |
| 32 | +import org.springframework.core.io.ProtocolResolver; |
| 33 | +import org.springframework.core.io.Resource; |
| 34 | +import org.springframework.core.io.ResourceLoader; |
27 | 35 | import org.springframework.core.metrics.jfr.FlightRecorderApplicationStartup;
|
28 | 36 |
|
| 37 | +import static java.nio.charset.StandardCharsets.UTF_8; |
29 | 38 | import static org.assertj.core.api.Assertions.assertThat;
|
30 | 39 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
31 | 40 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 41 | +import static org.assertj.core.api.InstanceOfAssertFactories.type; |
32 | 42 |
|
33 | 43 | /**
|
| 44 | + * Tests for {@link GenericApplicationContext}. |
| 45 | + * |
34 | 46 | * @author Juergen Hoeller
|
35 | 47 | * @author Chris Beams
|
| 48 | + * @author Sam Brannen |
36 | 49 | */
|
37 | 50 | class GenericApplicationContextTests {
|
38 | 51 |
|
@@ -209,6 +222,39 @@ void configureApplicationStartupOnBeanFactory() {
|
209 | 222 | assertThat(context.getBeanFactory().getApplicationStartup()).isEqualTo(applicationStartup);
|
210 | 223 | }
|
211 | 224 |
|
| 225 | + @Test |
| 226 | + void getResourceWithDefaultResourceLoader() { |
| 227 | + assertGetResourceSemantics(null, ClassPathResource.class); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + void getResourceWithCustomResourceLoader() { |
| 232 | + assertGetResourceSemantics(new FileSystemResourceLoader(), FileSystemResource.class); |
| 233 | + } |
| 234 | + |
| 235 | + private void assertGetResourceSemantics(ResourceLoader resourceLoader, Class<? extends Resource> defaultResouceType) { |
| 236 | + if (resourceLoader != null) { |
| 237 | + context.setResourceLoader(resourceLoader); |
| 238 | + } |
| 239 | + |
| 240 | + String pingLocation = "ping:foo"; |
| 241 | + String fileLocation = "file:foo"; |
| 242 | + |
| 243 | + Resource resource = context.getResource(pingLocation); |
| 244 | + assertThat(resource).isInstanceOf(defaultResouceType); |
| 245 | + resource = context.getResource(fileLocation); |
| 246 | + assertThat(resource).isInstanceOf(FileUrlResource.class); |
| 247 | + |
| 248 | + context.addProtocolResolver(new PingPongProtocolResolver()); |
| 249 | + |
| 250 | + resource = context.getResource(pingLocation); |
| 251 | + assertThat(resource).asInstanceOf(type(ByteArrayResource.class)) |
| 252 | + .extracting(bar -> new String(bar.getByteArray(), UTF_8)) |
| 253 | + .isEqualTo("pong:foo"); |
| 254 | + resource = context.getResource(fileLocation); |
| 255 | + assertThat(resource).isInstanceOf(FileUrlResource.class); |
| 256 | + } |
| 257 | + |
212 | 258 |
|
213 | 259 | static class BeanA {
|
214 | 260 |
|
@@ -236,4 +282,15 @@ public void setApplicationContext(ApplicationContext applicationContext) {
|
236 | 282 |
|
237 | 283 | static class BeanC {}
|
238 | 284 |
|
| 285 | + static class PingPongProtocolResolver implements ProtocolResolver { |
| 286 | + |
| 287 | + @Override |
| 288 | + public Resource resolve(String location, ResourceLoader resourceLoader) { |
| 289 | + if (location.startsWith("ping:")) { |
| 290 | + return new ByteArrayResource(("pong:" + location.substring(5)).getBytes(UTF_8)); |
| 291 | + } |
| 292 | + return null; |
| 293 | + } |
| 294 | + } |
| 295 | + |
239 | 296 | }
|
0 commit comments