use of com.opensymphony.xwork2.validator.validators.URLValidator in project struts by apache.
the class URLValidatorTest method testCollectionOfUrls.
public void testCollectionOfUrls() throws Exception {
URLValidator validator = new URLValidator();
validator.setValidatorContext(new DummyValidatorContext(new Object(), tpf));
validator.setFieldName("urlCollection");
validator.setValueStack(ActionContext.getContext().getValueStack());
validator.setDefaultMessage("Wrong URL provided: ${currentValue}");
validator.validate(new MyObject());
assertTrue(validator.getValidatorContext().hasErrors());
assertFalse(validator.getValidatorContext().hasActionErrors());
assertFalse(validator.getValidatorContext().hasActionMessages());
assertTrue(validator.getValidatorContext().hasFieldErrors());
assertEquals(1, validator.getValidatorContext().getFieldErrors().get("urlCollection").size());
assertEquals("Wrong URL provided: htps://wrong.side.com", validator.getValidatorContext().getFieldErrors().get("urlCollection").get(0));
}
use of com.opensymphony.xwork2.validator.validators.URLValidator in project struts by apache.
the class URLValidatorTest method testValidUrlCaseInsensitive.
public void testValidUrlCaseInsensitive() throws Exception {
// given
final Map<String, Object> fieldErrors = new HashMap<>();
URLValidator validator = new URLValidator() {
@Override
public String getFieldName() {
return "url";
}
@Override
protected Object getFieldValue(String name, Object object) throws ValidationException {
return object;
}
@Override
protected void addFieldError(String propertyName, Object object) {
fieldErrors.put(propertyName, object);
}
};
// when
validator.validate("http://localhost:8080/myapp");
// then
assertTrue(fieldErrors.isEmpty());
// when
validator.validate("http://LOCALHOST:8080/MYAPP");
// then
assertTrue(fieldErrors.isEmpty());
// when
validator.validate("http://www.appache.org/TEST");
// then
assertTrue(fieldErrors.isEmpty());
}
use of com.opensymphony.xwork2.validator.validators.URLValidator 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.validators.URLValidator 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