Fix ordering of property sources on refresh

If there were *no* bootstrapProperties (i.e. properties added by
PropertySourceLocators) on startup, and then in a refresh event
some new sources were picked up, they would have been added to the
end, instead of the beginning of the list.

Fixes https://github.com/spring-cloud/spring-cloud-config/issues/815
This commit is contained in:
Dave Syer
2017-10-19 13:41:05 +01:00
parent 52a5c31c26
commit 1add1067df
3 changed files with 100 additions and 6 deletions

View File

@@ -0,0 +1,96 @@
package org.springframework.cloud.context.refresh;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
public class ContextRefresherTests {
private RefreshScope scope = Mockito.mock(RefreshScope.class);
private ConfigurableApplicationContext context;
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void orderNewPropertiesConsistentWithNewContext() {
context = SpringApplication.run(ContextRefresherTests.class,
"--spring.main.webEnvironment=false", "--debug=false",
"--spring.main.bannerMode=OFF");
context.getEnvironment().setActiveProfiles("refresh");
List<String> names = names(context.getEnvironment().getPropertySources());
assertThat(names).doesNotContain(
"applicationConfig: [classpath:/bootstrap-refresh.properties]");
ContextRefresher refresher = new ContextRefresher(context, scope);
refresher.refresh();
names = names(context.getEnvironment().getPropertySources());
assertThat(names)
.contains("applicationConfig: [classpath:/bootstrap-refresh.properties]");
assertThat(names).containsSequence(
"applicationConfig: [classpath:/bootstrap-refresh.properties]",
"applicationConfig: [classpath:/bootstrap.properties]");
}
@Test
public void bootstrapPropertySourceAlwaysFirst() {
// Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up
// a bootstrapProperties immediately
context = SpringApplication.run(ContextRefresherTests.class,
"--spring.main.webEnvironment=false", "--debug=false",
"--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh");
List<String> names = names(context.getEnvironment().getPropertySources());
assertThat(names).doesNotContain("bootstrapProperties");
ContextRefresher refresher = new ContextRefresher(context, scope);
EnvironmentTestUtils.addEnvironment(context,
"spring.cloud.bootstrap.sources: org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration\n"
+ "");
refresher.refresh();
names = names(context.getEnvironment().getPropertySources());
assertThat(names).first().isEqualTo("bootstrapProperties");
}
private List<String> names(MutablePropertySources propertySources) {
List<String> list = new ArrayList<>();
for (PropertySource<?> p : propertySources) {
list.add(p.getName());
}
return list;
}
@Configuration
// This is added to bootstrap context as a source in bootstrap.properties
protected static class PropertySourceConfiguration implements PropertySourceLocator {
public static Map<String, Object> MAP = new HashMap<String, Object>(
Collections.<String, Object>singletonMap("bootstrap.foo", "refresh"));
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("refreshTest", MAP);
}
}
}