Use new configuration properties in spring-boot-test

Update `spring-boot-test` to use the new configuration properties
support.

See gh-9000
pull/8802/merge
Madhura Bhave 8 years ago committed by Phillip Webb
parent d4ef523e64
commit c0d4a771c9

@ -21,13 +21,14 @@ import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType; 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.mock.web.SpringBootMockServletContext;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.web.reactive.context.GenericReactiveWebApplicationContext; 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.AnnotatedElementUtils;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment; 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.env.StandardEnvironment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextConfigurationAttributes;
@ -76,6 +73,7 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Madhura Bhave
* @see SpringBootTest * @see SpringBootTest
*/ */
public class SpringBootContextLoader extends AbstractContextLoader { public class SpringBootContextLoader extends AbstractContextLoader {
@ -171,19 +169,15 @@ public class SpringBootContextLoader extends AbstractContextLoader {
} }
private boolean hasCustomServerPort(List<String> properties) { private boolean hasCustomServerPort(List<String> properties) {
PropertySources sources = convertToPropertySources(properties); Binder binder = new Binder(convertToConfigurationPropertySource(properties));
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver( return binder.bind("server.port", Bindable.of(String.class)).isBound();
new PropertySourcesPropertyResolver(sources), "server.");
return resolver.containsProperty("port");
} }
private PropertySources convertToPropertySources(List<String> properties) { private MapConfigurationPropertySource convertToConfigurationPropertySource(
Map<String, Object> source = TestPropertySourceUtils List<String> properties) {
.convertInlinedPropertiesToMap( String[] array = properties.toArray(new String[properties.size()]);
properties.toArray(new String[properties.size()])); return new MapConfigurationPropertySource(
MutablePropertySources sources = new MutablePropertySources(); TestPropertySourceUtils.convertInlinedPropertiesToMap(array));
sources.addFirst(new MapPropertySource("inline", source));
return sources;
} }
private List<ApplicationContextInitializer<?>> getInitializers( private List<ApplicationContextInitializer<?>> getInitializers(

@ -21,7 +21,6 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log; 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.SpringBootConfiguration;
import org.springframework.boot.WebApplicationType; 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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.Environment; 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.core.io.support.SpringFactoriesLoader;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextConfigurationAttributes; import org.springframework.test.context.ContextConfigurationAttributes;
@ -71,6 +69,7 @@ import org.springframework.util.ObjectUtils;
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Brian Clozel * @author Brian Clozel
* @author Madhura Bhave
* @since 1.4.0 * @since 1.4.0
* @see SpringBootTest * @see SpringBootTest
* @see TestConfiguration * @see TestConfiguration
@ -98,7 +97,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
verifyConfiguration(context.getTestClass()); verifyConfiguration(context.getTestClass());
WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass()); WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
if (webEnvironment == WebEnvironment.MOCK if (webEnvironment == WebEnvironment.MOCK
&& deduceWebApplication() == WebApplicationType.SERVLET) { && deduceWebApplicationType() == WebApplicationType.SERVLET) {
context.setAttribute(ACTIVATE_SERVLET_LISTENER, true); context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
} }
else if (webEnvironment != null && webEnvironment.isEmbedded()) { else if (webEnvironment != null && webEnvironment.isEmbedded()) {
@ -181,15 +180,17 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
private WebApplicationType getWebApplicationType( private WebApplicationType getWebApplicationType(
MergedContextConfiguration configuration) { MergedContextConfiguration configuration) {
WebApplicationType webApplicationType = getConfiguredWebApplicationType( ConfigurationPropertySource source = new MapConfigurationPropertySource(
configuration); TestPropertySourceUtils.convertInlinedPropertiesToMap(
if (webApplicationType != null) { configuration.getPropertySourceProperties()));
return webApplicationType; Binder binder = new Binder(source);
} return binder
return deduceWebApplication(); .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) if (ClassUtils.isPresent(REACTIVE_WEB_ENVIRONMENT_CLASS, null)
&& !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) { && !ClassUtils.isPresent(MVC_WEB_ENVIRONMENT_CLASS, null)) {
return WebApplicationType.REACTIVE; return WebApplicationType.REACTIVE;
@ -228,25 +229,6 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
return false; 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( protected Class<?>[] getOrFindConfigurationClasses(
MergedContextConfiguration mergedConfig) { MergedContextConfiguration mergedConfig) {
Class<?>[] classes = mergedConfig.getClasses(); Class<?>[] classes = mergedConfig.getClasses();

@ -16,7 +16,6 @@
package org.springframework.boot.test.web.client; package org.springframework.boot.test.web.client;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.web.client.RootUriTemplateHandler; import org.springframework.boot.web.client.RootUriTemplateHandler;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -30,6 +29,7 @@ import org.springframework.web.util.UriTemplateHandler;
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Madhura Bhave
* @since 1.4.0 * @since 1.4.0
*/ */
public class LocalHostUriTemplateHandler extends RootUriTemplateHandler { public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
@ -38,7 +38,7 @@ public class LocalHostUriTemplateHandler extends RootUriTemplateHandler {
private final String scheme; private final String scheme;
private RelaxedPropertyResolver servletPropertyResolver; private final String prefix = "server.servlet.";
/** /**
* Create a new {@code LocalHostUriTemplateHandler} that will generate {@code http} * 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"); Assert.notNull(scheme, "Scheme must not be null");
this.environment = environment; this.environment = environment;
this.scheme = scheme; this.scheme = scheme;
this.servletPropertyResolver = new RelaxedPropertyResolver(environment,
"server.servlet.");
} }
@Override @Override
public String getRootUri() { public String getRootUri() {
String port = this.environment.getProperty("local.server.port", "8080"); 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; return this.scheme + "://localhost:" + port + contextPath;
} }

Loading…
Cancel
Save