@ -19,7 +19,7 @@ import java.util.Objects;
@@ -19,7 +19,7 @@ import java.util.Objects;
import org.springframework.boot.SpringApplication ;
import org.springframework.boot.env.EnvironmentPostProcessor ;
import org.springframework.core.convert.ConversionService ;
import org.springframework.core.convert.ConversionFailedException ;
import org.springframework.core.env.ConfigurableEnvironment ;
import org.springframework.core.env.MapPropertySource ;
import org.springframework.core.env.PropertySource ;
@ -46,8 +46,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
@@ -46,8 +46,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
SpringApplication application ) {
MapPropertySource source = ( MapPropertySource ) environment . getPropertySources ( )
. get ( TestPropertySourceUtils . INLINED_PROPERTIES_PROPERTY_SOURCE_NAME ) ;
if ( source = = null
| | isTestServerPortFixed ( source , environment . getConversionService ( ) )
if ( source = = null | | isTestServerPortFixed ( source , environment )
| | isTestManagementPortConfigured ( source ) ) {
return ;
}
@ -67,9 +66,9 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
@@ -67,9 +66,9 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
}
private boolean isTestServerPortFixed ( MapPropertySource source ,
ConversionService conversionService ) {
return ! Integer . valueOf ( 0 ) . equals (
getPropertyAsInteger ( source , SERVER_PORT_PROPERTY , conversionService ) ) ;
ConfigurableEnvironment environment ) {
return ! Integer . valueOf ( 0 )
. equals ( getPropertyAsInteger ( source , SERVER_PORT_PROPERTY , environment ) ) ;
}
private boolean isTestManagementPortConfigured ( PropertySource < ? > source ) {
@ -81,13 +80,12 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
@@ -81,13 +80,12 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
return environment . getPropertySources ( ) . stream ( )
. filter ( ( source ) - > ! source . getName ( ) . equals (
TestPropertySourceUtils . INLINED_PROPERTIES_PROPERTY_SOURCE_NAME ) )
. map ( ( source ) - > getPropertyAsInteger ( source , property ,
environment . getConversionService ( ) ) )
. map ( ( source ) - > getPropertyAsInteger ( source , property , environment ) )
. filter ( Objects : : nonNull ) . findFirst ( ) . orElse ( defaultValue ) ;
}
private Integer getPropertyAsInteger ( PropertySource < ? > source , String property ,
ConversionService conversionService ) {
ConfigurableEnvironment environment ) {
Object value = source . getProperty ( property ) ;
if ( value = = null ) {
return null ;
@ -95,7 +93,21 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
@@ -95,7 +93,21 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
if ( ClassUtils . isAssignableValue ( Integer . class , value ) ) {
return ( Integer ) value ;
}
return conversionService . convert ( value , Integer . class ) ;
try {
return environment . getConversionService ( ) . convert ( value , Integer . class ) ;
}
catch ( ConversionFailedException ex ) {
if ( ClassUtils . isAssignable ( value . getClass ( ) , String . class ) ) {
return getResolvedValueIfPossible ( environment , ( String ) value ) ;
}
throw ex ;
}
}
private Integer getResolvedValueIfPossible ( ConfigurableEnvironment environment ,
String value ) {
String resolvedValue = environment . resolveRequiredPlaceholders ( value ) ;
return environment . getConversionService ( ) . convert ( resolvedValue , Integer . class ) ;
}
}