Search in sources :

Example 1 with DummyValidatorContext

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

the class RegexFieldValidatorTest method testNoFieldName.

public void testNoFieldName() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setUsername("NoExpression");
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("^Sec.*");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName(null);
    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 2 with DummyValidatorContext

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

the class RegexFieldValidatorTest method testArrayOfStringField.

public void testArrayOfStringField() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setFriends(new String[] { "Alice", "Matt" });
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("A([a-zA-Z]*)");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("friends");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.setDefaultMessage("Only names starting with A are allowed!");
    validator.validate(testPerson);
    assertTrue(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
    assertEquals(1, validator.getValidatorContext().getFieldErrors().size());
    assertEquals(1, validator.getValidatorContext().getFieldErrors().get("friends").size());
    assertEquals("Only names starting with A are allowed!", validator.getValidatorContext().getFieldErrors().get("friends").get(0));
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 3 with DummyValidatorContext

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

the class RegexFieldValidatorTest method testListOfStringField.

public void testListOfStringField() throws Exception {
    MyTestPerson testPerson = new MyTestPerson();
    testPerson.setCars(Arrays.asList("Audi", "BMW"));
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("A([a-zA-Z]*)");
    validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
    validator.setFieldName("cars");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    validator.setDefaultMessage("Only cars starting with A are allowed!");
    validator.validate(testPerson);
    assertTrue(validator.getValidatorContext().hasErrors());
    assertFalse(validator.getValidatorContext().hasActionErrors());
    assertFalse(validator.getValidatorContext().hasActionMessages());
    assertTrue(validator.getValidatorContext().hasFieldErrors());
    assertEquals(1, validator.getValidatorContext().getFieldErrors().size());
    assertEquals(1, validator.getValidatorContext().getFieldErrors().get("cars").size());
    assertEquals("Only cars starting with A are allowed!", validator.getValidatorContext().getFieldErrors().get("cars").get(0));
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 4 with DummyValidatorContext

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

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

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