|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2017 the original author or authors. |
| 2 | + * Copyright 2012-2018 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.
|
|
40 | 40 | import org.springframework.context.annotation.Bean;
|
41 | 41 | import org.springframework.context.annotation.Configuration;
|
42 | 42 | import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
| 43 | +import org.springframework.core.io.ClassPathResource; |
| 44 | +import org.springframework.core.io.ProtocolResolver; |
| 45 | +import org.springframework.core.io.Resource; |
| 46 | +import org.springframework.core.io.ResourceLoader; |
43 | 47 | import org.springframework.mock.env.MockEnvironment;
|
44 | 48 | import org.springframework.test.context.support.TestPropertySourceUtils;
|
45 | 49 | import org.springframework.test.util.ReflectionTestUtils;
|
|
52 | 56 |
|
53 | 57 | import static org.assertj.core.api.Assertions.assertThat;
|
54 | 58 | import static org.junit.Assert.fail;
|
| 59 | +import static org.mockito.BDDMockito.given; |
| 60 | +import static org.mockito.Matchers.any; |
| 61 | +import static org.mockito.Matchers.anyString; |
| 62 | +import static org.mockito.Matchers.eq; |
| 63 | +import static org.mockito.Mockito.mock; |
| 64 | +import static org.mockito.Mockito.verify; |
55 | 65 |
|
56 | 66 | /**
|
57 | 67 | * Tests for {@link ConfigurationPropertiesBindingPostProcessor}.
|
@@ -366,6 +376,38 @@ private void assertBindingFailure(int errorCount) {
|
366 | 376 | }
|
367 | 377 | }
|
368 | 378 |
|
| 379 | + @Test |
| 380 | + public void customProtocolResolverIsInvoked() { |
| 381 | + this.context = new AnnotationConfigApplicationContext(); |
| 382 | + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, |
| 383 | + "test.resource=application.properties"); |
| 384 | + ProtocolResolver protocolResolver = mock(ProtocolResolver.class); |
| 385 | + given(protocolResolver.resolve(anyString(), any(ResourceLoader.class))) |
| 386 | + .willReturn(null); |
| 387 | + this.context.addProtocolResolver(protocolResolver); |
| 388 | + this.context.register(PropertiesWithResource.class); |
| 389 | + this.context.refresh(); |
| 390 | + verify(protocolResolver).resolve(eq("application.properties"), |
| 391 | + any(ResourceLoader.class)); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void customProtocolResolver() { |
| 396 | + this.context = new AnnotationConfigApplicationContext(); |
| 397 | + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, |
| 398 | + "test.resource=test:/application.properties"); |
| 399 | + this.context.addProtocolResolver(new TestProtocolResolver()); |
| 400 | + this.context.register(PropertiesWithResource.class); |
| 401 | + this.context.refresh(); |
| 402 | + Resource resource = this.context.getBean(PropertiesWithResource.class) |
| 403 | + .getResource(); |
| 404 | + assertThat(resource).isNotNull(); |
| 405 | + assertThat(resource).isInstanceOf(ClassPathResource.class); |
| 406 | + assertThat(resource.exists()).isTrue(); |
| 407 | + assertThat(((ClassPathResource) resource).getPath()) |
| 408 | + .isEqualTo("application.properties"); |
| 409 | + } |
| 410 | + |
369 | 411 | @Configuration
|
370 | 412 | @EnableConfigurationProperties
|
371 | 413 | public static class TestConfigurationWithValidatingSetter {
|
@@ -819,4 +861,35 @@ public void setName(String name) {
|
819 | 861 |
|
820 | 862 | }
|
821 | 863 |
|
| 864 | + @Configuration |
| 865 | + @EnableConfigurationProperties |
| 866 | + @ConfigurationProperties(prefix = "test") |
| 867 | + public static class PropertiesWithResource { |
| 868 | + |
| 869 | + private Resource resource; |
| 870 | + |
| 871 | + public Resource getResource() { |
| 872 | + return this.resource; |
| 873 | + } |
| 874 | + |
| 875 | + public void setResource(Resource resource) { |
| 876 | + this.resource = resource; |
| 877 | + } |
| 878 | + |
| 879 | + } |
| 880 | + |
| 881 | + private static class TestProtocolResolver implements ProtocolResolver { |
| 882 | + |
| 883 | + public static final String PREFIX = "test:/"; |
| 884 | + |
| 885 | + @Override |
| 886 | + public Resource resolve(String location, ResourceLoader resourceLoader) { |
| 887 | + if (location.startsWith(PREFIX)) { |
| 888 | + String path = location.substring(PREFIX.length(), location.length()); |
| 889 | + return new ClassPathResource(path); |
| 890 | + } |
| 891 | + return null; |
| 892 | + } |
| 893 | + } |
| 894 | + |
822 | 895 | }
|
0 commit comments