Ignores MapPropertySources not created by boot

Prior to this change, any `MapPropertySource` created by an `EnvironmentPostProcessor` would be processed by `BootstrapApplicationListener`. If that property source had a conditional property that it created itself, the 2nd run (bootstrap causes EPPs to run twice) would not have the property and that property source would end up in the main `Environment` effectively deleting a property.

This change only deals with `MapPropertySource`s created by boot testing for a specific sub-type.

Fixes gh-476
This commit is contained in:
Spencer Gibb
2019-02-13 11:33:06 -05:00
committed by GitHub
parent dd3909611d
commit 109e5779e9
4 changed files with 89 additions and 4 deletions

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2013-2019 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
*
* http://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.cloud.bootstrap;
import java.util.HashMap;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
// see https://github.com/spring-cloud/spring-cloud-commons/issues/476
@RunWith(SpringRunner.class)
@SpringBootTest
public class BootstrapEnvironmentPostProcessorIntegrationTests {
@Autowired
private ConfigurableEnvironment env;
@Test
public void conditionalValuesFromMapProperySourceCreatedByEPPExist() {
assertThat(this.env.containsProperty("unconditional.property"))
.as("Environment does not contain unconditional.property")
.isTrue();
assertThat(this.env.getProperty("unconditional.property"))
.as("Environment has wrong value for unconditional.property")
.isEqualTo("unconditional.value");
assertThat(this.env.containsProperty("conditional.property"))
.as("Environment does not contain conditional.property")
.isTrue();
assertThat(this.env.getProperty("conditional.property"))
.as("Environment has wrong value for conditional.property")
.isEqualTo("conditional.value");
}
@EnableAutoConfiguration
@SpringBootConfiguration
protected static class TestConfig {
}
public static class TestConditionalEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
HashMap<String, Object> map = new HashMap<>();
if (!environment.containsProperty("conditional.property")) {
map.put("conditional.property", "conditional.value");
}
map.put("unconditional.property", "unconditional.value");
MapPropertySource propertySource = new MapPropertySource("test-epp-map", map);
environment.getPropertySources().addFirst(propertySource);
}
}
}

View File

@@ -24,6 +24,7 @@ import org.junit.runner.RunWith;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -35,7 +36,6 @@ import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderIntegrationTests.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
@@ -117,7 +117,7 @@ public class ConfigurationPropertiesRebinderIntegrationTests {
@Configuration
@EnableConfigurationProperties
@Import({ RefreshConfiguration.RebinderConfiguration.class,
@ImportAutoConfiguration({ RefreshConfiguration.RebinderConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
protected static class TestConfiguration {