Polish (Mutable)PropertySources
* PropertySources is now an Iterable<PropertySource> in favor of exposing an asList() method * Otherwise reduced the set of methods exposed by PropertySources to the absolute minimum * Added Javadoc for both types and all methods
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,44 +16,80 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* Default implementation of the {@link PropertySources} interface.
|
||||
* Allows manipulation of contained property sources and provides constructor
|
||||
* to copying an existing {@code PropertySources} instance.
|
||||
*
|
||||
* <p>Where <em>precedence</em> is mentioned in methods such as {@link #addFirst}
|
||||
* and {@link #addLast}, this is with regard to the order in which property sources
|
||||
* will be searched when resolving a given property with a {@link PropertyResolver}.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
* @see PropertySourcesPropertyResolver
|
||||
*/
|
||||
public class MutablePropertySources implements PropertySources {
|
||||
|
||||
private final LinkedList<PropertySource<?>> propertySourceList = new LinkedList<PropertySource<?>>();
|
||||
|
||||
static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist";
|
||||
static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself";
|
||||
|
||||
private final LinkedList<PropertySource<?>> propertySourceList = new LinkedList<PropertySource<?>>();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new {@link MutablePropertySources} object.
|
||||
*/
|
||||
public MutablePropertySources() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@code MutablePropertySources} from the given propertySources
|
||||
* object, preserving the original order of contained {@code PropertySource} objects.
|
||||
*/
|
||||
public MutablePropertySources(PropertySources propertySources) {
|
||||
this.addAll(propertySources);
|
||||
}
|
||||
|
||||
public void addAll(PropertySources propertySources) {
|
||||
for (PropertySource<?> propertySource : propertySources.asList()) {
|
||||
for (PropertySource<?> propertySource : propertySources) {
|
||||
this.addLast(propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(String name) {
|
||||
return this.propertySourceList.contains(PropertySource.named(name));
|
||||
}
|
||||
|
||||
public PropertySource<?> get(String name) {
|
||||
return this.propertySourceList.get(this.propertySourceList.indexOf(PropertySource.named(name)));
|
||||
}
|
||||
|
||||
public Iterator<PropertySource<?>> iterator() {
|
||||
return this.propertySourceList.iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with highest precedence.
|
||||
*/
|
||||
public void addFirst(PropertySource<?> propertySource) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.addFirst(propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with lowest precedence.
|
||||
*/
|
||||
public void addLast(PropertySource<?> propertySource) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.addLast(propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with precedence immediately greater
|
||||
* than the named relative property source.
|
||||
*/
|
||||
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
|
||||
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
|
||||
removeIfPresent(propertySource);
|
||||
@@ -61,6 +97,10 @@ public class MutablePropertySources implements PropertySources {
|
||||
addAtIndex(index, propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the given property source object with precedence immediately less than
|
||||
* than the named relative property source.
|
||||
*/
|
||||
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
|
||||
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
|
||||
removeIfPresent(propertySource);
|
||||
@@ -68,56 +108,79 @@ public class MutablePropertySources implements PropertySources {
|
||||
addAtIndex(index+1, propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the precedence of the given property source, {@code -1} if not found.
|
||||
*/
|
||||
public int precedenceOf(PropertySource<?> propertySource) {
|
||||
return this.propertySourceList.indexOf(propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove and return the property source with the given name, {@code null} if not found.
|
||||
* @param name the name of the property source to find and remove
|
||||
*/
|
||||
public PropertySource<?> remove(String name) {
|
||||
int index = this.propertySourceList.indexOf(PropertySource.named(name));
|
||||
if (index >= 0) {
|
||||
return this.propertySourceList.remove(index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the property source with the given name with the given property source object.
|
||||
* @param name the name of the property source to find and replace
|
||||
* @param propertySource the replacement property source
|
||||
* @throws IllegalArgumentException if no property source with the given name is present
|
||||
* @see #contains
|
||||
*/
|
||||
public void replace(String name, PropertySource<?> propertySource) {
|
||||
int index = assertPresentAndGetIndex(name);
|
||||
this.propertySourceList.set(index, propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of {@link PropertySource} objects contained.
|
||||
*/
|
||||
public int size() {
|
||||
return this.propertySourceList.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the given property source is not being added relative to itself.
|
||||
*/
|
||||
protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource<?> propertySource) {
|
||||
String newPropertySourceName = propertySource.getName();
|
||||
Assert.isTrue(!relativePropertySourceName.equals(newPropertySourceName),
|
||||
String.format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, newPropertySourceName));
|
||||
}
|
||||
|
||||
protected void addAtIndex(int index, PropertySource<?> propertySource) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.add(index, propertySource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the removal of the given propertySource if it is present.
|
||||
*/
|
||||
protected void removeIfPresent(PropertySource<?> propertySource) {
|
||||
if (this.propertySourceList.contains(propertySource)) {
|
||||
// TODO SPR-7508: add logging
|
||||
this.propertySourceList.remove(propertySource);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean contains(String propertySourceName) {
|
||||
return propertySourceList.contains(PropertySource.named(propertySourceName));
|
||||
/**
|
||||
* Add the given property source at a particular index in the list.
|
||||
*/
|
||||
private void addAtIndex(int index, PropertySource<?> propertySource) {
|
||||
removeIfPresent(propertySource);
|
||||
this.propertySourceList.add(index, propertySource);
|
||||
}
|
||||
|
||||
public PropertySource<?> remove(String propertySourceName) {
|
||||
int index = propertySourceList.indexOf(PropertySource.named(propertySourceName));
|
||||
if (index >= 0) {
|
||||
return propertySourceList.remove(index);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void replace(String propertySourceName, PropertySource<?> propertySource) {
|
||||
int index = assertPresentAndGetIndex(propertySourceName);
|
||||
this.propertySourceList.set(index, propertySource);
|
||||
}
|
||||
|
||||
protected int assertPresentAndGetIndex(String propertySourceName) {
|
||||
/**
|
||||
* Assert that the named property source is present and return its index.
|
||||
* @throws IllegalArgumentException if the named property source is not present
|
||||
*/
|
||||
private int assertPresentAndGetIndex(String propertySourceName) {
|
||||
int index = this.propertySourceList.indexOf(PropertySource.named(propertySourceName));
|
||||
Assert.isTrue(index >= 0, String.format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, propertySourceName));
|
||||
return index;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return propertySourceList.size();
|
||||
}
|
||||
|
||||
public List<PropertySource<?>> asList() {
|
||||
return Collections.unmodifiableList(this.propertySourceList);
|
||||
}
|
||||
|
||||
public PropertySource<?> get(String propertySourceName) {
|
||||
return propertySourceList.get(propertySourceList.indexOf(PropertySource.named(propertySourceName)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,23 +16,24 @@
|
||||
|
||||
package org.springframework.core.env;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* TODO SPR-7508: document
|
||||
* Holder containing one or more {@link PropertySource} objects.
|
||||
*
|
||||
* @author Chris Beams
|
||||
* @since 3.1
|
||||
*/
|
||||
public interface PropertySources {
|
||||
public interface PropertySources extends Iterable<PropertySource<?>> {
|
||||
|
||||
PropertySource<?> get(String propertySourceName);
|
||||
/**
|
||||
* Return whether a property source with the given name is contained.
|
||||
* @param name the {@linkplain PropertySource#getName() name of the property source} to find
|
||||
*/
|
||||
boolean contains(String name);
|
||||
|
||||
// TODO make iterable
|
||||
List<PropertySource<?>> asList();
|
||||
|
||||
int size();
|
||||
|
||||
boolean contains(String propertySourceName);
|
||||
/**
|
||||
* Return the property source with the given name, {@code null} if not found.
|
||||
* @param name the {@linkplain PropertySource#getName() name of the property source} to find
|
||||
*/
|
||||
PropertySource<?> get(String name);
|
||||
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||
}
|
||||
|
||||
public boolean containsProperty(String key) {
|
||||
for (PropertySource<?> propertySource : this.propertySources.asList()) {
|
||||
for (PropertySource<?> propertySource : this.propertySources) {
|
||||
if (propertySource.getProperty(key) != null) {
|
||||
return true;
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
|
||||
logger.trace(format("getProperty(\"%s\", %s)", key, targetValueType.getSimpleName()));
|
||||
}
|
||||
|
||||
for (PropertySource<?> propertySource : this.propertySources.asList()) {
|
||||
for (PropertySource<?> propertySource : this.propertySources) {
|
||||
if (debugEnabled) {
|
||||
logger.debug(format("Searching for key '%s' in [%s]", key, propertySource.getName()));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,8 +20,6 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class DefaultEnvironmentTests {
|
||||
@@ -29,9 +27,9 @@ public class DefaultEnvironmentTests {
|
||||
@Test
|
||||
public void propertySourceOrder() {
|
||||
ConfigurableEnvironment env = new DefaultEnvironment();
|
||||
List<PropertySource<?>> sources = env.getPropertySources().asList();
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(1));
|
||||
assertThat(sources.size(), is(2));
|
||||
assertThat(sources.get(0).getName(), equalTo(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME));
|
||||
assertThat(sources.get(1).getName(), equalTo(DefaultEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,43 +53,43 @@ public class PropertySourcesTests {
|
||||
sources.addAfter("b", new MockPropertySource("c"));
|
||||
|
||||
assertThat(sources.size(), equalTo(5));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("f")), is(4));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
|
||||
|
||||
sources.addBefore("f", new MockPropertySource("e"));
|
||||
sources.addAfter("f", new MockPropertySource("g"));
|
||||
|
||||
assertThat(sources.size(), equalTo(7));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("e")), is(4));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("f")), is(5));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("g")), is(6));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
|
||||
|
||||
sources.addLast(new MockPropertySource("a"));
|
||||
assertThat(sources.size(), equalTo(7));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("d")), is(2));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("e")), is(3));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("f")), is(4));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("g")), is(5));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a")), is(6));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("d")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("e")), is(3));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("g")), is(5));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a")), is(6));
|
||||
|
||||
sources.addFirst(new MockPropertySource("a"));
|
||||
assertThat(sources.size(), equalTo(7));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("e")), is(4));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("f")), is(5));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("g")), is(6));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));
|
||||
|
||||
assertEquals(sources.remove("a"), PropertySource.named("a"));
|
||||
assertThat(sources.size(), equalTo(6));
|
||||
@@ -109,15 +109,15 @@ public class PropertySourcesTests {
|
||||
|
||||
sources.addFirst(new MockPropertySource("a"));
|
||||
assertThat(sources.size(), equalTo(7));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
|
||||
|
||||
sources.replace("a", new MockPropertySource("a-replaced"));
|
||||
assertThat(sources.size(), equalTo(7));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("a-replaced")), is(0));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.asList().indexOf(PropertySource.named("c")), is(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("a-replaced")), is(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
|
||||
|
||||
sources.replace("a-replaced", new MockPropertySource("a"));
|
||||
|
||||
|
||||
@@ -438,16 +438,16 @@ public class EnvironmentIntegrationTests {
|
||||
|
||||
// ServletConfig gets precedence
|
||||
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||
|
||||
// but all params are available
|
||||
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
|
||||
|
||||
// Servlet* PropertySources have precedence over System* PropertySources
|
||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
|
||||
// Replace system properties with a mock property source for convenience
|
||||
MockPropertySource mockSystemProperties = new MockPropertySource(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
@@ -480,8 +480,8 @@ public class EnvironmentIntegrationTests {
|
||||
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||
|
||||
// Servlet* PropertySources have precedence over System* PropertySources
|
||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
|
||||
// Replace system properties with a mock property source for convenience
|
||||
MockPropertySource mockSystemProperties = new MockPropertySource(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
@@ -515,16 +515,16 @@ public class EnvironmentIntegrationTests {
|
||||
|
||||
// ServletConfig gets precedence
|
||||
assertThat(environment.getProperty("pCommon"), is("pCommonConfigValue"));
|
||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))));
|
||||
|
||||
// but all params are available
|
||||
assertThat(environment.getProperty("pContext1"), is("pContext1Value"));
|
||||
assertThat(environment.getProperty("pConfig1"), is("pConfig1Value"));
|
||||
|
||||
// Servlet* PropertySources have precedence over System* PropertySources
|
||||
assertThat(propertySources.asList().indexOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.asList().indexOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
assertThat(propertySources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)),
|
||||
lessThan(propertySources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))));
|
||||
|
||||
// Replace system properties with a mock property source for convenience
|
||||
MockPropertySource mockSystemProperties = new MockPropertySource(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -20,24 +20,22 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.DefaultEnvironment;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
|
||||
public class DefaultWebEnvironmentTests {
|
||||
|
||||
@Test
|
||||
public void propertySourceOrder() {
|
||||
ConfigurableEnvironment env = new DefaultWebEnvironment();
|
||||
List<PropertySource<?>> sources = env.getPropertySources().asList();
|
||||
MutablePropertySources sources = env.getPropertySources();
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(2));
|
||||
assertThat(sources.precedenceOf(PropertySource.named(DefaultEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(3));
|
||||
assertThat(sources.size(), is(4));
|
||||
assertThat(sources.get(0).getName(), equalTo(DefaultWebEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
|
||||
assertThat(sources.get(1).getName(), equalTo(DefaultWebEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
|
||||
assertThat(sources.get(2).getName(), equalTo(DefaultEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME));
|
||||
assertThat(sources.get(3).getName(), equalTo(DefaultEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user