use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class ValidatorAnnotationTest method testNotAnnotatedMethodSuccess2.
public void testNotAnnotatedMethodSuccess2() throws Exception {
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create().build());
ActionProxy proxy = actionProxyFactory.createActionProxy("", "notAnnotatedMethod", null, extraContext);
proxy.execute();
assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
assertEquals(0, errors.size());
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class SimpleActionValidationTest method testSubPropertiesAreValidated.
public void testSubPropertiesAreValidated() {
HashMap<String, Object> params = new HashMap<>();
params.put("baz", "10");
// valid values
params.put("foo", "8");
params.put("bar", "7");
params.put("date", "12/23/2002");
params.put("bean.name", "Name should be valid");
// this should cause a message
params.put("bean.count", "100");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> beanCountErrors = errors.get("bean.count");
assertEquals(1, beanCountErrors.size());
String errorMessage = beanCountErrors.get(0);
assertNotNull(errorMessage);
assertEquals("bean.count out of range.", errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class SimpleActionValidationTest method testAliasValidation.
public void testAliasValidation() {
Map<String, Object> params = new HashMap<>();
params.put("baz", "10");
// valid values
params.put("bar", "7");
params.put("date", "12/23/2002");
params.put("percentage", "1.23456789");
Map<String, Object> extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
proxy.execute();
ValidationAware validationAware = (ValidationAware) proxy.getAction();
assertFalse(validationAware.hasFieldErrors());
params.put("bar", "42");
extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, null, extraContext);
proxy.execute();
validationAware = (ValidationAware) proxy.getAction();
assertTrue(validationAware.hasFieldErrors());
Map<String, List<String>> errors = validationAware.getFieldErrors();
assertTrue(errors.containsKey("baz"));
List<String> bazErrors = errors.get("baz");
assertEquals(1, bazErrors.size());
String message = bazErrors.get(0);
assertEquals("baz out of range.", message);
assertTrue(errors.containsKey("bar"));
List<String> barErrors = errors.get("bar");
assertEquals(1, barErrors.size());
message = barErrors.get(0);
assertEquals("bar must be between 6 and 10, current value is 42.", message);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class SimpleActionValidationTest method testLookingUpFieldNameAsTextKey.
public void testLookingUpFieldNameAsTextKey() {
HashMap<String, Object> params = new HashMap<>();
// should cause a message
params.put("baz", "-1");
// valid values
params.put("bar", "7");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> bazErrors = errors.get("baz");
assertEquals(1, bazErrors.size());
String errorMessage = bazErrors.get(0);
assertNotNull(errorMessage);
assertEquals("Baz Field must be greater than 0", errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.
the class RestActionInvocation method selectTarget.
@SuppressWarnings("unchecked")
protected void selectTarget() {
// Select target (content to return)
Throwable e = (Throwable) stack.findValue("exception");
if (e != null) {
// Exception
target = e;
hasErrors = true;
} else if (action instanceof ValidationAware && ((ValidationAware) action).hasErrors()) {
// Error messages
ValidationAware validationAwareAction = ((ValidationAware) action);
Map errors = new HashMap();
if (validationAwareAction.getActionErrors().size() > 0) {
errors.put("actionErrors", validationAwareAction.getActionErrors());
}
if (validationAwareAction.getFieldErrors().size() > 0) {
errors.put("fieldErrors", validationAwareAction.getFieldErrors());
}
target = errors;
hasErrors = true;
} else if (action instanceof ModelDriven) {
// Model
target = ((ModelDriven) action).getModel();
} else {
target = action;
}
if (shouldRestrictToGET()) {
target = null;
}
}
Aggregations