@ -16,14 +16,18 @@
@@ -16,14 +16,18 @@
package org.springframework.boot.env ;
import java.util.Collections ;
import org.junit.Test ;
import org.springframework.boot.json.JsonParseException ;
import org.springframework.boot.origin.PropertySourceOrigin ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.core.env.MapPropertySource ;
import org.springframework.core.env.PropertySource ;
import org.springframework.core.env.StandardEnvironment ;
import org.springframework.test.context.support.TestPropertySourceUtils ;
import org.springframework.web.context.support.StandardServletEnvironment ;
import static org.assertj.core.api.Assertions.assertThat ;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType ;
@ -134,4 +138,48 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
@@ -134,4 +138,48 @@ public class SpringApplicationJsonEnvironmentPostProcessorTests {
assertThat ( this . environment . resolvePlaceholders ( "${foo:}" ) ) . isEqualTo ( "bar" ) ;
}
@Test
public void propertySourceShouldBeOrderedBeforeJndiPropertySource ( ) {
testServletPropertySource ( StandardServletEnvironment . JNDI_PROPERTY_SOURCE_NAME ) ;
}
@Test
public void propertySourceShouldBeOrderedBeforeServletContextPropertySource ( ) {
testServletPropertySource ( StandardServletEnvironment . SERVLET_CONTEXT_PROPERTY_SOURCE_NAME ) ;
}
@Test
public void propertySourceShouldBeOrderedBeforeServletConfigPropertySource ( ) {
testServletPropertySource ( StandardServletEnvironment . SERVLET_CONFIG_PROPERTY_SOURCE_NAME ) ;
}
@Test
public void propertySourceOrderingWhenMultipleServletSpecificPropertySources ( ) {
MapPropertySource jndi = getPropertySource ( StandardServletEnvironment . JNDI_PROPERTY_SOURCE_NAME , "jndi" ) ;
this . environment . getPropertySources ( ) . addFirst ( jndi ) ;
MapPropertySource servlet = getPropertySource ( StandardServletEnvironment . SERVLET_CONTEXT_PROPERTY_SOURCE_NAME ,
"servlet" ) ;
this . environment . getPropertySources ( ) . addFirst ( servlet ) ;
MapPropertySource custom = getPropertySource ( "custom" , "custom" ) ;
this . environment . getPropertySources ( ) . addFirst ( custom ) ;
TestPropertySourceUtils . addInlinedPropertiesToEnvironment ( this . environment ,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}" ) ;
this . processor . postProcessEnvironment ( this . environment , null ) ;
PropertySource < ? > json = this . environment . getPropertySources ( ) . get ( "spring.application.json" ) ;
assertThat ( this . environment . getProperty ( "foo" ) ) . isEqualTo ( "custom" ) ;
assertThat ( this . environment . getPropertySources ( ) ) . containsSequence ( custom , json , servlet , jndi ) ;
}
private void testServletPropertySource ( String servletContextPropertySourceName ) {
this . environment . getPropertySources ( ) . addFirst ( getPropertySource ( servletContextPropertySourceName , "servlet" ) ) ;
TestPropertySourceUtils . addInlinedPropertiesToEnvironment ( this . environment ,
"SPRING_APPLICATION_JSON={\"foo\":\"bar\"}" ) ;
this . processor . postProcessEnvironment ( this . environment , null ) ;
assertThat ( this . environment . getProperty ( "foo" ) ) . isEqualTo ( "bar" ) ;
}
private MapPropertySource getPropertySource ( String name , String value ) {
return new MapPropertySource ( name , Collections . singletonMap ( "foo" , value ) ) ;
}
}