Search in sources :

Example 6 with ConversionData

use of com.opensymphony.xwork2.conversion.impl.ConversionData 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)

Example 7 with ConversionData

use of com.opensymphony.xwork2.conversion.impl.ConversionData in project struts by apache.

the class StrutsConversionErrorInterceptorTest method testFieldErrorAdded.

public void testFieldErrorAdded() throws Exception {
    conversionErrors.put("foo", new ConversionData("bar", Integer.class));
    ActionSupport action = new ActionSupport();
    mockInvocation.expectAndReturn("getAction", action);
    stack.push(action);
    mockInvocation.matchAndReturn("getAction", action);
    assertNull(action.getFieldErrors().get("foo"));
    interceptor.doIntercept(invocation);
    assertTrue(action.hasFieldErrors());
    assertNotNull(action.getFieldErrors().get("foo"));
}
Also used : ActionSupport(com.opensymphony.xwork2.ActionSupport) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 8 with ConversionData

use of com.opensymphony.xwork2.conversion.impl.ConversionData in project struts by apache.

the class StrutsConversionErrorInterceptorTest method testEmptyValuesDoNotSetFieldErrors.

public void testEmptyValuesDoNotSetFieldErrors() throws Exception {
    conversionErrors.put("foo", new ConversionData("bar", Integer.class));
    conversionErrors.put("bar", new ConversionData("", Integer.class));
    conversionErrors.put("baz", new ConversionData(new String[] { "" }, Integer.class));
    ActionSupport action = new ActionSupport();
    mockInvocation.expectAndReturn("getAction", action);
    stack.push(action);
    mockInvocation.matchAndReturn("getAction", action);
    assertNull(action.getFieldErrors().get("foo"));
    assertNull(action.getFieldErrors().get("bar"));
    assertNull(action.getFieldErrors().get("baz"));
    interceptor.doIntercept(invocation);
    assertTrue(action.hasFieldErrors());
    assertNotNull(action.getFieldErrors().get("foo"));
    assertNull(action.getFieldErrors().get("bar"));
    assertNull(action.getFieldErrors().get("baz"));
}
Also used : ActionSupport(com.opensymphony.xwork2.ActionSupport) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 9 with ConversionData

use of com.opensymphony.xwork2.conversion.impl.ConversionData in project struts by apache.

the class ActionSupportTest method testFormattingSupportWithConversionError.

public void testFormattingSupportWithConversionError() {
    ActionContext.getContext().getConversionErrors().put("val", new ConversionData(new String[] { "4567def" }, Double.class));
    ActionContext.getContext().withLocale(new Locale("da"));
    MyActionSupport mas = new MyActionSupport();
    container.inject(mas);
    ActionContext.getContext().getValueStack().push(mas);
    mas.setVal(234d);
    String formatted = mas.getFormatted("format.number", "val");
    assertEquals("4567def", formatted);
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 10 with ConversionData

use of com.opensymphony.xwork2.conversion.impl.ConversionData in project struts by apache.

the class ConversionErrorInterceptor method doIntercept.

@Override
public String doIntercept(ActionInvocation invocation) throws Exception {
    ActionContext invocationContext = invocation.getInvocationContext();
    Map<String, ConversionData> conversionErrors = invocationContext.getConversionErrors();
    ValueStack stack = invocationContext.getValueStack();
    HashMap<Object, Object> fakie = null;
    for (Map.Entry<String, ConversionData> entry : conversionErrors.entrySet()) {
        String propertyName = entry.getKey();
        ConversionData conversionData = entry.getValue();
        if (shouldAddError(propertyName, conversionData.getValue())) {
            String message = XWorkConverter.getConversionErrorMessage(propertyName, conversionData.getToClass(), stack);
            Object action = invocation.getAction();
            if (action instanceof ValidationAware) {
                ValidationAware va = (ValidationAware) action;
                va.addFieldError(propertyName, message);
            }
            if (fakie == null) {
                fakie = new HashMap<>();
            }
            fakie.put(propertyName, getOverrideExpr(invocation, conversionData.getValue()));
        }
    }
    if (fakie != null) {
        // if there were some errors, put the original (fake) values in place right before the result
        stack.getContext().put(ORIGINAL_PROPERTY_OVERRIDE, fakie);
        invocation.addPreResultListener(new PreResultListener() {

            public void beforeResult(ActionInvocation invocation, String resultCode) {
                Map<Object, Object> fakie = (Map<Object, Object>) invocation.getInvocationContext().get(ORIGINAL_PROPERTY_OVERRIDE);
                if (fakie != null) {
                    invocation.getStack().setExprOverrides(fakie);
                }
            }
        });
    }
    return invocation.invoke();
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)14 ValueStack (com.opensymphony.xwork2.util.ValueStack)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)3 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)3 HashMap (java.util.HashMap)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)2 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)2 ValidationAwareSupport (com.opensymphony.xwork2.ValidationAwareSupport)1 PreResultListener (com.opensymphony.xwork2.interceptor.PreResultListener)1 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1