Search in sources :

Example 1 with JavaBean

use of cn.taketoday.context.properties.bind.BinderTests.JavaBean in project today-infrastructure by TAKETODAY.

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(cn.taketoday.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 2 with JavaBean

use of cn.taketoday.context.properties.bind.BinderTests.JavaBean in project today-infrastructure by TAKETODAY.

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(cn.taketoday.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(cn.taketoday.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 cn.taketoday.context.properties.bind.BinderTests.JavaBean in project today-framework by TAKETODAY.

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(cn.taketoday.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(cn.taketoday.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 4 with JavaBean

use of cn.taketoday.context.properties.bind.BinderTests.JavaBean in project today-framework by TAKETODAY.

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(cn.taketoday.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 5 with JavaBean

use of cn.taketoday.context.properties.bind.BinderTests.JavaBean in project today-framework by TAKETODAY.

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(cn.taketoday.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(cn.taketoday.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)

Aggregations

JavaBean (cn.taketoday.context.properties.bind.BinderTests.JavaBean)6 MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Test (org.junit.jupiter.api.Test)6 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 Map (java.util.Map)4 LinkedList (java.util.LinkedList)2