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