use of com.opensymphony.xwork2.validator.ValidatorContext 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;
}
use of com.opensymphony.xwork2.validator.ValidatorContext 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);
}
use of com.opensymphony.xwork2.validator.ValidatorContext 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));
}
use of com.opensymphony.xwork2.validator.ValidatorContext 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));
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class AnnotationActionValidatorManagerTest method testSkipUserMarkerActionLevelShortCircuit.
public void testSkipUserMarkerActionLevelShortCircuit() {
// get validators
List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
assertEquals(10, validatorList.size());
try {
AnnotationUser user = new AnnotationUser();
user.setName("Mark");
user.setEmail("bad_email");
user.setEmail2("bad_email");
ValidatorContext context = new DummyValidatorContext(user, tpf);
annotationActionValidatorManager.validate(user, null, context);
assertTrue(context.hasFieldErrors());
// check field errors
List<String> l = context.getFieldErrors().get("email");
assertNotNull(l);
assertEquals(1, l.size());
assertEquals("Not a valid e-mail.", l.get(0));
l = context.getFieldErrors().get("email2");
assertNotNull(l);
assertEquals(2, l.size());
assertEquals("Not a valid e-mail2.", l.get(0));
assertEquals("Email2 not from the right company.", l.get(1));
// check action errors
assertTrue(context.hasActionErrors());
l = (List<String>) context.getActionErrors();
assertNotNull(l);
// both expression test failed see AnnotationUser-validation.xml
assertEquals(2, l.size());
assertEquals("Email does not start with mark", l.get(0));
} catch (ValidationException ex) {
ex.printStackTrace();
fail("Validation error: " + ex.getMessage());
}
}
Aggregations