Add missing tests for Collection and Map binders

pull/2315/merge
Madhura Bhave 8 years ago
parent e9d7441e4d
commit 366031cfb6

@ -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<List<JavaBean>> target = Bindable.listOf(JavaBean.class);
List<JavaBean> result = this.binder.bind("foo", target).get();
assertThat(result).hasSize(3);
List<String> values = result.stream().map(JavaBean::getValue).collect(Collectors.toList());
assertThat(values).containsExactly("a", "b", "c");
}
}

@ -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<List<JavaBean>> valueType = Bindable.listOf(JavaBean.class);
ResolvableType mapType = ResolvableType.forClassWithGenerics(Map.class, ResolvableType.forClass(String.class), valueType.getType());
Bindable<Map<String, List<JavaBean>>> 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

Loading…
Cancel
Save