From 945e5b92224ce58146a8f20c69f0a7f888860f7e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 13 Jul 2020 21:54:54 -0700 Subject: [PATCH] Add ConfigurationPropertySource.from method Add a factory method to `ConfigurationPropertySource` that allows a single Spring `PropertySource` instance to be adapted. Closes gh-22494 --- .../source/ConfigurationPropertySource.java | 17 ++++++- .../source/ConfigurationPropertySources.java | 4 +- .../ConfigurationPropertySourceTests.java | 48 +++++++++++++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourceTests.java diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySource.java index a1739294c3..cc4d1cc106 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -19,6 +19,7 @@ package org.springframework.boot.context.properties.source; import java.util.function.Predicate; import org.springframework.boot.origin.OriginTrackedValue; +import org.springframework.core.env.PropertySource; /** * A source of {@link ConfigurationProperty ConfigurationProperties}. @@ -81,4 +82,18 @@ public interface ConfigurationPropertySource { return null; } + /** + * Return a single new {@link ConfigurationPropertySource} adapted from the given + * Spring {@link PropertySource} or {@code null} if the source cannot be adapted. + * @param source the Spring property source to adapt + * @return an adapted source or {@code null} {@link SpringConfigurationPropertySource} + * @since 2.4.0 + */ + static ConfigurationPropertySource from(PropertySource source) { + if (source instanceof ConfigurationPropertySourcesPropertySource) { + return null; + } + return SpringConfigurationPropertySource.from(source); + } + } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java index 54b85381e6..57ba2e8459 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -110,7 +110,7 @@ public final class ConfigurationPropertySources { * {@link SpringConfigurationPropertySource} */ public static Iterable from(PropertySource source) { - return Collections.singleton(SpringConfigurationPropertySource.from(source)); + return Collections.singleton(ConfigurationPropertySource.from(source)); } /** diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourceTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourceTests.java new file mode 100644 index 0000000000..dda5128a53 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertySourceTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2012-2020 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.context.properties.source; + +import org.junit.jupiter.api.Test; + +import org.springframework.mock.env.MockPropertySource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +/** + * Tests for {@link ConfigurationPropertySource}. + * + * @author Phillip Webb + */ +class ConfigurationPropertySourceTests { + + @Test + void fromCreatesConfigurationPropertySourcesPropertySource() { + MockPropertySource source = new MockPropertySource(); + source.setProperty("spring", "boot"); + ConfigurationPropertySource adapted = ConfigurationPropertySource.from(source); + assertThat(adapted.getConfigurationProperty(ConfigurationPropertyName.of("spring")).getValue()) + .isEqualTo("boot"); + } + + @Test + void fromWhenSourceIsAlreadyConfigurationPropertySourcesPropertySourceReturnsNull() { + ConfigurationPropertySourcesPropertySource source = mock(ConfigurationPropertySourcesPropertySource.class); + assertThat(ConfigurationPropertySource.from(source)).isNull(); + } + +}