mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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>
This commit is contained in:
parent
de8915432a
commit
7ab2bca376
@ -153,8 +153,11 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
}
|
||||
|
||||
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles) {
|
||||
TestPropertyValues.of("spring.profiles.active=" + StringUtils.arrayToCommaDelimitedString(profiles))
|
||||
.applyTo(environment);
|
||||
String[] pairs = new String[profiles.length];
|
||||
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) {
|
||||
|
@ -21,7 +21,9 @@ import java.util.Map;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
@ -41,41 +43,41 @@ class SpringBootContextLoaderTests {
|
||||
|
||||
@Test
|
||||
void environmentPropertiesSimple() {
|
||||
Map<String, Object> config = getEnvironmentProperties(SimpleConfig.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(SimpleConfig.class);
|
||||
assertKey(config, "key", "myValue");
|
||||
assertKey(config, "anotherKey", "anotherValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentPropertiesSimpleNonAlias() {
|
||||
Map<String, Object> config = getEnvironmentProperties(SimpleConfigNonAlias.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(SimpleConfigNonAlias.class);
|
||||
assertKey(config, "key", "myValue");
|
||||
assertKey(config, "anotherKey", "anotherValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentPropertiesOverrideDefaults() {
|
||||
Map<String, Object> config = getEnvironmentProperties(OverrideConfig.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(OverrideConfig.class);
|
||||
assertKey(config, "server.port", "2345");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentPropertiesAppend() {
|
||||
Map<String, Object> config = getEnvironmentProperties(AppendConfig.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(AppendConfig.class);
|
||||
assertKey(config, "key", "myValue");
|
||||
assertKey(config, "otherKey", "otherValue");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentPropertiesSeparatorInValue() {
|
||||
Map<String, Object> config = getEnvironmentProperties(SameSeparatorInValue.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(SameSeparatorInValue.class);
|
||||
assertKey(config, "key", "my=Value");
|
||||
assertKey(config, "anotherKey", "another:Value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void environmentPropertiesAnotherSeparatorInValue() {
|
||||
Map<String, Object> config = getEnvironmentProperties(AnotherSeparatorInValue.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(AnotherSeparatorInValue.class);
|
||||
assertKey(config, "key", "my:Value");
|
||||
assertKey(config, "anotherKey", "another=Value");
|
||||
}
|
||||
@ -84,12 +86,33 @@ class SpringBootContextLoaderTests {
|
||||
@Disabled
|
||||
void environmentPropertiesNewLineInValue() {
|
||||
// gh-4384
|
||||
Map<String, Object> config = getEnvironmentProperties(NewLineInValue.class);
|
||||
Map<String, Object> config = getMergedContextConfigurationProperties(NewLineInValue.class);
|
||||
assertKey(config, "key", "myValue");
|
||||
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();
|
||||
MergedContextConfiguration config = (MergedContextConfiguration) ReflectionTestUtils.getField(context,
|
||||
"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)
|
||||
static class Config {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user