Commits

plightbo  committed a09b856

resolved various bugs relating to type conversion and exception handling

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

  • Participants
  • Parent commits 4629a84

Comments (0)

Files changed (6)

File java/com/opensymphony/xwork/XWorkBasicConverter.java

 
         if (toType == String.class) {
             result = doConvertToString(context, value);
+        } else if (toType == boolean.class) {
+            result = doConvertToBoolean(context, value);
+        } else if (toType == Boolean.class) {
+            result = doConvertToBoolean(context, value);
+        } else if (toType == boolean[].class) {
+            result = doConvertToBoolArray(context, value);
+        } else if (toType == Boolean[].class) {
+            result = doConvertToBooleanArray(context, value);
         } else if (toType == int[].class) {
             result = doConvertToIntArray(context, value);
         } else if (toType == Integer[].class) {
             result = doConvertToDate(context, value);
         } else if (toType == List.class) {
             result = doConvertToList(context, value);
+        } else if (toType == Set.class) {
+            result = doConvertToSet(context, value);
         } else if (toType == Collection.class) {
             result = doConvertToList(context, value);
         } else if (toType == Class.class) {
         return result;
     }
 
+    private Object doConvertToBoolArray(Map context, Object value) {
+        boolean[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            boolean[] ba = new boolean[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                ba[i] = ((Boolean) doConvertToBoolean(context, sa[i])).booleanValue();
+            }
+
+            result = ba;
+        }
+
+        return result;
+    }
+
+    private Object doConvertToBoolean(Map context, Object value) {
+        if (value instanceof String) {
+            String bStr = (String) value;
+
+            return Boolean.valueOf(bStr);
+        }
+
+        return null;
+    }
+
+    private Object doConvertToBooleanArray(Map context, Object value) {
+        Boolean[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            Boolean[] ba = new Boolean[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                ba[i] = ((Boolean) doConvertToBoolean(context, sa[i]));
+            }
+
+            result = ba;
+        }
+
+        return result;
+    }
+
     private Class doConvertToClass(Map context, Object value) {
         Class clazz = null;
 
         return result;
     }
 
+    private Object doConvertToSet(Map context, Object value) {
+        Set result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            result = new HashSet(sa.length);
+
+            for (int i = 0; i < sa.length; i++) {
+                String s = sa[i];
+                result.add(s);
+            }
+        }
+
+        return result;
+    }
+
     private String doConvertToString(Map context, Object value) {
         String result = null;
 
             }
 
             result = TextUtils.join(", ", intArray);
