@ -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 = get Environment Properties( SimpleConfig . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( SimpleConfig . class ) ;
assertKey ( config , "key" , "myValue" ) ;
assertKey ( config , "anotherKey" , "anotherValue" ) ;
}
@Test
void environmentPropertiesSimpleNonAlias ( ) {
Map < String , Object > config = get Environment Properties( SimpleConfigNonAlias . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( SimpleConfigNonAlias . class ) ;
assertKey ( config , "key" , "myValue" ) ;
assertKey ( config , "anotherKey" , "anotherValue" ) ;
}
@Test
void environmentPropertiesOverrideDefaults ( ) {
Map < String , Object > config = get Environment Properties( OverrideConfig . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( OverrideConfig . class ) ;
assertKey ( config , "server.port" , "2345" ) ;
}
@Test
void environmentPropertiesAppend ( ) {
Map < String , Object > config = get Environment Properties( AppendConfig . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( AppendConfig . class ) ;
assertKey ( config , "key" , "myValue" ) ;
assertKey ( config , "otherKey" , "otherValue" ) ;
}
@Test
void environmentPropertiesSeparatorInValue ( ) {
Map < String , Object > config = get Environment Properties( SameSeparatorInValue . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( SameSeparatorInValue . class ) ;
assertKey ( config , "key" , "my=Value" ) ;
assertKey ( config , "anotherKey" , "another:Value" ) ;
}
@Test
void environmentPropertiesAnotherSeparatorInValue ( ) {
Map < String , Object > config = get Environment Properties( AnotherSeparatorInValue . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( 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 = get Environment Properties( NewLineInValue . class ) ;
Map < String , Object > config = get MergedContextConfiguration Properties( 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 {