Search in sources :

Example 1 with ConversionData

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

the class ActionSupport method getFormatted.

/**
 * Dedicated method to support I10N and conversion errors
 *
 * @param key message which contains formatting string
 * @param expr that should be formatted
 * @return formatted expr with format specified by key
 */
public String getFormatted(String key, String expr) {
    Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
    if (conversionErrors.containsKey(expr)) {
        String[] vals = (String[]) conversionErrors.get(expr).getValue();
        return vals[0];
    } else {
        final ValueStack valueStack = ActionContext.getContext().getValueStack();
        final Object val = valueStack.findValue(expr);
        return getText(key, Arrays.asList(val));
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 2 with ConversionData

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

the class ConversionErrorInterceptorTest method testFieldErrorWithMapKeyAdded.

public void testFieldErrorWithMapKeyAdded() throws Exception {
    String fieldName = "foo['1'].intValue";
    conversionErrors.put(fieldName, new ConversionData("bar", int.class));
    ActionSupport action = new ActionSupport();
    mockInvocation.expectAndReturn("getAction", action);
    stack.push(action);
    mockInvocation.matchAndReturn("getAction", action);
    assertNull(action.getFieldErrors().get(fieldName));
    interceptor.doIntercept(invocation);
    // This fails!
    assertTrue(action.hasFieldErrors());
    assertNotNull(action.getFieldErrors().get(fieldName));
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 3 with ConversionData

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

the class AnnotationXWorkConverterTest method testFieldErrorMessageAddedWhenConversionFails.

public void testFieldErrorMessageAddedWhenConversionFails() {
    SimpleAnnotationAction action = new SimpleAnnotationAction();
    action.setDate(null);
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    String[] value = new String[] { "invalid date" };
    assertEquals("Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue(ognlStackContext, action, null, "date", value, Date.class));
    stack.pop();
    Map<String, ConversionData> conversionErrors = ActionContext.of(ognlStackContext).getConversionErrors();
    assertNotNull(conversionErrors);
    assertEquals(1, conversionErrors.size());
    assertNotNull(conversionErrors.get("date"));
    assertEquals(value, conversionErrors.get("date").getValue());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 4 with ConversionData

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

the class AnnotationXWorkConverterTest method testFieldErrorMessageAddedWhenConversionFailsOnModelDriven.

public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() {
    ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());
    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    String[] value = new String[] { "invalid date" };
    assertEquals("Conversion should have failed.", OgnlRuntime.NoConversionPossible, converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class));
    stack.pop();
    stack.pop();
    Map<String, ConversionData> conversionErrors = ActionContext.of(ognlStackContext).getConversionErrors();
    assertNotNull(conversionErrors);
    assertEquals(1, conversionErrors.size());
    assertNotNull(conversionErrors.get("birth"));
    assertEquals(value, conversionErrors.get("birth").getValue());
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 5 with ConversionData

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

the class RepopulateConversionErrorFieldValidatorSupportTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    ValueStack stack = ActionContext.getContext().getValueStack();
    MockActionInvocation invocation = new MockActionInvocation();
    invocation.setStack(stack);
    ActionContext.getContext().withActionInvocation(invocation);
    String[] conversionErrorValue = new String[] { "some value" };
    Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
    conversionErrors.put("someFieldName", new ConversionData(conversionErrorValue, Integer.class));
    conversionErrors.put("xxxsomeFieldName", new ConversionData(conversionErrorValue, Integer.class));
    TextProviderFactory tpf = container.getInstance(TextProviderFactory.class);
    action = container.inject(ActionSupport.class);
    validator1 = new InternalRepopulateConversionErrorFieldValidatorSupport();
    validator1.setFieldName("someFieldName");
    validator1.setValidatorContext(new DelegatingValidatorContext(action, tpf));
    validator2 = new InternalRepopulateConversionErrorFieldValidatorSupport();
    validator2.setFieldName("someFieldName");
    validator2.setValidatorContext(new DelegatingValidatorContext(action, tpf) {

        @Override
        public String getFullFieldName(String fieldName) {
            return "xxx" + fieldName;
        }
    });
}
Also used : TextProviderFactory(com.opensymphony.xwork2.TextProviderFactory) ValueStack(com.opensymphony.xwork2.util.ValueStack) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

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