|
|
|
|
@ -22,6 +22,7 @@ import java.util.HashMap;
@@ -22,6 +22,7 @@ import java.util.HashMap;
|
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
import java.util.Properties; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
@ -429,6 +430,109 @@ public class MapBinderTests {
@@ -429,6 +430,109 @@ public class MapBinderTests {
|
|
|
|
|
eq(target), any(), isA(Map.class)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapStringArrayWithDotKeysShouldPreserveDot() throws Exception { |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo.bar.baz[0]", "a"); |
|
|
|
|
mockSource.put("foo.bar.baz[1]", "b"); |
|
|
|
|
mockSource.put("foo.bar.baz[2]", "c"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
Map<String, String[]> map = this.binder.bind("foo", STRING_ARRAY_MAP).get(); |
|
|
|
|
assertThat(map.get("bar.baz")).containsExactly("a", "b", "c"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapStringArrayWithDotKeysAndCommaSeparatedShouldPreserveDot() throws Exception { |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo.bar.baz", "a,b,c"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
Map<String, String[]> map = this.binder.bind("foo", STRING_ARRAY_MAP).get(); |
|
|
|
|
assertThat(map.get("bar.baz")).containsExactly("a", "b", "c"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapStringCollectionWithDotKeysShouldPreserveDot() throws Exception { |
|
|
|
|
Bindable<List<String>> valueType = Bindable.listOf(String.class); |
|
|
|
|
Bindable<Map<String, List<String>>> target = getMapBindable(String.class, valueType.getType()); |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo.bar.baz[0]", "a"); |
|
|
|
|
mockSource.put("foo.bar.baz[1]", "b"); |
|
|
|
|
mockSource.put("foo.bar.baz[2]", "c"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
Map<String, List<String>> map = this.binder.bind("foo", target).get(); |
|
|
|
|
List<String> values = map.get("bar.baz"); |
|
|
|
|
assertThat(values).containsExactly("a", "b", "c"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapNonScalarCollectionWithDotKeysShouldBind() throws Exception { |
|
|
|
|
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<JavaBean> values = map.get("bar.baz"); |
|
|
|
|
assertThat(values.stream().map(JavaBean::getValue).collect(Collectors.toList())).containsExactly("a", "b", "c"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToListOfMaps() throws Exception { |
|
|
|
|
Bindable<List<Integer>> listBindable = Bindable.listOf(Integer.class); |
|
|
|
|
Bindable<Map<String, List<Integer>>> mapBindable = getMapBindable(String.class, listBindable.getType()); |
|
|
|
|
Bindable<List<Map<String, List<Integer>>>> target = getListBindable(mapBindable.getType()); |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo[0].a", "1,2,3"); |
|
|
|
|
mockSource.put("foo[1].b", "4,5,6"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
List<Map<String, List<Integer>>> list = this.binder.bind("foo", target).get(); |
|
|
|
|
assertThat(list.get(0).get("a")).containsExactly(1, 2, 3); |
|
|
|
|
assertThat(list.get(1).get("b")).containsExactly(4, 5, 6); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapWithNumberKeyAndCommaSeparated() throws Exception { |
|
|
|
|
Bindable<List<String>> listBindable = Bindable.listOf(String.class); |
|
|
|
|
Bindable<Map<Integer, List<String>>> target = getMapBindable(Integer.class, listBindable.getType()); |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo[0]", "a,b,c"); |
|
|
|
|
mockSource.put("foo[1]", "e,f,g"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
Map<Integer, List<String>> map = this.binder.bind("foo", target).get(); |
|
|
|
|
assertThat(map.get(0)).containsExactly("a", "b", "c"); |
|
|
|
|
assertThat(map.get(1)).containsExactly("e", "f", "g"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void bindToMapWithNumberKeyAndIndexed() throws Exception { |
|
|
|
|
Bindable<List<Integer>> listBindable = Bindable.listOf(Integer.class); |
|
|
|
|
Bindable<Map<Integer, List<Integer>>> target = getMapBindable(Integer.class, listBindable.getType()); |
|
|
|
|
MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource(); |
|
|
|
|
mockSource.put("foo[0][0]", "8"); |
|
|
|
|
mockSource.put("foo[0][1]", "9"); |
|
|
|
|
this.sources |
|
|
|
|
.add(mockSource); |
|
|
|
|
Map<Integer, List<Integer>> map = this.binder.bind("foo", target).get(); |
|
|
|
|
assertThat(map.get(0)).containsExactly(8, 9); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, ResolvableType valueType) { |
|
|
|
|
ResolvableType keyType = ResolvableType.forClass(keyGeneric); |
|
|
|
|
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private <T> Bindable<List<T>> getListBindable(ResolvableType type) { |
|
|
|
|
return Bindable.of(ResolvableType.forClassWithGenerics(List.class, type)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static class Foo { |
|
|
|
|
private String pattern; |
|
|
|
|
|
|
|
|
|
|