Search in sources :

Example 16 with DummyValidatorContext

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

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

Example 18 with DummyValidatorContext

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

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

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

Aggregations

DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)21 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)17 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)8 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)8 List (java.util.List)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)4 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)4 Test (org.junit.Test)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)1