Search in sources :

Example 6 with ModelDrivenAction

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

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

the class ValidateAction method testModelDrivenParameters.

public void testModelDrivenParameters() throws Exception {
    Map<String, Object> params = new HashMap<>();
    final String fooVal = "com.opensymphony.xwork2.interceptor.ParametersInterceptorTest.foo";
    params.put("foo", fooVal);
    final String nameVal = "com.opensymphony.xwork2.interceptor.ParametersInterceptorTest.name";
    params.put("name", nameVal);
    params.put("count", "15");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, null, extraContext);
    assertEquals(Action.SUCCESS, proxy.execute());
    ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
    TestBean model = (TestBean) action.getModel();
    assertEquals(nameVal, model.getName());
    assertEquals(15, model.getCount());
    assertEquals(fooVal, action.getFoo());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TestBean(com.opensymphony.xwork2.TestBean) ModelDrivenAction(com.opensymphony.xwork2.ModelDrivenAction)

Example 8 with ModelDrivenAction

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

the class OValValidationInterceptorTest method testModelDrivenAction.

public void testModelDrivenAction() throws Exception {
    ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "modelDrivenAction", null, null);
    ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction();
    action.getModel().setName(null);
    action.getModel().setEmail(null);
    action.getModel().getAddress().setStreet("short");
    baseActionProxy.execute();
    Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors();
    assertNotNull(fieldErrors);
    assertEquals(3, fieldErrors.size());
    assertValue(fieldErrors, "person.name", Collections.singletonList("person.name cannot be null"));
    assertValue(fieldErrors, "person.email", Collections.singletonList("person.email cannot be null"));
    assertValue(fieldErrors, "person.address.street", Collections.singletonList("person.address.street cannot be shorter than 7 characters"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

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