Browse Source

Make some tests more robust so they pass with BOot 1.4 as well

A couple of tests here make assumptions about the property source
names added using @IntegrationTest. If we are a bit more defensive
and search for a source with a matching name instead of grabbing
one by the precise name, we can have tests that pass in Boot 1.3
and 1.4.
pull/123/head
Dave Syer 9 years ago
parent
commit
31a2b3ea0e
  1. 37
      spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java
  2. 24
      spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java

37
spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java

@ -15,8 +15,6 @@ @@ -15,8 +15,6 @@
*/
package org.springframework.cloud.context.properties;
import static org.junit.Assert.assertEquals;
import java.util.List;
import java.util.Map;
@ -39,10 +37,13 @@ import org.springframework.context.annotation.Bean; @@ -39,10 +37,13 @@ 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.core.env.PropertySource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringApplicationConfiguration(classes=TestConfiguration.class)
import static org.junit.Assert.assertEquals;
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest("messages=one,two")
public class ConfigurationPropertiesRebinderListIntegrationTests {
@ -70,30 +71,39 @@ public class ConfigurationPropertiesRebinderListIntegrationTests { @@ -70,30 +71,39 @@ public class ConfigurationPropertiesRebinderListIntegrationTests {
@Ignore("Can't rebind to list and re-initialize it (need refresh scope for this to work)")
public void testReplaceProperties() throws Exception {
assertEquals("[one, two]", this.properties.getMessages().toString());
@SuppressWarnings("unchecked")
Map<String,Object> map = (Map<String, Object>) this.environment.getPropertySources().get("integrationTest").getSource();
Map<String, Object> map = findTestProperties();
map.clear();
EnvironmentTestUtils.addEnvironment(this.environment, "messages[0]:foo");
this.rebinder.rebind();
assertEquals("[foo]", this.properties.getMessages().toString());
}
private Map<String, Object> findTestProperties() {
for (PropertySource<?> source : this.environment.getPropertySources()) {
if (source.getName().toLowerCase().contains("test")) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) source.getSource();
return map;
}
}
throw new IllegalStateException("Could not find test property source");
}
@Test
@DirtiesContext
public void testReplacePropertiesWithCommaSeparated() throws Exception {
assertEquals("[one, two]", this.properties.getMessages().toString());
@SuppressWarnings("unchecked")
Map<String,Object> map = (Map<String, Object>) this.environment.getPropertySources().get("integrationTest").getSource();
Map<String, Object> map = findTestProperties();
map.clear();
EnvironmentTestUtils.addEnvironment(this.environment, "messages:foo");
this.rebinder.rebind();
assertEquals("[foo]", this.properties.getMessages().toString());
}
@Configuration
@EnableConfigurationProperties
@Import({RefreshConfiguration.RebinderConfiguration.class, PropertyPlaceholderAutoConfiguration.class})
@Import({ RefreshConfiguration.RebinderConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
protected static class TestConfiguration {
@Bean
@ -106,7 +116,8 @@ public class ConfigurationPropertiesRebinderListIntegrationTests { @@ -106,7 +116,8 @@ public class ConfigurationPropertiesRebinderListIntegrationTests {
// Hack out a protected inner class for testing
protected static class RefreshConfiguration extends RefreshAutoConfiguration {
@Configuration
protected static class RebinderConfiguration extends ConfigurationPropertiesRebinderAutoConfiguration {
protected static class RebinderConfiguration
extends ConfigurationPropertiesRebinderAutoConfiguration {
}
}
@ -115,18 +126,22 @@ public class ConfigurationPropertiesRebinderListIntegrationTests { @@ -115,18 +126,22 @@ public class ConfigurationPropertiesRebinderListIntegrationTests {
protected static class TestProperties {
private List<String> messages;
private int count;
public List<String> getMessages() {
return this.messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
public int getCount() {
return this.count;
}
@PostConstruct
public void init() {
this.count ++;
this.count++;
}
}

24
spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java

@ -16,9 +16,6 @@ @@ -16,9 +16,6 @@
package org.springframework.cloud.context.scope.refresh;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -40,10 +37,14 @@ import org.springframework.context.annotation.Bean; @@ -40,10 +37,14 @@ 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.core.env.PropertySource;
import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@RunWith(SpringJUnit4ClassRunner.class)
@IntegrationTest("messages=one,two")
@ -73,17 +74,28 @@ public class RefreshScopeListBindingIntegrationTests { @@ -73,17 +74,28 @@ public class RefreshScopeListBindingIntegrationTests {
public void testReplaceProperties() throws Exception {
assertEquals("[one, two]", this.properties.getMessages().toString());
assertTrue(this.properties instanceof Advised);
@SuppressWarnings("unchecked")
Map<String,Object> map = (Map<String, Object>) this.environment.getPropertySources().get("integrationTest").getSource();
Map<String, Object> map = findTestProperties();
map.clear();
EnvironmentTestUtils.addEnvironment(this.environment, "messages[0]:foo");
this.scope.refreshAll();
assertEquals("[foo]", this.properties.getMessages().toString());
}
private Map<String, Object> findTestProperties() {
for (PropertySource<?> source : this.environment.getPropertySources()) {
if (source.getName().toLowerCase().contains("test")) {
@SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) source.getSource();
return map;
}
}
throw new IllegalStateException("Could not find test property source");
}
@Configuration
@EnableConfigurationProperties
@Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
@Import({ RefreshAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
protected static class TestConfiguration {
@Bean

Loading…
Cancel
Save