From d65b9a0310ecae6e7f2283520baf7d7f896308cb Mon Sep 17 00:00:00 2001 From: Michael Sievers Date: Thu, 3 Dec 2020 20:51:26 +0100 Subject: [PATCH] (Fix) incorrect property source order (#271) Do not re-reverse contexts in ZookeeperPropertySourceLocator Fixes gh-270 Co-authored-by: Michael Sievers --- .../ZookeeperPropertySourceLocator.java | 3 -- .../ZookeeperPropertySourceLocatorTests.java | 46 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java index 1755c197..cd0c4837 100644 --- a/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java +++ b/spring-cloud-zookeeper-config/src/main/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocator.java @@ -17,7 +17,6 @@ package org.springframework.cloud.zookeeper.config; import java.util.Arrays; -import java.util.Collections; import java.util.List; import javax.annotation.PreDestroy; @@ -96,8 +95,6 @@ public class ZookeeperPropertySourceLocator implements PropertySourceLocator { CompositePropertySource composite = new CompositePropertySource("zookeeper"); - Collections.reverse(this.contexts); - for (String propertySourceContext : this.contexts) { PropertySource propertySource = sources.createPropertySource(propertySourceContext, true, this.curator); composite.addPropertySource(propertySource); diff --git a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java index 9879a008..a4f06258 100644 --- a/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java +++ b/spring-cloud-zookeeper-config/src/test/java/org/springframework/cloud/zookeeper/config/ZookeeperPropertySourceLocatorTests.java @@ -16,6 +16,7 @@ package org.springframework.cloud.zookeeper.config; +import java.util.ArrayList; import java.util.List; import java.util.UUID; import java.util.concurrent.CountDownLatch; @@ -25,6 +26,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.framework.api.GetChildrenBuilder; import org.apache.curator.retry.RetryOneTime; import org.apache.zookeeper.KeeperException; import org.junit.After; @@ -43,9 +45,14 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.PropertySource; +import org.springframework.mock.env.MockEnvironment; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Spencer Gibb @@ -187,6 +194,45 @@ public class ZookeeperPropertySourceLocatorTests { .isEqualTo("testPropValUpdate"); } + @Test + public void compositePropertySourceHoldsPropertySourcesInCorrectOrder() { + + // given + final String defaultContext = "someDefaultContext"; + final String someName = "someName"; + final String someProfile = "someProfile"; + + final CuratorFramework curator = mock(CuratorFramework.class); + when(curator.getChildren()).thenReturn(mock(GetChildrenBuilder.class)); + + final MockEnvironment mockEnvironment = new MockEnvironment(); + mockEnvironment.setActiveProfiles(someProfile); + + final ZookeeperConfigProperties properties = new ZookeeperConfigProperties(); + properties.setName(someName); + properties.setDefaultContext(defaultContext); + + final ZookeeperPropertySourceLocator locator = new ZookeeperPropertySourceLocator( + curator, properties); + + // when + final PropertySource propertySource = locator.locate(mockEnvironment); + + // then + assertThat(propertySource).isInstanceOf(CompositePropertySource.class); + + // and + final ArrayList> propertySources = new ArrayList<>( + ((CompositePropertySource) propertySource).getPropertySources()); + + assertThat(propertySources.get(0).getName()) + .endsWith(someName + properties.getProfileSeparator() + someProfile); + assertThat(propertySources.get(1).getName()).endsWith(someName); + assertThat(propertySources.get(2).getName()).endsWith( + defaultContext + properties.getProfileSeparator() + someProfile); + assertThat(propertySources.get(3).getName()).endsWith(defaultContext); + } + @Configuration @EnableAutoConfiguration static class Config implements ApplicationListener {