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"));
}
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);
}
});
}
}
}
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);
}
}
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());
}
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"));
}
Aggregations