Search in sources :

Example 26 with DummyValidatorContext

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

the class RequiredFieldValidatorTest method testArrayObject.

@Test
public void testArrayObject() throws Exception {
    // given
    RequiredFieldValidator rfv = container.inject(RequiredFieldValidator.class);
    rfv.setValueStack(ActionContext.getContext().getValueStack());
    rfv.setFieldName("ints");
    rfv.setDefaultMessage("${fieldName} field is required!");
    ValidationAction action = new ValidationAction();
    action.setInts(new Integer[] {});
    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("ints"));
    assertEquals("ints field is required!", context.getFieldErrors().get("ints").get(0));
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) Test(org.junit.Test)

Example 27 with DummyValidatorContext

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

the class RequiredStringValidatorTest method testRequiredArrayOfStringsPass.

public void testRequiredArrayOfStringsPass() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    action.setStrings(new String[] { "", "12334", null });
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("strings");
    validator.setValueStack(valueStack);
    // when
    validator.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("strings").size());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 28 with DummyValidatorContext

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

the class RequiredStringValidatorTest method testRequiredStringFails.

public void testRequiredStringFails() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("stringValue");
    validator.setValueStack(valueStack);
    validator.setDefaultMessage("Field ${fieldName} is required");
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals(context.getFieldErrors().get("stringValue").get(0), "Field stringValue is required");
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 29 with DummyValidatorContext

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

the class RequiredStringValidatorTest method testRequiredCollectionOfStringsPass.

public void testRequiredCollectionOfStringsPass() throws Exception {
    // given
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    ValidationAction action = new ValidationAction();
    action.setStringCollection(Arrays.asList("", "123456", null));
    valueStack.push(action);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    RequiredStringValidator validator = new RequiredStringValidator();
    validator.setValidatorContext(context);
    validator.setFieldName("stringCollection");
    validator.setValueStack(valueStack);
    // when
    validator.validate(action);
    // then
    assertTrue(context.hasFieldErrors());
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("stringCollection").size());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 30 with DummyValidatorContext

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

the class ShortRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction((short) 1);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    ShortRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 1);
    assertEquals("Max is 10, min is 2 but value is 1", context.getFieldErrors().get("shortRange").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