use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class ConversionErrorFieldValidatorTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
ValueStack stack = ActionContext.getContext().getValueStack();
Map<String, ConversionData> conversionErrors = new HashMap<>();
conversionErrors.put("foo", new ConversionData("bar", Integer.class));
ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
validator = new ConversionErrorFieldValidator();
validationAware = new ValidationAwareSupport();
DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware, container.getInstance(TextProviderFactory.class));
stack.push(validatorContext);
validator.setValidatorContext(validatorContext);
validator.setFieldName("foo");
validator.setValueStack(ActionContext.getContext().getValueStack());
assertEquals(0, validationAware.getFieldErrors().size());
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class DoubleRangeFieldValidatorTest method testNoValue.
public void testNoValue() throws Exception {
val.setFieldName("price");
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
val.setValidatorContext(context);
val.setMinInclusive(9.95d);
val.validate(null);
// should pass as null value passed in
assertFalse(context.hasErrors());
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.
the class DoubleRangeFieldValidatorTest method testExpressionParams.
public void testExpressionParams() throws Exception {
ValueStack stack = ActionContext.getContext().getValueStack();
ActionSupport action = new ActionSupport() {
public Double getMinInclusiveValue() {
return 10d;
}
public Double getMaxInclusiveValue() {
return 11d;
}
public Double getMinExclusiveValue() {
return 13d;
}
public Double getMaxExclusiveValue() {
return 14d;
}
public Double getPrice() {
return 15d;
}
};
stack.push(action);
val.setMinInclusiveExpression("${minInclusiveValue}");
val.setMaxInclusiveExpression("${maxInclusiveValue}");
val.setMinExclusiveExpression("${minExclusiveValue}");
val.setMaxExclusiveExpression("${maxExclusiveValue}");
val.setFieldName("price");
val.setDefaultMessage("Price is wrong!");
DelegatingValidatorContext context = new DelegatingValidatorContext(action, tpf);
val.setValidatorContext(context);
val.validate(action);
assertTrue(action.getFieldErrors().get("price").size() == 1);
}
use of com.opensymphony.xwork2.validator.DelegatingValidatorContext 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.DelegatingValidatorContext in project struts by apache.
the class DoubleRangeFieldValidatorTest method testEdgeOfMaxRange.
public void testEdgeOfMaxRange() throws Exception {
MyTestProduct prod = new MyTestProduct();
prod.setName("coca cola");
prod.setPrice(9.95);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(prod);
val.setFieldName("price");
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
val.setValidatorContext(context);
val.setMaxInclusive(9.95d);
// should pass
val.validate(prod);
assertFalse(context.hasErrors());
assertEquals(9.95d, val.getMaxInclusive());
val.setMaxExclusive(9.95d);
// should not pass
val.validate(prod);
assertTrue(context.hasErrors());
assertEquals(9.95d, val.getMaxExclusive());
}
Aggregations