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    *      https://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.beanutils2.bugs;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.apache.commons.beanutils2.BeanUtils;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Indexed List Setters no longer work.
31   *
32   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-465">https://issues.apache.org/jira/browse/BEANUTILS-465</a>
33   */
34  public class Jira465Test {
35      public static class ArrayIndexedProp {
36          private final Object[] foo = { OLD_VALUE };
37  
38          public Object getFoo(final int i) {
39              return foo[i];
40          }
41  
42          public void setFoo(final int i, final Object value) {
43              this.foo[i] = value;
44          }
45      }
46  
47      public static class ArrayProp {
48          private Object[] foo = { OLD_VALUE };
49  
50          public Object[] getFoo() {
51              return foo;
52          }
53  
54          public void setFoo(final Object[] foo) {
55              this.foo = foo;
56          }
57      }
58  
59      public static class ListIndexedProp {
60          private final List<String> foo = new ArrayList<>(Arrays.asList(OLD_VALUE));
61  
62          public String getFoo(final int i) {
63              return foo.get(i);
64          }
65  
66          public void setFoo(final int i, final String value) {
67              this.foo.set(i, value);
68          }
69      }
70  
71      public static class ListProp {
72          private List<String> foo = new ArrayList<>(Arrays.asList(OLD_VALUE));
73  
74          public List<String> getFoo() {
75              return foo;
76          }
77  
78          public void setFoo(final List<String> foo) {
79              this.foo = foo;
80          }
81      }
82  
83      /** Constant for the property path. */
84      private static final String PATH = "foo[0]";
85  
86      /** Constant for the updated value. */
87      private static final String NEW_VALUE = "2";
88  
89      /** Constant for the original value. */
90      private static final String OLD_VALUE = "1";
91  
92      /**
93       * Changes the value of the test property.
94       *
95       * @param bean the bean to be updated
96       * @throws InvocationTargetException
97       * @throws IllegalAccessException
98       */
99      private static void changeValue(final Object bean) throws IllegalAccessException, InvocationTargetException {
100         BeanUtils.setProperty(bean, PATH, NEW_VALUE);
101     }
102 
103     @Test
104     public void testArrayIndexedProperty() throws Exception {
105         final ArrayIndexedProp bean = new ArrayIndexedProp();
106         changeValue(bean);
107         assertEquals(NEW_VALUE, bean.getFoo(0), "Wrong value");
108     }
109 
110     @Test
111     public void testArrayProperty() throws Exception {
112         final ArrayProp bean = new ArrayProp();
113         changeValue(bean);
114         assertEquals(NEW_VALUE, bean.getFoo()[0], "Wrong value");
115     }
116 
117     @Test
118     public void testListIndexedProperty() throws Exception {
119         final ListIndexedProp bean = new ListIndexedProp();
120         changeValue(bean);
121         assertEquals(NEW_VALUE, bean.getFoo(0), "Wrong value");
122     }
123 
124     @Test
125     public void testListProperty() throws Exception {
126         final ListProp bean = new ListProp();
127         changeValue(bean);
128         assertEquals(NEW_VALUE, bean.getFoo().get(0), "Wrong value");
129     }
130 }