Search in sources :

Example 11 with URLValidator

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));
}
Also used : URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 12 with URLValidator

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());
}
Also used : HashMap(java.util.HashMap) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 13 with URLValidator

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());
}
Also used : URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Example 14 with URLValidator

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());
}
Also used : Pattern(java.util.regex.Pattern) URLValidator(com.opensymphony.xwork2.validator.validators.URLValidator)

Aggregations

URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)14 Pattern (java.util.regex.Pattern)4 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)1 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)1 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)1 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)1 IntRangeFieldValidator (com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator)1 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)1 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)1 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)1 HashMap (java.util.HashMap)1 List (java.util.List)1