use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SpringValidatorAdapterTests method testMapEntryConstraint.
// SPR-15839
@Test
public void testMapEntryConstraint() {
Map<String, String> property = new HashMap<>();
property.put(null, null);
BeanWithMapEntryConstraint bean = new BeanWithMapEntryConstraint();
bean.setProperty(property);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(bean, "bean");
validatorAdapter.validate(bean, errors);
assertThat(errors.hasFieldErrors("property[]")).isTrue();
assertThat(errors.getFieldValue("property[]")).isNull();
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class SpringValidatorAdapterTests method testWithSet.
// SPR-16177
@Test
public void testWithSet() {
Parent parent = new Parent();
parent.setName("Parent with set");
parent.getChildSet().addAll(createChildren(parent));
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(parent, "parent");
validatorAdapter.validate(parent, errors);
assertThat(errors.getErrorCount() > 0).isTrue();
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testSpringValidationWithClassLevel.
@Test
public void testSpringValidationWithClassLevel() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.setName("Juergen");
person.getAddress().setStreet("Juergen's Street");
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertThat(result.getErrorCount()).isEqualTo(1);
ObjectError globalError = result.getGlobalError();
List<String> errorCodes = Arrays.asList(globalError.getCodes());
assertThat(errorCodes.size()).isEqualTo(2);
assertThat(errorCodes.contains("NameAddressValid.person")).isTrue();
assertThat(errorCodes.contains("NameAddressValid")).isTrue();
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testValidationWithOptionalField.
@Test
public void testValidationWithOptionalField() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
MainBeanWithOptional mainBean = new MainBeanWithOptional();
Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean");
validator.validate(mainBean, errors);
Object rejected = errors.getFieldValue("inner.value");
assertThat(rejected).isNull();
}
use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testSpringValidation.
@Test
public void testSpringValidation() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertThat(result.getErrorCount()).isEqualTo(2);
FieldError fieldError = result.getFieldError("name");
assertThat(fieldError.getField()).isEqualTo("name");
List<String> errorCodes = Arrays.asList(fieldError.getCodes());
assertThat(errorCodes.size()).isEqualTo(4);
assertThat(errorCodes.contains("NotNull.person.name")).isTrue();
assertThat(errorCodes.contains("NotNull.name")).isTrue();
assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
assertThat(errorCodes.contains("NotNull")).isTrue();
fieldError = result.getFieldError("address.street");
assertThat(fieldError.getField()).isEqualTo("address.street");
errorCodes = Arrays.asList(fieldError.getCodes());
assertThat(errorCodes.size()).isEqualTo(5);
assertThat(errorCodes.contains("NotNull.person.address.street")).isTrue();
assertThat(errorCodes.contains("NotNull.address.street")).isTrue();
assertThat(errorCodes.contains("NotNull.street")).isTrue();
assertThat(errorCodes.contains("NotNull.java.lang.String")).isTrue();
assertThat(errorCodes.contains("NotNull")).isTrue();
}
Aggregations