From 31a2b3ea0ec219ca6d9cfe3cfe17294a59a5f2ff Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 5 May 2016 14:05:46 +0100 Subject: [PATCH] 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. --- ...ropertiesRebinderListIntegrationTests.java | 37 +++++++++++++------ ...freshScopeListBindingIntegrationTests.java | 24 +++++++++--- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java index 01980f3a..33195eea 100644 --- a/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java +++ b/spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java @@ -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; 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 { @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 map = (Map) this.environment.getPropertySources().get("integrationTest").getSource(); + Map map = findTestProperties(); map.clear(); EnvironmentTestUtils.addEnvironment(this.environment, "messages[0]:foo"); this.rebinder.rebind(); assertEquals("[foo]", this.properties.getMessages().toString()); } + private Map findTestProperties() { + for (PropertySource source : this.environment.getPropertySources()) { + if (source.getName().toLowerCase().contains("test")) { + @SuppressWarnings("unchecked") + Map map = (Map) 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 map = (Map) this.environment.getPropertySources().get("integrationTest").getSource(); + Map 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 { // 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 { protected static class TestProperties { private List messages; private int count; + public List getMessages() { return this.messages; } + public void setMessages(List messages) { this.messages = messages; } + public int getCount() { return this.count; } + @PostConstruct public void init() { - this.count ++; + this.count++; } } 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 a1dc82bf..378ed12a 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 @@ -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; 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 { public void testReplaceProperties() throws Exception { assertEquals("[one, two]", this.properties.getMessages().toString()); assertTrue(this.properties instanceof Advised); - @SuppressWarnings("unchecked") - Map map = (Map) this.environment.getPropertySources().get("integrationTest").getSource(); + Map map = findTestProperties(); map.clear(); EnvironmentTestUtils.addEnvironment(this.environment, "messages[0]:foo"); this.scope.refreshAll(); assertEquals("[foo]", this.properties.getMessages().toString()); } + private Map findTestProperties() { + for (PropertySource source : this.environment.getPropertySources()) { + if (source.getName().toLowerCase().contains("test")) { + @SuppressWarnings("unchecked") + Map map = (Map) 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