Add ConfigurationPropertySource.from method
Add a factory method to `ConfigurationPropertySource` that allows a single Spring `PropertySource` instance to be adapted. Closes gh-22494
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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<ConfigurationPropertySource> from(PropertySource<?> source) {
|
||||
return Collections.singleton(SpringConfigurationPropertySource.from(source));
|
||||
return Collections.singleton(ConfigurationPropertySource.from(source));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user