BinderTests.java (spring-boot-2.7.3) | : | BinderTests.java (spring-boot-2.7.4) | ||
---|---|---|---|---|
skipping to change at line 30 | skipping to change at line 30 | |||
import java.time.LocalDate; | import java.time.LocalDate; | |||
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.List; | import java.util.List; | |||
import java.util.Map; | import java.util.Map; | |||
import javax.validation.Validation; | import javax.validation.Validation; | |||
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.bind.Bindable.BindRestriction ; | import org.springframework.boot.context.properties.bind.Bindable.BindRestriction ; | |||
import org.springframework.boot.context.properties.bind.validation.ValidationBin dHandler; | import org.springframework.boot.context.properties.bind.validation.ValidationBin dHandler; | |||
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.ConfigurationPropertyS ources; | import org.springframework.boot.context.properties.source.ConfigurationPropertyS ources; | |||
import org.springframework.boot.context.properties.source.MockConfigurationPrope rtySource; | import org.springframework.boot.context.properties.source.MockConfigurationPrope rtySource; | |||
import org.springframework.core.annotation.AnnotationUtils; | import org.springframework.core.annotation.AnnotationUtils; | |||
import org.springframework.core.convert.ConversionFailedException; | import org.springframework.core.convert.ConversionFailedException; | |||
import org.springframework.core.env.MapPropertySource; | import org.springframework.core.env.MapPropertySource; | |||
skipping to change at line 57 | skipping to change at line 58 | |||
import org.springframework.validation.Validator; | import org.springframework.validation.Validator; | |||
import org.springframework.validation.annotation.Validated; | import org.springframework.validation.annotation.Validated; | |||
import org.springframework.validation.beanvalidation.SpringValidatorAdapter; | import org.springframework.validation.beanvalidation.SpringValidatorAdapter; | |||
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.assertThatIllegalArgumentException ; | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException ; | |||
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 Binder}. | * Tests for {@link Binder}. | |||
* | * | |||
* @author Phillip Webb | * @author Phillip Webb | |||
* @author Madhura Bhave | * @author Madhura Bhave | |||
*/ | */ | |||
class BinderTests { | class BinderTests { | |||
skipping to change at line 177 | skipping to change at line 179 | |||
this.binder = new Binder(this.sources, null, null, | this.binder = new Binder(this.sources, null, null, | |||
(registry) -> registry.registerCustomEditor(JavaB ean.class, new JavaBeanPropertyEditor())); | (registry) -> registry.registerCustomEditor(JavaB ean.class, new JavaBeanPropertyEditor())); | |||
this.sources.add(new MockConfigurationPropertySource("foo", "123" )); | this.sources.add(new MockConfigurationPropertySource("foo", "123" )); | |||
BindResult<JavaBean> result = this.binder.bind("foo", Bindable.of (JavaBean.class)); | BindResult<JavaBean> result = this.binder.bind("foo", Bindable.of (JavaBean.class)); | |||
assertThat(result.get().getValue()).isEqualTo("123"); | assertThat(result.get().getValue()).isEqualTo("123"); | |||
} | } | |||
@Test | @Test | |||
void bindToValueShouldTriggerOnSuccess() { | void bindToValueShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Bindable<Integer> target = Bindable.of(Integer.class); | Bindable<Integer> target = Bindable.of(Integer.class); | |||
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")), eq(target), any(), eq(1)); | ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), eq(1)); | |||
} | } | |||
@Test | @Test | |||
void bindOrCreateWhenNotBoundShouldTriggerOnCreate() { | void bindOrCreateWhenNotBoundShouldTriggerOnCreate() { | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mock(BindHandler.class); | |||
Bindable<JavaBean> target = Bindable.of(JavaBean.class); | Bindable<JavaBean> target = Bindable.of(JavaBean.class); | |||
this.binder.bindOrCreate("foo", target, handler); | this.binder.bindOrCreate("foo", target, handler); | |||
InOrder ordered = inOrder(handler); | InOrder ordered = inOrder(handler); | |||
ordered.verify(handler).onCreate(eq(ConfigurationPropertyName.of( "foo")), eq(target), any(), any()); | ordered.verify(handler).onCreate(eq(ConfigurationPropertyName.of( "foo")), eq(target), any(), any()); | |||
} | } | |||
@Test | @Test | |||
void bindToJavaBeanShouldReturnPopulatedBean() { | void bindToJavaBeanShouldReturnPopulatedBean() { | |||
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar")); | this.sources.add(new MockConfigurationPropertySource("foo.value", "bar")); | |||
JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.cl ass)).get(); | JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.cl ass)).get(); | |||
skipping to change at line 222 | skipping to change at line 224 | |||
source.put("foo", "boom"); | source.put("foo", "boom"); | |||
source.put("foo.value", "bar"); | source.put("foo.value", "bar"); | |||
this.sources.add(source); | this.sources.add(source); | |||
JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.cl ass)).get(); | JavaBean result = this.binder.bind("foo", Bindable.of(JavaBean.cl ass)).get(); | |||
assertThat(result.getValue()).isEqualTo("bar"); | assertThat(result.getValue()).isEqualTo("bar"); | |||
} | } | |||
@Test | @Test | |||
void bindToJavaBeanShouldTriggerOnSuccess() { | void bindToJavaBeanShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Bindable<JavaBean> target = Bindable.of(JavaBean.class); | Bindable<JavaBean> target = Bindable.of(JavaBean.class); | |||
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.value")), eq(Bindable.of(String.class)), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.value")), eq(Bindable.of(String.class)), | |||
any(), eq("bar")); | any(), eq("bar")); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | |||
isA(JavaBean.class)); | isA(JavaBean.class)); | |||
} | } | |||
@Test | @Test | |||
void bindWhenHasCustomDefaultHandlerShouldTriggerOnSuccess() { | void bindWhenHasCustomDefaultHandlerShouldTriggerOnSuccess() { | |||
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1")); | this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1")); | |||
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_ METHODS); | BindHandler handler = mockBindHandler(); | |||
Binder binder = new Binder(this.sources, null, null, null, handle r); | Binder binder = new Binder(this.sources, null, null, null, handle r); | |||
Bindable<JavaBean> target = Bindable.of(JavaBean.class); | Bindable<JavaBean> target = Bindable.of(JavaBean.class); | |||
binder.bind("foo", target); | binder.bind("foo", target); | |||
InOrder inOrder = inOrder(handler); | InOrder inOrder = inOrder(handler); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.value")), eq(Bindable.of(String.class)), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo.value")), eq(Bindable.of(String.class)), | |||
any(), eq("bar")); | any(), eq("bar")); | |||
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of ("foo")), eq(target), any(), | |||
isA(JavaBean.class)); | isA(JavaBean.class)); | |||
} | } | |||
skipping to change at line 362 | skipping to change at line 364 | |||
private JavaBeanWithPublicConstructor bindToJavaBeanWithPublicConstructor ( | private JavaBeanWithPublicConstructor bindToJavaBeanWithPublicConstructor ( | |||
Bindable<JavaBeanWithPublicConstructor> bindable) { | Bindable<JavaBeanWithPublicConstructor> bindable) { | |||
MockConfigurationPropertySource source = new MockConfigurationPro pertySource(); | MockConfigurationPropertySource source = new MockConfigurationPro pertySource(); | |||
source.put("foo", "constructor"); | source.put("foo", "constructor"); | |||
source.put("foo.value", "setter"); | source.put("foo.value", "setter"); | |||
this.sources.add(source); | this.sources.add(source); | |||
return this.binder.bindOrCreate("foo", bindable); | return this.binder.bindOrCreate("foo", bindable); | |||
} | } | |||
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 JavaBean { | static class JavaBean { | |||
private String value; | private String value; | |||
private List<String> items = Collections.emptyList(); | private List<String> items = Collections.emptyList(); | |||
String getValue() { | String getValue() { | |||
return this.value; | return this.value; | |||
} | } | |||
skipping to change at line 490 | skipping to change at line 500 | |||
@Override | @Override | |||
public void setAsText(String text) { | public void setAsText(String text) { | |||
JavaBean value = new JavaBean(); | JavaBean value = new JavaBean(); | |||
value.setValue(text); | value.setValue(text); | |||
setValue(value); | setValue(value); | |||
} | } | |||
} | } | |||
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. 9 change blocks. | ||||
5 lines changed or deleted | 37 lines changed or added |