Commit 12724bf3 authored by Dave Syer's avatar Dave Syer

Fix ordering of keys in PropertySourcesPropertyValues

Since @ConfigurationProperties binding uses a single instance of
PropertySourcesPropertyValues per bean, there doesn't seem to be
any issue with using a normal LinkedHashMap. Then the order
passed in as PropertySources will be preserved.

Fixes gh-2487
parent 0ef3de4d
...@@ -19,8 +19,8 @@ package org.springframework.boot.bind; ...@@ -19,8 +19,8 @@ package org.springframework.boot.bind;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyValue; import org.springframework.beans.PropertyValue;
...@@ -42,7 +42,7 @@ import org.springframework.validation.DataBinder; ...@@ -42,7 +42,7 @@ import org.springframework.validation.DataBinder;
*/ */
public class PropertySourcesPropertyValues implements PropertyValues { public class PropertySourcesPropertyValues implements PropertyValues {
private final Map<String, PropertyValue> propertyValues = new ConcurrentHashMap<String, PropertyValue>(); private final Map<String, PropertyValue> propertyValues = new LinkedHashMap<String, PropertyValue>();
private final PropertySources propertySources; private final PropertySources propertySources;
......
...@@ -16,11 +16,14 @@ ...@@ -16,11 +16,14 @@
package org.springframework.boot.bind; package org.springframework.boot.bind;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.PropertyValue;
import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.MutablePropertySources;
...@@ -61,6 +64,26 @@ public class PropertySourcesPropertyValuesTests { ...@@ -61,6 +64,26 @@ public class PropertySourcesPropertyValuesTests {
assertEquals(1, propertyValues.getPropertyValues().length); assertEquals(1, propertyValues.getPropertyValues().length);
} }
@Test
public void testOrderPreserved() {
LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
this.propertySources.addFirst(new MapPropertySource("ordered", map));
PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues(
this.propertySources);
PropertyValue[] values = propertyValues.getPropertyValues();
assertEquals(6, values.length);
Collection<String> names = new ArrayList<String>();
for (PropertyValue value : values) {
names.add(value.getName());
}
assertEquals("[one, two, three, four, five, name]", names.toString());
}
@Test @Test
public void testNonEnumeratedValue() { public void testNonEnumeratedValue() {
PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues( PropertySourcesPropertyValues propertyValues = new PropertySourcesPropertyValues(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment