Use new boot ConfigData framework (#703)

Bootstrap is now opt-in using `spring.config.use-legacy-processing`. Otherwise, bootstrap is left as is.

If bootstrap is disabled, the `ContextRefresher` uses new `ConfigData` framework from boot.

See #608 for original motivation.
This commit is contained in:
Spencer Gibb
2020-08-06 12:54:25 -04:00
committed by GitHub
parent b38e31f263
commit a183bc709f
36 changed files with 1595 additions and 216 deletions

View File

@@ -31,7 +31,6 @@ import org.junit.runners.Suite.SuiteClasses;
org.springframework.cloud.logging.LoggingRebinderTests.class,
org.springframework.cloud.bootstrap.BootstrapSourcesOrderingTests.class,
org.springframework.cloud.bootstrap.BootstrapDisabledAutoConfigurationIntegrationTests.class,
org.springframework.cloud.bootstrap.BootstrapEnvironmentPostProcessorIntegrationTests.class,
org.springframework.cloud.bootstrap.encrypt.RsaDisabledTests.class,
org.springframework.cloud.bootstrap.encrypt.EncryptorFactoryTests.class,
org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializerTests.class,

View File

@@ -1,83 +0,0 @@
/*
* Copyright 2013-2020 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.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

@@ -16,7 +16,6 @@
package org.springframework.cloud.bootstrap;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -24,7 +23,6 @@ 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.BootstrapOrderingAutoConfigurationIntegrationTests.Application;
import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.test.context.ActiveProfiles;
@@ -33,7 +31,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class, properties = "encrypt.key:deadbeef")
@SpringBootTest(classes = Application.class, properties = { "encrypt.key:deadbeef",
"spring.config.use-legacy-processing=true" })
@ActiveProfiles("encrypt")
public class BootstrapOrderingAutoConfigurationIntegrationTests {
@@ -41,10 +40,9 @@ public class BootstrapOrderingAutoConfigurationIntegrationTests {
private ConfigurableEnvironment environment;
@Test
@Ignore // FIXME: spring boot 2.0.0
public void bootstrapPropertiesExist() {
then(this.environment.getPropertySources().contains(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME))
then(this.environment.getPropertySources()
.contains("applicationConfig: [classpath:/bootstrap.properties]"))
.isTrue();
}

View File

@@ -42,7 +42,8 @@ import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
properties = { "spring.cloud.bootstrap.name:ordering" })
properties = { "spring.config.use-legacy-processing=true",
"spring.cloud.bootstrap.name:ordering" })
public class BootstrapOrderingCustomOverrideSystemPropertiesIntegrationTests {
@Autowired

View File

@@ -20,7 +20,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -28,7 +27,6 @@ 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.PropertySourceBootstrapConfiguration;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
@@ -42,7 +40,8 @@ import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class,
properties = { "encrypt.key:deadbeef", "spring.cloud.bootstrap.name:custom" })
properties = { "encrypt.key:deadbeef", "spring.cloud.bootstrap.name:custom",
"spring.config.use-legacy-processing=true" })
@ActiveProfiles("encrypt")
public class BootstrapOrderingCustomPropertySourceIntegrationTests {
@@ -50,11 +49,9 @@ public class BootstrapOrderingCustomPropertySourceIntegrationTests {
private ConfigurableEnvironment environment;
@Test
@Ignore // FIXME: spring boot 2.0.0
public void bootstrapPropertiesExist() {
then(this.environment.getPropertySources().contains(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME))
.isTrue();
then(this.environment.getPropertySources()
.contains("applicationConfig: [classpath:/custom.properties]")).isTrue();
}
@Test

View File

@@ -29,7 +29,8 @@ import static org.assertj.core.api.BDDAssertions.then;
import static org.springframework.cloud.bootstrap.TestHigherPriorityBootstrapConfiguration.firstToBeCreated;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@SpringBootTest(classes = Application.class,
properties = "spring.config.use-legacy-processing=true")
public class BootstrapSourcesOrderingTests {
@Test

View File

@@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -83,7 +84,8 @@ public class BootstrapConfigurationTests {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.location=" + externalPropertiesPath)
.properties("spring.cloud.bootstrap.location=" + externalPropertiesPath,
"spring.config.use-legacy-processing=true")
.run();
then(this.context.getEnvironment().getProperty("info.name"))
.isEqualTo("externalPropertiesInfoName");
@@ -99,8 +101,10 @@ public class BootstrapConfigurationTests {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.additional-location="
+ externalPropertiesPath)
.properties(
"spring.cloud.bootstrap.additional-location="
+ externalPropertiesPath,
"spring.config.use-legacy-processing=true")
.run();
then(this.context.getEnvironment().getProperty("info.name"))
.isEqualTo("externalPropertiesInfoName");
@@ -114,6 +118,7 @@ public class BootstrapConfigurationTests {
@Test
public void bootstrapPropertiesAvailableInInitializer() {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).initializers(
new ApplicationContextInitializer<ConfigurableApplicationContext>() {
@Override
@@ -144,6 +149,7 @@ public class BootstrapConfigurationTests {
public void picksUpAdditionalPropertySource() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo")).isEqualTo("bar");
then(this.context.getEnvironment().getPropertySources().contains(
@@ -156,6 +162,7 @@ public class BootstrapConfigurationTests {
System.setProperty("expected.fail", "true");
this.expected.expectMessage("Planned");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
}
@@ -164,6 +171,7 @@ public class BootstrapConfigurationTests {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo")).isEqualTo("bar");
}
@@ -175,6 +183,7 @@ public class BootstrapConfigurationTests {
.put("spring.cloud.config.overrideSystemProperties", "false");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo"))
.isEqualTo("system");
@@ -191,6 +200,7 @@ public class BootstrapConfigurationTests {
PropertySourceConfiguration.MAP.put("spring.cloud.config.allowOverride", "false");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo")).isEqualTo("bar");
}
@@ -203,6 +213,7 @@ public class BootstrapConfigurationTests {
PropertySourceConfiguration.MAP.put("spring.cloud.config.allowOverride", "true");
System.setProperty("bootstrap.foo", "system");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo"))
.isEqualTo("system");
@@ -217,6 +228,7 @@ public class BootstrapConfigurationTests {
environment.getPropertySources().addLast(new MapPropertySource("last",
Collections.<String, Object>singletonMap("bootstrap.foo", "splat")));
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.config.use-legacy-processing=true")
.environment(environment).sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo"))
.isEqualTo("splat");
@@ -227,6 +239,7 @@ public class BootstrapConfigurationTests {
System.setProperty("expected.name", "main");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.cloud.bootstrap.name:other",
"spring.config.use-legacy-processing=true",
"spring.config.name:plain")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("spring.application.name"))
@@ -248,6 +261,7 @@ public class BootstrapConfigurationTests {
System.setProperty("expected.name", "main");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.cloud.bootstrap.name:application",
"spring.config.use-legacy-processing=true",
"spring.config.name:other")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().getProperty("spring.application.name"))
@@ -262,7 +276,8 @@ public class BootstrapConfigurationTests {
public void applicationNameOnlyInBootstrap() {
System.setProperty("expected.name", "main");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.properties("spring.cloud.bootstrap.name:other")
.properties("spring.cloud.bootstrap.name:other",
"spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class).run();
// The main context is called "main" because spring.application.name is specified
// in other.properties (and not in the main config file)
@@ -279,6 +294,7 @@ public class BootstrapConfigurationTests {
public void environmentEnrichedOnceWhenSharedWithChildContext() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.properties("spring.config.use-legacy-processing=true")
.environment(new StandardEnvironment()).child(BareConfiguration.class)
.web(WebApplicationType.NONE).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo")).isEqualTo("bar");
@@ -298,6 +314,7 @@ public class BootstrapConfigurationTests {
TestHigherPriorityBootstrapConfiguration.count.set(0);
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.properties("spring.config.use-legacy-processing=true")
.child(BareConfiguration.class).web(WebApplicationType.NONE).run();
then(TestHigherPriorityBootstrapConfiguration.count.get()).isEqualTo(1);
then(this.context.getParent()).isNotNull();
@@ -309,6 +326,7 @@ public class BootstrapConfigurationTests {
@Test
public void listOverride() {
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.properties("spring.config.use-legacy-processing=true")
.child(BareConfiguration.class).web(WebApplicationType.NONE).run();
ListProperties listProperties = new ListProperties();
Binder.get(this.context.getEnvironment()).bind("list",
@@ -322,6 +340,7 @@ public class BootstrapConfigurationTests {
TestHigherPriorityBootstrapConfiguration.count.set(0);
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
SpringApplicationBuilder builder = new SpringApplicationBuilder()
.properties("spring.config.use-legacy-processing=true")
.sources(BareConfiguration.class);
this.sibling = builder.child(BareConfiguration.class)
.properties("spring.application.name=sibling")
@@ -350,6 +369,7 @@ public class BootstrapConfigurationTests {
public void environmentEnrichedInParentContext() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
this.context = new SpringApplicationBuilder().sources(BareConfiguration.class)
.properties("spring.config.use-legacy-processing=true")
.child(BareConfiguration.class).web(WebApplicationType.NONE).run();
then(this.context.getEnvironment().getProperty("bootstrap.foo")).isEqualTo("bar");
then(this.context.getParent().getEnvironment())
@@ -364,6 +384,7 @@ public class BootstrapConfigurationTests {
}
@Test
@Ignore // FIXME: legacy
public void differentProfileInChild() {
PropertySourceConfiguration.MAP.put("bootstrap.foo", "bar");
// Profiles are always merged with the child
@@ -371,7 +392,8 @@ public class BootstrapConfigurationTests {
.sources(BareConfiguration.class).profiles("parent")
.web(WebApplicationType.NONE).run();
this.context = new SpringApplicationBuilder(BareConfiguration.class)
.profiles("child").parent(parent).web(WebApplicationType.NONE).run();
.properties("spring.config.use-legacy-processing=true").profiles("child")
.parent(parent).web(WebApplicationType.NONE).run();
then(this.context.getParent().getEnvironment())
.isNotSameAs(this.context.getEnvironment());
// The ApplicationContext merges profiles (profiles and property sources), see
@@ -401,7 +423,8 @@ public class BootstrapConfigurationTests {
public void includeProfileFromBootstrapPropertySource() {
PropertySourceConfiguration.MAP.put("spring.profiles.include", "bar,baz");
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.profiles("foo").sources(BareConfiguration.class).run();
.properties("spring.config.use-legacy-processing=true").profiles("foo")
.sources(BareConfiguration.class).run();
then(this.context.getEnvironment().acceptsProfiles("baz")).isTrue();
then(this.context.getEnvironment().acceptsProfiles("bar")).isTrue();
}
@@ -410,7 +433,9 @@ public class BootstrapConfigurationTests {
public void includeProfileFromBootstrapProperties() {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.name=local").run();
.properties("spring.config.use-legacy-processing=true",
"spring.cloud.bootstrap.name=local")
.run();
then(this.context.getEnvironment().acceptsProfiles("local")).isTrue();
then(this.context.getEnvironment().getProperty("added"))
.isEqualTo("Hello added!");
@@ -420,7 +445,9 @@ public class BootstrapConfigurationTests {
public void nonEnumerablePropertySourceWorks() {
this.context = new SpringApplicationBuilder().web(WebApplicationType.NONE)
.sources(BareConfiguration.class)
.properties("spring.cloud.bootstrap.name=nonenumerable").run();
.properties("spring.config.use-legacy-processing=true",
"spring.cloud.bootstrap.name=nonenumerable")
.run();
then(this.context.getEnvironment().getProperty("foo")).isEqualTo("bar");
}

View File

@@ -37,6 +37,7 @@ public class BootstrapListenerHierarchyIntegrationTests {
@Test
public void shouldAddInABootstrapContext() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.properties("spring.config.use-legacy-processing=true")
.sources(BasicConfiguration.class).web(NONE).run();
then(context.getParent()).isNotNull();
@@ -45,6 +46,7 @@ public class BootstrapListenerHierarchyIntegrationTests {
@Test
public void shouldAddInOneBootstrapForABasicParentChildHierarchy() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.properties("spring.config.use-legacy-processing=true")
.sources(RootConfiguration.class).web(NONE)
.child(BasicConfiguration.class).web(NONE).run();
@@ -66,6 +68,7 @@ public class BootstrapListenerHierarchyIntegrationTests {
@Test
public void shouldAddInOneBootstrapForSiblingsBasedHierarchy() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.properties("spring.config.use-legacy-processing=true")
.sources(RootConfiguration.class).web(NONE)
.child(BasicConfiguration.class).web(NONE)
.sibling(BasicConfiguration.class).web(NONE).run();

View File

@@ -33,7 +33,7 @@ public class EncryptionIntegrationTests {
public void symmetricPropertyValues() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:pie",
"spring.config.use-legacy-processing=true", "encrypt.key:pie",
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95")
.run();
then(context.getEnvironment().getProperty("foo.password")).isEqualTo("test");
@@ -43,7 +43,7 @@ public class EncryptionIntegrationTests {
public void symmetricConfigurationProperties() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestConfiguration.class).web(WebApplicationType.NONE).properties(
"encrypt.key:pie",
"spring.config.use-legacy-processing=true", "encrypt.key:pie",
"foo.password:{cipher}bf29452295df354e6153c5b31b03ef23c70e55fba24299aa85c63438f1c43c95")
.run();
then(context.getBean(PasswordProperties.class).getPassword()).isEqualTo("test");

View File

@@ -187,6 +187,7 @@ public class EnvironmentDecryptApplicationInitializerTests {
Collections.singletonMap("key", "{cipher}value2"));
CompositePropertySource cps = mock(CompositePropertySource.class);
when(cps.getName()).thenReturn("mock-composite-source");
when(cps.getPropertyNames()).thenReturn(devProfile.getPropertyNames());
when(cps.getPropertySources())
.thenReturn(Arrays.asList(devProfile, defaultProfile));

View File

@@ -44,7 +44,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = "spring.config.use-legacy-processing=true")
@ActiveProfiles("config")
public class ConfigurationPropertiesRebinderIntegrationTests {

View File

@@ -41,7 +41,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = "spring.config.use-legacy-processing=true")
public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests {
@Autowired

View File

@@ -37,7 +37,8 @@ import static org.assertj.core.api.BDDAssertions.then;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestConfiguration.class,
properties = { "spring.datasource.hikari.read-only=false", "debug=true" })
properties = { "spring.datasource.hikari.read-only=false",
"spring.config.use-legacy-processing=true" })
public class ContextRefresherIntegrationTests {
@Autowired

View File

@@ -44,7 +44,7 @@ import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest(properties = "spring.config.use-legacy-processing=true")
@DirtiesContext
public class ContextRefresherOrderingIntegrationTests {

View File

@@ -23,13 +23,17 @@ import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.test.util.TestPropertyValues.Type;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.bootstrap.TestBootstrapConfiguration;
import org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
@@ -41,6 +45,7 @@ import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.BDDAssertions.then;
public class ContextRefresherTests {
@@ -54,15 +59,18 @@ public class ContextRefresherTests {
}
@Test
@Ignore // FIXME: legacy config
public void orderNewPropertiesConsistentWithNewContext() {
try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
"--spring.config.use-legacy-processing=true",
"--spring.main.web-application-type=none", "--debug=false",
"--spring.main.bannerMode=OFF")) {
context.getEnvironment().setActiveProfiles("refresh");
List<String> names = names(context.getEnvironment().getPropertySources());
then(names).doesNotContain(
"applicationConfig: [classpath:/bootstrap-refresh.properties]");
ContextRefresher refresher = new ContextRefresher(context, this.scope);
LegacyContextRefresher refresher = new LegacyContextRefresher(context,
this.scope);
refresher.refresh();
names = names(context.getEnvironment().getPropertySources());
then(names).contains(
@@ -75,17 +83,19 @@ public class ContextRefresherTests {
}
@Test
@Ignore // FIXME: legacy
public void bootstrapPropertySourceAlwaysFirst() {
// Use spring.cloud.bootstrap.name to switch off the defaults (which would pick up
// a bootstrapProperties immediately
try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
"--spring.config.use-legacy-processing=true",
"--spring.main.web-application-type=none", "--debug=false",
"--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh")) {
List<String> names = names(context.getEnvironment().getPropertySources());
System.err.println("***** " + context.getEnvironment().getPropertySources());
then(names).doesNotContain("bootstrapProperties");
ContextRefresher refresher = new ContextRefresher(context, this.scope);
ContextRefresher refresher = new LegacyContextRefresher(context, this.scope);
TestPropertyValues.of("spring.cloud.bootstrap.sources: "
+ "org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration")
.applyTo(context.getEnvironment(), Type.MAP, "defaultProperties");
@@ -103,12 +113,15 @@ public class ContextRefresherTests {
// a bootstrapProperties immediately
try (ConfigurableApplicationContext context = SpringApplication.run(
ContextRefresherTests.class, "--spring.main.web-application-type=none",
"--debug=false", "--spring.main.bannerMode=OFF",
"--spring.config.use-legacy-processing=true", "--debug=false",
"--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh")) {
ContextRefresher refresher = new ContextRefresher(context, this.scope);
LegacyContextRefresher refresher = new LegacyContextRefresher(context,
this.scope);
TestPropertyValues.of("spring.cloud.bootstrap.sources: "
+ "org.springframework.cloud.context.refresh.ContextRefresherTests.PropertySourceConfiguration")
.applyTo(context);
ConfigurableApplicationContext refresherContext = refresher
.addConfigFilesToEnvironment();
then(refresherContext.getParent()).isNotNull()
@@ -127,11 +140,12 @@ public class ContextRefresherTests {
.get(getClass().getClassLoader());
then(system.getCount()).isEqualTo(0);
try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
"--spring.config.use-legacy-processing=true",
"--spring.main.web-application-type=none", "--debug=false",
"--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh")) {
then(system.getCount()).isEqualTo(4);
ContextRefresher refresher = new ContextRefresher(context, this.scope);
ContextRefresher refresher = new LegacyContextRefresher(context, this.scope);
refresher.refresh();
then(system.getCount()).isEqualTo(4);
}
@@ -144,10 +158,11 @@ public class ContextRefresherTests {
try (ConfigurableApplicationContext context = SpringApplication.run(
ContextRefresherTests.class, "--spring.main.web-application-type=none",
"--debug=false", "--spring.main.bannerMode=OFF",
"--spring.cloud.bootstrap.name=refresh", "--test.bootstrap.foo=bar")) {
"--spring.config.use-legacy-processing=true", "--debug=false",
"--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh",
"--test.bootstrap.foo=bar")) {
context.getEnvironment().setActiveProfiles("refresh");
ContextRefresher refresher = new ContextRefresher(context, this.scope);
ContextRefresher refresher = new LegacyContextRefresher(context, this.scope);
refresher.refresh();
then(TestBootstrapConfiguration.fooSightings).containsExactly("bar", "bar");
}
@@ -155,6 +170,38 @@ public class ContextRefresherTests {
TestBootstrapConfiguration.fooSightings = null;
}
@Test
public void legacyContextRefresherCreatedUsingBootstrapEnabled() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.withPropertyValues("spring.cloud.bootstrap.enabled=true")
.run(context -> {
assertThat(context).hasSingleBean(LegacyContextRefresher.class);
assertThat(context).hasSingleBean(ContextRefresher.class);
});
}
@Test
public void legacyContextRefresherCreated() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.withPropertyValues("spring.config.use-legacy-processing=true")
.run(context -> {
assertThat(context).hasSingleBean(LegacyContextRefresher.class);
assertThat(context).hasSingleBean(ContextRefresher.class);
});
}
@Test
public void configDataContextRefresherCreated() {
new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class))
.run(context -> {
assertThat(context).hasSingleBean(ConfigDataContextRefresher.class);
assertThat(context).hasSingleBean(ContextRefresher.class);
});
}
private List<String> names(MutablePropertySources propertySources) {
List<String> list = new ArrayList<>();
for (PropertySource<?> p : propertySources) {

View File

@@ -47,6 +47,7 @@ public class RestartIntegrationTests {
this.context = SpringApplication.run(TestConfiguration.class,
"--management.endpoint.restart.enabled=true", "--server.port=0",
"--spring.config.use-legacy-processing=true",
"--management.endpoints.web.exposure.include=restart",
"--spring.liveBeansView.mbeanDomain=livebeans");

View File

@@ -35,7 +35,9 @@ public class RefreshScopeSerializationTests {
@Test
public void defaultApplicationContextId() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
TestConfiguration.class).web(WebApplicationType.NONE).run();
TestConfiguration.class)
.properties("spring.config.use-legacy-processing=true")
.web(WebApplicationType.NONE).run();
then(context.getId()).isEqualTo("application-1");
}

View File

@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.boot.Banner.Mode;
@@ -34,6 +35,7 @@ import org.springframework.boot.test.util.TestPropertyValues.Type;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.refresh.LegacyContextRefresher;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationEvent;
@@ -65,45 +67,57 @@ public class RefreshEndpointTests {
}
@Test
@Ignore // FIXME: legacy
public void keysComputedWhenAdded() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
.properties("spring.cloud.bootstrap.name:none").run();
.properties("spring.config.use-legacy-processing=true",
"spring.cloud.bootstrap.name:none")
.run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
this.context.getEnvironment().setActiveProfiles("local");
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(this.context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
then(keys.contains("added")).isTrue().as("Wrong keys: " + keys);
}
@Test
@Ignore // FIXME: legacy
public void keysComputedWhenOveridden() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
.properties("spring.cloud.bootstrap.name:none").run();
.properties("spring.config.use-legacy-processing=true",
"spring.cloud.bootstrap.name:none")
.run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
this.context.getEnvironment().setActiveProfiles("override");
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(this.context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
then(keys.contains("message")).isTrue().as("Wrong keys: " + keys);
}
@Test
@Ignore // FIXME: legacy
public void keysComputedWhenChangesInExternalProperties() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
.properties("spring.cloud.bootstrap.name:none").run();
.properties("spring.cloud.bootstrap.name:none",
"spring.config.use-legacy-processing=true")
.run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
TestPropertyValues
.of("spring.cloud.bootstrap.sources="
+ ExternalPropertySourceLocator.class.getName())
.applyTo(this.context.getEnvironment(), Type.MAP, "defaultProperties");
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(this.context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
then(keys.contains("external.message")).isTrue().as("Wrong keys: " + keys);
@@ -123,7 +137,8 @@ public class RefreshEndpointTests {
.of("spring.main.sources="
+ ExternalPropertySourceLocator.class.getName())
.applyTo(this.context);
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(this.context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Collection<String> keys = endpoint.refresh();
then(keys.contains("external.message")).as("Wrong keys: " + keys).isFalse();
@@ -135,7 +150,8 @@ public class RefreshEndpointTests {
.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run();
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(this.context);
ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(this.context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
Empty empty = this.context.getBean(Empty.class);
endpoint.refresh();
@@ -150,7 +166,8 @@ public class RefreshEndpointTests {
Empty.class).web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
RefreshScope scope = new RefreshScope();
scope.setApplicationContext(context);
ContextRefresher contextRefresher = new ContextRefresher(context, scope);
ContextRefresher contextRefresher = new LegacyContextRefresher(context,
scope);
RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
int count = countShutdownHooks();
endpoint.refresh();