Author: oheger
Date: Tue Oct  7 20:22:52 2014
New Revision: 1629975

URL: http://svn.apache.org/r1629975
Log:
[BEANUTILS-465] Made indexed list setters work again.

Thanks to Daniel Atallah for the patch.

Added:
    
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira465TestCase.java
Modified:
    
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java

Modified: 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java?rev=1629975&r1=1629974&r2=1629975&view=diff
==============================================================================
--- 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java
 (original)
+++ 
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java
 Tue Oct  7 20:22:52 2014
@@ -27,6 +27,7 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.commons.beanutils.expression.Resolver;
@@ -952,6 +953,8 @@ public class BeanUtilsBean {
                 }
                 type = ((IndexedPropertyDescriptor) descriptor).
                     getIndexedPropertyType();
+            } else if (index >= 0 && 
List.class.isAssignableFrom(descriptor.getPropertyType())) {
+                type = Object.class;
             } else if (key != null) {
                 if (descriptor.getReadMethod() == null) {
                     if (log.isDebugEnabled()) {

Added: 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira465TestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira465TestCase.java?rev=1629975&view=auto
==============================================================================
--- 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira465TestCase.java
 (added)
+++ 
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira465TestCase.java
 Tue Oct  7 20:22:52 2014
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.bugs;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.beanutils.BeanUtils;
+
+/**
+ * Indexed List Setters no longer work.
+ *
+ * @version $Id$
+ * @see <a
+ * 
href="https://issues.apache.org/jira/browse/BEANUTILS-465";>https://issues.apache.org/jira/browse/BEANUTILS-465</a>
+ */
+public class Jira465TestCase extends TestCase {
+    /** Constant for the property path. */
+    private static final String PATH = "foo[0]";
+
+    /** Constant for the updated value. */
+    private static final String NEW_VALUE = "2";
+
+    /** Constant for the original value. */
+    private static final String OLD_VALUE = "1";
+
+    /**
+     * Changes the value of the test property.
+     *
+     * @param bean the bean to be updated
+     */
+    private static void changeValue(Object bean) {
+        try {
+            BeanUtils.setProperty(bean, PATH, NEW_VALUE);
+        } catch (Exception e) {
+            fail("Could not set property: " + e);
+        }
+    }
+
+    public void testArrayProperty() throws InvocationTargetException,
+            IllegalAccessException {
+        ArrayProp bean = new ArrayProp();
+        changeValue(bean);
+        assertEquals("Wrong value", NEW_VALUE, bean.getFoo()[0]);
+    }
+
+    public void testArrayIndexedProperty() {
+        ArrayIndexedProp bean = new ArrayIndexedProp();
+        changeValue(bean);
+        assertEquals("Wrong value", NEW_VALUE, bean.getFoo(0));
+    }
+
+    public void testListProperty() {
+        ListProp bean = new ListProp();
+        changeValue(bean);
+        assertEquals("Wrong value", NEW_VALUE, bean.getFoo().get(0));
+    }
+
+    public void testListIndexedProperty() {
+        ListIndexedProp bean = new ListIndexedProp();
+        changeValue(bean);
+        assertEquals("Wrong value", NEW_VALUE, bean.getFoo(0));
+    }
+
+    public static class ArrayProp {
+        private Object[] foo = new Object[] { OLD_VALUE };
+
+        public Object[] getFoo() {
+            return foo;
+        }
+
+        public void setFoo(Object[] foo) {
+            this.foo = foo;
+        }
+    }
+
+    public static class ArrayIndexedProp {
+        private final Object[] foo = new Object[] { OLD_VALUE };
+
+        public Object getFoo(int i) {
+            return foo[i];
+        }
+
+        public void setFoo(int i, Object value) {
+            this.foo[i] = value;
+        }
+    }
+
+    public static class ListProp {
+        private List<String> foo = new 
ArrayList<String>(Arrays.asList(OLD_VALUE));
+
+        public List<String> getFoo() {
+            return foo;
+        }
+
+        public void setFoo(List<String> foo) {
+            this.foo = foo;
+        }
+    }
+
+    public static class ListIndexedProp {
+        private final List<String> foo = new 
ArrayList<String>(Arrays.asList(OLD_VALUE));
+
+        public String getFoo(int i) {
+            return foo.get(i);
+        }
+
+        public void setFoo(int i, String value) {
+            this.foo.set(i, value);
+        }
+    }
+}


Reply via email to