Package org.springframework.validation

Examples of org.springframework.validation.BindingResult


 
  @Test
  public void create_InvalidPasswordLength() {
    final Model model = createNiceMock(Model.class);
    final NewUser newUser = new NewUser();
    final BindingResult errors = createNiceMock(BindingResult.class);
    final String username = "usename";
    final String password = "pas"; //invalid length password
    final String confirmPassword = password;
    List<ObjectError> errorList = new ArrayList<ObjectError>();
       
    newUser.setUsername(username);
    newUser.setPassword(password);
    newUser.setConfirmPassword(confirmPassword);
   
    errorList.add(new ObjectError("password.invalid.length", "Password must be atleast 4 characters long"));
   
    expect(errors.hasErrors()).andReturn(true).anyTimes();   
    expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
    replay(errors);
    replay(model);
    String result = new String(newAccountController.create(newUser, errors, model, request));
    errorList = errors.getAllErrors();

    assertThat(errorList.size(), CoreMatchers.equalTo(1));
    assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password must be atleast 4 characters long"));
    assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
  }
View Full Code Here


 
  @Test
  public void create_PasswordMismatchCaseOne() {
    final Model model = createNiceMock(Model.class);
    final NewUser newUser = new NewUser();
    final BindingResult errors = createNiceMock(BindingResult.class);
    final String username = "username";
    final String password = "password";
    final String confirmPassword = "passwor"; //confirm password not of same length as password
    List<ObjectError> errorList = new ArrayList<ObjectError>();
       
    newUser.setUsername(username);
    newUser.setPassword(password);
    newUser.setConfirmPassword(confirmPassword);
   
    errorList.add(new ObjectError("confirmPassword.mismatch", "Password mismatch"));
   
    expect(errors.hasErrors()).andReturn(true).anyTimes();   
    expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
    replay(errors);
    replay(model);
    String result = new String(newAccountController.create(newUser, errors, model, request));
    errorList = errors.getAllErrors();

    assertThat(errorList.size(), CoreMatchers.equalTo(1));
    assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password mismatch"));
    assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
  }
View Full Code Here

 
  @Test
  public void create_PasswordMismatchCaseTwo() {
    final Model model = createNiceMock(Model.class);
    final NewUser newUser = new NewUser();
    final BindingResult errors = createNiceMock(BindingResult.class);
    final String username = "username";
    final String password = "password";
    final String confirmPassword = "passwodr"; //confirm password mistyped
    List<ObjectError> errorList = new ArrayList<ObjectError>();
       
    newUser.setUsername(username);
    newUser.setPassword(password);
    newUser.setConfirmPassword(confirmPassword);
   
    errorList.add(new ObjectError("confirmPassword.mismatch", "Password mismatch"));
   
    expect(errors.hasErrors()).andReturn(true).anyTimes();   
    expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
    replay(errors);
        replay(model);
    String result = new String(newAccountController.create(newUser, errors, model, request));
    errorList = errors.getAllErrors();

    assertThat(errorList.size(), CoreMatchers.equalTo(1));
    assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Password mismatch"));
    assertThat(result, CoreMatchers.equalTo(ViewNames.NEW_ACCOUNT));
  }
