Commits

Anonymous committed e853b96

resolves XW-121 as well as some small cleanup

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@114e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 79ca7c2

Comments (0)

Files changed (4)

File src/java/com/opensymphony/xwork/interceptor/ParametersInterceptor.java

 import com.opensymphony.xwork.ActionInvocation;
 import com.opensymphony.xwork.util.InstantiatingNullHandler;
 import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.util.XWorkConverter;
 
 import java.util.Iterator;
 import java.util.Map;
 
         try {
             InstantiatingNullHandler.setState(true);
-            invocation.getInvocationContext().put("report.conversion.errors", Boolean.TRUE);
+            invocation.getInvocationContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
 
             if (parameters != null) {
                 final OgnlValueStack stack = ActionContext.getContext().getValueStack();
                 }
             }
         } finally {
-            invocation.getInvocationContext().put("report.conversion.errors", Boolean.FALSE);
+            invocation.getInvocationContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.FALSE);
             InstantiatingNullHandler.setState(false);
         }
     }

File src/java/com/opensymphony/xwork/util/OgnlValueStack.java

 
     public void setValue(String expr, Object value) {
         try {
+            getContext().put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, expr);
             Ognl.setValue(OgnlUtil.compile(expr), context, root, value);
         } catch (OgnlException e) {
             // ignore
+        } finally {
+            getContext().remove(XWorkConverter.CONVERSION_PROPERTY_FULLNAME);
         }
     }
 

File src/java/com/opensymphony/xwork/util/XWorkConverter.java

  */
 package com.opensymphony.xwork.util;
 
-import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.util.FileManager;
+import com.opensymphony.xwork.ActionContext;
 
 import ognl.*;
 
     private static XWorkConverter instance;
     private static final Log LOG = LogFactory.getLog(XWorkConverter.class);
 
+    public static final String REPORT_CONVERSION_ERRORS = "report.conversion.errors";
+    public static final String CONVERSION_PROPERTY_FULLNAME = "conversion.property.fullName";
+
     //~ Instance fields ////////////////////////////////////////////////////////
 
     HashMap defaultMappings = new HashMap();
             try {
                 return tc.convertValue(context, target, member, property, value, toClass);
             } catch (Exception e) {
-                handleConversionException(property, value, target);
+                handleConversionException(context, property, value, target);
 
                 return null;
             }
             try {
                 return defaultTypeConverter.convertValue(context, target, member, property, value, toClass);
             } catch (Exception e) {
-                handleConversionException(property, value, target);
+                handleConversionException(context, property, value, target);
 
                 return null;
             }
             try {
                 return super.convertValue(context, target, member, property, value, toClass);
             } catch (Exception e) {
-                handleConversionException(property, value, target);
+                handleConversionException(context, property, value, target);
 
                 return null;
             }
         defaultMappings.put(className, converter);
     }
 
-    protected void handleConversionException(String property, Object value, Object object) {
-        if (ActionContext.getContext().get("report.conversion.errors") == Boolean.TRUE) {
-            String defaultMessage = "Invalid field value for field \"" + property + "\".";
-            OgnlValueStack stack = ActionContext.getContext().getValueStack();
+    protected void handleConversionException(Map context, String property, Object value, Object object) {
+        if (context.get(REPORT_CONVERSION_ERRORS) == Boolean.TRUE) {
+            String realProperty = property;
+            String fullName = (String) context.get(CONVERSION_PROPERTY_FULLNAME);
+            if (fullName != null) {
+                realProperty = fullName;
+            }
+
+            String defaultMessage = "Invalid field value for field \"" + realProperty + "\".";
+            OgnlValueStack stack = (OgnlValueStack) context.get(ActionContext.VALUE_STACK);
 
-            String getTextExpression = "getText('invalid.fieldvalue." + property + "','" + defaultMessage + "')";
+            String getTextExpression = "getText('invalid.fieldvalue." + realProperty + "','" + defaultMessage + "')";
             String message = (String) stack.findValue(getTextExpression);
 
             if (message == null) {
                 message = defaultMessage;
             }
 
-            String addFieldErrorExpression = "addFieldError('" + property + "','" + message + "')";
+            String addFieldErrorExpression = "addFieldError('" + realProperty + "','" + message + "')";
             stack.findValue(addFieldErrorExpression);
         }
     }

File src/test/com/opensymphony/xwork/util/XWorkConverterTest.java

 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.ModelDrivenAction;
 import com.opensymphony.xwork.SimpleAction;
+import com.opensymphony.xwork.TestBean;
 import com.opensymphony.xwork.config.ConfigurationManager;
 
 import junit.framework.TestCase;
 
         OgnlValueStack stack = new OgnlValueStack();
         stack.push(action);
-        ActionContext.setContext(new ActionContext(stack.getContext()));
-        ActionContext.getContext().put("report.conversion.errors", Boolean.TRUE);
+        Map context = stack.getContext();
+        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
         assertEquals("Conversion should have failed.",
                 null,
                 converter.convertValue(context, action, null, "date", new String[]{
         assertEquals("Invalid field value for field \"date\".", ((List) action.getFieldErrors().get("date")).get(0));
     }
 
+    public void testFieldErrorMessageAddedForComplexProperty() {
+        SimpleAction action = new SimpleAction();
+        action.setBean(new TestBean());
+
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.push(action);
+        Map context = stack.getContext();
+        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
+        context.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth");
+        assertEquals("Conversion should have failed.",
+                null,
+                converter.convertValue(context, action.getBean(), null, "birth", new String[]{
+                    "invalid date"
+                }, Date.class));
+        stack.pop();
+        assertTrue(action.hasFieldErrors());
+        assertNotNull(action.getFieldErrors().get("bean.birth"));
+        assertEquals("Invalid field value for field \"bean.birth\".", ((List) action.getFieldErrors().get("bean.birth")).get(0));
+    }
+
     public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() {
         ModelDrivenAction action = new ModelDrivenAction();
         OgnlValueStack stack = new OgnlValueStack();
         stack.push(action);
         stack.push(action.getModel());
-        ActionContext.setContext(new ActionContext(stack.getContext()));
-        ActionContext.getContext().put("report.conversion.errors", Boolean.TRUE);
+        Map context = stack.getContext();
+        context.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
         assertEquals("Conversion should have failed.",
                 null,
                 converter.convertValue(context, action, null, "birth", new String[]{