use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class ShortRangeFieldValidatorTest method testMinValidation.
public void testMinValidation() throws Exception {
// given
ValidationAction action = prepareAction((short) 1);
ValidatorContext context = new DummyValidatorContext(action, tpf);
ShortRangeFieldValidator validator = prepareValidator(action, context);
// when
validator.validate(action);
// then
assertTrue(context.getFieldErrors().size() == 1);
assertEquals("Max is 10, min is 2 but value is 1", context.getFieldErrors().get("shortRange").get(0));
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class BeanValidationInterceptor method addBeanValidationErrors.
@SuppressWarnings("nls")
protected void addBeanValidationErrors(Set<ConstraintViolation<Object>> constraintViolations, Object action) {
if (constraintViolations != null) {
ValidatorContext validatorContext = new DelegatingValidatorContext(action, textProviderFactory);
for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
String key = constraintViolation.getMessage();
String message = key;
try {
message = validatorContext.getText(key);
if (convertToUtf8 && StringUtils.isNotBlank(message)) {
message = new String(message.getBytes(convertFromEncoding), "UTF-8");
}
} catch (Exception e) {
LOG.error("Error while trying to fetch message: {}", key, e);
}
if (isActionError(constraintViolation)) {
LOG.debug("Adding action error [{}]", message);
validatorContext.addActionError(message);
} else {
ValidationError validationError = buildBeanValidationError(constraintViolation, message);
String fieldName = validationError.getFieldName();
if (action instanceof ModelDriven && fieldName.startsWith(ValidatorConstants.MODELDRIVEN_PREFIX)) {
fieldName = fieldName.replace("model.", ValidatorConstants.EMPTY_SPACE);
}
LOG.debug("Adding field error [{}] with message [{}]", fieldName, validationError.getMessage());
validatorContext.addFieldError(fieldName, validationError.getMessage());
}
}
}
}
use of com.opensymphony.xwork2.validator.ValidatorContext in project struts by apache.
the class AnnotationActionValidatorManagerTest method testSkipAllActionLevelShortCircuit2.
public void testSkipAllActionLevelShortCircuit2() {
// get validators
List validatorList = annotationActionValidatorManager.getValidators(AnnotationUser.class, null);
assertEquals(10, validatorList.size());
try {
AnnotationUser user = new AnnotationUser();
user.setName("Mark");
// * mark both email to starts with mark to get pass the action-level validator,
// so we could concentrate on testing the field-level validators (AnnotationUser-validation.xml)
// * make both email the same to pass the action-level validator at
// AnnotationUserMarker-validation.xml
user.setEmail("mark_bad_email_for_field_val@foo.com");
user.setEmail2("mark_bad_email_for_field_val@foo.com");
ValidatorContext context = new DummyValidatorContext(user, tpf);
annotationActionValidatorManager.validate(user, null, context);
assertTrue(context.hasFieldErrors());
// check field errors
// we have an error in this field level, email does not ends with mycompany.com
List l = context.getFieldErrors().get("email");
assertNotNull(l);
// because email-field-val is short-circuit
assertEquals(1, l.size());
assertEquals("Email not from the right company.", l.get(0));
// check action errors
l = (List) context.getActionErrors();
assertFalse(context.hasActionErrors());
assertEquals(0, l.size());
} catch (ValidationException ex) {
ex.printStackTrace();
fail("Validation error: " + ex.getMessage());
}
}
use of com.opensymphony.xwork2.validator.ValidatorContext 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.ValidatorContext in project struts by apache.
the class DateRangeFieldValidatorTest method prepareValidator.
private DateRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
valueStack.getActionContext().withLocale(new Locale("de"));
valueStack.push(action);
DateRangeFieldValidator validator = new DateRangeFieldValidator();
validator.setValueStack(valueStack);
validator.setMaxExpression("${dateMaxValue}");
validator.setMinExpression("${dateMinValue}");
validator.setValidatorContext(context);
validator.setFieldName("dateRange");
validator.setDefaultMessage("Max is ${dateMaxValue}, min is ${dateMinValue} but value is ${dateRange}");
return validator;
}
Aggregations