use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.
the class ValidatorFactoryTests method testSpringValidationWithErrorInSetElement.
@Test
public void testSpringValidationWithErrorInSetElement() throws Exception {
LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
validator.afterPropertiesSet();
ValidPerson person = new ValidPerson();
person.getAddressSet().add(new ValidAddress());
BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
validator.validate(person, result);
assertEquals(3, result.getErrorCount());
FieldError fieldError = result.getFieldError("name");
assertEquals("name", fieldError.getField());
fieldError = result.getFieldError("address.street");
assertEquals("address.street", fieldError.getField());
fieldError = result.getFieldError("addressSet[].street");
assertEquals("addressSet[].street", fieldError.getField());
}
use of org.springframework.validation.BeanPropertyBindingResult in project chassis by Kixeye.
the class RawWebSocketMessage method deserialize.
/**
* Deserializes the given message.
*
* @param action
* @return
* @throws Exception
*/
public T deserialize(WebSocketAction action) throws Exception {
// first deserialize
T message = null;
if (messageClass != null) {
message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
}
// then validate
if (message != null && action.shouldValidatePayload()) {
SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
validatorAdapter.validate(message, result);
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
}
}
return message;
}
use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.
the class UserManagementControllerTest method testSubmitEditUserForm.
/**
* Test of submitEditUserForm method, of class UserManagementController.
*/
public void testSubmitEditUserForm() throws Exception {
System.out.println("submitEditUserForm");
instance = new UserManagementController();
setUpMockRoleDataService();
setUpMockUserDataService(false, false, false, true, false);
setUpMockAuthenticationContext();
instance.setUserDataService(mockUserDataService);
CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
createUserFormValidator.setUserDataService(mockUserDataService);
instance.setCreateUserFormValidator(createUserFormValidator);
// Finally the form is conform and the admin page is returned
CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
createUserCommand.setEmail("user@test.com");
createUserCommand.setLastName("newName");
createUserCommand.setFirstName("newFirstName");
createUserCommand.setPhoneNumber("0102030405");
createUserCommand.setActivated(false);
createUserCommand.setAdmin(true);
MockHttpServletRequest request = new MockHttpServletRequest();
request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, Long.valueOf(5));
BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
Model model = new ExtendedModelMap();
String result = instance.submitEditUserForm(createUserCommand, bindingResult, request, model);
assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
assertFalse(bindingResult.hasErrors());
assertTrue(bindingResult.getFieldErrors().isEmpty());
assertEquals(2, model.asMap().size());
assertEquals("user@test.com", model.asMap().get(TgolKeyStore.UPDATED_USER_NAME_KEY));
assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Aggregations