Search in sources :

Example 11 with Validator

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

the class RegexFieldValidatorTest method testEmptyName.

public void testEmptyName() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setUsername("");
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("^Sec.*");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("username");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.validate(testPerson);
    assertFalse(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertFalse(validator.getValidatorContext().hasFieldErrors());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 12 with Validator

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

the class RegexFieldValidatorTest method testIsTrimmed.

public void testIsTrimmed() {
    RegexFieldValidator validator = new RegexFieldValidator();
    assertTrue(validator.isTrimed());
    validator.setTrim(false);
    assertFalse(validator.isTrimed());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 13 with Validator

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

the class RegexFieldValidatorTest method testMatch.

public void testMatch() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setUsername("Secret");
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("^Sec.*");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("username");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.validate(testPerson);
    assertFalse(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertFalse(validator.getValidatorContext().hasFieldErrors());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 14 with Validator

use of com.opensymphony.xwork2.validator.Validator 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]);
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) SimpleAction(com.opensymphony.xwork2.SimpleAction) ValidatorSupport(com.opensymphony.xwork2.validator.validators.ValidatorSupport)

Example 15 with Validator

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

the class StringLengthFieldValidatorTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    action = new InternalActionSupport();
    container.inject(action);
    ValueStack valueStack = ActionContext.getContext().getValueStack();
    valueStack.push(action);
    validator = new StringLengthFieldValidator();
    validator.setFieldName("myField");
    validator.setMessageKey("error");
    validator.setValidatorContext(new DelegatingValidatorContext(action, container.getInstance(TextProviderFactory.class)));
    validator.setMaxLength(5);
    validator.setMinLength(2);
    validator.setValueStack(valueStack);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) StringLengthFieldValidator(com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3