Search in sources :

Example 41 with ValidationAware

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

the class ValidatorAnnotationTest method testNotAnnotatedMethodSuccess2.

public void testNotAnnotatedMethodSuccess2() throws Exception {
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create().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)

Example 42 with ValidationAware

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

the class SimpleActionValidationTest method testSubPropertiesAreValidated.

public void testSubPropertiesAreValidated() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("baz", "10");
    // valid values
    params.put("foo", "8");
    params.put("bar", "7");
    params.put("date", "12/23/2002");
    params.put("bean.name", "Name should be valid");
    // this should cause a message
    params.put("bean.count", "100");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, null, extraContext);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> beanCountErrors = errors.get("bean.count");
        assertEquals(1, beanCountErrors.size());
        String errorMessage = beanCountErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("bean.count out of range.", 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 43 with ValidationAware

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

the class SimpleActionValidationTest method testAliasValidation.

public void testAliasValidation() {
    Map<String, Object> params = new HashMap<>();
    params.put("baz", "10");
    // valid values
    params.put("bar", "7");
    params.put("date", "12/23/2002");
    params.put("percentage", "1.23456789");
    Map<String, Object> extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
    try {
        ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ACTION_NAME, null, extraContext);
        proxy.execute();
        ValidationAware validationAware = (ValidationAware) proxy.getAction();
        assertFalse(validationAware.hasFieldErrors());
        params.put("bar", "42");
        extraContext = ActionContext.of(new HashMap<>()).withParameters(HttpParameters.create(params).build()).bind().getContextMap();
        proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_ALIAS_NAME, null, extraContext);
        proxy.execute();
        validationAware = (ValidationAware) proxy.getAction();
        assertTrue(validationAware.hasFieldErrors());
        Map<String, List<String>> errors = validationAware.getFieldErrors();
        assertTrue(errors.containsKey("baz"));
        List<String> bazErrors = errors.get("baz");
        assertEquals(1, bazErrors.size());
        String message = bazErrors.get(0);
        assertEquals("baz out of range.", message);
        assertTrue(errors.containsKey("bar"));
        List<String> barErrors = errors.get("bar");
        assertEquals(1, barErrors.size());
        message = barErrors.get(0);
        assertEquals("bar must be between 6 and 10, current value is 42.", message);
    } 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 44 with ValidationAware

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

the class SimpleActionValidationTest method testLookingUpFieldNameAsTextKey.

public void testLookingUpFieldNameAsTextKey() {
    HashMap<String, Object> params = new HashMap<>();
    // should cause a message
    params.put("baz", "-1");
    // valid values
    params.put("bar", "7");
    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> bazErrors = errors.get("baz");
        assertEquals(1, bazErrors.size());
        String errorMessage = bazErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("Baz Field must be greater than 0", 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 45 with ValidationAware

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

the class RestActionInvocation method selectTarget.

@SuppressWarnings("unchecked")
protected void selectTarget() {
    // Select target (content to return)
    Throwable e = (Throwable) stack.findValue("exception");
    if (e != null) {
        // Exception
        target = e;
        hasErrors = true;
    } else if (action instanceof ValidationAware && ((ValidationAware) action).hasErrors()) {
        // Error messages
        ValidationAware validationAwareAction = ((ValidationAware) action);
        Map errors = new HashMap();
        if (validationAwareAction.getActionErrors().size() > 0) {
            errors.put("actionErrors", validationAwareAction.getActionErrors());
        }
        if (validationAwareAction.getFieldErrors().size() > 0) {
            errors.put("fieldErrors", validationAwareAction.getFieldErrors());
        }
        target = errors;
        hasErrors = true;
    } else if (action instanceof ModelDriven) {
        // Model
        target = ((ModelDriven) action).getModel();
    } else {
        target = action;
    }
    if (shouldRestrictToGET()) {
        target = null;
    }
}
Also used : HashMap(java.util.HashMap) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware) HashMap(java.util.HashMap) Map(java.util.Map)

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