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