View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils.bugs;
18  
19  import java.lang.reflect.InvocationTargetException;
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.commons.beanutils.BeanUtils;
27  
28  /**
29   * Indexed List Setters no longer work.
30   *
31   * @version $Id$
32   * @see <a
33   * href="https://issues.apache.org/jira/browse/BEANUTILS-465">https://issues.apache.org/jira/browse/BEANUTILS-465</a>
34   */
35  public class Jira465TestCase extends TestCase {
36      /** Constant for the property path. */
37      private static final String PATH = "foo[0]";
38  
39      /** Constant for the updated value. */
40      private static final String NEW_VALUE = "2";
41  
42      /** Constant for the original value. */
43      private static final String OLD_VALUE = "1";
44  
45      /**
46       * Changes the value of the test property.
47       *
48       * @param bean the bean to be updated
49       */
50      private static void changeValue(final Object bean) {
51          try {
52              BeanUtils.setProperty(bean, PATH, NEW_VALUE);
53          } catch (final Exception e) {
54              fail("Could not set property: " + e);
55          }
56      }
57  
58      public void testArrayProperty() throws InvocationTargetException,
59              IllegalAccessException {
60          final ArrayProp bean = new ArrayProp();
61          changeValue(bean);
62          assertEquals("Wrong value", NEW_VALUE, bean.getFoo()[0]);
63      }
64  
65      public void testArrayIndexedProperty() {
66          final ArrayIndexedProp bean = new ArrayIndexedProp();
67          changeValue(bean);
68          assertEquals("Wrong value", NEW_VALUE, bean.getFoo(0));
69      }
70  
71      public void testListProperty() {
72          final ListProp bean = new ListProp();
73          changeValue(bean);
74          assertEquals("Wrong value", NEW_VALUE, bean.getFoo().get(0));
75      }
76  
77      public void testListIndexedProperty() {
78          final ListIndexedProp bean = new ListIndexedProp();
79          changeValue(bean);
80          assertEquals("Wrong value", NEW_VALUE, bean.getFoo(0));
81      }
82  
83      public static class ArrayProp {
84          private Object[] foo = new Object[] { OLD_VALUE };
85  
86          public Object[] getFoo() {
87              return foo;
88          }
89  
90          public void setFoo(final Object[] foo) {
91              this.foo = foo;
92          }
93      }
94  
95      public static class ArrayIndexedProp {
96          private final Object[] foo = new Object[] { OLD_VALUE };
97  
98          public Object getFoo(final int i) {
99              return foo[i];
100         }
101 
102         public void setFoo(final int i, final Object value) {
103             this.foo[i] = value;
104         }
105     }
106 
107     public static class ListProp {
108         private List<String> foo = new ArrayList<String>(Arrays.asList(OLD_VALUE));
109 
110         public List<String> getFoo() {
111             return foo;
112         }
113 
114         public void setFoo(final List<String> foo) {
115             this.foo = foo;
116         }
117     }
118 
119     public static class ListIndexedProp {
120         private final List<String> foo = new ArrayList<String>(Arrays.asList(OLD_VALUE));
121 
122         public String getFoo(final int i) {
123             return foo.get(i);
124         }
125 
126         public void setFoo(final int i, final String value) {
127             this.foo.set(i, value);
128         }
129     }
130 }