Search in sources :

Example 31 with DummyValidatorContext

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

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

the class URLValidatorTest method testCollectionOfUrlsSafness.

public void testCollectionOfUrlsSafness() throws Exception {
    URLValidator validator = new URLValidator();
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("urlSafeness");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.setDefaultMessage("Wrong URL provided: ${currentValue}");
    validator.validate(new MyObject());
    assertTrue(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
    assertEquals(2, validator.getValidatorContext().getFieldErrors().get("urlSafeness").size());
    assertEquals("Wrong URL provided: ${1+2}", validator.getValidatorContext().getFieldErrors().get("urlSafeness").get(0));
    assertEquals("Wrong URL provided: %{2+3}", validator.getValidatorContext().getFieldErrors().get("urlSafeness").get(1));
}
Also used : URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 33 with DummyValidatorContext

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

the class URLValidatorTest method testCollectionOfUrls.

public void testCollectionOfUrls() throws Exception {
    URLValidator validator = new URLValidator();
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("urlCollection");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.setDefaultMessage("Wrong URL provided: ${currentValue}");
    validator.validate(new MyObject());
    assertTrue(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
    assertEquals(1, validator.getValidatorContext().getFieldErrors().get("urlCollection").size());
    assertEquals("Wrong URL provided: htps://wrong.side.com", validator.getValidatorContext().getFieldErrors().get("urlCollection").get(0));
}
Also used : URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 34 with DummyValidatorContext

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

the class DateRangeFieldValidatorTest method testMaxValidation.

public void testMaxValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(createDate(2014, Calendar.APRIL, 4));
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    DateRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals("Max is 12.12.13, min is 01.01.13 but value is 04.04.14", context.getFieldErrors().get("dateRange").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 DummyValidatorContext

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

the class DateRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(createDate(2012, Calendar.MARCH, 3));
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    DateRangeFieldValidator validator = prepareValidator(action, context);
    System.out.println(ActionContext.getContext().getLocale());
    // when
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals("Max is 12.12.13, min is 01.01.13 but value is 03.03.12", context.getFieldErrors().get("dateRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

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