+        } else if (value instanceof double[]) {
+            double[] x = (double[]) value;
+            List intArray = new ArrayList(x.length);
+
+            for (int i = 0; i < x.length; i++) {
+                intArray.add(new Double(x[i]));
+            }
+
+            result = TextUtils.join(", ", intArray);
+        } else if (value instanceof boolean[]) {
+            boolean[] x = (boolean[]) value;
+            List intArray = new ArrayList(x.length);
+
+            for (int i = 0; i < x.length; i++) {
+                intArray.add(new Boolean(x[i]));
+            }
+
+            result = TextUtils.join(", ", intArray);
         } else if (value instanceof Date) {
             SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
             result = sdf.format(value);

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

 
             try {
                 if (OgnlRuntime.hasSetProperty(ognlContext, o, name)) {
-                    OgnlUtil.setProperty((String) name, value, o, context);
+                    OgnlRuntime.setProperty(ognlContext, o, name, value);
+
+                    return;
+                }
+            } catch (OgnlException e) {
+                if (e.getReason() != null) {
+                    throw new RuntimeException(e.getReason());
                 }
             } catch (IntrospectionException e) {
-                e.printStackTrace();
             }
         }
     }
 
             return root.cutStack(index.intValue());
         } else if (name instanceof String) {
+            if ("that".equals(name)) {
+                if (root.size() > 0) {
+                    return root.get(0);
+                } else {
+                    return null;
+                }
+            }
+
             for (Iterator iterator = root.iterator(); iterator.hasNext();) {
                 Object o = iterator.next();
 
                 try {
-                    Object value = OgnlRuntime.getProperty(ognlContext, o, name);
+                    if (OgnlRuntime.hasGetProperty(ognlContext, o, name)) {
+                        Object value = OgnlRuntime.getProperty(ognlContext, o, name);
 
-                    //Ognl.getValue(OgnlUtil.compile((String) name), context, o);
-                    if (value != null) {
-                        ognlContext.pushEvaluation(new Evaluation(ognlContext.getCurrentEvaluation().getNode(), o));
+                        //Ognl.getValue(OgnlUtil.compile((String) name), context, o);
+                        if (value != null) {
+                            ognlContext.pushEvaluation(new Evaluation(ognlContext.getCurrentEvaluation().getNode(), o));
 
-                        return value;
+                            return value;
+                        }
                     }
                 } catch (OgnlException e) {
-                    // try the next one
+                    if (e.getReason() != null) {
+                        throw new RuntimeException(e.getReason());
+                    }
+                } catch (IntrospectionException e) {
                 }
             }
 

File java/xwork-default-conversion.properties

 java.lang.String     =com.opensymphony.xwork.XWorkBasicConverter
+[Z                   =com.opensymphony.xwork.XWorkBasicConverter
+boolean              =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Boolean    =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.Boolean; =com.opensymphony.xwork.XWorkBasicConverter
 [D                   =com.opensymphony.xwork.XWorkBasicConverter
 double               =com.opensymphony.xwork.XWorkBasicConverter
 java.lang.Double     =com.opensymphony.xwork.XWorkBasicConverter
 java.util.Date       =com.opensymphony.xwork.XWorkBasicConverter
 java.util.Collection =com.opensymphony.xwork.XWorkBasicConverter
 java.util.List       =com.opensymphony.xwork.XWorkBasicConverter
+java.util.Set        =com.opensymphony.xwork.XWorkBasicConverter
 java.lang.Class      =com.opensymphony.xwork.XWorkBasicConverter
 [Ljava.lang.String;  =com.opensymphony.xwork.XWorkBasicConverter
 

File test/com/opensymphony/xwork/util/Dog.java

     Cat hates;
     String name;
     int[] childAges;
+    boolean male;
     int age;
 
     //~ Methods ////////////////////////////////////////////////////////////////
         return childAges;
     }
 
+    public void setException(String blah) throws Exception {
+        throw new Exception("This is expected");
+    }
+
+    public String getException() throws Exception {
+        throw new Exception("This is expected");
+    }
+
     public void setHates(Cat hates) {
         this.hates = hates;
     }
         return hates;
     }
 
+    public void setMale(boolean male) {
+        this.male = male;
+    }
+
+    public boolean isMale() {
+        return male;
+    }
+
     public void setName(String name) {
         this.name = name;
     }

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

         assertEquals("Smokey", vs.findValue("hates.name", String.class));
     }
 
+    public void testExceptionThrown() {
+        OgnlValueStack vs = new OgnlValueStack();
+
+        Dog dog1 = new Dog();
+        dog1.setAge(12);
+        dog1.setName("Rover");
+
+        vs.push(dog1);
+
+        try {
+            vs.findValue("exception");
+            fail("An exception was expected");
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
+        try {
+            vs.setValue("exception", "blah");
+            fail("An exception was expected");
+        } catch (Exception e) {
+            System.out.println(e.getMessage());
+        }
+
+        try {
+            vs.findValue("this");
+        } catch (Exception e) {
+            fail("an exception was not expected");
+        }
+    }
+
     public void testFooBarAsString() {
         OgnlValueStack vs = new OgnlValueStack();
         Foo foo = new Foo();
         assertEquals("Rover", vs.findValue("[1].name"));
     }
 
+    public void testSettingDogGender() {
+        OgnlValueStack vs = new OgnlValueStack();
+
+        Dog dog = new Dog();
+        vs.push(dog);
+
+        vs.setValue("male", "false");
+
+        assertEquals(false, dog.isMale());
+    }
+
     public void testStatics() {
         OgnlValueStack vs = new OgnlValueStack();
 
         assertNull(vs.findValue("@com.nothing.here.Nothing@BLAH"));
     }
 
+    public void testThat() {
+        OgnlValueStack vs = new OgnlValueStack();
+
+        Dog dog1 = new Dog();
+        dog1.setAge(12);
+        dog1.setName("Rover");
+
+        Dog dog2 = new Dog();
+        dog2.setAge(1);
+        dog2.setName("Jack");
+        vs.push(dog1);
+        vs.push(dog2);
+
+        assertEquals(dog2, vs.findValue("that"));
+        assertEquals("Jack", vs.findValue("that.name"));
+    }
+
+    public void testThatNull() {
+        OgnlValueStack vs = new OgnlValueStack();
+
+        assertNull(vs.findValue("that"));
+    }
+
     public void testTwoDogs() {
         OgnlValueStack vs = new OgnlValueStack();
 

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

 import ognl.Ognl;
 import ognl.OgnlException;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 
 /**
             }, Double[].class);
         assertNotNull(doubles);
         assertTrue(Arrays.equals(new Double[] {new Double(123), new Double(456)}, doubles));
+
+        Boolean[] booleans = (Boolean[]) converter.convertValue(context, null, null, null, new String[] {
+                "true", "false"
+            }, Boolean[].class);
+        assertNotNull(booleans);
+        assertTrue(Arrays.equals(new Boolean[] {Boolean.TRUE, Boolean.FALSE}, booleans));
     }
 
     public void testStringArrayToPrimitives() throws OgnlException {
             }, double[].class);
         assertNotNull(doubles);
         assertTrue(Arrays.equals(new double[] {123, 456}, doubles));
+
+        boolean[] booleans = (boolean[]) converter.convertValue(context, null, null, null, new String[] {
+                "true", "false"
+            }, boolean[].class);
+        assertNotNull(booleans);
+        assertTrue(Arrays.equals(new boolean[] {true, false}, booleans));
+    }
+
+    public void testStringArrayToSet() {
+        HashSet list = new HashSet();
+        list.add("foo");
+        list.add("bar");
+        list.add("baz");
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
+                    "foo", "bar", "bar", "baz"
+                }, Set.class));
     }
 
     public void testStringToPrimitiveWrappers() {
         assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", Long.class));
         assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", Integer.class));
         assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", Double.class));
+        assertEquals(new Boolean(false), converter.convertValue(context, null, null, null, "false", Boolean.class));
+        assertEquals(new Boolean(true), converter.convertValue(context, null, null, null, "true", Boolean.class));
     }
 
     public void testStringToPrimitives() {
         assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", long.class));
         assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", int.class));
         assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", double.class));
+        assertEquals(new Boolean(false), converter.convertValue(context, null, null, null, "false", boolean.class));
+        assertEquals(new Boolean(true), converter.convertValue(context, null, null, null, "true", boolean.class));
     }
 
     protected void setUp() throws Exception {