Search in sources :

Example 11 with ValidatorContext

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

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

the class AnnotationActionValidatorManagerTest method testDefaultMessageInterpolation.

public void testDefaultMessageInterpolation() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
    assertEquals(3, validatorList.size());
    try {
        AnnotatedTestBean bean = new AnnotatedTestBean();
        bean.setName("foo");
        bean.setCount(99);
        ValidatorContext context = new DummyValidatorContext(bean, tpf);
        annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
        assertTrue(context.hasErrors());
        assertTrue(context.hasFieldErrors());
        List<String> l = context.getFieldErrors().get("count");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Smaller Invalid Count: 99", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : AnnotatedTestBean(com.opensymphony.xwork2.AnnotatedTestBean) List(java.util.List)

Example 13 with ValidatorContext

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

the class AnnotationActionValidatorManagerTest method testMessageInterpolation.

public void testMessageInterpolation() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
    assertEquals(3, validatorList.size());
    try {
        AnnotatedTestBean bean = new AnnotatedTestBean();
        bean.setName("foo");
        bean.setCount(150);
        ValidatorContext context = new DummyValidatorContext(bean, tpf);
        annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
        assertTrue(context.hasErrors());
        assertTrue(context.hasFieldErrors());
        List<String> l = context.getFieldErrors().get("count");
        assertNotNull(l);
        assertEquals(1, l.size());
        assertEquals("Count must be between 1 and 100, current value is 150.", l.get(0));
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : AnnotatedTestBean(com.opensymphony.xwork2.AnnotatedTestBean) List(java.util.List)

Example 14 with ValidatorContext

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

the class AnnotationActionValidatorManagerTest method testShortCircuitNoErrors.

public void testShortCircuitNoErrors() {
    // get validators
    List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
    assertEquals(10, validatorList.size());
    try {
        AnnotationUser user = new AnnotationUser();
        user.setName("Mark");
        user.setEmail("mark@mycompany.com");
        user.setEmail2("mark@mycompany.com");
        ValidatorContext context = new DummyValidatorContext(user, tpf);
        annotationActionValidatorManager.validate(user, null, context);
        assertFalse(context.hasErrors());
    } catch (ValidationException ex) {
        ex.printStackTrace();
        fail("Validation error: " + ex.getMessage());
    }
}
Also used : List(java.util.List) AnnotationUser(com.opensymphony.xwork2.test.AnnotationUser)

Example 15 with ValidatorContext

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

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