Search in sources :

Example 51 with Validator

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

the class AnnotationActionValidatorManagerTest method testSkipAllActionLevelShortCircuit2.

public void testSkipAllActionLevelShortCircuit2() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        // * mark both email to starts with mark to get pass the action-level validator,
        // so we could concentrate on testing the field-level validators (AnnotationUser-validation.xml)
        // * make both email the same to pass the action-level validator at
        // AnnotationUserMarker-validation.xml
        user.setEmail("mark_bad_email_for_field_val@foo.com");
        user.setEmail2("mark_bad_email_for_field_val@foo.com");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertTrue(context.hasFieldErrors());
        // check field errors
        // we have an error in this field level, email does not ends with mycompany.com
        List l = context.getFieldErrors().get("email");
        assertNotNull(l);
        // because email-field-val is short-circuit
        assertEquals(1, l.size());
        assertEquals("Email not from the right company.", l.get(0));
        // check action errors
        l = (List) context.getActionErrors();
        assertFalse(context.hasActionErrors());
        assertEquals(0, l.size());
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 52 with Validator

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

the class AnnotationActionValidatorManagerTest method no_testGetValidatorsFromInterface.

public void no_testGetValidatorsFromInterface() {
    List validatorList = annotationActionValidatorManager.getValidators(SimpleAnnotationAction3.class, alias);
    // 17 in the class hierarchy + 1 in the interface + 1 in interface alias
    assertEquals(19, validatorList.size());
    final FieldValidator v = (FieldValidator) validatorList.get(0);
    assertEquals("bar", v.getFieldName());
    assertTrue(v instanceof RequiredFieldValidator);
    final FieldValidator v1 = (FieldValidator) validatorList.get(1);
    assertEquals("bar", v1.getFieldName());
    assertTrue(v1 instanceof IntRangeFieldValidator);
    final FieldValidator vdouble = (FieldValidator) validatorList.get(2);
    assertEquals("percentage", vdouble.getFieldName());
    assertTrue(vdouble instanceof DoubleRangeFieldValidator);
    final FieldValidator v2 = (FieldValidator) validatorList.get(3);
    assertEquals("baz", v2.getFieldName());
    assertTrue(v2 instanceof IntRangeFieldValidator);
    final FieldValidator v3 = (FieldValidator) validatorList.get(4);
    assertEquals("date", v3.getFieldName());
    assertTrue(v3 instanceof DateRangeFieldValidator);
    // action-level validator comes first
    final Validator v4 = (Validator) validatorList.get(5);
    assertTrue(v4 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v5 = (Validator) validatorList.get(6);
    assertTrue(v5 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v6 = (Validator) validatorList.get(7);
    assertTrue(v6 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v7 = (Validator) validatorList.get(8);
    assertTrue(v7 instanceof ExpressionValidator);
    // action-level validator comes first
    final Validator v8 = (Validator) validatorList.get(9);
    assertTrue(v8 instanceof ExpressionValidator);
    final FieldValidator v9 = (FieldValidator) validatorList.get(10);
    assertEquals("datefield", v9.getFieldName());
    assertTrue(v9 instanceof DateRangeFieldValidator);
    final FieldValidator v10 = (FieldValidator) validatorList.get(11);
    assertEquals("emailaddress", v10.getFieldName());
    assertTrue(v10 instanceof EmailValidator);
    final FieldValidator v11 = (FieldValidator) validatorList.get(12);
    assertEquals("intfield", v11.getFieldName());
    assertTrue(v11 instanceof IntRangeFieldValidator);
    final FieldValidator v12 = (FieldValidator) validatorList.get(13);
    assertEquals("customfield", v12.getFieldName());
    assertTrue(v12 instanceof RequiredFieldValidator);
    final FieldValidator v13 = (FieldValidator) validatorList.get(14);
    assertEquals("stringisrequired", v13.getFieldName());
    assertTrue(v13 instanceof RequiredStringValidator);
    final FieldValidator v14 = (FieldValidator) validatorList.get(15);
    assertEquals("needstringlength", v14.getFieldName());
    assertTrue(v14 instanceof StringLengthFieldValidator);
    final FieldValidator v15 = (FieldValidator) validatorList.get(16);
    assertEquals("hreflocation", v15.getFieldName());
    assertTrue(v15 instanceof URLValidator);
    final FieldValidator v16 = (FieldValidator) validatorList.get(17);
    assertEquals("data", v16.getFieldName());
    assertTrue(v16 instanceof RequiredFieldValidator);
    final FieldValidator v17 = (FieldValidator) validatorList.get(18);
    assertEquals("data", v17.getFieldName());
    assertTrue(v17 instanceof RequiredStringValidator);
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) List(java.util.List) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) DateRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator) EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) DoubleRangeFieldValidator(com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator) IntRangeFieldValidator(com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator) RequiredFieldValidator(com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)

Example 53 with Validator

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

the class AnnotationValidationConfigurationBuilderTest method testValidationAnnotationExpParams.

public void testValidationAnnotationExpParams() throws Exception {
    // given
    AnnotationActionValidatorManager manager = createValidationManager(AnnotationValidationExpAction.class, Locale.US);
    // when
    List<Validator> validators = manager.getValidators(AnnotationValidationExpAction.class, null);
    // then
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.push(new AnnotationValidationExpAction());
    assertEquals(validators.size(), 16);
    for (Validator validator : validators) {
        validator.setValueStack(valueStack);
        validate(validator);
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory) 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 54 with Validator

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

the class EmailValidatorTest method verifyEmailValidity.

protected boolean verifyEmailValidity(final String email) throws Exception {
    ActionSupport action = new ActionSupport() {

        public String getMyEmail() {
            return email;
        }
    };
    EmailValidator validator = new EmailValidator();
    validator.setValidatorContext(new DelegatingValidatorContext(action, tpf));
    validator.setFieldName("myEmail");
    validator.setDefaultMessage("invalid email");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.validate(action);
    return (action.getFieldErrors().size() == 0);
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 55 with Validator

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

the class EmailValidatorTest method verifyTrim.

private EmailValidator verifyTrim(final boolean trim) {
    ActionSupport action = new ActionSupport() {

        public boolean getTrimEmail() {
            return trim;
        }
    };
    EmailValidator validator = new EmailValidator();
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    valueStack.push(action);
    validator.setValueStack(valueStack);
    validator.setTrimExpression("${trimEmail}");
    return validator;
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionSupport(com.opensymphony.xwork2.ActionSupport)

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