Fix configuration properties in refresh scope for Boot 2.0
The old hack in RefreshScope has been replaced with a new hack. It has to "reset" the ConfigurationProperties bean post processor and the new rebinder API has changed the internals of that, so we needed to adjust.
This commit is contained in:
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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<String, Object> properties = (Map<String, Object>) ReflectionUtils.getField(field, propertyValues);
|
||||
MutablePropertySources sources = this.environment.getPropertySources();
|
||||
addToSources(properties, sources, Type.MAP, "morerefreshtests");
|
||||
ConfigurationPropertySources.attach(this.environment);
|
||||
}
|
||||
|
||||
private void addToSources(Map<String, Object> properties, MutablePropertySources sources, Type type, String name) {
|
||||
if (sources.contains(name)) {
|
||||
PropertySource<?> propertySource = sources.get(name);
|
||||
if (propertySource.getClass().equals(type.getSourceClass())) {
|
||||
((Map<String, Object>) propertySource.getSource())
|
||||
.putAll(properties);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Map<String, Object> 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...
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user