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