View Full Code Here

 
  @Test
  public void create_BlankFormSubmitted() {
    final Model model = createNiceMock(Model.class);
    final NewUser newUser = new NewUser();
    final BindingResult errors = createNiceMock(BindingResult.class);
    final String username = ""; //Username not specified
    final String password = ""; //Password not specified
    final String confirmPassword = ""; //Confirm password not specified
    List<ObjectError> errorList = new ArrayList<ObjectError>();
       
    newUser.setUsername(username);
    newUser.setPassword(password);
    newUser.setConfirmPassword(confirmPassword);
   
    errorList.add(new ObjectError("username.required", "Username required"));
    errorList.add(new ObjectError("password.required", "Password required"));
    errorList.add(new ObjectError("confirmPassword.required", "Confirm password required"));
   
    expect(errors.hasErrors()).andReturn(true).anyTimes();   
    expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
    replay(errors);

        replay(model);
    String result = new String(newAccountController.create(newUser, errors, model, request));
    errorList = errors.getAllErrors();

    assertThat(errorList.size(), CoreMatchers.equalTo(3));
    assertThat(errorList.get(0).getDefaultMessage(), CoreMatchers.equalTo("Username required"));
    assertThat(errorList.get(1).getDefaultMessage(), CoreMatchers.equalTo("Password required"));
    assertThat(errorList.get(2).getDefaultMessage(), CoreMatchers.equalTo("Confirm password required"));
View Full Code Here

 
  @Test
  public void create_ValidFormSubmitted() {
    final Model model = createNiceMock(Model.class);
    final NewUser newUser = new NewUser();
    final BindingResult errors = createNiceMock(BindingResult.class);
    final String username = "username"; //Username not specified
    final String password = "password"; //Password not specified
    final String confirmPassword = password; //Confirm password not specified
    List<ObjectError> errorList = new ArrayList<ObjectError>();
       
    newUser.setUsername(username);
    newUser.setPassword(password);
    newUser.setConfirmPassword(confirmPassword);
   
    expect(errors.hasErrors()).andReturn(false).anyTimes();   
    expect(errors.getAllErrors()).andReturn(errorList).anyTimes();
    replay(errors);
   
    String result = new String(newAccountController.create(newUser, errors, model, request));
    errorList = errors.getAllErrors();

    assertThat(errorList.size(), CoreMatchers.equalTo(0));
    assertThat(result, CoreMatchers.equalTo(ViewNames.REDIRECT));
  }
View Full Code Here

        final Model model = createNiceMock(Model.class);
        RedirectAttributes redirectAttributes = createNiceMock(RedirectAttributes.class);
        NewUser newUser = new NewUser();
        replay(redirectAttributes);
        replay(model);
        BindingResult results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        String viewName = controller.update(newUser, results, redirectAttributes);
        assertThat(viewName, CoreMatchers.equalTo(ViewNames.PASSWORD_CHANGE));
        assertThat(results.hasErrors(), CoreMatchers.equalTo(true));
        assertThat(results.getErrorCount(), CoreMatchers.equalTo(2));
        // invalid password, to short:
        newUser.setPassword("123");
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        controller.update(newUser, results, redirectAttributes);
        assertEquals("Expected password errors", 2, results.getErrorCount());
        assertEquals("Expected password errors", "password.invalid.length", results.getFieldError().getCode());
        // missing password confirm:
        newUser.setPassword("1234");
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        controller.update(newUser, results, redirectAttributes);
        assertEquals("Expected password errors", 1, results.getErrorCount());
        assertEquals("Expected password errors", "confirmPassword.required", results.getFieldError().getCode());
        // password confirm not equal:
        newUser.setPassword("1234");
        newUser.setConfirmPassword("12345");
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        controller.update(newUser, results, redirectAttributes);
        assertEquals("Expected password errors", 1, results.getErrorCount());
        assertEquals("Expected password errors", "confirmPassword.mismatch", results.getFieldError().getCode());

        // ok request
        newUser.setPassword("1234");
        newUser.setConfirmPassword("1234");
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        controller.update(newUser, results, redirectAttributes);
        assertEquals("Expected password errors", 0, results.getErrorCount());


    }
