|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.context; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.springframework.beans.BeansException; |
| 21 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 22 | +import org.springframework.boot.context.properties.source.ConfigurationPropertySources; |
| 23 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.core.env.ConfigurablePropertyResolver; |
| 27 | +import org.springframework.core.env.MutablePropertySources; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link ConfigurationPropertySourcesPlaceholderConfigurer}. |
| 33 | + * |
| 34 | + * @author Guirong Hu |
| 35 | + */ |
| 36 | +class ConfigurationPropertySourcesPlaceholderConfigurerTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + void propertyResolverIsOptimizedForPropertyPlaceholder() { |
| 40 | + ConfigurablePropertyResolver expected = ConfigurationPropertySources |
| 41 | + .createPropertyResolver(new MutablePropertySources()); |
| 42 | + |
| 43 | + new ApplicationContextRunner().withUserConfiguration(GetPropertyResolverPlaceholderConfigurerConfig.class) |
| 44 | + .run((context) -> assertThat( |
| 45 | + context.getBean(GetPropertyResolverPlaceholderConfigurer.class).getPropertyResolver()) |
| 46 | + .hasSameClassAs(expected)); |
| 47 | + } |
| 48 | + |
| 49 | + @Configuration(proxyBeanMethods = false) |
| 50 | + static class GetPropertyResolverPlaceholderConfigurerConfig { |
| 51 | + |
| 52 | + @Bean |
| 53 | + static GetPropertyResolverPlaceholderConfigurer getPropertyResolverPlaceholderConfigurer() { |
| 54 | + return new GetPropertyResolverPlaceholderConfigurer(); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + static class GetPropertyResolverPlaceholderConfigurer extends ConfigurationPropertySourcesPlaceholderConfigurer { |
| 60 | + |
| 61 | + private ConfigurablePropertyResolver propertyResolver; |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, |
| 65 | + ConfigurablePropertyResolver propertyResolver) throws BeansException { |
| 66 | + this.propertyResolver = propertyResolver; |
| 67 | + super.processProperties(beanFactoryToProcess, propertyResolver); |
| 68 | + } |
| 69 | + |
| 70 | + public ConfigurablePropertyResolver getPropertyResolver() { |
| 71 | + return this.propertyResolver; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments