Search in sources :

Example 1 with IntRangeFieldValidator

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

the class IntRangeFieldValidatorTest method testArrayOfIntValidation.

public void testArrayOfIntValidation() throws Exception {
    // given
    ValidationAction action = new ValidationAction();
    action.setInts(new Integer[] { 99, 100, 101, 102 });
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    IntRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.setMin(100);
    validator.setMax(101);
    validator.setFieldName("ints");
    validator.setDefaultMessage("Max is ${max}, min is ${min} but value is ${currentValue}");
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals(2, context.getFieldErrors().get("ints").size());
    assertEquals("Max is 101, min is 100 but value is 99", context.getFieldErrors().get("ints").get(0));
    assertEquals("Max is 101, min is 100 but value is 102", context.getFieldErrors().get("ints").get(1));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 2 with IntRangeFieldValidator

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

the class IntRangeFieldValidatorTest method prepareValidator.

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

Example 3 with IntRangeFieldValidator

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

the class IntRangeFieldValidatorTest method testPassValidation.

public void testPassValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(100);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    IntRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 0);
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 4 with IntRangeFieldValidator

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

the class IntRangeFieldValidatorTest method testMinValidation.

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

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

Aggregations

DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)4 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)4 ValueStack (com.opensymphony.xwork2.util.ValueStack)1 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)1 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)1 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)1 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)1 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)1 IntRangeFieldValidator (com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator)1 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)1 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)1 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)1 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)1 List (java.util.List)1