|
|
@ -27,6 +27,8 @@ import javax.validation.constraints.NotNull;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.context.properties.bind.AbstractBindHandler;
|
|
|
|
|
|
|
|
import org.springframework.boot.context.properties.bind.BindContext;
|
|
|
|
import org.springframework.boot.context.properties.bind.BindException;
|
|
|
|
import org.springframework.boot.context.properties.bind.BindException;
|
|
|
|
import org.springframework.boot.context.properties.bind.Bindable;
|
|
|
|
import org.springframework.boot.context.properties.bind.Bindable;
|
|
|
|
import org.springframework.boot.context.properties.bind.Binder;
|
|
|
|
import org.springframework.boot.context.properties.bind.Binder;
|
|
|
@ -35,6 +37,7 @@ import org.springframework.boot.context.properties.source.ConfigurationPropertyN
|
|
|
|
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
|
|
|
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
|
|
|
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
|
|
|
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
|
|
|
|
import org.springframework.boot.origin.Origin;
|
|
|
|
import org.springframework.boot.origin.Origin;
|
|
|
|
|
|
|
|
import org.springframework.core.convert.ConverterNotFoundException;
|
|
|
|
import org.springframework.validation.FieldError;
|
|
|
|
import org.springframework.validation.FieldError;
|
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
|
import org.springframework.validation.ObjectError;
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
@ -57,12 +60,14 @@ public class ValidationBindHandlerTests {
|
|
|
|
|
|
|
|
|
|
|
|
private Binder binder;
|
|
|
|
private Binder binder;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private LocalValidatorFactoryBean validator;
|
|
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
public void setup() {
|
|
|
|
this.binder = new Binder(this.sources);
|
|
|
|
this.binder = new Binder(this.sources);
|
|
|
|
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
|
|
|
|
this.validator = new LocalValidatorFactoryBean();
|
|
|
|
validator.afterPropertiesSet();
|
|
|
|
this.validator.afterPropertiesSet();
|
|
|
|
this.handler = new ValidationBindHandler(validator);
|
|
|
|
this.handler = new ValidationBindHandler(this.validator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
@ -162,6 +167,34 @@ public class ValidationBindHandlerTests {
|
|
|
|
this.handler);
|
|
|
|
this.handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void bindShouldNotValidateIfOtherHandlersInChainThrowError() {
|
|
|
|
|
|
|
|
this.sources.add(new MockConfigurationPropertySource("foo", "hello"));
|
|
|
|
|
|
|
|
ExampleValidatedBean bean = new ExampleValidatedBean();
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BindException.class)
|
|
|
|
|
|
|
|
.isThrownBy(
|
|
|
|
|
|
|
|
() -> this.binder.bind("foo",
|
|
|
|
|
|
|
|
Bindable.of(ExampleValidatedBean.class)
|
|
|
|
|
|
|
|
.withExistingValue(bean),
|
|
|
|
|
|
|
|
this.handler))
|
|
|
|
|
|
|
|
.withCauseInstanceOf(ConverterNotFoundException.class);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
public void bindShouldValidateIfOtherHandlersInChainIgnoreError() {
|
|
|
|
|
|
|
|
TestHandler testHandler = new TestHandler();
|
|
|
|
|
|
|
|
this.handler = new ValidationBindHandler(testHandler, this.validator);
|
|
|
|
|
|
|
|
this.sources.add(new MockConfigurationPropertySource("foo", "hello"));
|
|
|
|
|
|
|
|
ExampleValidatedBean bean = new ExampleValidatedBean();
|
|
|
|
|
|
|
|
assertThatExceptionOfType(BindException.class)
|
|
|
|
|
|
|
|
.isThrownBy(
|
|
|
|
|
|
|
|
() -> this.binder.bind("foo",
|
|
|
|
|
|
|
|
Bindable.of(ExampleValidatedBean.class)
|
|
|
|
|
|
|
|
.withExistingValue(bean),
|
|
|
|
|
|
|
|
this.handler))
|
|
|
|
|
|
|
|
.withCauseInstanceOf(BindValidationException.class);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private BindValidationException bindAndExpectValidationError(Runnable action) {
|
|
|
|
private BindValidationException bindAndExpectValidationError(Runnable action) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
action.run();
|
|
|
|
action.run();
|
|
|
@ -265,4 +298,14 @@ public class ValidationBindHandlerTests {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static class TestHandler extends AbstractBindHandler {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Object onFailure(ConfigurationPropertyName name, Bindable<?> target,
|
|
|
|
|
|
|
|
BindContext context, Exception error) throws Exception {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|