From 232c7f71d2a38300cc676dc53cd03dab6721cdde Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Wed, 15 Jan 2020 19:30:45 -0500 Subject: [PATCH] Adjusts ordering when override system properties is false. (#670) This fixes as reversed property source bug. fixes gh-668 --- .../PropertySourceBootstrapConfiguration.java | 4 +- ...rrideSystemPropertiesIntegrationTests.java | 98 +++++++++++++++++++ .../src/test/resources/ordering.properties | 1 + 3 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests.java create mode 100644 spring-cloud-context/src/test/resources/ordering.properties diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index 27f7cad6..8c190296 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -193,12 +193,12 @@ public class PropertySourceBootstrapConfiguration implements } if (propertySources.contains(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)) { if (!remoteProperties.isOverrideSystemProperties()) { - for (PropertySource p : composite) { + for (PropertySource p : reversedComposite) { propertySources.addAfter(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, p); } } else { - for (PropertySource p : reversedComposite) { + for (PropertySource p : composite) { propertySources.addBefore(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, p); } } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests.java new file mode 100644 index 00000000..071eb514 --- /dev/null +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests.java @@ -0,0 +1,98 @@ +/* + * Copyright 2012-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 + * + * 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.cloud.bootstrap; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.bootstrap.BootstrapOrderingCustomPropertySourceIntegrationTests.Application; +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static java.util.Collections.singletonMap; +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, + properties = { "spring.cloud.bootstrap.name:ordering" }) +public class BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests { + + @Autowired + private ConfigurableEnvironment environment; + + @Test + public void orderingIsCorrect() { + MutablePropertySources propertySources = environment.getPropertySources(); + PropertySource test1 = propertySources + .get("bootstrapProperties-testBootstrap1"); + PropertySource test2 = propertySources + .get("bootstrapProperties-testBootstrap2"); + PropertySource test3 = propertySources + .get("bootstrapProperties-testBootstrap3"); + int index1 = propertySources.precedenceOf(test1); + int index2 = propertySources.precedenceOf(test2); + int index3 = propertySources.precedenceOf(test3); + assertThat(index1).as("source1 index not less then source2").isLessThan(index2); + assertThat(index2).as("source2 index not less then source3").isLessThan(index3); + } + + @EnableAutoConfiguration + @Configuration(proxyBeanMethods = false) + protected static class Application { + + } + + @Configuration(proxyBeanMethods = false) + // This is added to bootstrap context as a source in ordering.properties + protected static class PropertySourceConfiguration implements PropertySourceLocator { + + @Override + public PropertySource locate(Environment environment) { + throw new UnsupportedOperationException(); + } + + @Override + public Collection> locateCollection(Environment environment) { + ArrayList> sources = new ArrayList<>(); + sources.add(new MapPropertySource("testBootstrap1", + singletonMap("key1", "value1"))); + sources.add(new MapPropertySource("testBootstrap2", + singletonMap("key2", "value2"))); + Map map = new HashMap<>(); + map.put("key3", "value3"); + map.put("spring.cloud.config.override-system-properties", "false"); + sources.add(new MapPropertySource("testBootstrap3", map)); + return sources; + } + + } + +} diff --git a/spring-cloud-context/src/test/resources/ordering.properties b/spring-cloud-context/src/test/resources/ordering.properties new file mode 100644 index 00000000..7ace5026 --- /dev/null +++ b/spring-cloud-context/src/test/resources/ordering.properties @@ -0,0 +1 @@ +spring.main.sources:org.springframework.cloud.bootstrap.BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests.PropertySourceConfiguration