From 8d186945e73319ab8c469f9f478dcf850db155a5 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 28 Oct 2013 17:57:21 -0400 Subject: [PATCH] Use RelaxedNames to search for prefix in RelaxedBinder RelaxedDataBinder now supports "env var" style variables that include the path prefix, e.g. FOO_BAR_BAZ=boom will bind to a bean with property "baz" and a binder with prefix "foo.bar". Fixes gh-98 --- .../boot/bind/RelaxedDataBinder.java | 8 +++-- .../boot/bind/RelaxedDataBinderTests.java | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java index 250be706d4..3f63ba07fe 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/RelaxedDataBinder.java @@ -121,9 +121,11 @@ public class RelaxedDataBinder extends DataBinder { MutablePropertyValues rtn = new MutablePropertyValues(); for (PropertyValue pv : propertyValues.getPropertyValues()) { String name = pv.getName(); - if (name.startsWith(this.namePrefix)) { - name = name.substring(this.namePrefix.length()); - rtn.add(name, pv.getValue()); + for (String candidate : new RelaxedNames(this.namePrefix)) { + if (name.startsWith(candidate)) { + name = name.substring(candidate.length()); + rtn.add(name, pv.getValue()); + } } } return rtn; diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java index 8df8a72a84..14d415af21 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/RelaxedDataBinderTests.java @@ -41,7 +41,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.NotWritablePropertyException; -import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.context.support.StaticMessageSource; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.support.DefaultConversionService; @@ -75,6 +74,34 @@ public class RelaxedDataBinderTests { assertEquals("bar", target.getFoo()); } + @Test + public void testBindStringWithPrefix() throws Exception { + VanillaTarget target = new VanillaTarget(); + bind(target, "test.foo: bar", "test"); + assertEquals("bar", target.getFoo()); + } + + @Test + public void testBindFromEnvironmentStyleWithPrefix() throws Exception { + VanillaTarget target = new VanillaTarget(); + bind(target, "TEST_FOO: bar", "test"); + assertEquals("bar", target.getFoo()); + } + + @Test + public void testBindFromEnvironmentStyleWithNestedPrefix() throws Exception { + VanillaTarget target = new VanillaTarget(); + bind(target, "TEST_IT_FOO: bar", "test.it"); + assertEquals("bar", target.getFoo()); + } + + @Test + public void testBindCapitals() throws Exception { + VanillaTarget target = new VanillaTarget(); + bind(target, "FOO: bar"); + assertEquals("bar", target.getFoo()); + } + @Test public void testBindUnderscoreInActualPropertyName() throws Exception { VanillaTarget target = new VanillaTarget(); @@ -215,8 +242,6 @@ public class RelaxedDataBinderTests { } @Test - // @Ignore("Should be possible but currently not supported") - // FIXME: bind to map containing beans public void testBindNestedMapOfBean() throws Exception { TargetWithNestedMapOfBean target = new TargetWithNestedMapOfBean(); bind(target, "nested.foo.foo: bar\n" + "nested.bar.foo: bucket"); @@ -225,8 +250,6 @@ public class RelaxedDataBinderTests { } @Test - // @Ignore("Should be possible but currently not supported") - // FIXME: bind to map containing beans public void testBindNestedMapOfListOfBean() throws Exception { TargetWithNestedMapOfListOfBean target = new TargetWithNestedMapOfListOfBean(); bind(target, "nested.foo[0].foo: bar\n" + "nested.bar[0].foo: bucket\n"