Fix service-registry and refresh endpoint tests
This commit is contained in:
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.cloud.context.scope.refresh;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.junit.After;
|
||||
@@ -28,27 +32,23 @@ import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
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.cloud.autoconfigure.RefreshAutoConfiguration;
|
||||
import org.springframework.boot.test.util.TestPropertyValues.Type;
|
||||
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.context.annotation.Import;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedResource;
|
||||
import org.springframework.core.env.*;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
|
||||
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 static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = TestConfiguration.class)
|
||||
@@ -94,16 +94,42 @@ public class MoreRefreshScopeIntegrationTests {
|
||||
assertEquals("Hello scope!", this.service.getMessage());
|
||||
String id1 = this.service.toString();
|
||||
// Change the dynamic property source...
|
||||
TestPropertyValues.of("message:Foo").applyTo(this.environment);
|
||||
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();
|
||||
assertEquals("Foo", this.service.getMessage());
|
||||
String message = this.service.getMessage();
|
||||
assertEquals("Foo", message);
|
||||
assertEquals(1, TestService.getInitCount());
|
||||
assertEquals(1, TestService.getDestroyCount());
|
||||
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
|
||||
@@ -128,6 +154,7 @@ public class MoreRefreshScopeIntegrationTests {
|
||||
assertEquals("Foo", this.service.getMessage());
|
||||
}
|
||||
|
||||
|
||||
public static class TestService implements InitializingBean, DisposableBean {
|
||||
|
||||
private static Log logger = LogFactory.getLog(TestService.class);
|
||||
@@ -191,8 +218,7 @@ public class MoreRefreshScopeIntegrationTests {
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
@Import({ RefreshAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class })
|
||||
@EnableAutoConfiguration
|
||||
protected static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
@@ -213,14 +239,14 @@ public class MoreRefreshScopeIntegrationTests {
|
||||
}
|
||||
|
||||
@ConfigurationProperties
|
||||
@ManagedResource
|
||||
// @ManagedResource
|
||||
protected static class TestProperties {
|
||||
|
||||
private String message;
|
||||
|
||||
private int delay;
|
||||
|
||||
@ManagedAttribute
|
||||
// @ManagedAttribute
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
@@ -229,7 +255,7 @@ public class MoreRefreshScopeIntegrationTests {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@ManagedAttribute
|
||||
// @ManagedAttribute
|
||||
public int getDelay() {
|
||||
return this.delay;
|
||||
}
|
||||
|
||||
@@ -17,10 +17,9 @@ package org.springframework.cloud.context.scope.refresh;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
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;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -38,8 +37,6 @@ import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@@ -51,20 +48,19 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
|
||||
*
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = ClientApp.class, webEnvironment = RANDOM_PORT)
|
||||
@SpringBootTest(classes = ClientApp.class, properties = "endpoints.default.web.enabled=true", webEnvironment = RANDOM_PORT)
|
||||
public class RefreshEndpointIntegrationTests {
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
@Test
|
||||
@Ignore //FIXME: 2.0.x
|
||||
public void webAccess() throws Exception {
|
||||
TestRestTemplate template = new TestRestTemplate();
|
||||
template.exchange(
|
||||
getUrlEncodedEntity("http://localhost:" + this.port + "/application/env", "message",
|
||||
"Hello Dave!"), String.class);
|
||||
template.postForObject("http://localhost:" + this.port + "/application/refresh", "", String.class);
|
||||
template.postForObject("http://localhost:" + this.port + "/application/refresh", null, String.class);
|
||||
String message = template.getForObject("http://localhost:" + this.port + "/",
|
||||
String.class);
|
||||
assertEquals("Hello Dave!", message);
|
||||
@@ -72,12 +68,13 @@ public class RefreshEndpointIntegrationTests {
|
||||
|
||||
private RequestEntity<?> getUrlEncodedEntity(String uri, String key, String value)
|
||||
throws URISyntaxException {
|
||||
MultiValueMap<String, String> env = new LinkedMultiValueMap<String, String>(
|
||||
Collections.singletonMap(key, Arrays.asList(value)));
|
||||
Map<String, String> property = new HashMap<>();
|
||||
property.put("name", key);
|
||||
property.put("value", value);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||||
RequestEntity<MultiValueMap<String, String>> entity = new RequestEntity<MultiValueMap<String, String>>(
|
||||
env, headers, HttpMethod.POST, new URI(uri));
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
RequestEntity<Map<String, String>> entity = new RequestEntity<>(
|
||||
property, headers, HttpMethod.POST, new URI(uri));
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user