diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index 52baf222426..29ab5b3ee3e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -314,7 +314,7 @@ public class PropertiesConfigurationFactory implements FactoryBean, // We can filter properties to those starting with the target name, but // we can't do a complete filter since we need to trigger the // unknown fields check - return new DefaultPropertyNamePatternsMatcher(TARGET_NAME_DELIMITERS, + return new DefaultPropertyNamePatternsMatcher(TARGET_NAME_DELIMITERS, true, this.targetName); } // Not ideal, we basically can't filter anything diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryMapTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryMapTests.java index 530c969d0a5..ae337b2b084 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryMapTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryMapTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -33,7 +33,7 @@ import org.springframework.validation.Validator; import static org.junit.Assert.assertEquals; /** - * Tests for {@link PropertiesConfigurationFactory}. + * Tests for {@link PropertiesConfigurationFactory} binding to a map. * * @author Dave Syer */ diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryParameterizedTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryParameterizedTests.java new file mode 100644 index 00000000000..f7dd0037910 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryParameterizedTests.java @@ -0,0 +1,172 @@ +/* + * Copyright 2012-2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.bind; + +import java.io.IOException; +import java.util.Properties; + +import javax.validation.constraints.NotNull; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.springframework.context.support.StaticMessageSource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertiesPropertySource; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.support.PropertiesLoaderUtils; + +import static org.junit.Assert.assertEquals; + +/** + * Parameterized tests for {@link PropertiesConfigurationFactory} + * + * @author Dave Syer + * @author Andy Wilkinson + */ +@RunWith(Parameterized.class) +public class PropertiesConfigurationFactoryParameterizedTests { + + private final boolean usePropertySource; + + private String targetName; + + private PropertiesConfigurationFactory factory = new PropertiesConfigurationFactory( + Foo.class); + + @Parameters + public static Object[] parameters() { + return new Object[] { new Object[] { false, false }, + new Object[] { false, true }, new Object[] { true, false }, + new Object[] { true, true } }; + } + + public PropertiesConfigurationFactoryParameterizedTests(boolean ignoreUnknownFields, + boolean usePropertySource) { + this.factory.setIgnoreUnknownFields(ignoreUnknownFields); + this.usePropertySource = usePropertySource; + } + + @Test + public void testValidPropertiesLoadsWithNoErrors() throws Exception { + Foo foo = createFoo("name: blah\nbar: blah"); + assertEquals("blah", foo.bar); + assertEquals("blah", foo.name); + } + + @Test + public void testValidPropertiesLoadsWithUpperCase() throws Exception { + Foo foo = createFoo("NAME: blah\nbar: blah"); + assertEquals("blah", foo.bar); + assertEquals("blah", foo.name); + } + + @Test + public void testUnderscore() throws Exception { + Foo foo = createFoo("spring_foo_baz: blah\nname: blah"); + assertEquals("blah", foo.spring_foo_baz); + assertEquals("blah", foo.name); + } + + @Test + public void testBindToNamedTarget() throws Exception { + this.targetName = "foo"; + Foo foo = createFoo("hi: hello\nfoo.name: foo\nfoo.bar: blah"); + assertEquals("blah", foo.bar); + } + + @Test + public void testBindToNamedTargetUppercaseUnderscores() throws Exception { + this.targetName = "foo"; + Foo foo = createFoo("FOO_NAME: foo\nFOO_BAR: blah"); + assertEquals("blah", foo.bar); + } + + private Foo createFoo(final String values) throws Exception { + setupFactory(); + return bindFoo(values); + } + + private Foo bindFoo(final String values) throws Exception { + Properties properties = PropertiesLoaderUtils + .loadProperties(new ByteArrayResource(values.getBytes())); + if (this.usePropertySource) { + MutablePropertySources propertySources = new MutablePropertySources(); + propertySources.addFirst(new PropertiesPropertySource("test", properties)); + this.factory.setPropertySources(propertySources); + } + else { + this.factory.setProperties(properties); + } + + this.factory.afterPropertiesSet(); + return this.factory.getObject(); + } + + private void setupFactory() throws IOException { + this.factory.setTargetName(this.targetName); + this.factory.setMessageSource(new StaticMessageSource()); + } + + // Foo needs to be public and to have setters for all properties + public static class Foo { + + @NotNull + private String name; + + private String bar; + + private String spring_foo_baz; + + private String fooBar; + + public String getSpringFooBaz() { + return this.spring_foo_baz; + } + + public void setSpringFooBaz(String spring_foo_baz) { + this.spring_foo_baz = spring_foo_baz; + } + + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + + public String getBar() { + return this.bar; + } + + public void setBar(String bar) { + this.bar = bar; + } + + public String getFooBar() { + return this.fooBar; + } + + public void setFooBar(String fooBar) { + this.fooBar = fooBar; + } + + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java index 9d839cd32f1..3d6398805b4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryPerformanceTests.java @@ -33,7 +33,7 @@ import org.springframework.validation.Validator; import static org.junit.Assert.assertEquals; /** - * Tests for {@link PropertiesConfigurationFactory}. + * Performance tests for {@link PropertiesConfigurationFactory}. * * @author Dave Syer */ diff --git a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java index c568e855658..3eab669c4f9 100644 --- a/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/bind/PropertiesConfigurationFactoryTests.java @@ -47,20 +47,6 @@ public class PropertiesConfigurationFactoryTests { private String targetName = null; - @Test - public void testValidPropertiesLoadsWithNoErrors() throws Exception { - Foo foo = createFoo("name: blah\nbar: blah"); - assertEquals("blah", foo.bar); - assertEquals("blah", foo.name); - } - - @Test - public void testValidPropertiesLoadsWithUpperCase() throws Exception { - Foo foo = createFoo("NAME: blah\nbar: blah"); - assertEquals("blah", foo.bar); - assertEquals("blah", foo.name); - } - @Test public void testValidPropertiesLoadsWithDash() throws Exception { Foo foo = createFoo("na-me: blah\nbar: blah"); @@ -68,13 +54,6 @@ public class PropertiesConfigurationFactoryTests { assertEquals("blah", foo.name); } - @Test - public void testUnderscore() throws Exception { - Foo foo = createFoo("spring_foo_baz: blah\nname: blah"); - assertEquals("blah", foo.spring_foo_baz); - assertEquals("blah", foo.name); - } - @Test public void testUnknownPropertyOkByDefault() throws Exception { Foo foo = createFoo("hi: hello\nname: foo\nbar: blah"); @@ -103,13 +82,6 @@ public class PropertiesConfigurationFactoryTests { bindFoo("bar: blah"); } - @Test - public void testBindToNamedTarget() throws Exception { - this.targetName = "foo"; - Foo foo = createFoo("hi: hello\nfoo.name: foo\nfoo.bar: blah"); - assertEquals("blah", foo.bar); - } - private Foo createFoo(final String values) throws Exception { setupFactory(); return bindFoo(values);