From 366031cfb68467a954764645fd5209b6555cad38 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Wed, 17 May 2017 15:53:46 -0700 Subject: [PATCH] Add missing tests for Collection and Map binders --- .../bind/CollectionBinderTests.java | 15 +++++++++++ .../properties/bind/MapBinderTests.java | 26 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java index edeed1ec56..a64c3149ef 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/CollectionBinderTests.java @@ -20,10 +20,12 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; import org.junit.Before; import org.junit.Test; +import org.springframework.boot.context.properties.bind.BinderTests.JavaBean; import org.springframework.boot.context.properties.source.ConfigurationProperty; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.MockConfigurationPropertySource; @@ -274,4 +276,17 @@ public class CollectionBinderTests { assertThat(result).isNotNull().isEmpty(); } + @Test + public void bindToNonScalarCollectionShouldReturnPopulatedCollection() throws Exception { + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo[0].value", "a"); + source.put("foo[1].value", "b"); + source.put("foo[2].value", "c"); + this.sources.add(source); + Bindable> target = Bindable.listOf(JavaBean.class); + List result = this.binder.bind("foo", target).get(); + assertThat(result).hasSize(3); + List values = result.stream().map(JavaBean::getValue).collect(Collectors.toList()); + assertThat(values).containsExactly("a", "b", "c"); + } } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java index 9217e0efae..ef11f56e16 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java @@ -30,6 +30,7 @@ import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum; +import org.springframework.boot.context.properties.bind.BinderTests.JavaBean; import org.springframework.boot.context.properties.source.ConfigurationPropertyName; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; @@ -373,6 +374,31 @@ public class MapBinderTests { eq(target), any(), isA(Map.class)); } + @Test + public void bindToMapNonScalarCollectionShouldTriggerOnSuccess() throws Exception { + Bindable> valueType = Bindable.listOf(JavaBean.class); + ResolvableType mapType = ResolvableType.forClassWithGenerics(Map.class, ResolvableType.forClass(String.class), valueType.getType()); + Bindable>> target = Bindable.of(mapType); + MockConfigurationPropertySource source = new MockConfigurationPropertySource(); + source.put("foo.bar[0].value", "a"); + source.put("foo.bar[1].value", "b"); + source.put("foo.bar[2].value", "c"); + this.sources + .add(source); + BindHandler handler = mock(BindHandler.class, + withSettings().defaultAnswer(Answers.CALLS_REAL_METHODS)); + this.binder.bind("foo", target, handler); + InOrder inOrder = inOrder(handler); + inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo.bar[0].value")), + eq(Bindable.of(String.class)), any(), eq("a")); + inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo.bar[1].value")), + eq(Bindable.of(String.class)), any(), eq("b")); + inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo.bar[2].value")), + eq(Bindable.of(String.class)), any(), eq("c")); + inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo")), + eq(target), any(), isA(Map.class)); + } + @Test public void bindToPropertiesShouldBeEquivalentToMapOfStringString() throws Exception { this.sources