ArrayBinderTests.java (spring-boot-2.7.3) | : | ArrayBinderTests.java (spring-boot-2.7.4) | ||
---|---|---|---|---|
skipping to change at line 24 | skipping to change at line 24 | |||
* limitations under the License. | * limitations under the License. | |||
*/ | */ | |||
package org.springframework.boot.context.properties.bind; | package org.springframework.boot.context.properties.bind; | |||
import java.util.ArrayList; | import java.util.ArrayList; | |||
import java.util.List; | import java.util.List; | |||
import java.util.Set; | import java.util.Set; | |||
import org.junit.jupiter.api.Test; | import org.junit.jupiter.api.Test; | |||
import org.mockito.Answers; | ||||
import org.mockito.InOrder; | import org.mockito.InOrder; | |||
import org.mockito.invocation.InvocationOnMock; | ||||
import org.mockito.stubbing.Answer; | ||||
import org.springframework.boot.context.properties.source.ConfigurationProperty; | import org.springframework.boot.context.properties.source.ConfigurationProperty; | |||
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.MockConfigurationPrope rtySource; | import org.springframework.boot.context.properties.source.MockConfigurationPrope rtySource; | |||
import org.springframework.core.ResolvableType; | import org.springframework.core.ResolvableType; | |||
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.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 ArrayBinder}. | * Tests for {@link ArrayBinder}. | |||
* | * | |||
* @author Phillip Webb | * @author Phillip Webb | |||
* @author Madhura Bhave | * @author Madhura Bhave | |||
*/ | */ | |||
class ArrayBinderTests { | class ArrayBinderTests { | |||
skipping to change at line 71 | skipping to change at line 73 | |||
source.put("foo[1]", "2"); | source.put("foo[1]", "2"); | |||
source.put("foo[2]", "3"); | source.put("foo[2]", "3"); | |||
this.sources.add(source); | this.sources.add(source); | |||
Integer[] result = this.binder.bind("foo", INTEGER_ARRAY).get(); | Integer[] result = this.binder.bind("foo", INTEGER_ARRAY).get(); | |||
assertThat(result).containsExactly(1, 2, 3); | assertThat(result).containsExactly(1, 2, 3); | |||
} | } | |||
@Test | @Test | |||
void bindToCollectionShouldTriggerOnSuccess() { | void bindToCollectionShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo[0]", "1 ", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo[0]", "1 ", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
this.binder.bind("foo", INTEGER_LIST, handler); | this.binder.bind("foo", INTEGER_LIST, handler); | |||
InOrder inOrder = inOrder(handler); | InOrder inOrder = inOrder(handler); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo[0]")), eq(Bindable.of(Integer.class)), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo[0]")), eq(Bindable.of(Integer.class)), | |||
any(), eq(1)); | any(), eq(1)); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(INTEGER_LIST), any(), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(INTEGER_LIST), any(), | |||
isA(List.class)); | isA(List.class)); | |||
} | } | |||
@Test | @Test | |||
void bindToArrayShouldReturnPrimitiveArray() { | void bindToArrayShouldReturnPrimitiveArray() { | |||
skipping to change at line 201 | skipping to change at line 203 | |||
@Test | @Test | |||
void bindToArrayWhenNoValueShouldReturnUnbound() { | void bindToArrayWhenNoValueShouldReturnUnbound() { | |||
this.sources.add(new MockConfigurationPropertySource("faf.bar", " 1")); | this.sources.add(new MockConfigurationPropertySource("faf.bar", " 1")); | |||
BindResult<Integer[]> result = this.binder.bind("foo", INTEGER_AR RAY); | BindResult<Integer[]> result = this.binder.bind("foo", INTEGER_AR RAY); | |||
assertThat(result.isBound()).isFalse(); | assertThat(result.isBound()).isFalse(); | |||
} | } | |||
@Test | @Test | |||
void bindToArrayShouldTriggerOnSuccess() { | void bindToArrayShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo[0]", "1 ", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo[0]", "1 ", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Bindable<Integer[]> target = INTEGER_ARRAY; | Bindable<Integer[]> target = INTEGER_ARRAY; | |||
this.binder.bind("foo", target, handler); | this.binder.bind("foo", target, handler); | |||
InOrder inOrder = inOrder(handler); | InOrder inOrder = inOrder(handler); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo[0]")), eq(Bindable.of(Integer.class)), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo[0]")), eq(Bindable.of(Integer.class)), | |||
any(), eq(1)); | any(), eq(1)); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | |||
isA(Integer[].class)); | isA(Integer[].class)); | |||
} | } | |||
@Test | @Test | |||
skipping to change at line 288 | skipping to change at line 290 | |||
@Test | @Test | |||
void bindToArrayWhenStringShouldUsePropertyEditor() { | void bindToArrayWhenStringShouldUsePropertyEditor() { | |||
// gh-12166 | // gh-12166 | |||
MockConfigurationPropertySource source = new MockConfigurationPro pertySource(); | MockConfigurationPropertySource source = new MockConfigurationPro pertySource(); | |||
source.put("foo", "java.lang.RuntimeException,java.lang.IllegalSt ateException"); | source.put("foo", "java.lang.RuntimeException,java.lang.IllegalSt ateException"); | |||
this.sources.add(source); | this.sources.add(source); | |||
assertThat(this.binder.bind("foo", Bindable.of(Class[].class)).ge t()).containsExactly(RuntimeException.class, | assertThat(this.binder.bind("foo", Bindable.of(Class[].class)).ge t()).containsExactly(RuntimeException.class, | |||
IllegalStateException.class); | IllegalStateException.class); | |||
} | } | |||
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; | ||||
} | ||||
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. 6 change blocks. | ||||
3 lines changed or deleted | 35 lines changed or added |