diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/refresh/RefreshScope.java b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/refresh/RefreshScope.java index f91ccdf7..2e2d90dc 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/refresh/RefreshScope.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/context/scope/refresh/RefreshScope.java @@ -27,6 +27,7 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.event.EventListener; import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.util.ReflectionUtils; @@ -151,7 +152,8 @@ public class RefreshScope extends GenericScope try { ConfigurationPropertiesBindingPostProcessor processor = context .getBean(ConfigurationPropertiesBindingPostProcessor.class); - setField(processor, "propertySources", null); + setField(processor, "propertySources", ((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()); + setField(processor, "configurationPropertiesBinder", null); processor.afterPropertiesSet(); } catch (Exception e) { diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.java index 948892f6..04a07ff3 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.java @@ -15,9 +15,10 @@ */ package org.springframework.cloud.context.properties; +import static org.junit.Assert.assertEquals; + import javax.annotation.PostConstruct; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -26,6 +27,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.TestConfiguration; @@ -36,8 +38,6 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import static org.junit.Assert.assertEquals; - @RunWith(SpringRunner.class) @SpringBootTest(classes = TestConfiguration.class) public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests { @@ -47,10 +47,10 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests { @Autowired private ConfigurationPropertiesRebinder rebinder; - + @Autowired private org.springframework.cloud.context.scope.refresh.RefreshScope refreshScope; - + @Autowired private ConfigurableEnvironment environment; @@ -67,7 +67,6 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests { @Test @DirtiesContext - @Ignore //FIXME: 2.0.x public void testRefresh() throws Exception { assertEquals(1, properties.getCount()); assertEquals("Hello scope!", properties.getMessage()); @@ -84,18 +83,20 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests { // It's a new instance so the initialization count is 1 assertEquals(1, properties.getCount()); } - + @Configuration @EnableConfigurationProperties - @Import({RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class}) + @Import({ RefreshAutoConfiguration.class, + ConfigurationPropertiesRebinderAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class }) protected static class TestConfiguration { - + @Bean @RefreshScope protected TestProperties properties() { return new TestProperties(); } - + } @ConfigurationProperties @@ -103,25 +104,31 @@ public class ConfigurationPropertiesRebinderRefreshScopeIntegrationTests { private String message; private int delay; private int count = 0; + public int getCount() { return count; } + public String getMessage() { return message; } + public void setMessage(String message) { this.message = message; } + public int getDelay() { return delay; } + public void setDelay(int delay) { this.delay = delay; } + @PostConstruct public void init() { - this.count ++; + this.count++; } } - + } diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java index 74d50ab2..164ebcfd 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java @@ -16,15 +16,15 @@ package org.springframework.cloud.context.scope.refresh; -import java.lang.reflect.Field; -import java.util.LinkedHashMap; -import java.util.Map; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.junit.After; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.aop.framework.Advised; @@ -35,7 +35,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.boot.context.properties.source.ConfigurationPropertySources; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues.Type; @@ -43,12 +42,9 @@ import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.cloud.context.scope.refresh.MoreRefreshScopeIntegrationTests.TestConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.env.*; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.util.ReflectionUtils; - -import static org.junit.Assert.*; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestConfiguration.class) @@ -89,13 +85,11 @@ public class MoreRefreshScopeIntegrationTests { @Test @DirtiesContext - @Ignore //FIXME: 2.0.x public void testRefresh() throws Exception { assertEquals("Hello scope!", this.service.getMessage()); String id1 = this.service.toString(); // Change the dynamic property source... TestPropertyValues.of("message:Foo").applyTo(this.environment, Type.MAP, "morerefreshtests"); - // apply(TestPropertyValues.of("message:Foo")); // ...and then refresh, so the bean is re-initialized: this.scope.refreshAll(); String id2 = this.service.toString(); @@ -106,33 +100,8 @@ public class MoreRefreshScopeIntegrationTests { assertNotSame(id1, id2); } - @SuppressWarnings("unchecked") - private void apply(TestPropertyValues propertyValues) { - Field field = ReflectionUtils.findField(TestPropertyValues.class, "properties"); - ReflectionUtils.makeAccessible(field); - Map properties = (Map) ReflectionUtils.getField(field, propertyValues); - MutablePropertySources sources = this.environment.getPropertySources(); - addToSources(properties, sources, Type.MAP, "morerefreshtests"); - ConfigurationPropertySources.attach(this.environment); - } - - private void addToSources(Map properties, MutablePropertySources sources, Type type, String name) { - if (sources.contains(name)) { - PropertySource propertySource = sources.get(name); - if (propertySource.getClass().equals(type.getSourceClass())) { - ((Map) propertySource.getSource()) - .putAll(properties); - return; - } - } - Map source = new LinkedHashMap<>(properties); - sources.addLast((type.equals(Type.MAP) ? new MapPropertySource(name, source) - : new SystemEnvironmentPropertySource(name, source))); - } - @Test @DirtiesContext - @Ignore //FIXME: 2.0.x public void testRefreshFails() throws Exception { assertEquals("Hello scope!", this.service.getMessage()); // Change the dynamic property source... diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java index a0731f6a..2e73f628 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java @@ -61,7 +61,6 @@ public class RefreshScopeListBindingIntegrationTests { @Test @DirtiesContext - @Ignore //FIXME: 2.0.x public void testAppendProperties() throws Exception { assertEquals("[one, two]", this.properties.getMessages().toString()); assertTrue(this.properties instanceof Advised); @@ -72,7 +71,6 @@ public class RefreshScopeListBindingIntegrationTests { @Test @DirtiesContext - @Ignore //FIXME: 2.0.x public void testReplaceProperties() throws Exception { assertEquals("[one, two]", this.properties.getMessages().toString()); assertTrue(this.properties instanceof Advised);