use of org.springframework.validation.FieldError in project spring-framework by spring-projects.
the class SpringValidatorAdapterTests method testNoStringArgumentValue.
// SPR-13406
@Test
public void testNoStringArgumentValue() throws Exception {
TestBean testBean = new TestBean();
testBean.setPassword("pass");
testBean.setConfirmPassword("pass");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(testBean, "testBean");
validatorAdapter.validate(testBean, errors);
assertThat(errors.getFieldErrorCount("password")).isEqualTo(1);
assertThat(errors.getFieldValue("password")).isEqualTo("pass");
FieldError error = errors.getFieldError("password");
assertThat(error).isNotNull();
assertThat(messageSource.getMessage(error, Locale.ENGLISH)).isEqualTo("Size of Password must be between 8 and 128");
assertThat(error.contains(ConstraintViolation.class)).isTrue();
assertThat(error.unwrap(ConstraintViolation.class).getPropertyPath().toString()).isEqualTo("password");
assertThat(SerializationTestUtils.serializeAndDeserialize(error.toString())).isEqualTo(error.toString());
}
use of org.springframework.validation.FieldError 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();
}
use of org.springframework.validation.FieldError in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testSpringValidationWithErrorInListElement.
@Test
public void testSpringValidationWithErrorInListElement() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressList().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertThat(result.getErrorCount()).isEqualTo(3);
FieldError fieldError = result.getFieldError("name");
assertThat(fieldError.getField()).isEqualTo("name");
fieldError = result.getFieldError("address.street");
assertThat(fieldError.getField()).isEqualTo("address.street");
fieldError = result.getFieldError("addressList[0].street");
assertThat(fieldError.getField()).isEqualTo("addressList[0].street");
}
use of org.springframework.validation.FieldError in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testListValidation.
@Test
public void testListValidation() {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ListContainer listContainer = new ListContainer();
listContainer.addString("A");
listContainer.addString("X");
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(listContainer, "listContainer");
errors.initConversion(new DefaultConversionService());
validator.validate(listContainer, errors);
FieldError fieldError = errors.getFieldError("list[1]");
assertThat(fieldError).isNotNull();
assertThat(fieldError.getRejectedValue()).isEqualTo("X");
assertThat(errors.getFieldValue("list[1]")).isEqualTo("X");
}
use of org.springframework.validation.FieldError in project molgenis by molgenis.
the class RepositoryConstraintViolationExceptionTest method testGetFieldError.
@Test
void testGetFieldError() {
EntityErrors entityErrors = mock(EntityErrors.class);
FieldError fieldError = new FieldError("name1", "field", "message1");
when(entityErrors.getFieldError("field")).thenReturn(fieldError);
RepositoryConstraintViolationException exception = new RepositoryConstraintViolationException(entityErrors);
assertEquals(exception.getFieldError("field"), fieldError);
}
Aggregations