Example usage for org.springframework.validation BindException getGlobalError

List of usage examples for org.springframework.validation BindException getGlobalError

Introduction

In this page you can find the example usage for org.springframework.validation BindException getGlobalError.

Prototype

@Override
    @Nullable
    public ObjectError getGlobalError() 

Source Link

Usage

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithCustomValangFunctions() throws Exception {

    Map functionsByName = new HashMap();
    functionsByName.put("tupper", UpperCaseFunction.class.getName());
    ValangCondition goodCondition = new ValangCondition("tupper(name) == 'URI'", functionsByName, null);
    ValangCondition badCondition = new ValangCondition("tupper(name) == 'Uri'", functionsByName, null);

    // creating the validation configuration for the bean.
    DefaultBeanValidationConfiguration personValidationConfiguration = new DefaultBeanValidationConfiguration();
    personValidationConfiguration.addGlobalRule(new DefaultValidationRule(goodCondition, "good"));
    personValidationConfiguration.addGlobalRule(new DefaultValidationRule(badCondition, "bad"));
    SimpleBeanValidationConfigurationLoader loader = new SimpleBeanValidationConfigurationLoader();
    loader.setClassValidation(Person.class, personValidationConfiguration);

    BeanValidator validator = new BeanValidator(loader);

    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());
    validator.setConfigurationLoader(loader);

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);/*ww  w.  java2s .c o  m*/

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());
}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithCustomValangFunctions2() throws Exception {

    Map functionsByName = new HashMap();
    functionsByName.put("tupper", UpperCaseFunction.class.getName());

    ValangConditionExpressionParser conditionExpressionParser = new ValangConditionExpressionParser();
    conditionExpressionParser.setCustomFunctions(functionsByName);

    DefaultValidationRuleElementHandlerRegistry registry = new DefaultValidationRuleElementHandlerRegistry();
    registry.setConditionExpressionParser(conditionExpressionParser);
    registry.afterPropertiesSet();//from   ww w  .j a  va  2  s .  c  om

    DefaultXmlBeanValidationConfigurationLoader loader = new DefaultXmlBeanValidationConfigurationLoader();
    loader.setResource(new ClassPathResource("validation.xml", getClass()));
    loader.setElementHandlerRegistry(registry);
    loader.afterPropertiesSet();

    BeanValidator validator = new BeanValidator(loader);
    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());

    Person person = new Person("Uri");
    person.setHomeless(false);
    person.setAddress(new Address(null, "Amsterdam"));
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals(1, errors.getFieldErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());
    assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());
}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithApplicationContext() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "org/springmodules/validation/bean/beanValidator-tests.xml");

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    Validator validator = (Validator) context.getBean("validator");
    validator.validate(person, errors);//from  w  w  w. j a  v  a 2  s  .  co m

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());

}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithManualConfiguration() throws Exception {

    // creating the validation configuration for the bean.
    DefaultBeanValidationConfiguration personValidationConfiguration = new DefaultBeanValidationConfiguration();
    personValidationConfiguration/*from   w w  w. j  av  a  2 s  .c  o  m*/
            .addGlobalRule(new DefaultValidationRule(Conditions.minLength("name", 4), "minLength"));
    personValidationConfiguration.addPropertyRule("name",
            new DefaultValidationRule(Conditions.minLength("name", 5), "minLength"));
    SimpleBeanValidationConfigurationLoader loader = new SimpleBeanValidationConfigurationLoader();
    loader.setClassValidation(Person.class, personValidationConfiguration);

    BeanValidator validator = new BeanValidator();
    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());
    validator.setConfigurationLoader(loader);

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);

    assertTrue(errors.hasFieldErrors("name"));
    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getFieldErrorCount("name"));
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[minLength]", errors.getGlobalError().getCode());
    assertEquals("Person.name[minLength]", errors.getFieldError("name").getCode());

}

From source file:org.iwethey.forums.web.user.test.NewUserValidatorTest.java