View Full Code Here

    @Test
    public void testCreate() throws Exception {
        Model model = createNiceMock(Model.class);
        NewUser newUser = new NewUser();
        BindingResult results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        RedirectAttributes redirectAttributes = createNiceMock(RedirectAttributes.class);
        replay(redirectAttributes);
        replay(model);

        // user part
        // required email
        controller.requestUsername(newUser, results, model, request, redirectAttributes);
        assertThat(captchaService.isValid(request), CoreMatchers.equalTo(true));
        assertEquals("Expected email errors", 1, results.getErrorCount());
        assertEquals("Expected email errors", "email.required", results.getFieldError().getCode());
        // invalid email
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        newUser.setEmail("test_email");
        controller.requestUsername(newUser, results, model, request, redirectAttributes);
        assertEquals("Expected email errors", "email.invalid", results.getFieldError().getCode());
        // does not exists
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        newUser.setEmail("test@mail.com");
        String viewResult = controller.requestUsername(newUser, results, model, request, redirectAttributes);
        assertEquals("Expected email errors", 1, results.getErrorCount());
        assertEquals("Expected email errors", "email.doesnot.exist", results.getFieldError().getCode());
        assertThat(viewResult, CoreMatchers.equalTo(ViewNames.USERNAME_REQUEST));
        // password part:
        model = createNiceMock(Model.class);
        newUser = new NewUser();
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        redirectAttributes = createNiceMock(RedirectAttributes.class);
        replay(redirectAttributes);
        replay(model);
        // required email
        controller.requestPassword(newUser, results, model, request, redirectAttributes);
        assertThat(captchaService.isValid(request), CoreMatchers.equalTo(true));
        assertEquals("Expected email errors", 1, results.getErrorCount());
        assertEquals("Expected email errors", "email.required", results.getFieldError().getCode());
        // invalid email
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        newUser.setEmail("test_email");
        controller.requestPassword(newUser, results, model, request, redirectAttributes);
        assertEquals("Expected email errors", "email.invalid", results.getFieldError().getCode());
        // does not exists
        results = new DirectFieldBindingResult(newUser, ModelKeys.NEW_USER);
        newUser.setEmail("test@mail.com");
        viewResult = controller.requestPassword(newUser, results, model, request, redirectAttributes);
        assertEquals("Expected email errors", 1, results.getErrorCount());
        assertEquals("Expected email errors", "email.doesnot.exist", results.getFieldError().getCode());
        assertThat(viewResult, CoreMatchers.equalTo(ViewNames.NEW_PASSWORD_REQUEST));

    }
View Full Code Here

        final String widgetUrl = "http://example.com/widget";
        Widget widget = new Widget(123L, widgetUrl);
        widget.setTitle("Widget title");
        widget.setType("OpenSocial");
        widget.setDescription("Lorem ipsum");
        BindingResult errors = new BeanPropertyBindingResult(widget, "widget");
        SessionStatus sessionStatus = createMock(SessionStatus.class);
        ModelMap modelMap = new ExtendedModelMap();

        expect(service.getWidgetByUrl(widgetUrl)).andReturn(widget);
        service.updateWidget(widget);
        sessionStatus.setComplete();
        expectLastCall();
        replay(service, sessionStatus);
        String view = controller.updateWidgetDetail(widget, errors, validToken, validToken, modelMap, sessionStatus);
        verify(service, sessionStatus);

        assertFalse("No errors", errors.hasErrors());
        assertEquals("redirect:/app/admin/widgets?action=update", view);

    }
View Full Code Here

    }

    @Test(expected = SecurityException.class)
    public void updateWidget_wrongToken() {
        Widget widget = new Widget();
        BindingResult errors = new BeanPropertyBindingResult(widget, "widget");
        SessionStatus sessionStatus = createMock(SessionStatus.class);
        ModelMap modelMap = new ExtendedModelMap();

        sessionStatus.setComplete();
        expectLastCall();
View Full Code Here

    }

    @Test
    public void updateWidget_invalid() {
        Widget widget = new Widget(123L, "http://broken/url");
        BindingResult errors = new BeanPropertyBindingResult(widget, "widget");
        SessionStatus sessionStatus = createMock(SessionStatus.class);
        ModelMap modelMap = new ExtendedModelMap();

        String view = controller.updateWidgetDetail(widget, errors, validToken, validToken, modelMap, sessionStatus);

        assertTrue("Errors", errors.hasErrors());
        assertEquals(ViewNames.ADMIN_WIDGETDETAIL, view);

    }
View Full Code Here

TOP

Related Classes of org.springframework.validation.BindingResult

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.