1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
31
32
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
84 private static final String PATH = "foo[0]";
85
86
87 private static final String NEW_VALUE = "2";
88
89
90 private static final String OLD_VALUE = "1";
91
92
93
94
95
96
97
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 }