public void testValidateExists() {
    User u = new User();
    BindException errors = new BindException(u, "userInfo");
    u.setNickname("ut_spork1");
    u.setUnencryptedPassword("itchy1");
    u.setPasswordCheck("itchy1");

    mVal.validate(u, errors);/* w  w w.  j  a va  2 s.  c  o m*/
    assertEquals("nickname", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("password", 0, errors.getFieldErrorCount("password"));
    assertEquals("passwordCheck", 0, errors.getFieldErrorCount("passwordCheck"));

    ObjectError err = errors.getGlobalError();
    assertEquals("global", "error.existing.login", err.getCode());
}

From source file:org.openmrs.web.controller.user.ChangePasswordFormControllerTest.java

/**
 * @see ChangePasswordFormController#handleSubmission(HttpSession, String, String, String, String, String, User, BindingResult)
 *           test =/*from   w  ww .  ja  va  2  s . c  o  m*/
 */
@Test
@Verifies(value = "display error message when the password is empty", method = "handleSubmission()")
public void handleSubmission_shouldDisplayErrorMessageWhenPasswordIsEmpty() throws Exception {
    ChangePasswordFormController controller = new ChangePasswordFormController();
    BindException errors = new BindException(controller.formBackingObject(), "user");

    String result = controller.handleSubmission(new MockHttpSession(), oldPassword, "", "", "", "", "",
            Context.getAuthenticatedUser(), errors);

    assertTrue(errors.hasErrors());
    assertEquals("error.password.weak", errors.getGlobalError().getCode());
}

From source file:org.iwethey.forums.web.user.test.LoginValidatorTest.java

public void testValidateBadLogin() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("spork");
    u.setUnencryptedPassword("blancmange");

    mVal.validate(u, errors);// w w  w . j  a v  a  2s.  co m
    assertEquals("nickname", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("unencryptedPassword", 0, errors.getFieldErrorCount("unencryptedPassword"));
    assertEquals("bad login", 1, errors.getGlobalErrorCount());

    ObjectError err = errors.getGlobalError();
    assertEquals("global", "error.invalid.login", err.getCode());
}

From source file:org.openmrs.web.controller.user.ChangePasswordFormControllerTest.java

/**
 * @see ChangePasswordFormController#handleSubmission(HttpSession, String, String, String, String, String, User, BindingResult)
 *///from   w  ww.  ja  va2  s.c  om
@Test
@Verifies(value = "display error message when the answer is empty and question is not empty", method = "handleSubmission()")
public void handleSubmission_shouldDisplayErrorMessageIfQuestionIsNotEmptyAndAnswerIsEmpty() throws Exception {
    ChangePasswordFormController controller = new ChangePasswordFormController();
    BindException errors = new BindException(controller.formBackingObject(), "user");

    String result = controller.handleSubmission(new MockHttpSession(), oldPassword, "Passw0rd", "Passw0rd",
            "question", "", "", Context.getAuthenticatedUser(), errors);

    assertTrue(errors.hasErrors());
    assertEquals("auth.question.fill", errors.getGlobalError().getCode());
}

From source file:org.openmrs.web.controller.user.ChangePasswordFormControllerTest.java

/**
 * @see ChangePasswordFormController#handleSubmission(HttpSession, String, String, String, String, String, User, BindingResult)
 *///  ww  w  . j  av a 2 s.  co m
@Test
@Verifies(value = "display an error message when the password and confirm password entries are different", method = "handleSubmission()")
public void handleSubmission_shouldDisplayErrorMessageWhenPasswordAndConfirmPasswordAreNotSame()
        throws Exception {
    ChangePasswordFormController controller = new ChangePasswordFormController();
    BindException errors = new BindException(controller.formBackingObject(), "user");

    String result = controller.handleSubmission(new MockHttpSession(), oldPassword, "password",
            "differentPassword", "", "", "", Context.getAuthenticatedUser(), errors);

    assertTrue(errors.hasErrors());
    assertEquals("error.password.match", errors.getGlobalError().getCode());
}

From source file:org.openmrs.web.controller.user.ChangePasswordFormControllerTest.java

/**
 * @see ChangePasswordFormController#handleSubmission(HttpSession, String, String, String, String, String, User, BindingResult)
 */// w  w w.  j  a  v  a  2s  .co m
@Test
@Verifies(value = "display error message if password is weak", method = "handleSubmission()")
public void handleSubmission_shouldDiplayErrorMessageOnWeakPasswords() throws Exception {
    ChangePasswordFormController controller = new ChangePasswordFormController();
    BindException errors = new BindException(controller.formBackingObject(), "user");

    String result = controller.handleSubmission(new MockHttpSession(), oldPassword, "password", "password", "",
            "", "", Context.getAuthenticatedUser(), errors);

    assertTrue(errors.hasErrors());
    assertEquals("error.password.requireMixedCase", errors.getGlobalError().getCode());
}