Search in sources :

Example 11 with ConversionData

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

the class VisitorFieldValidatorTest method testVisitorChildConversionValidation.

public void testVisitorChildConversionValidation() throws Exception {
    // add conversion error
    Map<String, ConversionData> conversionErrors = new HashMap<>();
    conversionErrors.put("bean.child.count", new ConversionData("bar", Integer.class));
    ActionContext.getContext().withConversionErrors(conversionErrors);
    validate("visitorChildValidation");
    assertTrue(action.hasFieldErrors());
    Map<String, List<String>> fieldErrors = action.getFieldErrors();
    assertEquals(6, fieldErrors.size());
    assertFalse(fieldErrors.containsKey("bean.count"));
    assertTrue(fieldErrors.containsKey("bean.name"));
    assertTrue(fieldErrors.containsKey("bean.birth"));
    assertTrue(fieldErrors.containsKey("bean.child.name"));
    assertTrue(fieldErrors.containsKey("bean.child.birth"));
    // the error from the action should be there too
    assertTrue(fieldErrors.containsKey("context"));
    // nested visitor conversion error
    assertTrue(fieldErrors.containsKey("bean.child.count"));
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Example 12 with ConversionData

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

the class RepopulateConversionErrorFieldValidatorSupport method repopulateField.

public void repopulateField(Object object) throws ValidationException {
    ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
    Map<String, ConversionData> conversionErrors = ActionContext.getContext().getConversionErrors();
    String fieldName = getFieldName();
    String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
    if (conversionErrors.containsKey(fullFieldName)) {
        Object value = conversionErrors.get(fullFieldName).getValue();
        final Map<Object, Object> fakeParams = new LinkedHashMap<Object, Object>();
        boolean doExprOverride = false;
        if (value instanceof String[]) {
            // take the first element, if possible
            String[] tmpValue = (String[]) value;
            if ((tmpValue.length > 0)) {
                doExprOverride = true;
                fakeParams.put(fullFieldName, escape(tmpValue[0]));
            } else {
                LOG.warn("value is an empty array of String or with first element in it as null [{}], will not repopulate conversion error", value);
            }
        } else if (value instanceof String) {
            String tmpValue = (String) value;
            doExprOverride = true;
            fakeParams.put(fullFieldName, escape(tmpValue));
        } else {
            // opps... it should be
            LOG.warn("conversion error value is not a String or array of String but instead is [{}], will not repopulate conversion error", value);
        }
        if (doExprOverride) {
            invocation.addPreResultListener(new PreResultListener() {

                public void beforeResult(ActionInvocation invocation, String resultCode) {
                    ValueStack stack = ActionContext.getContext().getValueStack();
                    stack.setExprOverrides(fakeParams);
                }
            });
        }
    }
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) PreResultListener(com.opensymphony.xwork2.interceptor.PreResultListener) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with ConversionData

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

the class ConversionErrorFieldValidator method doValidate.

/**
 * The validation implementation must guarantee that setValidatorContext will
 * be called with a non-null ValidatorContext before validate is called.
 *
 * @param object the object to be validated
 * @throws ValidationException in case of validation problems
 */
@Override
public void doValidate(Object object) throws ValidationException {
    String fieldName = getFieldName();
    String fullFieldName = getValidatorContext().getFullFieldName(fieldName);
    ActionContext context = ActionContext.getContext();
    Map<String, ConversionData> conversionErrors = context.getConversionErrors();
    if (conversionErrors.containsKey(fullFieldName)) {
        if (StringUtils.isBlank(defaultMessage)) {
            defaultMessage = XWorkConverter.getConversionErrorMessage(fullFieldName, conversionErrors.get(fullFieldName).getToClass(), context.getValueStack());
        }
        addFieldError(fieldName, object);
    }
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 14 with ConversionData

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

the class AnnotationXWorkConverterTest method testFieldErrorMessageAddedForComplexProperty.

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

Example 15 with ConversionData

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

the class ConversionErrorInterceptorTest method testFieldErrorAdded.

public void testFieldErrorAdded() throws Exception {
    conversionErrors.put("foo", new ConversionData(123L, int.class));
    SimpleAction action = new SimpleAction();
    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 : 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