Search in sources :

Example 6 with ValidatorContext

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

the class IntRangeFieldValidatorTest method prepareValidator.

private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.push(action);
    IntRangeFieldValidator validator = new IntRangeFieldValidator();
    validator.setValueStack(valueStack);
    validator.setMaxExpression("${intMaxValue}");
    validator.setMinExpression("${intMinValue}");
    validator.setValidatorContext(context);
    validator.setFieldName("intRange");
    validator.setDefaultMessage("Max is ${intMaxValue}, min is ${intMinValue} but value is ${intRange}");
    return validator;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory)

Example 7 with ValidatorContext

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

the class IntRangeFieldValidatorTest method testPassValidation.

public void testPassValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(100);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    IntRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 0);
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 8 with ValidatorContext

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

the class IntRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(98);
    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 98", 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 9 with ValidatorContext

use of com.opensymphony.xwork2.validator.ValidatorContext 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 10 with ValidatorContext

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

the class AnnotationActionValidatorManagerTest method testSkipUserMarkerActionLevelShortCircuit.

public void testSkipUserMarkerActionLevelShortCircuit() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        user.setEmail("bad_email");
        user.setEmail2("bad_email");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertTrue(context.hasFieldErrors());
        // check field errors
        List<String> l = context.getFieldErrors().get("email");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Not a valid e-mail.", l.get(0));
        l = context.getFieldErrors().get("email2");
        assertNotNull(l);
        assertEquals(2, l.size());
        assertEquals("Not a valid e-mail2.", l.get(0));
        assertEquals("Email2 not from the right company.", l.get(1));
        // check action errors
        assertTrue(context.hasActionErrors());
        l = (List<String>) context.getActionErrors();
        assertNotNull(l);
        // both expression test failed see AnnotationUser-validation.xml
        assertEquals(2, l.size());
        assertEquals("Email does not start with mark", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Aggregations

ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)21 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)17 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 List (java.util.List)6 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)4 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)4 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ActionContext (com.opensymphony.xwork2.ActionContext)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)1 VisitorValidatorTestAction (com.opensymphony.xwork2.validator.VisitorValidatorTestAction)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 ValidatorSupport (com.opensymphony.xwork2.validator.validators.ValidatorSupport)1 AppendingValidatorContext (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext)1 HashMap (java.util.HashMap)1