Search in sources :

Example 26 with ValidationAware

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

the class BeanValidationInterceptorTest method testModelDrivenAction.

public void testModelDrivenAction() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName(null);
    action.getModel().setEmail(null);
    action.getModel().getAddress().setStreet(null);
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(3, fieldErrors.size());
    assertTrue(fieldErrors.get("name").size() > 0);
    assertEquals(fieldErrors.get("name").get(0), "nameNotNull");
    assertTrue(fieldErrors.get("email").size() > 0);
    assertEquals(fieldErrors.get("email").get(0), "emailNotNull");
    assertTrue(fieldErrors.get("address.street").size() > 0);
    assertEquals(fieldErrors.get("address.street").get(0), "streetNotNull");
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) ModelDrivenAction(org.apache.struts.beanvalidation.actions.ModelDrivenAction)

Example 27 with ValidationAware

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

the class BeanValidationInterceptorTest method testFieldMatchAction.

public void testFieldMatchAction() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "fieldMatchAction", null, null);
    FieldMatchAction action = (FieldMatchAction) baseActionProxy.getAction();
    action.setPassword("pass1");
    action.setConfirmPassword("pass2");
    action.setEmail("test1@mail.org");
    action.setConfirmEmail("test2@mail.org");
    baseActionProxy.execute();
    Collection<String> actionErrors = ((ValidationAware) baseActionProxy.getAction()).getActionErrors();
    System.out.println(actionErrors);
    assertNotNull(actionErrors);
    assertEquals(2, actionErrors.size());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) FieldMatchAction(org.apache.struts.beanvalidation.actions.FieldMatchAction)

Example 28 with ValidationAware

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

the class BeanValidationInterceptorTest method testModelDrivenActionSize.

public void testModelDrivenActionSize() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName("j");
    action.getModel().setEmail("jogep@apache.org");
    action.getModel().getAddress().setStreet("st");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    System.out.println(fieldErrors);
    assertNotNull(fieldErrors);
    assertEquals(2, fieldErrors.size());
    assertTrue(fieldErrors.get("name").size() > 0);
    assertEquals(fieldErrors.get("name").get(0), "nameSize");
    assertTrue(fieldErrors.get("address.street").size() > 0);
    assertEquals(fieldErrors.get("address.street").get(0), "streetSize");
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) ModelDrivenAction(org.apache.struts.beanvalidation.actions.ModelDrivenAction)

Example 29 with ValidationAware

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

the class BeanValidationInterceptorTest method testValidationGroupActionStandard.

public void testValidationGroupActionStandard() throws Exception {
    ActionProxy baseActionProxy = getValidateGroupAction("actionStandard");
    ValidateGroupAction action = (ValidateGroupAction) baseActionProxy.getAction();
    action.getModel().setName(null);
    action.getModel().setEmail(null);
    action.getModel().getAddress().setStreet(null);
    baseActionProxy.execute();
    assertEquals("every properties not valid", 3, ((ValidationAware) baseActionProxy.getAction()).getFieldErrors().size());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ValidateGroupAction(org.apache.struts.beanvalidation.actions.ValidateGroupAction) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 30 with ValidationAware

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

the class JSONValidationInterceptor method doIntercept.

@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
    HttpServletResponse response = ServletActionContext.getResponse();
    HttpServletRequest request = ServletActionContext.getRequest();
    Object action = invocation.getAction();
    if (isJsonEnabled(request)) {
        if (action instanceof ValidationAware) {
            // generate json
            ValidationAware validationAware = (ValidationAware) action;
            if (validationAware.hasErrors()) {
                return generateJSON(request, response, validationAware);
            }
        }
        if (isValidateOnly(request)) {
            // there were no errors
            setupEncoding(response, request);
            response.getWriter().print("{}");
            response.setContentType("application/json");
            return Action.NONE;
        } else {
            return invocation.invoke();
        }
    } else
        return invocation.invoke();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) 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