Search in sources :

Example 1 with JavaBean

use of org.springframework.boot.context.properties.bind.BinderTests.JavaBean in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapNonScalarCollectionShouldPopulateMap.

@Test
void bindToMapNonScalarCollectionShouldPopulateMap() {
    Bindable<List<JavaBean>> valueType = Bindable.listOf(JavaBean.class);
    Bindable<Map<String, List<JavaBean>>> target = getMapBindable(String.class, valueType.getType());
    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);
    Map<String, List<JavaBean>> map = this.binder.bind("foo", target).get();
    List<String> values = map.get("bar").stream().map(JavaBean::getValue).collect(Collectors.toList());
    assertThat(values).containsExactly("a", "b", "c");
}
Also used : JavaBean(org.springframework.boot.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with JavaBean

use of org.springframework.boot.context.properties.bind.BinderTests.JavaBean in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapNonScalarCollectionWithDotKeysShouldBind.

@Test
void bindToMapNonScalarCollectionWithDotKeysShouldBind() {
    Bindable<List<JavaBean>> valueType = Bindable.listOf(JavaBean.class);
    Bindable<Map<String, List<JavaBean>>> target = getMapBindable(String.class, valueType.getType());
    MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource();
    mockSource.put("foo.bar.baz[0].value", "a");
    mockSource.put("foo.bar.baz[1].value", "b");
    mockSource.put("foo.bar.baz[2].value", "c");
    this.sources.add(mockSource);
    Map<String, List<JavaBean>> map = this.binder.bind("foo", target).get();
    List<String> values = map.get("bar.baz").stream().map(JavaBean::getValue).collect(Collectors.toList());
    assertThat(values).containsExactly("a", "b", "c");
}
Also used : JavaBean(org.springframework.boot.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with JavaBean

use of org.springframework.boot.context.properties.bind.BinderTests.JavaBean in project spring-boot by spring-projects.

the class CollectionBinderTests method bindToNonScalarCollectionShouldReturnPopulatedCollection.

@Test
void bindToNonScalarCollectionShouldReturnPopulatedCollection() {
    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");
}
Also used : JavaBean(org.springframework.boot.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 Test (org.junit.jupiter.api.Test)3 JavaBean (org.springframework.boot.context.properties.bind.BinderTests.JavaBean)3 MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)3 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 LinkedList (java.util.LinkedList)1