Commit f708aace authored by Andy Wilkinson's avatar Andy Wilkinson

Merge branch '2.1.x' into 2.2.x

Fixes gh-19821
parents 94b14600 c85f19b7
...@@ -29,6 +29,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS ...@@ -29,6 +29,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyS
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.mock.web.SpringBootMockServletContext; import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer; import org.springframework.boot.web.servlet.support.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
...@@ -97,7 +98,7 @@ public class SpringBootContextLoader extends AbstractContextLoader { ...@@ -97,7 +98,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
application.getSources().addAll(Arrays.asList(configLocations)); application.getSources().addAll(Arrays.asList(configLocations));
ConfigurableEnvironment environment = getEnvironment(); ConfigurableEnvironment environment = getEnvironment();
if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { if (!ObjectUtils.isEmpty(config.getActiveProfiles())) {
environment.setActiveProfiles(config.getActiveProfiles()); setActiveProfiles(environment, config.getActiveProfiles());
} }
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader() ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
: new DefaultResourceLoader(getClass().getClassLoader()); : new DefaultResourceLoader(getClass().getClassLoader());
...@@ -155,6 +156,11 @@ public class SpringBootContextLoader extends AbstractContextLoader { ...@@ -155,6 +156,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
.getValue("args", String[].class).orElse(NO_ARGS); .getValue("args", String[].class).orElse(NO_ARGS);
} }
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles))
.applyTo(environment);
}
protected String[] getInlinedProperties(MergedContextConfiguration config) { protected String[] getInlinedProperties(MergedContextConfiguration config) {
ArrayList<String> properties = new ArrayList<>(); ArrayList<String> properties = new ArrayList<>();
// JMX bean names will clash if the same bean is used in multiple contexts // JMX bean names will clash if the same bean is used in multiple contexts
......
...@@ -22,8 +22,6 @@ import org.junit.jupiter.api.Disabled; ...@@ -22,8 +22,6 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.MergedContextConfiguration; import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext; import org.springframework.test.context.TestContext;
...@@ -91,40 +89,13 @@ class SpringBootContextLoaderTests { ...@@ -91,40 +89,13 @@ class SpringBootContextLoaderTests {
assertKey(config, "variables", "foo=FOO\n bar=BAR"); assertKey(config, "variables", "foo=FOO\n bar=BAR");
} }
@Test
void noActiveProfiles() {
Environment environment = getApplicationEnvironment(SimpleConfig.class);
assertThat(environment.getActiveProfiles()).isEmpty();
}
@Test
void multipleActiveProfiles() {
Environment environment = getApplicationEnvironment(MultipleActiveProfiles.class);
assertThat(environment.getActiveProfiles()).containsExactly("profile1", "profile2");
}
@Test
void activeProfileWithComma() {
Environment environment = getApplicationEnvironment(ActiveProfileWithComma.class);
assertThat(environment.getActiveProfiles()).containsExactly("profile1,2");
}
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) { private Map<String, Object> getEnvironmentProperties(Class<?> testClass) {
TestContext context = getTestContext(testClass); TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
"mergedContextConfiguration"); "mergedContextConfiguration");
return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties()); return TestPropertySourceUtils.convertInlinedPropertiesToMap(config.getPropertySourceProperties());
} }
private Environment getApplicationEnvironment(Class<?> testClass) {
TestContext context = getTestContext(testClass);
return context.getApplicationContext().getEnvironment();
}
private TestContext getTestContext(Class<?> testClass) {
return new ExposedTestContextManager(testClass).getExposedTestContext();
}
private void assertKey(Map<String, Object> actual, String key, Object value) { private void assertKey(Map<String, Object> actual, String key, Object value) {
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue(); assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
assertThat(actual.get(key)).isEqualTo(value); assertThat(actual.get(key)).isEqualTo(value);
...@@ -172,20 +143,6 @@ class SpringBootContextLoaderTests { ...@@ -172,20 +143,6 @@ class SpringBootContextLoaderTests {
} }
@SpringBootTest
@ActiveProfiles({ "profile1", "profile2" })
@ContextConfiguration(classes = Config.class)
static class MultipleActiveProfiles {
}
@SpringBootTest
@ActiveProfiles({ "profile1,2" })
@ContextConfiguration(classes = Config.class)
static class ActiveProfileWithComma {
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class Config { static class Config {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment