Search in sources :

Example 31 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class IntRangeFieldValidatorTest method testMaxValidation.

public void testMaxValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(102);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    IntRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals("Max is 101, min is 99 but value is 102", context.getFieldErrors().get("intRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 32 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class AnnotationActionValidatorManagerTest method testActionLevelShortCircuit.

public void testActionLevelShortCircuit() throws Exception {
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    AnnotationUser user = new AnnotationUser();
    // all fields will trigger error, but sc of action-level, cause it to not appear
    user.setName(null);
    user.setEmail("rainerh(at)example.com");
    user.setEmail("rainer_h(at)example.com");
    ValidatorContext context = new DummyValidatorContext(user, tpf);
    annotationActionValidatorManager.validate(user, null, context);
    // check field level errors
    // shouldn't have any because action error prevents validation of anything else
    List l = context.getFieldErrors().get("email2");
    assertNull(l);
    // check action errors
    assertTrue(context.hasActionErrors());
    l = (List) context.getActionErrors();
    assertNotNull(l);
    // we only get one, because AnnotationUserMarker-validation.xml action-level validator
    // already sc it   :-)
    assertEquals(1, l.size());
    assertEquals("Email not the same as email2", l.get(0));
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 33 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class AnnotationValidationConfigurationBuilderTest method testValidationAnnotation.

public void testValidationAnnotation() throws Exception {
    // given
    AnnotationActionValidatorManager manager = createValidationManager(AnnotationValidationAction.class, Locale.US);
    // when
    List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
    // then
    assertEquals(validators.size(), 18);
    for (Validator validator : validators) {
        validate(validator);
    }
}
Also used : LongRangeFieldValidator(com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator) RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator) FieldExpressionValidator(com.opensymphony.xwork2.validator.validators.FieldExpressionValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) CreditCardValidator(com.opensymphony.xwork2.validator.validators.CreditCardValidator) ShortRangeFieldValidator(com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) ConversionErrorFieldValidator(com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) ConditionalVisitorFieldValidator(com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)

Example 34 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class LongRangeFieldValidatorTest method testMaxValidation.

public void testMaxValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(102);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    LongRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals("Max is 101, min is 99 but value is 102", context.getFieldErrors().get("longRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 35 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class LongRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(98);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    LongRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals("Max is 101, min is 99 but value is 98", context.getFieldErrors().get("longRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3