use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class AppendingValidatorContextTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
action = container.inject(VisitorValidatorTestAction.class);
TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);
ValidatorContext vc1 = new DelegatingValidatorContext(action, tpf);
VisitorFieldValidator.AppendingValidatorContext vc2 = new AppendingValidatorContext(vc1, createTextProvider(action, vc1), FIRST_NAME, "");
validatorContext = new AppendingValidatorContext(vc2, createTextProvider(action, vc2), SECOND_NAME, "");
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class StringValidatorTest method testStringLength.
public void testStringLength() throws Exception {
Equidae equidae = new Equidae();
equidae.setCow("asdf");
equidae.setDonkey("asdf");
ActionContext.getContext().getValueStack().push(equidae);
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
Map fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
List errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5", errors.get(0));
assertEquals("noTrim-min5-max10", errors.get(1));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5", errors.get(0));
assertEquals("trim-min5-max10", errors.get(1));
equidae.setCow("asdf ");
equidae.setDonkey("asdf ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertFalse(fieldErrors.containsKey("cow"));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5", errors.get(0));
assertEquals("trim-min5-max10", errors.get(1));
equidae.setCow("asdfasdf");
equidae.setDonkey("asdfasdf");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertFalse(fieldErrors.containsKey("cow"));
// donkey
assertFalse(fieldErrors.containsKey("donkey"));
equidae.setCow("asdfasdf ");
equidae.setDonkey("asdfasdf ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5-max10", errors.get(0));
assertEquals("noTrim-max10", errors.get(1));
// donkey
assertFalse(fieldErrors.containsKey("donkey"));
equidae.setCow("asdfasdfasdf");
equidae.setDonkey("asdfasdfasdf");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5-max10", errors.get(0));
assertEquals("noTrim-max10", errors.get(1));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5-max10", errors.get(0));
assertEquals("trim-max10", errors.get(1));
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class StringValidatorTest method testRequiredString.
public void testRequiredString() throws Exception {
Equidae equidae = new Equidae();
// everything should fail
equidae.setHorse("");
ActionContext.getContext().getValueStack().push(equidae);
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
Map fieldErrors = context.getFieldErrors();
assertTrue(fieldErrors.containsKey("horse"));
assertEquals(2, ((List) fieldErrors.get("horse")).size());
// trim = false should fail
equidae.setHorse(" ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
assertTrue(fieldErrors.containsKey("horse"));
List errors = (List) fieldErrors.get("horse");
assertEquals(1, errors.size());
assertEquals("trim", (String) errors.get(0));
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class MethodNameExtractor method addValidationErrors.
private void addValidationErrors(ConstraintViolation[] violations, Object action, ValueStack valueStack, String parentFieldname) {
if (violations != null) {
ValidatorContext validatorContext = new DelegatingValidatorContext(action, textProviderFactory);
for (ConstraintViolation violation : violations) {
// translate message
String key = violation.getMessage();
String message;
// push context variable into stack, to allow use ${max}, ${min} etc in error messages
valueStack.push(violation.getMessageVariables());
// push the validator into the stack
valueStack.push(violation.getContext());
try {
message = validatorContext.getText(key);
} finally {
valueStack.pop();
valueStack.pop();
}
if (isActionError(violation)) {
LOG.debug("Adding action error '{}'", message);
validatorContext.addActionError(message);
} else {
ValidationError validationError = buildValidationError(violation.getContextPath(), message);
// build field name
String fieldName = validationError.getFieldName();
if (parentFieldname != null) {
fieldName = parentFieldname + "." + fieldName;
}
LOG.debug("Adding field error [{}] with message '{}'", fieldName, validationError.getMessage());
validatorContext.addFieldError(fieldName, validationError.getMessage());
// add violations of member object fields
addValidationErrors(violation.getCauses(), action, valueStack, fieldName);
}
}
}
}
Aggregations