@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 201 4 the original author or authors .
* Copyright 2012 - 201 5 the original author or authors .
*
* Licensed under the Apache License , Version 2.0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -14,7 +14,10 @@
* limitations under the License .
* /
package org.springframework.boot.test ;
package org.springframework.boot.context.web ;
import java.util.HashMap ;
import java.util.Map ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.boot.context.embedded.EmbeddedServletContainer ;
@ -24,7 +27,10 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer ;
import org.springframework.context.ApplicationListener ;
import org.springframework.context.ConfigurableApplicationContext ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.core.env.Environment ;
import org.springframework.core.env.MapPropertySource ;
import org.springframework.core.env.MutablePropertySources ;
import org.springframework.util.StringUtils ;
/ * *
@ -75,8 +81,21 @@ public class ServerPortInfoApplicationContextInitializer implements
private void setPortProperty ( ApplicationContext context , String propertyName , int port ) {
if ( context instanceof ConfigurableApplicationContext ) {
EnvironmentTestUtils . addEnvironment ( ( ConfigurableApplicationContext ) context ,
propertyName + ":" + port ) ;
ConfigurableEnvironment environment = ( ( ConfigurableApplicationContext ) context ) . getEnvironment ( ) ;
MutablePropertySources sources = environment . getPropertySources ( ) ;
Map < String , Object > map ;
if ( ! sources . contains ( "server.ports" ) ) {
map = new HashMap < String , Object > ( ) ;
MapPropertySource source = new MapPropertySource ( "server.ports" , map ) ;
sources . addFirst ( source ) ;
}
else {
@SuppressWarnings ( "unchecked" )
Map < String , Object > value = ( Map < String , Object > ) sources . get ( "server.ports" )
. getSource ( ) ;
map = value ;
}
map . put ( propertyName , port ) ;
}
if ( context . getParent ( ) ! = null ) {
setPortProperty ( context . getParent ( ) , propertyName , port ) ;