|
|
|
@ -19,10 +19,12 @@ import java.util.Objects; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.SpringApplication; |
|
|
|
import org.springframework.boot.SpringApplication; |
|
|
|
import org.springframework.boot.env.EnvironmentPostProcessor; |
|
|
|
import org.springframework.boot.env.EnvironmentPostProcessor; |
|
|
|
|
|
|
|
import org.springframework.core.convert.ConversionService; |
|
|
|
import org.springframework.core.env.ConfigurableEnvironment; |
|
|
|
import org.springframework.core.env.ConfigurableEnvironment; |
|
|
|
import org.springframework.core.env.MapPropertySource; |
|
|
|
import org.springframework.core.env.MapPropertySource; |
|
|
|
import org.springframework.core.env.PropertySource; |
|
|
|
import org.springframework.core.env.PropertySource; |
|
|
|
import org.springframework.test.context.support.TestPropertySourceUtils; |
|
|
|
import org.springframework.test.context.support.TestPropertySourceUtils; |
|
|
|
|
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* {@link EnvironmentPostProcessor} implementation to start the management context on a |
|
|
|
* {@link EnvironmentPostProcessor} implementation to start the management context on a |
|
|
|
@ -44,15 +46,18 @@ class SpringBootTestRandomPortEnvironmentPostProcessor |
|
|
|
SpringApplication application) { |
|
|
|
SpringApplication application) { |
|
|
|
MapPropertySource source = (MapPropertySource) environment.getPropertySources() |
|
|
|
MapPropertySource source = (MapPropertySource) environment.getPropertySources() |
|
|
|
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); |
|
|
|
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME); |
|
|
|
if (source == null || isTestServerPortFixed(source) |
|
|
|
if (source == null |
|
|
|
|
|
|
|
|| isTestServerPortFixed(source, environment.getConversionService()) |
|
|
|
|| isTestManagementPortConfigured(source)) { |
|
|
|
|| isTestManagementPortConfigured(source)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
String managementPort = getProperty(environment, MANAGEMENT_PORT_PROPERTY, null); |
|
|
|
Integer managementPort = getPropertyAsInteger(environment, |
|
|
|
if (managementPort == null || managementPort.equals("-1")) { |
|
|
|
MANAGEMENT_PORT_PROPERTY, null); |
|
|
|
|
|
|
|
if (managementPort == null || managementPort.equals(-1)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
String serverPort = getProperty(environment, SERVER_PORT_PROPERTY, "8080"); |
|
|
|
Integer serverPort = getPropertyAsInteger(environment, SERVER_PORT_PROPERTY, |
|
|
|
|
|
|
|
8080); |
|
|
|
if (!managementPort.equals(serverPort)) { |
|
|
|
if (!managementPort.equals(serverPort)) { |
|
|
|
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0"); |
|
|
|
source.getSource().put(MANAGEMENT_PORT_PROPERTY, "0"); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -61,21 +66,36 @@ class SpringBootTestRandomPortEnvironmentPostProcessor |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean isTestServerPortFixed(MapPropertySource source) { |
|
|
|
private boolean isTestServerPortFixed(MapPropertySource source, |
|
|
|
return !"0".equals(source.getProperty(SERVER_PORT_PROPERTY)); |
|
|
|
ConversionService conversionService) { |
|
|
|
|
|
|
|
return !Integer.valueOf(0).equals( |
|
|
|
|
|
|
|
getPropertyAsInteger(source, SERVER_PORT_PROPERTY, conversionService)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean isTestManagementPortConfigured(PropertySource<?> source) { |
|
|
|
private boolean isTestManagementPortConfigured(PropertySource<?> source) { |
|
|
|
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null; |
|
|
|
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String getProperty(ConfigurableEnvironment environment, String property, |
|
|
|
private Integer getPropertyAsInteger(ConfigurableEnvironment environment, |
|
|
|
String defaultValue) { |
|
|
|
String property, Integer defaultValue) { |
|
|
|
return environment.getPropertySources().stream() |
|
|
|
return environment.getPropertySources().stream() |
|
|
|
.filter((source) -> !source.getName().equals( |
|
|
|
.filter((source) -> !source.getName().equals( |
|
|
|
TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME)) |
|
|
|
TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME)) |
|
|
|
.map((source) -> (String) source.getProperty(property)) |
|
|
|
.map((source) -> getPropertyAsInteger(source, property, |
|
|
|
|
|
|
|
environment.getConversionService())) |
|
|
|
.filter(Objects::nonNull).findFirst().orElse(defaultValue); |
|
|
|
.filter(Objects::nonNull).findFirst().orElse(defaultValue); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Integer getPropertyAsInteger(PropertySource<?> source, String property, |
|
|
|
|
|
|
|
ConversionService conversionService) { |
|
|
|
|
|
|
|
Object value = source.getProperty(property); |
|
|
|
|
|
|
|
if (value == null) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (ClassUtils.isAssignableValue(Integer.class, value)) { |
|
|
|
|
|
|
|
return (Integer) value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return conversionService.convert(value, Integer.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|