@ -16,9 +16,6 @@
package org.springframework.boot.test ;
package org.springframework.boot.test ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertTrue ;
import java.util.Map ;
import java.util.Map ;
import org.junit.Test ;
import org.junit.Test ;
@ -27,6 +24,9 @@ import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager ;
import org.springframework.test.context.TestContextManager ;
import org.springframework.test.util.ReflectionTestUtils ;
import org.springframework.test.util.ReflectionTestUtils ;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertTrue ;
/ * *
/ * *
* Tests for { @link SpringApplicationContextLoader }
* Tests for { @link SpringApplicationContextLoader }
*
*
@ -43,24 +43,12 @@ public class SpringApplicationContextLoaderTests {
assertKey ( config , "anotherKey" , "anotherValue" ) ;
assertKey ( config , "anotherKey" , "anotherValue" ) ;
}
}
@Test
public void environmentPropertiesDefaults ( ) throws Exception {
Map < String , Object > config = getEnvironmentProperties ( SimpleConfig . class ) ;
assertMissingKey ( config , "server.port" ) ;
assertKey ( config , "spring.jmx.enabled" , "false" ) ;
}
@Test
@Test
public void environmentPropertiesOverrideDefaults ( ) throws Exception {
public void environmentPropertiesOverrideDefaults ( ) throws Exception {
Map < String , Object > config = getEnvironmentProperties ( OverrideConfig . class ) ;
Map < String , Object > config = getEnvironmentProperties ( OverrideConfig . class ) ;
assertKey ( config , "server.port" , "2345" ) ;
assertKey ( config , "server.port" , "2345" ) ;
}
}
@Test ( expected = IllegalStateException . class )
public void environmentPropertiesIllegal ( ) throws Exception {
getEnvironmentProperties ( IllegalConfig . class ) ;
}
@Test
@Test
public void environmentPropertiesAppend ( ) throws Exception {
public void environmentPropertiesAppend ( ) throws Exception {
Map < String , Object > config = getEnvironmentProperties ( AppendConfig . class ) ;
Map < String , Object > config = getEnvironmentProperties ( AppendConfig . class ) ;
@ -82,12 +70,15 @@ public class SpringApplicationContextLoaderTests {
assertKey ( config , "anotherKey" , "another=Value" ) ;
assertKey ( config , "anotherKey" , "another=Value" ) ;
}
}
private Map < String , Object > getEnvironmentProperties ( Class < ? > testClass ) throws Exception {
private Map < String , Object > getEnvironmentProperties ( Class < ? > testClass )
TestContext context = new ExposedTestContextManager ( testClass ) . getExposedTestContext ( ) ;
throws Exception {
TestContext context = new ExposedTestContextManager ( testClass )
. getExposedTestContext ( ) ;
new IntegrationTestPropertiesListener ( ) . prepareTestInstance ( context ) ;
new IntegrationTestPropertiesListener ( ) . prepareTestInstance ( context ) ;
MergedContextConfiguration config = ( MergedContextConfiguration ) ReflectionTestUtils . getField (
MergedContextConfiguration config = ( MergedContextConfiguration ) ReflectionTestUtils
context , "mergedContextConfiguration" ) ;
. getField ( context , "mergedContextConfiguration" ) ;
return this . loader . extractEnvironmentProperties ( config . getPropertySourceProperties ( ) ) ;
return this . loader . extractEnvironmentProperties ( config
. getPropertySourceProperties ( ) ) ;
}
}
private void assertKey ( Map < String , Object > actual , String key , Object value ) {
private void assertKey ( Map < String , Object > actual , String key , Object value ) {
@ -95,10 +86,6 @@ public class SpringApplicationContextLoaderTests {
assertEquals ( value , actual . get ( key ) ) ;
assertEquals ( value , actual . get ( key ) ) ;
}
}
private void assertMissingKey ( Map < String , Object > actual , String key ) {
assertTrue ( "Key '" + key + "' found" , ! actual . containsKey ( key ) ) ;
}
@IntegrationTest ( { "key=myValue" , "anotherKey:anotherValue" } )
@IntegrationTest ( { "key=myValue" , "anotherKey:anotherValue" } )
static class SimpleConfig {
static class SimpleConfig {
}
}
@ -107,11 +94,7 @@ public class SpringApplicationContextLoaderTests {
static class OverrideConfig {
static class OverrideConfig {
}
}
@IntegrationTest ( value = { "key=aValue" , "anotherKey:anotherValue" } , properties = { "key=myValue" , "otherKey=otherValue" } )
@IntegrationTest ( { "key=myValue" , "otherKey=otherValue" } )
static class IllegalConfig {
}
@IntegrationTest ( properties = { "key=myValue" , "otherKey=otherValue" } )
static class AppendConfig {
static class AppendConfig {
}
}
@ -122,18 +105,20 @@ public class SpringApplicationContextLoaderTests {
@IntegrationTest ( { "key=my:Value" , "anotherKey:another=Value" } )
@IntegrationTest ( { "key=my:Value" , "anotherKey:another=Value" } )
static class AnotherSeparatorInValue {
static class AnotherSeparatorInValue {
}
}
/ * *
* { @link TestContextManager } which exposes the { @link TestContext } .
* /
private static class ExposedTestContextManager extends TestContextManager {
private static class ExposedTestContextManager extends TestContextManager {
public ExposedTestContextManager ( Class < ? > testClass ) {
public ExposedTestContextManager ( Class < ? > testClass ) {
super ( testClass ) ;
super ( testClass ) ;
}
}
public final TestContext getExposedTestContext ( ) {
public final TestContext getExposedTestContext ( ) {
return super . getTestContext ( ) ;
return super . getTestContext ( ) ;
}
}
}
}
}
}