Search in sources :

Example 1 with ValidationAware

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

the class ParametersInterceptor method notifyDeveloperParameterException.

protected void notifyDeveloperParameterException(Object action, String property, String message) {
    String developerNotification = "Unexpected Exception caught setting '" + property + "' on '" + action.getClass() + ": " + message;
    if (action instanceof TextProvider) {
        TextProvider tp = (TextProvider) action;
        developerNotification = tp.getText("devmode.notification", "Developer Notification:\n{0}", new String[] { developerNotification });
    }
    LOG.error(developerNotification);
    if (action instanceof ValidationAware) {
        // see https://issues.apache.org/jira/browse/WW-4066
        Collection<String> messages = ((ValidationAware) action).getActionMessages();
        messages.add(message);
        ((ValidationAware) action).setActionMessages(messages);
    }
}
Also used : TextProvider(com.opensymphony.xwork2.TextProvider)

Example 2 with ValidationAware

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

the class ShortRangeValidatorTest method testRangeValidation.

public void testRangeValidation() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("shortFoo", "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("shortFoo");
        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 3 with ValidationAware

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

the class SimpleActionValidationTest method testMessageKey.

public void testMessageKey() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("foo", "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);
        ValueStack stack = ActionContext.getContext().getValueStack();
        stack.getActionContext().withLocale(Locale.US);
        proxy.execute();
        assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
        Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
        List<String> fooErrors = errors.get("foo");
        assertEquals(1, fooErrors.size());
        String errorMessage = fooErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("Foo Range Message", errorMessage);
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) StubValueStack(com.opensymphony.xwork2.StubValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 4 with ValidationAware

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

the class SimpleActionValidationTest method testParamterizedMessage.

public void testParamterizedMessage() {
    HashMap<String, Object> params = new HashMap<>();
    params.put("bar", "42");
    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> barErrors = errors.get("bar");
        assertEquals(1, barErrors.size());
        String errorMessage = barErrors.get(0);
        assertNotNull(errorMessage);
        assertEquals("bar must be between 6 and 10, current value is 42.", 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 5 with ValidationAware

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

the class ConversionErrorFieldValidatorTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, ConversionData> conversionErrors = new HashMap<>();
    conversionErrors.put("foo", new ConversionData("bar", Integer.class));
    ActionContext.of(stack.getContext()).withConversionErrors(conversionErrors).bind();
    validator = new ConversionErrorFieldValidator();
    validationAware = new ValidationAwareSupport();
    DelegatingValidatorContext validatorContext = new DelegatingValidatorContext(validationAware, container.getInstance(TextProviderFactory.class));
    stack.push(validatorContext);
    validator.setValidatorContext(validatorContext);
    validator.setFieldName("foo");
    validator.setValueStack(ActionContext.getContext().getValueStack());
    assertEquals(0, validationAware.getFieldErrors().size());
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValidationAwareSupport(com.opensymphony.xwork2.ValidationAwareSupport) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ConversionErrorFieldValidator(com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)

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