Search in sources :

Example 26 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddAdminUserForm.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddAdminUserForm() throws Exception {
    System.out.println("SubmitAddAdminUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, true, true, false, 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.setSiteUrl("http://www.newSite.com/");
    createUserCommand.setEmail("newUser@test.com");
    createUserCommand.setPassword("P4sSw0rD");
    createUserCommand.setConfirmPassword("P4sSw0rD");
    createUserCommand.setAdmin(true);
    createUserCommand.setActivated(true);
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("newUser@test.com", model.asMap().get(TgolKeyStore.ADDED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 27 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddUserForm.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddUserForm() throws Exception {
    System.out.println("submitAddUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, true, false, false, 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.setSiteUrl("http://www.newSite.com/");
    createUserCommand.setEmail("newUser@test.com");
    createUserCommand.setPassword("P4sSw0rD");
    createUserCommand.setConfirmPassword("P4sSw0rD");
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("newUser@test.com", model.asMap().get(TgolKeyStore.ADDED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 28 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitEditUserChangeToExistingUserForm.

/**
     * Test of submitEditUserForm method, of class UserManagementController.
     */
public void testSubmitEditUserChangeToExistingUserForm() throws Exception {
    System.out.println("SubmitEditUserChangeToExistingUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, false, 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.setSiteUrl("http://www.newSite.com/");
    createUserCommand.setEmail("admin@test.com");
    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.EDIT_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(1, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.existingAccountWithEmail", bindingResult.getFieldErrors("email").get(0).getCode());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 29 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddUserFormWithErrors.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddUserFormWithErrors() throws Exception {
    System.out.println("SubmitAddUserFormWithErrors");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, false, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // First the form is sent back empty
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.missingEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.missingPassword", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
    // First the form is sent back with passwords that don't match
    createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("user@test.com");
    createUserCommand.setPassword("password");
    createUserCommand.setConfirmPassword("confirmation");
    bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    model = new ExtendedModelMap();
    result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.existingAccountWithEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.passwordNotIdentical", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
    // Then the url is not conform but in this context it is not tested, so ignored
    // The Email1 is not conform regardin the pattern EMAIL_CHECKER_REGEXP
    // The passwords match but don't respect the constraints
    createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("null");
    createUserCommand.setPassword("password");
    createUserCommand.setConfirmPassword("password");
    bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    model = new ExtendedModelMap();
    result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.invalidEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.invalidPassword", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 30 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ModelResultMatchersTests method setUp.

@Before
public void setUp() throws Exception {
    this.matchers = new ModelResultMatchers();
    ModelAndView mav = new ModelAndView("view", "good", "good");
    BindingResult bindingResult = new BeanPropertyBindingResult("good", "good");
    mav.addObject(BindingResult.MODEL_KEY_PREFIX + "good", bindingResult);
    this.mvcResult = getMvcResult(mav);
    Date date = new Date();
    BindingResult bindingResultWithError = new BeanPropertyBindingResult(date, "date");
    bindingResultWithError.rejectValue("time", "error");
    ModelAndView mavWithError = new ModelAndView("view", "good", "good");
    mavWithError.addObject("date", date);
    mavWithError.addObject(BindingResult.MODEL_KEY_PREFIX + "date", bindingResultWithError);
    this.mvcResultWithError = getMvcResult(mavWithError);
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ModelAndView(org.springframework.web.servlet.ModelAndView) Date(java.util.Date) Before(org.junit.Before)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)73 Test (org.junit.Test)60 TestBean (org.springframework.tests.sample.beans.TestBean)19 Errors (org.springframework.validation.Errors)18 StringReader (java.io.StringReader)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 BindingResult (org.springframework.validation.BindingResult)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 LinkedList (java.util.LinkedList)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 Model (org.springframework.ui.Model)5 FieldError (org.springframework.validation.FieldError)5 ObjectError (org.springframework.validation.ObjectError)5