Search in sources :

Example 36 with DummyValidatorContext

use of com.opensymphony.xwork2.validator.DummyValidatorContext 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 37 with DummyValidatorContext

use of com.opensymphony.xwork2.validator.DummyValidatorContext 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 38 with DummyValidatorContext

use of com.opensymphony.xwork2.validator.DummyValidatorContext 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 39 with DummyValidatorContext

use of com.opensymphony.xwork2.validator.DummyValidatorContext 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 40 with DummyValidatorContext

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

the class LongRangeFieldValidatorTest method testPassValidation.

public void testPassValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(100);
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    LongRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertTrue(context.getFieldErrors().size() == 0);
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Aggregations

DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)21 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)17 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)8 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)8 List (java.util.List)6 ValueStack (com.opensymphony.xwork2.util.ValueStack)5 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)4 AnnotationUser (com.opensymphony.xwork2.test.AnnotationUser)4 Test (org.junit.Test)3 AnnotatedTestBean (com.opensymphony.xwork2.AnnotatedTestBean)2 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)1