Fix nasty state bug in BootstrapApplicationListener

In Spring 5.0.0 the SpringFactoriesLoader is stateful and
BootstrapApplicationListener inadvertently modifies the state
by adding class names to a list loaded from spring.factories.

Probably harmless in most apps, but caused some tests to fail
(depending on the order they were run in).
This commit is contained in:
Dave Syer
2017-10-20 12:50:04 +01:00
parent 2fd2f99c9d
commit 801183e01b
3 changed files with 11 additions and 8 deletions

View File

@@ -1,5 +1,7 @@
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;
@@ -19,15 +21,13 @@ 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;
public class ContextRefresherTests {
private RefreshScope scope = Mockito.mock(RefreshScope.class);
@Test
public void orderNewPropertiesConsistentWithNewContext() {
try (ConfigurableApplicationContext context = SpringApplication.run(ContextRefresherTests.class,
try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
"--spring.main.webEnvironment=false", "--debug=false",
"--spring.main.bannerMode=OFF")) {
context.getEnvironment().setActiveProfiles("refresh");
@@ -50,10 +50,11 @@ public class ContextRefresherTests {
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(ContextRefresherTests.class,
try (ConfigurableApplicationContext context = SpringApplication.run(Empty.class,
"--spring.main.webEnvironment=false", "--debug=false",
"--spring.main.bannerMode=OFF", "--spring.cloud.bootstrap.name=refresh")) {
List<String> names = names(context.getEnvironment().getPropertySources());
System.err.println("***** " + context.getEnvironment().getPropertySources());
assertThat(names).doesNotContain("bootstrapProperties");
ContextRefresher refresher = new ContextRefresher(context, scope);
TestPropertyValues.of(
@@ -73,6 +74,10 @@ public class ContextRefresherTests {
return list;
}
@Configuration
protected static class Empty {
}
@Configuration
// This is added to bootstrap context as a source in bootstrap.properties
protected static class PropertySourceConfiguration implements PropertySourceLocator {

View File

@@ -28,7 +28,6 @@ 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;
import org.springframework.boot.WebApplicationType;
@@ -94,7 +93,6 @@ public class RefreshEndpointTests {
}
@Test
// @Ignore
public void keysComputedWhenChangesInExternalProperties() throws Exception {
this.context = new SpringApplicationBuilder(Empty.class)
.web(WebApplicationType.NONE).bannerMode(Mode.OFF)