use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class VisitorFieldValidator method validateObject.
private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(o);
ValidatorContext validatorContext;
if (appendPrefix) {
ValidatorContext parent = getValidatorContext();
validatorContext = new AppendingValidatorContext(parent, createTextProvider(o, parent), fieldName, getMessage(o));
} else {
ValidatorContext parent = getValidatorContext();
CompositeTextProvider textProvider = createTextProvider(o, parent);
validatorContext = new DelegatingValidatorContext(parent, textProvider, parent);
}
actionValidatorManager.validate(o, visitorContext, validatorContext);
stack.pop();
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class SimpleActionValidationTest method testMessageKeyIsReturnedIfNoOtherDefault.
public void testMessageKeyIsReturnedIfNoOtherDefault() throws ValidationException {
Validator validator = new ValidatorSupport() {
public void validate(Object object) throws ValidationException {
addActionError(object);
}
};
validator.setValueStack(ActionContext.getContext().getValueStack());
String messageKey = "does.not.exist";
validator.setMessageKey(messageKey);
SimpleAction action = new SimpleAction();
container.inject(action);
ValidatorContext validatorContext = new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class));
validator.setValidatorContext(validatorContext);
validator.validate(this);
assertTrue(validatorContext.hasActionErrors());
Collection<String> errors = validatorContext.getActionErrors();
assertEquals(1, errors.size());
assertEquals(messageKey, errors.toArray()[0]);
}
use of com.opensymphony.xwork2.validator.ValidationException 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());
}
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class AnnotationActionValidatorManagerTest method testDefaultMessageInterpolation.
public void testDefaultMessageInterpolation() {
// get validators
List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
assertEquals(3, validatorList.size());
try {
AnnotatedTestBean bean = new AnnotatedTestBean();
bean.setName("foo");
bean.setCount(99);
ValidatorContext context = new DummyValidatorContext(bean, tpf);
annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
assertTrue(context.hasErrors());
assertTrue(context.hasFieldErrors());
List<String> l = context.getFieldErrors().get("count");
assertNotNull(l);
assertEquals(1, l.size());
assertEquals("Smaller Invalid Count: 99", l.get(0));
} catch (ValidationException ex) {
ex.printStackTrace();
fail("Validation error: " + ex.getMessage());
}
}
use of com.opensymphony.xwork2.validator.ValidationException in project struts by apache.
the class AnnotationActionValidatorManagerTest method testMessageInterpolation.
public void testMessageInterpolation() {
// get validators
List validatorList = annotationActionValidatorManager.getValidators(AnnotatedTestBean.class, "beanMessageBundle");
assertEquals(3, validatorList.size());
try {
AnnotatedTestBean bean = new AnnotatedTestBean();
bean.setName("foo");
bean.setCount(150);
ValidatorContext context = new DummyValidatorContext(bean, tpf);
annotationActionValidatorManager.validate(bean, "beanMessageBundle", context);
assertTrue(context.hasErrors());
assertTrue(context.hasFieldErrors());
List<String> l = context.getFieldErrors().get("count");
assertNotNull(l);
assertEquals(1, l.size());
assertEquals("Count must be between 1 and 100, current value is 150.", l.get(0));
} catch (ValidationException ex) {
ex.printStackTrace();
fail("Validation error: " + ex.getMessage());
}
}
Aggregations