Search in sources :

Example 6 with DelegatingValidatorContext

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());
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ConversionErrorFieldValidator(com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)

Example 7 with DelegatingValidatorContext

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());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport)

Example 8 with DelegatingValidatorContext

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);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 9 with DelegatingValidatorContext

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());
            }
        }
    }
}
Also used : DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) DelegatingValidatorContext(com.opensymphony.xwork2.validator.DelegatingValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) ModelDriven(com.opensymphony.xwork2.ModelDriven)

Example 10 with DelegatingValidatorContext

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());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)9 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)4 DelegatingValidatorContext (com.opensymphony.xwork2.validator.DelegatingValidatorContext)4 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)4 Equidae (com.opensymphony.xwork2.test.Equidae)3 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)2 List (java.util.List)2 Map (java.util.Map)2 Mock (com.mockobjects.dynamic.Mock)1 CompositeTextProvider (com.opensymphony.xwork2.CompositeTextProvider)1 ModelDriven (com.opensymphony.xwork2.ModelDriven)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)1 VisitorValidatorTestAction (com.opensymphony.xwork2.validator.VisitorValidatorTestAction)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)1 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)1