Search in sources :

Example 61 with Validator

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

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

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

the class DateRangeFieldValidatorTest method prepareValidator.

private DateRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
    ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
    valueStack.getActionContext().withLocale(new Locale("de"));
    valueStack.push(action);
    DateRangeFieldValidator validator = new DateRangeFieldValidator();
    validator.setValueStack(valueStack);
    validator.setMaxExpression("${dateMaxValue}");
    validator.setMinExpression("${dateMinValue}");
    validator.setValidatorContext(context);
    validator.setFieldName("dateRange");
    validator.setDefaultMessage("Max is ${dateMaxValue}, min is ${dateMinValue} but value is ${dateRange}");
    return validator;
}
Also used : Locale(java.util.Locale) ValueStack(com.opensymphony.xwork2.util.ValueStack) ValueStackFactory(com.opensymphony.xwork2.util.ValueStackFactory)

Example 64 with Validator

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

the class DateRangeFieldValidatorTest method testMaxValidation.

public void testMaxValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(createDate(2014, Calendar.APRIL, 4));
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    DateRangeFieldValidator validator = prepareValidator(action, context);
    // when
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals("Max is 12.12.13, min is 01.01.13 but value is 04.04.14", context.getFieldErrors().get("dateRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Example 65 with Validator

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

the class DateRangeFieldValidatorTest method testMinValidation.

public void testMinValidation() throws Exception {
    // given
    ValidationAction action = prepareAction(createDate(2012, Calendar.MARCH, 3));
    ValidatorContext context = new DummyValidatorContext(action, tpf);
    DateRangeFieldValidator validator = prepareValidator(action, context);
    System.out.println(ActionContext.getContext().getLocale());
    // when
    validator.validate(action);
    // then
    assertEquals(1, context.getFieldErrors().size());
    assertEquals("Max is 12.12.13, min is 01.01.13 but value is 03.03.12", context.getFieldErrors().get("dateRange").get(0));
}
Also used : DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext) ValidatorContext(com.opensymphony.xwork2.validator.ValidatorContext) DummyValidatorContext(com.opensymphony.xwork2.validator.DummyValidatorContext)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3