commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r552384 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/PropertyUtilsBean.java test/org/apache/commons/beanutils/PropertyUtilsTestCase.java test/org/apache/commons/beanutils/TestBean.java
Date Mon, 02 Jul 2007 02:22:57 GMT
Author: niallp
Date: Sun Jul  1 19:22:57 2007
New Revision: 552384

URL: http://svn.apache.org/viewvc?view=rev&rev=552384
Log:
BEANUTILS-247 - Support Arrays with multiple dimension

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java?view=diff&rev=552384&r1=552383&r2=552384
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/PropertyUtilsBean.java
Sun Jul  1 19:22:57 2007
@@ -26,6 +26,7 @@
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -429,6 +430,13 @@
         if (bean == null) {
             throw new IllegalArgumentException("No bean specified");
         }
+        if (name == null || name.length() == 0) {
+            if (bean.getClass().isArray()) {
+                return Array.get(bean, index);
+            } else if (bean instanceof List) {
+                return ((List)bean).get(index);   
+            }
+        }
         if (name == null) {
             throw new IllegalArgumentException("No name specified for bean class '" +
                     bean.getClass() + "'");
@@ -1499,6 +1507,15 @@
 
         if (bean == null) {
             throw new IllegalArgumentException("No bean specified");
+        }
+        if (name == null || name.length() == 0) {
+            if (bean.getClass().isArray()) {
+                Array.set(bean, index, value);
+                return;
+            } else if (bean instanceof List) {
+                ((List)bean).set(index, value);   
+                return;
+            }
         }
         if (name == null) {
             throw new IllegalArgumentException("No name specified for bean class '" +

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java?view=diff&rev=552384&r1=552383&r2=552384
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/PropertyUtilsTestCase.java
Sun Jul  1 19:22:57 2007
@@ -22,6 +22,8 @@
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -1003,6 +1005,59 @@
             fail("Threw " + t + " instead of ArrayIndexOutOfBoundsException");
         }
 
+    }
+
+
+    /**
+     * Test getting an indexed value out of a multi-dimensional array
+     */
+    public void testGetIndexedArray() {
+        String[] firstArray = new String[] {"FIRST-1", "FIRST-2", "FIRST-3"};
+        String[] secondArray = new String[] {"SECOND-1", "SECOND-2", "SECOND-3",  "SECOND-4"};
+        String[][] mainArray = {firstArray, secondArray};
+        TestBean bean = new TestBean(mainArray);
+        try {
+            assertEquals("firstArray[0]", firstArray[0],  PropertyUtils.getProperty(bean,
"string2dArray[0][0]"));
+            assertEquals("firstArray[1]", firstArray[1],  PropertyUtils.getProperty(bean,
"string2dArray[0][1]"));
+            assertEquals("firstArray[2]", firstArray[2],  PropertyUtils.getProperty(bean,
"string2dArray[0][2]"));
+            assertEquals("secondArray[0]", secondArray[0], PropertyUtils.getProperty(bean,
"string2dArray[1][0]"));
+            assertEquals("secondArray[1]", secondArray[1], PropertyUtils.getProperty(bean,
"string2dArray[1][1]"));
+            assertEquals("secondArray[2]", secondArray[2], PropertyUtils.getProperty(bean,
"string2dArray[1][2]"));
+            assertEquals("secondArray[3]", secondArray[3], PropertyUtils.getProperty(bean,
"string2dArray[1][3]"));
+            
+            PropertyUtils.setProperty(bean, "string2dArray[1][3]", "FOO");
+            assertEquals("WRITE", "FOO", secondArray[3]);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail("Threw " + t + "");
+        }
+    }
+
+    /**
+     * Test getting an indexed value out of List of Lists
+     */
+    public void testGetIndexedList() {
+        String[] firstArray = new String[] {"FIRST-1", "FIRST-2", "FIRST-3"};
+        String[] secondArray = new String[] {"SECOND-1", "SECOND-2", "SECOND-3",  "SECOND-4"};
+        List mainList   = new ArrayList();
+        mainList.add(Arrays.asList(firstArray));
+        mainList.add(Arrays.asList(secondArray));
+        TestBean bean = new TestBean(mainList);
+        try {
+            assertEquals("firstArray[0]", firstArray[0],  PropertyUtils.getProperty(bean,
"listIndexed[0][0]"));
+            assertEquals("firstArray[1]", firstArray[1],  PropertyUtils.getProperty(bean,
"listIndexed[0][1]"));
+            assertEquals("firstArray[2]", firstArray[2],  PropertyUtils.getProperty(bean,
"listIndexed[0][2]"));
+            assertEquals("secondArray[0]", secondArray[0], PropertyUtils.getProperty(bean,
"listIndexed[1][0]"));
+            assertEquals("secondArray[1]", secondArray[1], PropertyUtils.getProperty(bean,
"listIndexed[1][1]"));
+            assertEquals("secondArray[2]", secondArray[2], PropertyUtils.getProperty(bean,
"listIndexed[1][2]"));
+            assertEquals("secondArray[3]", secondArray[3], PropertyUtils.getProperty(bean,
"listIndexed[1][3]"));
+            
+            PropertyUtils.setProperty(bean, "listIndexed[1][3]", "FOO");
+            assertEquals("WRITE", "FOO", secondArray[3]);
+        } catch (Throwable t) {
+            t.printStackTrace();
+            fail("Threw " + t + "");
+        }
     }
 
 

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java?view=diff&rev=552384&r1=552383&r2=552384
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/TestBean.java
Sun Jul  1 19:22:57 2007
@@ -95,6 +95,14 @@
         setStringProperty(stringProperty);
     }
 
+    public TestBean(List listIndexed) {
+        this.listIndexed = listIndexed;
+    }
+
+    public TestBean(String[][] string2dArray) {
+        this.string2dArray = string2dArray;
+    }
+
     // ------------------------------------------------------------- Properties
 
 
@@ -492,6 +500,10 @@
         stringIndexed[index] = value;
     }
 
+    private String[][] string2dArray = new String[][] {new String[] {"1", "2", "3"}, new
String[] {"4","5","6"}};
+    public String[] getString2dArray(int index) {
+        return string2dArray[index];
+    }
 
     /**
      * A String property.



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message