Perform placeholder resolution in test env post processor
Fixes gh-15354
This commit is contained in:
parent
4c6c07ddbb
commit
31a5e85ab6
|
@ -19,7 +19,7 @@ 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.convert.ConversionFailedException;
|
||||||
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;
|
||||||
|
@ -46,8 +46,7 @@ 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
|
if (source == null || isTestServerPortFixed(source, environment)
|
||||||
|| isTestServerPortFixed(source, environment.getConversionService())
|
|
||||||
|| isTestManagementPortConfigured(source)) {
|
|| isTestManagementPortConfigured(source)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -67,9 +66,9 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isTestServerPortFixed(MapPropertySource source,
|
private boolean isTestServerPortFixed(MapPropertySource source,
|
||||||
ConversionService conversionService) {
|
ConfigurableEnvironment environment) {
|
||||||
return !Integer.valueOf(0).equals(
|
return !Integer.valueOf(0)
|
||||||
getPropertyAsInteger(source, SERVER_PORT_PROPERTY, conversionService));
|
.equals(getPropertyAsInteger(source, SERVER_PORT_PROPERTY, environment));
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isTestManagementPortConfigured(PropertySource<?> source) {
|
private boolean isTestManagementPortConfigured(PropertySource<?> source) {
|
||||||
|
@ -81,13 +80,12 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
|
||||||
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) -> getPropertyAsInteger(source, property,
|
.map((source) -> getPropertyAsInteger(source, property, environment))
|
||||||
environment.getConversionService()))
|
|
||||||
.filter(Objects::nonNull).findFirst().orElse(defaultValue);
|
.filter(Objects::nonNull).findFirst().orElse(defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer getPropertyAsInteger(PropertySource<?> source, String property,
|
private Integer getPropertyAsInteger(PropertySource<?> source, String property,
|
||||||
ConversionService conversionService) {
|
ConfigurableEnvironment environment) {
|
||||||
Object value = source.getProperty(property);
|
Object value = source.getProperty(property);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -95,7 +93,21 @@ class SpringBootTestRandomPortEnvironmentPostProcessor
|
||||||
if (ClassUtils.isAssignableValue(Integer.class, value)) {
|
if (ClassUtils.isAssignableValue(Integer.class, value)) {
|
||||||
return (Integer) 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.springframework.mock.env.MockEnvironment;
|
||||||
import org.springframework.test.context.support.TestPropertySourceUtils;
|
import org.springframework.test.context.support.TestPropertySourceUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
|
* Tests for {@link SpringBootTestRandomPortEnvironmentPostProcessor}.
|
||||||
|
@ -141,6 +142,56 @@ public class SpringBootTestRandomPortEnvironmentPostProcessorTests {
|
||||||
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postProcessWhenManagementServerPortPlaceholderPresentShouldResolvePlaceholder() {
|
||||||
|
addTestPropertySource("0", null);
|
||||||
|
MapPropertySource testPropertySource = (MapPropertySource) this.propertySources
|
||||||
|
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||||
|
testPropertySource.getSource().put("port", "9090");
|
||||||
|
this.propertySources.addLast(new MapPropertySource("other",
|
||||||
|
Collections.singletonMap("management.server.port", "${port}")));
|
||||||
|
this.postProcessor.postProcessEnvironment(this.environment, null);
|
||||||
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
|
||||||
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postProcessWhenManagementServerPortPlaceholderAbsentShouldFail() {
|
||||||
|
addTestPropertySource("0", null);
|
||||||
|
this.propertySources.addLast(new MapPropertySource("other",
|
||||||
|
Collections.singletonMap("management.server.port", "${port}")));
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
|
() -> this.postProcessor.postProcessEnvironment(this.environment, null))
|
||||||
|
.withMessage("Could not resolve placeholder 'port' in value \"${port}\"");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postProcessWhenServerPortPlaceholderPresentShouldResolvePlaceholder() {
|
||||||
|
addTestPropertySource("0", null);
|
||||||
|
MapPropertySource testPropertySource = (MapPropertySource) this.propertySources
|
||||||
|
.get(TestPropertySourceUtils.INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||||
|
testPropertySource.getSource().put("port", "8080");
|
||||||
|
Map<String, Object> source = new HashMap<>();
|
||||||
|
source.put("server.port", "${port}");
|
||||||
|
source.put("management.server.port", "9090");
|
||||||
|
this.propertySources.addLast(new MapPropertySource("other", source));
|
||||||
|
this.postProcessor.postProcessEnvironment(this.environment, null);
|
||||||
|
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
|
||||||
|
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("0");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void postProcessWhenServerPortPlaceholderAbsentShouldFail() {
|
||||||
|
addTestPropertySource("0", null);
|
||||||
|
Map<String, Object> source = new HashMap<>();
|
||||||
|
source.put("server.port", "${port}");
|
||||||
|
source.put("management.server.port", "9090");
|
||||||
|
this.propertySources.addLast(new MapPropertySource("other", source));
|
||||||
|
assertThatIllegalArgumentException().isThrownBy(
|
||||||
|
() -> this.postProcessor.postProcessEnvironment(this.environment, null))
|
||||||
|
.withMessage("Could not resolve placeholder 'port' in value \"${port}\"");
|
||||||
|
}
|
||||||
|
|
||||||
private void addTestPropertySource(String serverPort, String managementPort) {
|
private void addTestPropertySource(String serverPort, String managementPort) {
|
||||||
Map<String, Object> source = new HashMap<>();
|
Map<String, Object> source = new HashMap<>();
|
||||||
source.put("server.port", serverPort);
|
source.put("server.port", serverPort);
|
||||||
|
|
Loading…
Reference in New Issue