Search in sources :

Example 11 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.

the class DoubleRangeFieldValidatorTest method testRangeNotADoubleObjectValueInStack.

public void testRangeNotADoubleObjectValueInStack() throws Exception {
    MyTestProduct prod = new MyTestProduct();
    prod.setName("coca cola");
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(prod);
    val.setMinInclusive(0d);
    val.setMaxInclusive(10d);
    val.setFieldName("name");
    DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
    val.setValidatorContext(context);
    val.validate(prod);
    assertEquals(0d, val.getMinInclusive());
    assertEquals(10d, val.getMaxInclusive());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 12 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.

the class DoubleRangeFieldValidatorTest method testEdgeOfMinRange.

public void testEdgeOfMinRange() 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.setMinInclusive(9.95d);
    // should pass
    val.validate(prod);
    assertFalse(context.hasErrors());
    val.setMinExclusive(9.95d);
    // should not pass
    val.validate(prod);
    assertTrue(context.hasErrors());
}
Also used : ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 13 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.

the class EmailValidatorTest method verifyEmailValidity.

protected boolean verifyEmailValidity(final String email) throws Exception {
    ActionSupport action = new ActionSupport() {

        public String getMyEmail() {
            return email;
        }
    };
    EmailValidator validator = new EmailValidator();
    validator.setValidatorContext(new DelegatingValidatorContext(action, tpf));
    validator.setFieldName("myEmail");
    validator.setDefaultMessage("invalid email");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.validate(action);
    return (action.getFieldErrors().size() == 0);
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 14 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.

the class EmailValidatorTest method verifyEmailValidityWithExpression.

public boolean verifyEmailValidityWithExpression(final String email, final String expression) throws Exception {
    ActionSupport action = new ActionSupport() {

        public String getMyEmail() {
            return email;
        }

        public String getEmailExpression() {
            return expression;
        }
    };
    EmailValidator validator = new EmailValidator();
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    valueStack.push(action);
    validator.setValueStack(valueStack);
    validator.setValidatorContext(new DelegatingValidatorContext(action, tpf));
    validator.setFieldName("myEmail");
    validator.setDefaultMessage("invalid email");
    validator.setRegexExpression("${emailExpression}");
    validator.validate(action);
    return (action.getFieldErrors().size() == 0);
}
Also used : EmailValidator(com.opensymphony.xwork2.validator.validators.EmailValidator) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 15 with DelegatingValidatorContext

use of com.opensymphony.xwork2.validator.DelegatingValidatorContext in project struts by apache.

the class ExpressionValidatorTest method testNoBooleanExpression.

public void testNoBooleanExpression() throws Exception {
    Mock mock = new Mock(ValidationAware.class);
    mock.expect("addActionError", C.ANY_ARGS);
    ExpressionValidator ev = new ExpressionValidator();
    ev.setValidatorContext(new DelegatingValidatorContext(mock.proxy(), tpf));
    ev.setExpression("{top}");
    ev.setValueStack(ActionContext.getContext().getValueStack());
    // {top} will evaluate to Hello that is not a Boolean
    ev.validate("Hello");
    mock.verify();
}
Also used : ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator) EasyMock(org.easymock.EasyMock) Mock(com.mockobjects.dynamic.Mock)

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