1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
32
33
34
35 public class Jira465TestCase extends TestCase {
36
37 private static final String PATH = "foo[0]";
38
39
40 private static final String NEW_VALUE = "2";
41
42
43 private static final String OLD_VALUE = "1";
44
45
46
47
48
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 }