Search in sources :

Example 21 with DummyValidatorContext

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

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

Example 23 with DummyValidatorContext

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

the class LongRangeFieldValidatorTest method testMinValidation.

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

Example 24 with DummyValidatorContext

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

the class RequiredFieldValidatorTest method testNullObject.

@Test
public void testNullObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("stringValue");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
    rfv.setValidatorContext(context);
    // when
    rfv.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertNotNull(context.getFieldErrors().get("stringValue"));
    assertEquals("stringValue field is required!", context.getFieldErrors().get("stringValue").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

Example 25 with DummyValidatorContext

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

the class RequiredFieldValidatorTest method testCollectionObject.

@Test
public void testCollectionObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("shorts");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    action.setShorts(new ArrayList<Short>());
    DummyValidatorContext context = new DummyValidatorContext(action, container.getInstance(TextProviderFactory.class));
    rfv.setValidatorContext(context);
    // when
    rfv.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertNotNull(context.getFieldErrors().get("shorts"));
    assertEquals("shorts field is required!", context.getFieldErrors().get("shorts").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

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