Search in sources :

Example 1 with RegexFieldValidator

use of com.opensymphony.xwork2.validator.validators.RegexFieldValidator 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 RegexFieldValidator

use of com.opensymphony.xwork2.validator.validators.RegexFieldValidator 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 RegexFieldValidator

use of com.opensymphony.xwork2.validator.validators.RegexFieldValidator 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 RegexFieldValidator

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

the class RegexFieldValidatorTest method testGetExpression.

public void testGetExpression() {
    RegexFieldValidator validator = new RegexFieldValidator();
    validator.setRegex("^Hello.*");
    assertEquals("^Hello.*", validator.getRegex());
}
Also used : RegexFieldValidator(com.opensymphony.xwork2.validator.validators.RegexFieldValidator)

Example 5 with RegexFieldValidator

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

Aggregations

RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)10