Search in sources :

Example 16 with ValidatorContext

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

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

the class LongRangeFieldValidatorTest method prepareValidator.

private LongRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.push(action);
    LongRangeFieldValidator validator = new LongRangeFieldValidator();
    validator.setValueStack(valueStack);
    validator.setMaxExpression("${longMaxValue}");
    validator.setMinExpression("${longMinValue}");
    validator.setValidatorContext(context);
    validator.setFieldName("longRange");
    validator.setDefaultMessage("Max is ${longMaxValue}, min is ${longMinValue} but value is ${longRange}");
    return validator;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory)

Example 18 with ValidatorContext

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

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

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

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