Use new configuration properties in spring-boot-test
Update `spring-boot-test` to use the new configuration properties support. See gh-9000
This commit is contained in:
parent
d4ef523e64
commit
c0d4a771c9
|
|
@ -21,13 +21,14 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.test.mock.web.SpringBootMockServletContext;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext;
|
||||
|
|
@ -40,10 +41,6 @@ import org.springframework.core.SpringVersion;
|
|||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySources;
|
||||
import org.springframework.core.env.PropertySourcesPropertyResolver;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
|
|
@ -76,6 +73,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
|||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Stephane Nicoll
|
||||
* @author Madhura Bhave
|
||||
* @see SpringBootTest
|
||||
*/
|
||||
public class SpringBootContextLoader extends AbstractContextLoader {
|
||||
|
|
@ -171,19 +169,15 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
|||
}
|
||||
|
||||
private boolean hasCustomServerPort(List<String> properties) {
|
||||
PropertySources sources = convertToPropertySources(properties);
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
|
||||
new PropertySourcesPropertyResolver(sources), "server.");
|
||||
return resolver.containsProperty("port");
|
||||
Binder binder = new Binder(convertToConfigurationPropertySource(properties));
|
||||
return binder.bind("server.port", Bindable.of(String.class)).isBound();
|
||||
}
|
||||
|
||||
private PropertySources convertToPropertySources(List<String> properties) {
|
||||
Map<String, Object> source = TestPropertySourceUtils
|
||||
.convertInlinedPropertiesToMap(
|
||||
properties.toArray(new String[properties.size()]));
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
sources.addFirst(new MapPropertySource("inline", source));
|
||||
return sources;
|
||||
private MapConfigurationPropertySource convertToConfigurationPropertySource(
|
||||
List<String> properties) {
|
||||
String[] array = properties.toArray(new String[properties.size()]);
|
||||
return new MapConfigurationPropertySource(
|
||||
TestPropertySourceUtils.convertInlinedPropertiesToMap(array));
|
||||
}
|
||||
|
||||
private List<ApplicationContextInitializer<?>> getInitializers(
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ import java.util.ArrayList;
|
|||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
|
|
@ -29,15 +28,14 @@ import org.apache.commons.logging.LogFactory;
|
|||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.context.properties.bind.Bindable;
|
||||
import org.springframework.boot.context.properties.bind.Binder;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySources;
|
||||
import org.springframework.core.env.PropertySourcesPropertyResolver;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
|
|
@ -71,6 +69,7 @@ import org.springframework.util.ObjectUtils;
|
|||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Brian Clozel
|
||||
* @author Madhura Bhave
|
||||
* @since 1.4.0
|
||||
* @see SpringBootTest
|
||||
* @see TestConfiguration
|
||||
|
|
@ -98,7 +97,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
verifyConfiguration(context.getTestClass());
|
||||
WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
|
||||
if (webEnvironment == WebEnvironment.MOCK
|
||||
&& deduceWebApplication() == WebApplicationType.SERVLET) {
|
||||
&& deduceWebApplicationType() == WebApplicationType.SERVLET) {
|
||||
context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
|
||||
}
|
||||
else if (webEnvironment != null && webEnvironment.isEmbedded()) {
|
||||
|
|
@ -181,15 +180,17 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
|
||||
private WebApplicationType getWebApplicationType(
|
||||
MergedContextConfiguration configuration) {
|
||||
WebApplicationType webApplicationType = getConfiguredWebApplicationType(
|
||||
configuration);
|
||||
if (webApplicationType != null) {
|
||||
return webApplicationType;
|
||||
}
|
||||
return deduceWebApplication();
|
||||
ConfigurationPropertySource source = new MapConfigurationPropertySource(
|
||||
TestPropertySourceUtils.convertInlinedPropertiesToMap(
|
||||
configuration.getPropertySourceProperties()));
|
||||
Binder binder = new Binder(source);
|
||||
return binder
|
||||
.bind("spring.main.web-application-type",
|
||||
Bindable.of(WebApplicationType.class))
|
||||
.orElseGet(this::deduceWebApplicationType);
|
||||
}
|
||||
|
||||
private WebApplicationType deduceWebApplication() {
|
||||
private WebApplicationType deduceWebApplicationType() {
|
||||
if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
|
||||
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
|
||||
return WebApplicationType.REACTIVE;
|
||||
|
|
@ -228,25 +229,6 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
|
|||
return false;
|
||||
}
|
||||
|
||||
private WebApplicationType getConfiguredWebApplicationType(
|
||||
MergedContextConfiguration configuration) {
|
||||
PropertySources sources = convertToPropertySources(
|
||||
configuration.getPropertySourceProperties());
|
||||
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
|
||||
new PropertySourcesPropertyResolver(sources), "spring.main.");
|
||||
String property = resolver.getProperty("web-application-type");
|
||||
return (property != null ? WebApplicationType.valueOf(property.toUpperCase())
|
||||
: null);
|
||||
}
|
||||
|
||||
private PropertySources convertToPropertySources(String[] properties) {
|
||||
Map<String, Object> source = TestPropertySourceUtils
|
||||
.convertInlinedPropertiesToMap(properties);
|
||||
MutablePropertySources sources = new MutablePropertySources();
|
||||
sources.addFirst(new MapPropertySource("inline", source));
|
||||
return sources;
|
||||
}
|
||||
|
||||
protected Class<?>[] getOrFindConfigurationClasses(
|
||||
MergedContextConfiguration mergedConfig) {
|
||||
Class<?>[] classes = mergedConfig.getClasses();
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.boot.test.web.client;
|
||||
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.web.client.RootUriTemplateHandler;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.util.Assert;
|
||||
|
|
@ -30,6 +29,7 @@ import org.springframework.web.util.UriTemplateHandler;
|
|||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Eddú Meléndez
|
||||
* @author Madhura Bhave
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
|
||||
|
|
@ -38,7 +38,7 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
|
|||
|
||||
private final String scheme;
|
||||
|
||||
private RelaxedPropertyResolver servletPropertyResolver;
|
||||
private final String prefix = "server.servlet.";
|
||||
|
||||
/**
|
||||
* Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http}
|
||||
|
|
@ -63,14 +63,13 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
|
|||
Assert.notNull(scheme, "Scheme must not be null");
|
||||
this.environment = environment;
|
||||
this.scheme = scheme;
|
||||
this.servletPropertyResolver = new RelaxedPropertyResolver(environment,
|
||||
"server.servlet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getRootUri() {
|
||||
String port = this.environment.getProperty("local.server.port", "8080");
|
||||
String contextPath = this.servletPropertyResolver.getProperty("context-path", "");
|
||||
String contextPath = this.environment.getProperty(this.prefix + "context-path",
|
||||
"");
|
||||
return this.scheme + "://localhost:" + port + contextPath;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue