use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class DWRValidator method doPost.
public ValidationAwareSupport doPost(String namespace, String actionName, Map params) throws Exception {
HttpServletRequest req = WebContextFactory.get().getHttpServletRequest();
ServletContext servletContext = WebContextFactory.get().getServletContext();
HttpServletResponse res = WebContextFactory.get().getHttpServletResponse();
HttpParameters.Builder requestParams = HttpParameters.create(req.getParameterMap());
if (params != null) {
requestParams = requestParams.withExtraParams(params);
}
Map<String, Object> requestMap = new RequestMap(req);
Map<String, Object> session = new SessionMap<>(req);
Map<String, Object> application = new ApplicationMap(servletContext);
Dispatcher du = Dispatcher.getInstance();
Map<String, Object> ctx = du.createContextMap(requestMap, requestParams.build(), session, application, req, res);
try {
ActionProxyFactory actionProxyFactory = du.getContainer().getInstance(ActionProxyFactory.class);
ActionProxy proxy = actionProxyFactory.createActionProxy(namespace, actionName, null, ctx, true, true);
proxy.execute();
Object action = proxy.getAction();
if (action instanceof ValidationAware) {
ValidationAware aware = (ValidationAware) action;
ValidationAwareSupport vas = new ValidationAwareSupport();
vas.setActionErrors(aware.getActionErrors());
vas.setActionMessages(aware.getActionMessages());
vas.setFieldErrors(aware.getFieldErrors());
return vas;
} else {
return null;
}
} catch (Exception e) {
LOG.error("Error while trying to validate", e);
return null;
}
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class DoubleRangeFieldValidatorTest method testEdgeOfMaxRange.
public void testEdgeOfMaxRange() throws Exception {
MyTestProduct prod = new MyTestProduct();
prod.setName("coca cola");
prod.setPrice(9.95);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(prod);
val.setFieldName("price");
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
val.setValidatorContext(context);
val.setMaxInclusive(9.95d);
// should pass
val.validate(prod);
assertFalse(context.hasErrors());
assertEquals(9.95d, val.getMaxInclusive());
val.setMaxExclusive(9.95d);
// should not pass
val.validate(prod);
assertTrue(context.hasErrors());
assertEquals(9.95d, val.getMaxExclusive());
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class DoubleRangeFieldValidatorTest method testRangeNotADoubleObjectValueInStack.
public void testRangeNotADoubleObjectValueInStack() throws Exception {
MyTestProduct prod = new MyTestProduct();
prod.setName("coca cola");
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(prod);
val.setMinInclusive(0d);
val.setMaxInclusive(10d);
val.setFieldName("name");
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
val.setValidatorContext(context);
val.validate(prod);
assertEquals(0d, val.getMinInclusive());
assertEquals(10d, val.getMaxInclusive());
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class DoubleRangeFieldValidatorTest method testEdgeOfMinRange.
public void testEdgeOfMinRange() throws Exception {
MyTestProduct prod = new MyTestProduct();
prod.setName("coca cola");
prod.setPrice(9.95);
ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(prod);
val.setFieldName("price");
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
val.setValidatorContext(context);
val.setMinInclusive(9.95d);
// should pass
val.validate(prod);
assertFalse(context.hasErrors());
val.setMinExclusive(9.95d);
// should not pass
val.validate(prod);
assertTrue(context.hasErrors());
}
use of com.opensymphony.xwork2.ValidationAwareSupport in project struts by apache.
the class StringValidatorTest method testStringLength.
public void testStringLength() throws Exception {
Equidae equidae = new Equidae();
equidae.setCow("asdf");
equidae.setDonkey("asdf");
ActionContext.getContext().getValueStack().push(equidae);
DelegatingValidatorContext context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
Map fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
List errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5", errors.get(0));
assertEquals("noTrim-min5-max10", errors.get(1));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5", errors.get(0));
assertEquals("trim-min5-max10", errors.get(1));
equidae.setCow("asdf ");
equidae.setDonkey("asdf ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertFalse(fieldErrors.containsKey("cow"));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5", errors.get(0));
assertEquals("trim-min5-max10", errors.get(1));
equidae.setCow("asdfasdf");
equidae.setDonkey("asdfasdf");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertFalse(fieldErrors.containsKey("cow"));
// donkey
assertFalse(fieldErrors.containsKey("donkey"));
equidae.setCow("asdfasdf ");
equidae.setDonkey("asdfasdf ");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5-max10", errors.get(0));
assertEquals("noTrim-max10", errors.get(1));
// donkey
assertFalse(fieldErrors.containsKey("donkey"));
equidae.setCow("asdfasdfasdf");
equidae.setDonkey("asdfasdfasdf");
ActionContext.getContext().getValueStack().push(equidae);
context = new DelegatingValidatorContext(new ValidationAwareSupport(), tpf);
container.getInstance(ActionValidatorManager.class).validate(equidae, null, context);
assertTrue(context.hasFieldErrors());
fieldErrors = context.getFieldErrors();
// cow
assertTrue(fieldErrors.containsKey("cow"));
errors = (List) fieldErrors.get("cow");
assertEquals(2, errors.size());
assertEquals("noTrim-min5-max10", errors.get(0));
assertEquals("noTrim-max10", errors.get(1));
// donkey
assertTrue(fieldErrors.containsKey("donkey"));
errors = (List) fieldErrors.get("donkey");
assertEquals(2, errors.size());
assertEquals("trim-min5-max10", errors.get(0));
assertEquals("trim-max10", errors.get(1));
}
Aggregations