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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations