commit
05a1ef233a
|
@ -18,12 +18,10 @@ package org.springframework.boot.test.context;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.boot.ApplicationContextFactory;
|
import org.springframework.boot.ApplicationContextFactory;
|
||||||
import org.springframework.boot.DefaultPropertiesPropertySource;
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.WebApplicationType;
|
import org.springframework.boot.WebApplicationType;
|
||||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||||
|
@ -43,10 +41,7 @@ import org.springframework.core.SpringVersion;
|
||||||
import org.springframework.core.annotation.MergedAnnotations;
|
import org.springframework.core.annotation.MergedAnnotations;
|
||||||
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.env.CommandLinePropertySource;
|
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.MutablePropertySources;
|
|
||||||
import org.springframework.core.env.PropertySource;
|
|
||||||
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.core.io.ResourceLoader;
|
import org.springframework.core.io.ResourceLoader;
|
||||||
|
@ -89,9 +84,6 @@ import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
*/
|
*/
|
||||||
public class SpringBootContextLoader extends AbstractContextLoader {
|
public class SpringBootContextLoader extends AbstractContextLoader {
|
||||||
|
|
||||||
private static final String[] PRIORITY_PROPERTY_SOURCES = { "configurationProperties",
|
|
||||||
DefaultPropertiesPropertySource.NAME, CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME };
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
|
public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception {
|
||||||
Class<?>[] configClasses = config.getClasses();
|
Class<?>[] configClasses = config.getClasses();
|
||||||
|
@ -138,25 +130,12 @@ public class SpringBootContextLoader extends AbstractContextLoader {
|
||||||
|
|
||||||
private void prepareEnvironment(MergedContextConfiguration config, SpringApplication application,
|
private void prepareEnvironment(MergedContextConfiguration config, SpringApplication application,
|
||||||
ConfigurableEnvironment environment, boolean applicationEnvironment) {
|
ConfigurableEnvironment environment, boolean applicationEnvironment) {
|
||||||
MutablePropertySources propertySources = environment.getPropertySources();
|
|
||||||
List<PropertySource<?>> priorityPropertySources = new ArrayList<>();
|
|
||||||
if (applicationEnvironment) {
|
|
||||||
for (String priorityPropertySourceName : PRIORITY_PROPERTY_SOURCES) {
|
|
||||||
PropertySource<?> priorityPropertySource = propertySources.get(priorityPropertySourceName);
|
|
||||||
if (priorityPropertySource != null) {
|
|
||||||
priorityPropertySources.add(priorityPropertySource);
|
|
||||||
propertySources.remove(priorityPropertySourceName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
setActiveProfiles(environment, config.getActiveProfiles(), applicationEnvironment);
|
setActiveProfiles(environment, config.getActiveProfiles(), applicationEnvironment);
|
||||||
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
|
ResourceLoader resourceLoader = (application.getResourceLoader() != null) ? application.getResourceLoader()
|
||||||
: new DefaultResourceLoader(null);
|
: new DefaultResourceLoader(null);
|
||||||
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
|
TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, resourceLoader,
|
||||||
config.getPropertySourceLocations());
|
config.getPropertySourceLocations());
|
||||||
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
|
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config));
|
||||||
Collections.reverse(priorityPropertySources);
|
|
||||||
priorityPropertySources.forEach(propertySources::addFirst);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
|
private void setActiveProfiles(ConfigurableEnvironment environment, String[] profiles,
|
||||||
|
|
|
@ -140,7 +140,7 @@ class SpringBootContextLoaderTests {
|
||||||
List<String> names = environment.getPropertySources().stream().map(PropertySource::getName)
|
List<String> names = environment.getPropertySources().stream().map(PropertySource::getName)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
String last = names.remove(names.size() - 1);
|
String last = names.remove(names.size() - 1);
|
||||||
assertThat(names).containsExactly("configurationProperties", "commandLineArgs", "Inlined Test Properties",
|
assertThat(names).containsExactly("configurationProperties", "Inlined Test Properties", "commandLineArgs",
|
||||||
"servletConfigInitParams", "servletContextInitParams", "systemProperties", "systemEnvironment",
|
"servletConfigInitParams", "servletContextInitParams", "systemProperties", "systemEnvironment",
|
||||||
"random");
|
"random");
|
||||||
assertThat(last).startsWith("Config resource");
|
assertThat(last).startsWith("Config resource");
|
||||||
|
|
Loading…
Reference in New Issue