Search in sources :

Example 66 with Validator

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

the class RegexFieldValidatorTest method testMatchNoTrim.

public void testMatchNoTrim() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    // must end with one whitespace
    testPerson.setUsername("Secret ");
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setTrim(false);
    validator.setRegex("^Sec.*\\s");
    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 67 with Validator

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

the class RegexFieldValidatorTest method testFail.

public void testFail() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setUsername("Superman");
    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);
    assertTrue(validator.getValidatorContext().hasErrors());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
    List<String> msgs = validator.getValidatorContext().getFieldErrors().get("username");
    assertNotNull(msgs);
    // should contain 1 error message
    assertEquals(1, msgs.size());
    // when failing the validator will not add action errors/msg
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 68 with Validator

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

the class RegexFieldValidatorTest method testNoStringField.

public void testNoStringField() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setAge(33);
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("[0-9][0-9]");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("age");
    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 69 with Validator

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

the class URLValidatorTest method testInvalidValue.

public void testInvalidValue() throws Exception {
    URLValidator validator = new URLValidator();
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("testingUrl3");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.validate(new MyObject());
    assertTrue(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
}
Also used : URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 70 with Validator

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

the class URLValidatorTest method testValidUrlWithRegexExpression.

public void testValidUrlWithRegexExpression() throws Exception {
    URLValidator validator = new URLValidator();
    ActionContext.getContext().getValueStack().push(new MyAction());
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.setUrlRegexExpression("${urlRegex}");
    Pattern pattern = Pattern.compile(validator.getUrlRegex());
    assertTrue(pattern.matcher("myapp://test.com").matches());
    assertFalse(pattern.matcher("myap://test.com").matches());
}
Also used : Pattern(java.util.regex.Pattern) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

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