MapBinderTests.java (spring-boot-2.7.3) | : | MapBinderTests.java (spring-boot-2.7.4) | ||
---|---|---|---|---|
skipping to change at line 30 | skipping to change at line 30 | |||
import java.util.ArrayList; | import java.util.ArrayList; | |||
import java.util.Collections; | import java.util.Collections; | |||
import java.util.HashMap; | import java.util.HashMap; | |||
import java.util.LinkedHashMap; | import java.util.LinkedHashMap; | |||
import java.util.List; | import java.util.List; | |||
import java.util.Map; | import java.util.Map; | |||
import java.util.Properties; | import java.util.Properties; | |||
import java.util.stream.Collectors; | import java.util.stream.Collectors; | |||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | |||
import org.mockito.Answers; | ||||
import org.mockito.ArgumentCaptor; | import org.mockito.ArgumentCaptor; | |||
import org.mockito.InOrder; | import org.mockito.InOrder; | |||
import org.mockito.invocation.InvocationOnMock; | ||||
import org.mockito.stubbing.Answer; | ||||
import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum; | import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum; | |||
import org.springframework.boot.context.properties.bind.BinderTests.JavaBean; | import org.springframework.boot.context.properties.bind.BinderTests.JavaBean; | |||
import org.springframework.boot.context.properties.source.ConfigurationPropertyN ame; | import org.springframework.boot.context.properties.source.ConfigurationPropertyN ame; | |||
import org.springframework.boot.context.properties.source.ConfigurationPropertyS ource; | import org.springframework.boot.context.properties.source.ConfigurationPropertyS ource; | |||
import org.springframework.boot.context.properties.source.MapConfigurationProper tySource; | import org.springframework.boot.context.properties.source.MapConfigurationProper tySource; | |||
import org.springframework.boot.context.properties.source.MockConfigurationPrope rtySource; | import org.springframework.boot.context.properties.source.MockConfigurationPrope rtySource; | |||
import org.springframework.core.ResolvableType; | import org.springframework.core.ResolvableType; | |||
import org.springframework.core.convert.converter.Converter; | import org.springframework.core.convert.converter.Converter; | |||
import org.springframework.core.convert.support.DefaultConversionService; | import org.springframework.core.convert.support.DefaultConversionService; | |||
import org.springframework.core.env.StandardEnvironment; | import org.springframework.core.env.StandardEnvironment; | |||
import org.springframework.test.context.support.TestPropertySourceUtils; | import org.springframework.test.context.support.TestPropertySourceUtils; | |||
import org.springframework.util.StringUtils; | import org.springframework.util.StringUtils; | |||
import static org.assertj.core.api.Assertions.assertThat; | import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | |||
import static org.assertj.core.api.Assertions.entry; | import static org.assertj.core.api.Assertions.entry; | |||
import static org.mockito.ArgumentMatchers.any; | import static org.mockito.ArgumentMatchers.any; | |||
import static org.mockito.ArgumentMatchers.eq; | import static org.mockito.ArgumentMatchers.eq; | |||
import static org.mockito.ArgumentMatchers.isA; | import static org.mockito.ArgumentMatchers.isA; | |||
import static org.mockito.BDDMockito.given; | ||||
import static org.mockito.Mockito.inOrder; | import static org.mockito.Mockito.inOrder; | |||
import static org.mockito.Mockito.mock; | import static org.mockito.Mockito.mock; | |||
/** | /** | |||
* Tests for {@link MapBinder}. | * Tests for {@link MapBinder}. | |||
* | * | |||
* @author Phillip Webb | * @author Phillip Webb | |||
* @author Madhura Bhave | * @author Madhura Bhave | |||
*/ | */ | |||
class MapBinderTests { | class MapBinderTests { | |||
skipping to change at line 332 | skipping to change at line 334 | |||
void bindToMapWithNoPropertiesShouldReturnUnbound() { | void bindToMapWithNoPropertiesShouldReturnUnbound() { | |||
this.binder = new Binder(this.sources); | this.binder = new Binder(this.sources); | |||
BindResult<Map<String, ExampleEnum>> result = this.binder.bind("f oo", | BindResult<Map<String, ExampleEnum>> result = this.binder.bind("f oo", | |||
Bindable.mapOf(String.class, ExampleEnum.class)); | Bindable.mapOf(String.class, ExampleEnum.class)); | |||
assertThat(result.isBound()).isFalse(); | assertThat(result.isBound()).isFalse(); | |||
} | } | |||
@Test | @Test | |||
void bindToMapShouldTriggerOnSuccess() { | void bindToMapShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo.bar", " 1", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo.bar", " 1", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Bindable<Map<String, Integer>> target = STRING_INTEGER_MAP; | Bindable<Map<String, Integer>> target = STRING_INTEGER_MAP; | |||
this.binder.bind("foo", target, handler); | this.binder.bind("foo", target, handler); | |||
InOrder ordered = inOrder(handler); | InOrder ordered = inOrder(handler); | |||
ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.bar")), eq(Bindable.of(Integer.class)), | ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.bar")), eq(Bindable.of(Integer.class)), | |||
any(), eq(1)); | any(), eq(1)); | |||
ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), isA(Map.class)); | ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), isA(Map.class)); | |||
} | } | |||
@Test | @Test | |||
void bindToMapStringArrayShouldTriggerOnSuccess() { | void bindToMapStringArrayShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo.bar", " a,b,c", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo.bar", " a,b,c", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Bindable<Map<String, String[]>> target = STRING_ARRAY_MAP; | Bindable<Map<String, String[]>> target = STRING_ARRAY_MAP; | |||
this.binder.bind("foo", target, handler); | this.binder.bind("foo", target, handler); | |||
InOrder ordered = inOrder(handler); | InOrder ordered = inOrder(handler); | |||
ArgumentCaptor<String[]> array = ArgumentCaptor.forClass(String[] .class); | ArgumentCaptor<String[]> array = ArgumentCaptor.forClass(String[] .class); | |||
ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.bar")), eq(Bindable.of(String[].class)), | ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.bar")), eq(Bindable.of(String[].class)), | |||
any(), array.capture()); | any(), array.capture()); | |||
assertThat(array.getValue()).containsExactly("a", "b", "c"); | assertThat(array.getValue()).containsExactly("a", "b", "c"); | |||
ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), isA(Map.class)); | ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), isA(Map.class)); | |||
} | } | |||
skipping to change at line 617 | skipping to change at line 619 | |||
private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, Re solvableType valueType) { | private <K, V> Bindable<Map<K, V>> getMapBindable(Class<K> keyGeneric, Re solvableType valueType) { | |||
ResolvableType keyType = ResolvableType.forClass(keyGeneric); | ResolvableType keyType = ResolvableType.forClass(keyGeneric); | |||
return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType)); | return Bindable.of(ResolvableType.forClassWithGenerics(Map.class, keyType, valueType)); | |||
} | } | |||
private <T> Bindable<List<T>> getListBindable(ResolvableType type) { | private <T> Bindable<List<T>> getListBindable(ResolvableType type) { | |||
return Bindable.of(ResolvableType.forClassWithGenerics(List.class , type)); | return Bindable.of(ResolvableType.forClassWithGenerics(List.class , type)); | |||
} | } | |||
private BindHandler mockBindHandler() { | ||||
BindHandler handler = mock(BindHandler.class); | ||||
given(handler.onStart(any(), any(), any())).willAnswer(Invocation | ||||
Argument.index(1)); | ||||
given(handler.onCreate(any(), any(), any(), any())).willAnswer(In | ||||
vocationArgument.index(3)); | ||||
given(handler.onSuccess(any(), any(), any(), any())).willAnswer(I | ||||
nvocationArgument.index(3)); | ||||
return handler; | ||||
} | ||||
static class Foo { | static class Foo { | |||
private String pattern; | private String pattern; | |||
Foo() { | Foo() { | |||
} | } | |||
Foo(String pattern) { | Foo(String pattern) { | |||
this.pattern = pattern; | this.pattern = pattern; | |||
} | } | |||
skipping to change at line 737 | skipping to change at line 747 | |||
Map<String, ? extends List<? extends InetAddress>> getAddresses() { | Map<String, ? extends List<? extends InetAddress>> getAddresses() { | |||
return this.addresses; | return this.addresses; | |||
} | } | |||
void setAddresses(Map<String, ? extends List<? extends InetAddres s>> addresses) { | void setAddresses(Map<String, ? extends List<? extends InetAddres s>> addresses) { | |||
this.addresses = addresses; | this.addresses = addresses; | |||
} | } | |||
} | } | |||
private static final class InvocationArgument<T> implements Answer<T> { | ||||
private final int index; | ||||
private InvocationArgument(int index) { | ||||
this.index = index; | ||||
} | ||||
@Override | ||||
public T answer(InvocationOnMock invocation) throws Throwable { | ||||
return invocation.getArgument(this.index); | ||||
} | ||||
private static <T> InvocationArgument<T> index(int index) { | ||||
return new InvocationArgument<>(index); | ||||
} | ||||
} | ||||
} | } | |||
End of changes. 7 change blocks. | ||||
3 lines changed or deleted | 35 lines changed or added |