Commits

Anonymous committed c2fddd4

- added unit test to verify that you can set indexed values

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

  • Participants
  • Parent commits f640ed6

Comments (0)

Files changed (1)

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

 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 
-import java.util.Calendar;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
 
 
 /**
         }
     }
 
+    public void testCanSetDependentObjectArray() {
+        EmailAction action = new EmailAction();
+        Map context = Ognl.createDefaultContext(action);
+
+        HashMap props = new HashMap();
+        props.put("email[0].address", "addr1");
+        props.put("email[1].address", "addr2");
+        props.put("email[2].address", "addr3");
+
+        OgnlUtil.setProperties(props, action, context);
+        assertEquals(3, action.email.size());
+        assertEquals("addr1", action.email.get(0).toString());
+        assertEquals("addr2", action.email.get(1).toString());
+        assertEquals("addr3", action.email.get(2).toString());
+    }
+
     public void testCopySameType() {
         Foo foo1 = new Foo();
         Foo foo2 = new Foo();
         OgnlUtil.setProperties(props, foo, context);
         assertEquals(123, foo.getaLong());
     }
+
+    //~ Inner Classes //////////////////////////////////////////////////////////
+
+    public static class Email {
+        String address;
+
+        public void setAddress(String address) {
+            this.address = address;
+        }
+
+        public String toString() {
+            return address;
+        }
+    }
+
+    class EmailAction {
+        public List email = new OgnlList(Email.class);
+
+        public List getEmail() {
+            return this.email;
+        }
+    }
+
+    class OgnlList extends ArrayList {
+        private Class clazz;
+
+        public OgnlList(Class clazz) {
+            this.clazz = clazz;
+        }
+
+        public synchronized Object get(int index) {
+            while (index >= this.size()) {
+                try {
+                    this.add(clazz.newInstance());
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
+            return super.get(index);
+        }
+    }
 }