Search in sources :

Example 1 with ModelDrivenAction

use of com.opensymphony.xwork2.ModelDrivenAction in project struts by apache.

the class ModelDrivenValidationTest method testModelDrivenValidation.

public void testModelDrivenValidation() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("count", new String[] { "11" });
    Map<String, Object> context = new HashMap<>();
    context.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(provider);
    loadConfigurationProviders(provider);
    ActionProxy proxy = actionProxyFactory.createActionProxy(null, "TestModelDrivenValidation", null, context);
    assertEquals(Action.SUCCESS, proxy.execute());
    ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
    assertTrue(action.hasFieldErrors());
    assertTrue(action.getFieldErrors().containsKey("count"));
    assertEquals("count must be between 1 and 10, current value is 11.", ((List) action.getFieldErrors().get("count")).get(0));
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction)

Example 2 with ModelDrivenAction

use of com.opensymphony.xwork2.ModelDrivenAction in project struts by apache.

the class BeanValidationInterceptorTest method testModelDrivenActionEmailField.

public void testModelDrivenActionEmailField() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName("name");
    action.getModel().setEmail("notamail");
    action.getModel().getAddress().setStreet("street");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(1, fieldErrors.size());
    assertTrue(fieldErrors.get("email").size() > 0);
    assertEquals(fieldErrors.get("email").get(0), "emailNotValid");
}
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 3 with ModelDrivenAction

use of com.opensymphony.xwork2.ModelDrivenAction in project struts by apache.

the class BeanValidationInterceptorTest method testModelDrivenActionSkipValidationByInterface.

public void testModelDrivenActionSkipValidationByInterface() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenActionSkipValidationByInterface", 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(0, fieldErrors.size());
}
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 4 with ModelDrivenAction

use of com.opensymphony.xwork2.ModelDrivenAction in project struts by apache.

the class BeanValidationInterceptorTest method testModelDrivenActionSuccess.

public void testModelDrivenActionSuccess() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName("name");
    action.getModel().setEmail("jogep@apache.org");
    action.getModel().getAddress().setStreet("street");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(0, fieldErrors.size());
}
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 5 with ModelDrivenAction

use of com.opensymphony.xwork2.ModelDrivenAction 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)

Aggregations

ActionProxy (com.opensymphony.xwork2.ActionProxy)8 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)6 List (java.util.List)6 ModelDrivenAction (org.apache.struts.beanvalidation.actions.ModelDrivenAction)5 ModelDrivenAction (com.opensymphony.xwork2.ModelDrivenAction)2 HashMap (java.util.HashMap)2 TestBean (com.opensymphony.xwork2.TestBean)1 XmlConfigurationProvider (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider)1 LinkedHashMap (java.util.LinkedHashMap)1 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)1