From 1abd0879cab4aeadffa4687666120c9512fa8ccd Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Thu, 2 Jul 2015 09:02:08 +0100 Subject: [PATCH] Add and fix tests for overridden bindings The order of property values is only preserved if we check carefully that each property is actually present in the source being inspected. Fixes gh-3385 --- .../bind/PropertySourcesPropertyValues.java | 3 ++ .../bind/PropertySourcesBindingTests.java | 30 +++++++++++++++++++ .../src/test/resources/override.properties | 2 ++ .../src/test/resources/some.properties | 4 ++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java index ec13ad1a32..9d9c8721ac 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertySourcesPropertyValues.java @@ -153,6 +153,9 @@ public class PropertySourcesPropertyValues implements PropertyValues { PropertySourcesPropertyResolver resolver, PropertyNamePatternsMatcher includes, Collection exacts) { for (String propertyName : exacts) { + if (!source.containsProperty(propertyName)) { + continue; + } Object value = null; try { value = resolver.getProperty(propertyName, Object.class); diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java index d314ec28d5..d44bbfa9cc 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertySourcesBindingTests.java @@ -57,6 +57,16 @@ public class PropertySourcesBindingTests { assertThat(this.properties.getBar(), is("override")); } + @Test + public void overridingOfPropertiesOrderOfAtPropertySourcesWherePropertyIsCapitalized() { + assertThat(this.properties.getSpam(), is("BUCKET")); + } + + @Test + public void overridingOfPropertiesOrderOfAtPropertySourcesWherePropertyNamesDiffer() { + assertThat(this.properties.getTheName(), is("NAME")); + } + @Test public void overridingOfPropertiesAndBindToAtValue() { assertThat(this.foo, is(this.properties.getFoo())); @@ -93,6 +103,10 @@ public class PropertySourcesBindingTests { private String bar; + private String spam; + + private String theName; + public String getBar() { return this.bar; } @@ -108,6 +122,22 @@ public class PropertySourcesBindingTests { public void setFoo(String foo) { this.foo = foo; } + + public String getSpam() { + return this.spam; + } + + public void setSpam(String spam) { + this.spam = spam; + } + + public String getTheName() { + return this.theName; + } + + public void setTheName(String theName) { + this.theName = theName; + } } } diff --git a/spring-boot/src/test/resources/override.properties b/spring-boot/src/test/resources/override.properties index 37621c0a2c..ab4caf3fff 100644 --- a/spring-boot/src/test/resources/override.properties +++ b/spring-boot/src/test/resources/override.properties @@ -1,2 +1,4 @@ foo=bar bar=override +SPAM=BUCKET +THE_NAME=NAME \ No newline at end of file diff --git a/spring-boot/src/test/resources/some.properties b/spring-boot/src/test/resources/some.properties index 8648daf6df..f7ce261c18 100644 --- a/spring-boot/src/test/resources/some.properties +++ b/spring-boot/src/test/resources/some.properties @@ -1,2 +1,4 @@ foo=spam -bar=some \ No newline at end of file +bar=some +spam=bucket +the-name=name \ No newline at end of file