Search in sources :

Example 6 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class DoubleRangeFieldValidatorTest method testRangeValidationWithError.

public void testRangeValidationWithError() throws Exception {
    // Explicitly set an out-of-range double for DoubleRangeValidatorTest
    Map<String, Object> context = new HashMap<>();
    HashMap<String, Object> params = new HashMap<>();
    params.put("percentage", 100.12);
    context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, context);
    proxy.execute();
    assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
    Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
    List<String> errorMessages = errors.get("percentage");
    assertNotNull("Expected double range validation error message.", errorMessages);
    assertEquals(1, errorMessages.size());
    String errorMessage = errorMessages.get(0);
    assertNotNull("Expecting: percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
    assertEquals("percentage must be between 0.1 and 10.1, current value is 100.12.", errorMessage);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 7 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class DoubleRangeFieldValidatorTest method testRangeValidationNoError.

public void testRangeValidationNoError() throws Exception {
    Map<String, Object> context = new HashMap<>();
    HashMap<String, Object> params = new HashMap<>();
    params.put("percentage", 1.234567d);
    context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", "percentage", null, context);
    proxy.execute();
    assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
    Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
    List<String> errorMessages = errors.get("percentage");
    assertNull("Expected no double range validation error message.", errorMessages);
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 8 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class ValidatorAnnotationTest method testAnnotatedMethodSuccess2.

public void testAnnotatedMethodSuccess2() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    // make it not fail
    params.put("param2", "key2");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", null, extraContext);
    proxy.execute();
    assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap)

Example 9 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class ValidatorAnnotationTest method testAnnotatedMethodFailure.

public void testAnnotatedMethodFailure() throws Exception {
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create().build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", null, extraContext);
    proxy.execute();
    assertTrue(((ValidationAware) proxy.getAction()).hasActionErrors());
    Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
    assertEquals(1, errors.size());
    assertEquals("Need param1 or param2.", errors.iterator().next());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) Collection(java.util.Collection) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 10 with ValidationAware

use of com.opensymphony.xwork2.interceptor.ValidationAware in project struts by apache.

the class ValidatorAnnotationTest method testNotAnnotatedMethodSuccess.

public void testNotAnnotatedMethodSuccess() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    params.put("date", "12/23/2002");
    params.put("foo", "5");
    params.put("bar", "7");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).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());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) Collection(java.util.Collection) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Aggregations

ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)42 ActionProxy (com.opensymphony.xwork2.ActionProxy)38 List (java.util.List)28 HashMap (java.util.HashMap)23 Map (java.util.Map)8 ValueStack (com.opensymphony.xwork2.util.ValueStack)6 Collection (java.util.Collection)5 ModelDrivenAction (org.apache.struts.beanvalidation.actions.ModelDrivenAction)5 ActionContext (com.opensymphony.xwork2.ActionContext)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)2 ClearableValueStack (com.opensymphony.xwork2.util.ClearableValueStack)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ValidateGroupAction (org.apache.struts.beanvalidation.actions.ValidateGroupAction)2 HttpParameters (org.apache.struts2.dispatcher.HttpParameters)2 Parameter (org.apache.struts2.dispatcher.Parameter)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)1 ActionProxyFactory (com.opensymphony.xwork2.ActionProxyFactory)1