Search in sources :

Example 36 with ValidationAware

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

the class ExpressionValidatorTest method testExpressionValidatorFailure.

public void testExpressionValidatorFailure() 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("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
    proxy.execute();
    assertTrue(((ValidationAware) proxy.getAction()).hasActionErrors());
    Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
    assertEquals(1, errors.size());
    String message = (String) errors.iterator().next();
    assertNotNull(message);
    assertEquals("Foo must be greater than Bar. Foo = 5, Bar = 7.", message);
}
Also used : HashMap(java.util.HashMap) Collection(java.util.Collection) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 37 with ValidationAware

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

the class IntRangeValidatorTest method testRangeValidation.

public void testRangeValidation() {
    HashMap<String, String> params = new HashMap<>();
    params.put("bar", "5");
    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> errorMessages = errors.get("bar");
        assertEquals(1, errorMessages.size());
        String errorMessage = errorMessages.get(0);
        assertNotNull(errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 38 with ValidationAware

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

the class LongRangeValidatorTest method testRangeValidation.

public void testRangeValidation() {
    HashMap<String, String> params = new HashMap<>();
    params.put("longFoo", "200");
    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 errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List errorMessages = (List) errors.get("longFoo");
        assertEquals(1, errorMessages.size());
        String errorMessage = (String) errorMessages.get(0);
        assertNotNull(errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) Map(java.util.Map) HashMap(java.util.HashMap)

Example 39 with ValidationAware

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

the class ValidatorAnnotationTest method testAnnotatedMethodSuccess.

public void testAnnotatedMethodSuccess() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    // make it not fail
    params.put("param1", "key1");
    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 40 with ValidationAware

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

the class ValidatorAnnotationTest method testAnnotatedMethodSuccess3.

public void testAnnotatedMethodSuccess3() throws Exception {
    HashMap<String, Object> params = new HashMap<>();
    // make it not fail
    params.put("param1", "key1");
    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)

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