Skip to content

Commit 945e5b9

Browse files
committed
Add ConfigurationPropertySource.from method
Add a factory method to `ConfigurationPropertySource` that allows a single Spring `PropertySource` instance to be adapted. Closes gh-22494
1 parent 078e146 commit 945e5b9

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySource.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.function.Predicate;
2020

2121
import org.springframework.boot.origin.OriginTrackedValue;
22+
import org.springframework.core.env.PropertySource;
2223

2324
/**
2425
* A source of {@link ConfigurationProperty ConfigurationProperties}.
@@ -81,4 +82,18 @@ default Object getUnderlyingSource() {
8182
return null;
8283
}
8384

85+
/**
86+
* Return a single new {@link ConfigurationPropertySource} adapted from the given
87+
* Spring {@link PropertySource} or {@code null} if the source cannot be adapted.
88+
* @param source the Spring property source to adapt
89+
* @return an adapted source or {@code null} {@link SpringConfigurationPropertySource}
90+
* @since 2.4.0
91+
*/
92+
static ConfigurationPropertySource from(PropertySource<?> source) {
93+
if (source instanceof ConfigurationPropertySourcesPropertySource) {
94+
return null;
95+
}
96+
return SpringConfigurationPropertySource.from(source);
97+
}
98+
8499
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertySources.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,7 +110,7 @@ public static Iterable<ConfigurationPropertySource> get(Environment environment)
110110
* {@link SpringConfigurationPropertySource}
111111
*/
112112
public static Iterable<ConfigurationPropertySource> from(PropertySource<?> source) {
113-
return Collections.singleton(SpringConfigurationPropertySource.from(source));
113+
return Collections.singleton(ConfigurationPropertySource.from(source));
114114
}
115115

116116
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2020 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.context.properties.source;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.mock.env.MockPropertySource;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.mockito.Mockito.mock;
25+
26+
/**
27+
* Tests for {@link ConfigurationPropertySource}.
28+
*
29+
* @author Phillip Webb
30+
*/
31+
class ConfigurationPropertySourceTests {
32+
33+
@Test
34+
void fromCreatesConfigurationPropertySourcesPropertySource() {
35+
MockPropertySource source = new MockPropertySource();
36+
source.setProperty("spring", "boot");
37+
ConfigurationPropertySource adapted = ConfigurationPropertySource.from(source);
38+
assertThat(adapted.getConfigurationProperty(ConfigurationPropertyName.of("spring")).getValue())
39+
.isEqualTo("boot");
40+
}
41+
42+
@Test
43+
void fromWhenSourceIsAlreadyConfigurationPropertySourcesPropertySourceReturnsNull() {
44+
ConfigurationPropertySourcesPropertySource source = mock(ConfigurationPropertySourcesPropertySource.class);
45+
assertThat(ConfigurationPropertySource.from(source)).isNull();
46+
}
47+
48+
}

0 commit comments

Comments
 (0)