1. opensymphony
  2. xwork

Commits

plightbo  committed a2b4bf6

random little bugs

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

  • Participants
  • Parent commits 452cd56
  • Branches master

Comments (0)

Files changed (4)

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

View file
 
             Class clazz = o.getClass();
             Class[] argTypes = getArgTypes(objects);
-            CompoundRootAccessor.MethodCall mc = new CompoundRootAccessor.MethodCall(clazz, name, argTypes);
-            if (!invalidMethods.containsKey(mc)) {
+
+            CompoundRootAccessor.MethodCall mc = null;
+            if (argTypes != null) {
+                mc = new CompoundRootAccessor.MethodCall(clazz, name, argTypes);
+            }
+
+            if (argTypes == null || !invalidMethods.containsKey(mc)) {
                 try {
                     Object value = OgnlRuntime.callMethod((OgnlContext) context, o, name, name, objects);
 
                     }
                 } catch (OgnlException e) {
                     // try the next one
-                    invalidMethods.put(mc, Boolean.TRUE);
+                    Throwable reason = e.getReason();
+                    if (mc != null && reason != null && reason.getClass() == NoSuchMethodException.class) {
+                        invalidMethods.put(mc, Boolean.TRUE);
+                    }
                 }
             }
         }

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

View file
             } catch (Exception e) {
                 handleConversionException(context, property, value, target);
 
-                return null;
+                return acceptableErrorValue(toClass);
             }
         }
 
             } catch (Exception e) {
                 handleConversionException(context, property, value, target);
 
-                return null;
+                return acceptableErrorValue(toClass);
             }
         } else {
             try {
             } catch (Exception e) {
                 handleConversionException(context, property, value, target);
 
-                return null;
+                return acceptableErrorValue(toClass);
             }
         }
     }
 
+    private Object acceptableErrorValue(Class toClass) {
+        if (!toClass.isPrimitive()) {
+            return null;
+        }
+
+        if (toClass == int.class) {
+            return new Integer(0);
+        } else if (toClass == double.class) {
+            return new Double(0);
+        } else if (toClass == long.class) {
+            return new Long(0);
+        } else if (toClass == boolean.class) {
+            return Boolean.FALSE;
+        } else if (toClass == short.class) {
+            return new Short((short) 0);
+        } else if (toClass == float.class) {
+            return new Float(0);
+        } else if (toClass == byte.class) {
+            return new Byte((byte) 0);
+        } else if (toClass == char.class) {
+            return new Character((char) 0);
+        }
+
+        return null;
+    }
+
     private Map buildConverterMapping(Class clazz) throws Exception {
         Map mapping = new HashMap();
 

File src/test/com/opensymphony/xwork/SimpleAction.java

View file
     //~ Static fields/initializers /////////////////////////////////////////////
 
     public static final String COMMAND_RETURN_CODE = "com.opensymphony.xwork.SimpleAction.CommandInvoked";
+    private boolean throwException;
 
     public SimpleAction() {
         System.out.println("hi");
 
         return SUCCESS;
     }
+
+    public void setThrowException(boolean throwException) {
+        this.throwException = throwException;
+    }
+
+    public String exceptionMethod() throws Exception {
+        if (throwException) {
+            throw new Exception("We're supposed to throw this");
+        }
+
+        return "OK";
+    }
 }

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

View file
 import java.util.HashMap;
 import java.util.Map;
 
+import com.opensymphony.xwork.SimpleAction;
+
 
 /**
  *
         assertEquals("Rover", vs.findValue("name", String.class));
     }
 
+    public void testPrimitiveSettingWithInvalidValueAddsFieldError() {
+        SimpleAction action = new SimpleAction();
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.getContext().put(XWorkConverter.REPORT_CONVERSION_ERRORS,Boolean.TRUE);
+        stack.push(action);
+        stack.setValue("bar","3x");
+
+        assertTrue(action.getFieldErrors().containsKey("bar"));
+        assertEquals(0, action.getBar());
+    }
+
+    public void testCallMethodThatThrowsExceptionTwice() {
+        SimpleAction action = new SimpleAction();
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.push(action);
+
+        action.setThrowException(true);
+        assertNull(stack.findValue("exceptionMethod1()"));
+        action.setThrowException(false);
+        assertEquals("OK", stack.findValue("exceptionMethod()"));
+    }
+
+    public void testCallMethodWithNullArg() {
+        SimpleAction action = new SimpleAction();
+        OgnlValueStack stack = new OgnlValueStack();
+        stack.push(action);
+
+        stack.findValue("setName(blah)");
+        assertNull(action.getName());
+
+        action.setBlah("blah");
+        stack.findValue("setName(blah)");
+        assertEquals("blah", action.getName());
+    }
+
 }