(Fix) incorrect property source order (#271)

Do not re-reverse contexts in ZookeeperPropertySourceLocator

Fixes gh-270

Co-authored-by: Michael Sievers <msievers@wescale.com>
This commit is contained in:
Michael Sievers
2020-12-03 20:51:26 +01:00
committed by GitHub
parent 669d1095a6
commit d65b9a0310
2 changed files with 46 additions and 3 deletions

View File

@@ -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<CuratorFramework> propertySource = sources.createPropertySource(propertySourceContext, true, this.curator);
composite.addPropertySource(propertySource);

View File

@@ -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<PropertySource<?>> 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<EnvironmentChangeEvent> {