Search in sources :

Example 1 with MockConfigurationPropertySource

use of cn.taketoday.context.properties.source.MockConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ArrayBinderTests method bindToArrayWhenNestedShouldReturnPopulatedArray.

@Test
void bindToArrayWhenNestedShouldReturnPopulatedArray() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0][0]", "1");
    source.put("foo[0][1]", "2");
    source.put("foo[1][0]", "3");
    source.put("foo[1][1]", "4");
    this.sources.add(source);
    ResolvableType type = ResolvableType.fromArrayComponent(INTEGER_ARRAY.getType());
    Bindable<Integer[][]> target = Bindable.of(type);
    Integer[][] result = this.binder.bind("foo", target).get();
    assertThat(result).hasDimensions(2, 2);
    assertThat(result[0]).containsExactly(1, 2);
    assertThat(result[1]).containsExactly(3, 4);
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) ResolvableType(cn.taketoday.core.ResolvableType) Test(org.junit.jupiter.api.Test)

Example 2 with MockConfigurationPropertySource

use of cn.taketoday.context.properties.source.MockConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ArrayBinderTests method bindToArrayWhenNonIterableShouldReturnPopulatedArray.

@Test
void bindToArrayWhenNonIterableShouldReturnPopulatedArray() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[1]", "2");
    source.put("foo[0]", "1");
    source.put("foo[2]", "3");
    this.sources.add(source.nonIterable());
    Integer[] result = this.binder.bind("foo", INTEGER_ARRAY).get();
    assertThat(result).containsExactly(1, 2, 3);
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 3 with MockConfigurationPropertySource

use of cn.taketoday.context.properties.source.MockConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ArrayBinderTests method bindToArrayWhenIndexedAndCommaListShouldOnlyUseFirst.

@Test
void bindToArrayWhenIndexedAndCommaListShouldOnlyUseFirst() {
    MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
    source1.put("foo[0]", "1");
    source1.put("foo[1]", "2");
    this.sources.add(source1);
    MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
    source2.put("foo", "2,3");
    int[] result = this.binder.bind("foo", Bindable.of(int[].class)).get();
    assertThat(result).containsExactly(1, 2);
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 4 with MockConfigurationPropertySource

use of cn.taketoday.context.properties.source.MockConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ArrayBinderTests method bindToArrayWhenMultipleSourceShouldOnlyUseFirst.

@Test
void bindToArrayWhenMultipleSourceShouldOnlyUseFirst() {
    MockConfigurationPropertySource source1 = new MockConfigurationPropertySource();
    source1.put("bar", "baz");
    this.sources.add(source1);
    MockConfigurationPropertySource source2 = new MockConfigurationPropertySource();
    source2.put("foo[0]", "1");
    source2.put("foo[1]", "2");
    this.sources.add(source2);
    MockConfigurationPropertySource source3 = new MockConfigurationPropertySource();
    source3.put("foo[0]", "7");
    source3.put("foo[1]", "8");
    source3.put("foo[2]", "9");
    this.sources.add(source3);
    Integer[] result = this.binder.bind("foo", INTEGER_ARRAY).get();
    assertThat(result).containsExactly(1, 2);
}
Also used : MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 5 with MockConfigurationPropertySource

use of cn.taketoday.context.properties.source.MockConfigurationPropertySource in project today-infrastructure by TAKETODAY.

the class ArrayBinderTests method bindToArrayWhenNonSequentialShouldThrowException.

@Test
void bindToArrayWhenNonSequentialShouldThrowException() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo[0]", "2");
    source.put("foo[1]", "1");
    source.put("foo[3]", "3");
    this.sources.add(source);
    assertThatExceptionOfType(BindException.class).isThrownBy(() -> this.binder.bind("foo", INTEGER_ARRAY)).satisfies((ex) -> {
        Set<ConfigurationProperty> unbound = ((UnboundConfigurationPropertiesException) ex.getCause()).getUnboundProperties();
        assertThat(unbound.size()).isEqualTo(1);
        ConfigurationProperty property = unbound.iterator().next();
        assertThat(property.getName().toString()).isEqualTo("foo[3]");
        assertThat(property.getValue()).isEqualTo("3");
    });
}
Also used : ConfigurationProperty(cn.taketoday.context.properties.source.ConfigurationProperty) MockConfigurationPropertySource(cn.taketoday.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Aggregations

MockConfigurationPropertySource (cn.taketoday.context.properties.source.MockConfigurationPropertySource)416 Test (org.junit.jupiter.api.Test)410 Map (java.util.Map)36 HashMap (java.util.HashMap)34 LinkedHashMap (java.util.LinkedHashMap)34 ArrayList (java.util.ArrayList)32 List (java.util.List)32 Binder (cn.taketoday.context.properties.bind.Binder)30 InOrder (org.mockito.InOrder)16 ResolvableType (cn.taketoday.core.ResolvableType)14 LinkedList (java.util.LinkedList)14 ValidationBindHandler (cn.taketoday.context.properties.bind.validation.ValidationBindHandler)10 ConfigurationProperty (cn.taketoday.context.properties.source.ConfigurationProperty)10 StandardEnvironment (cn.taketoday.core.env.StandardEnvironment)10 ConfigurationPropertyName (cn.taketoday.context.properties.source.ConfigurationPropertyName)8 BindException (cn.taketoday.context.properties.bind.BindException)6 JavaBean (cn.taketoday.context.properties.bind.BinderTests.JavaBean)6 ConfigurationPropertySource (cn.taketoday.context.properties.source.ConfigurationPropertySource)6 Validator (cn.taketoday.validation.Validator)6 LocalDate (java.time.LocalDate)6