19
19
20
20
import org .springframework .boot .SpringApplication ;
21
21
import org .springframework .boot .env .EnvironmentPostProcessor ;
22
+ import org .springframework .core .convert .ConversionService ;
22
23
import org .springframework .core .env .ConfigurableEnvironment ;
23
24
import org .springframework .core .env .MapPropertySource ;
24
25
import org .springframework .core .env .PropertySource ;
25
26
import org .springframework .test .context .support .TestPropertySourceUtils ;
27
+ import org .springframework .util .ClassUtils ;
26
28
27
29
/**
28
30
* {@link EnvironmentPostProcessor} implementation to start the management context on a
@@ -44,15 +46,18 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
44
46
SpringApplication application ) {
45
47
MapPropertySource source = (MapPropertySource ) environment .getPropertySources ()
46
48
.get (TestPropertySourceUtils .INLINED_PROPERTIES_PROPERTY_SOURCE_NAME );
47
- if (source == null || isTestServerPortFixed (source )
49
+ if (source == null
50
+ || isTestServerPortFixed (source , environment .getConversionService ())
48
51
|| isTestManagementPortConfigured (source )) {
49
52
return ;
50
53
}
51
- String managementPort = getProperty (environment , MANAGEMENT_PORT_PROPERTY , null );
52
- if (managementPort == null || managementPort .equals ("-1" )) {
54
+ Integer managementPort = getPropertyAsInteger (environment ,
55
+ MANAGEMENT_PORT_PROPERTY , null );
56
+ if (managementPort == null || managementPort .equals (-1 )) {
53
57
return ;
54
58
}
55
- String serverPort = getProperty (environment , SERVER_PORT_PROPERTY , "8080" );
59
+ Integer serverPort = getPropertyAsInteger (environment , SERVER_PORT_PROPERTY ,
60
+ 8080 );
56
61
if (!managementPort .equals (serverPort )) {
57
62
source .getSource ().put (MANAGEMENT_PORT_PROPERTY , "0" );
58
63
}
@@ -61,21 +66,36 @@ public void postProcessEnvironment(ConfigurableEnvironment environment,
61
66
}
62
67
}
63
68
64
- private boolean isTestServerPortFixed (MapPropertySource source ) {
65
- return !"0" .equals (source .getProperty (SERVER_PORT_PROPERTY ));
69
+ private boolean isTestServerPortFixed (MapPropertySource source ,
70
+ ConversionService conversionService ) {
71
+ return !Integer .valueOf (0 ).equals (
72
+ getPropertyAsInteger (source , SERVER_PORT_PROPERTY , conversionService ));
66
73
}
67
74
68
75
private boolean isTestManagementPortConfigured (PropertySource <?> source ) {
69
76
return source .getProperty (MANAGEMENT_PORT_PROPERTY ) != null ;
70
77
}
71
78
72
- private String getProperty (ConfigurableEnvironment environment , String property ,
73
- String defaultValue ) {
79
+ private Integer getPropertyAsInteger (ConfigurableEnvironment environment ,
80
+ String property , Integer defaultValue ) {
74
81
return environment .getPropertySources ().stream ()
75
82
.filter ((source ) -> !source .getName ().equals (
76
83
TestPropertySourceUtils .INLINED_PROPERTIES_PROPERTY_SOURCE_NAME ))
77
- .map ((source ) -> (String ) source .getProperty (property ))
84
+ .map ((source ) -> getPropertyAsInteger (source , property ,
85
+ environment .getConversionService ()))
78
86
.filter (Objects ::nonNull ).findFirst ().orElse (defaultValue );
79
87
}
80
88
89
+ private Integer getPropertyAsInteger (PropertySource <?> source , String property ,
90
+ ConversionService conversionService ) {
91
+ Object value = source .getProperty (property );
92
+ if (value == null ) {
93
+ return null ;
94
+ }
95
+ if (ClassUtils .isAssignableValue (Integer .class , value )) {
96
+ return (Integer ) value ;
97
+ }
98
+ return conversionService .convert (value , Integer .class );
99
+ }
100
+
81
101
}
0 commit comments