Support commas in @ActiveProfiles

Update `SpringBootContextLoader` so that it correctly deals with an
`@ActiveProfiles` annotation that contains a comma.

Fixes gh-19537

Co-authored-by: Scott Frederick <sfrederick@pivotal.io>
Co-authored-by: Andy Wilkinson <awilkinson@pivotal.io>
pull/21009/head
Phillip Webb 5 years ago
parent de8915432a
commit 7ab2bca376

@ -153,8 +153,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
} }
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) { private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles)) String[] pairs = new String[profiles.length];
.applyTo(environment); for (int i = 0; i < profiles.length; i++) {
pairs[i] = "spring.profiles.active[" + i + "]=" + profiles[i];
}
TestPropertyValues.of(pairs).applyTo(environment);
} }
protected String[] getInlinedProperties(MergedContextConfiguration config) { protected String[] getInlinedProperties(MergedContextConfiguration config) {

@ -21,7 +21,9 @@ import java.util.Map;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
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;
@ -41,41 +43,41 @@ class SpringBootContextLoaderTests {
@Test @Test
void environmentPropertiesSimple() { void environmentPropertiesSimple() {
Map<String, Object> config = getEnvironmentProperties(SimpleConfig.class); Map<String, Object> config = getMergedContextConfigurationProperties(SimpleConfig.class);
assertKey(config, "key", "myValue"); assertKey(config, "key", "myValue");
assertKey(config, "anotherKey", "anotherValue"); assertKey(config, "anotherKey", "anotherValue");
} }
@Test @Test
void environmentPropertiesSimpleNonAlias() { void environmentPropertiesSimpleNonAlias() {
Map<String, Object> config = getEnvironmentProperties(SimpleConfigNonAlias.class); Map<String, Object> config = getMergedContextConfigurationProperties(SimpleConfigNonAlias.class);
assertKey(config, "key", "myValue"); assertKey(config, "key", "myValue");
assertKey(config, "anotherKey", "anotherValue"); assertKey(config, "anotherKey", "anotherValue");
} }
@Test @Test
void environmentPropertiesOverrideDefaults() { void environmentPropertiesOverrideDefaults() {
Map<String, Object> config = getEnvironmentProperties(OverrideConfig.class); Map<String, Object> config = getMergedContextConfigurationProperties(OverrideConfig.class);
assertKey(config, "server.port", "2345"); assertKey(config, "server.port", "2345");
} }
@Test @Test
void environmentPropertiesAppend() { void environmentPropertiesAppend() {
Map<String, Object> config = getEnvironmentProperties(AppendConfig.class); Map<String, Object> config = getMergedContextConfigurationProperties(AppendConfig.class);
assertKey(config, "key", "myValue"); assertKey(config, "key", "myValue");
assertKey(config, "otherKey", "otherValue"); assertKey(config, "otherKey", "otherValue");
} }
@Test @Test
void environmentPropertiesSeparatorInValue() { void environmentPropertiesSeparatorInValue() {
Map<String, Object> config = getEnvironmentProperties(SameSeparatorInValue.class); Map<String, Object> config = getMergedContextConfigurationProperties(SameSeparatorInValue.class);
assertKey(config, "key", "my=Value"); assertKey(config, "key", "my=Value");
assertKey(config, "anotherKey", "another:Value"); assertKey(config, "anotherKey", "another:Value");
} }
@Test @Test
void environmentPropertiesAnotherSeparatorInValue() { void environmentPropertiesAnotherSeparatorInValue() {
Map<String, Object> config = getEnvironmentProperties(AnotherSeparatorInValue.class); Map<String, Object> config = getMergedContextConfigurationProperties(AnotherSeparatorInValue.class);
assertKey(config, "key", "my:Value"); assertKey(config, "key", "my:Value");
assertKey(config, "anotherKey", "another=Value"); assertKey(config, "anotherKey", "another=Value");
} }
@ -84,12 +86,33 @@ class SpringBootContextLoaderTests {
@Disabled @Disabled
void environmentPropertiesNewLineInValue() { void environmentPropertiesNewLineInValue() {
// gh-4384 // gh-4384
Map<String, Object> config = getEnvironmentProperties(NewLineInValue.class); Map<String, Object> config = getMergedContextConfigurationProperties(NewLineInValue.class);
assertKey(config, "key", "myValue"); assertKey(config, "key", "myValue");
assertKey(config, "variables", "foo=FOO\n bar=BAR"); assertKey(config, "variables", "foo=FOO\n bar=BAR");
} }
private Map<String, Object> getEnvironmentProperties(Class<?> testClass) { @Test
void noActiveProfiles() {
assertThat(getActiveProfiles(SimpleConfig.class)).isEmpty();
}
@Test
void multipleActiveProfiles() {
assertThat(getActiveProfiles(MultipleActiveProfiles.class)).containsExactly("profile1", "profile2");
}
@Test
void activeProfileWithComma() {
assertThat(getActiveProfiles(ActiveProfileWithComma.class)).containsExactly("profile1,2");
}
private String[] getActiveProfiles(Class<?> testClass) {
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
ApplicationContext applicationContext = testContext.getApplicationContext();
return applicationContext.getEnvironment().getActiveProfiles();
}
private Map<String, Object> getMergedContextConfigurationProperties(Class<?> testClass) {
TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext(); TestContext context = new ExposedTestContextManager(testClass).getExposedTestContext();
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context, MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
"mergedContextConfiguration"); "mergedContextConfiguration");
@ -143,6 +166,20 @@ 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 {

Loading…
Cancel
Save