1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils2;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assumptions.assumeTrue;
23
24 import java.beans.IndexedPropertyDescriptor;
25 import java.beans.PropertyDescriptor;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 import org.apache.commons.beanutils2.bugs.other.Jira492IndexedListsSupport;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37
38
39
40 public class IndexedPropertyTest {
41
42
43
44
45 private IndexedTestBean bean;
46 private BeanUtilsBean beanUtilsBean;
47 private PropertyUtilsBean propertyUtilsBean;
48 private String[] testArray;
49 private String[] newArray;
50 private List<String> testList;
51 private List<Object> newList;
52 private ArrayList<Object> arrayList;
53
54
55
56
57 @BeforeEach
58 public void setUp() {
59
60
61 beanUtilsBean = new BeanUtilsBean();
62 propertyUtilsBean = beanUtilsBean.getPropertyUtils();
63
64
65 testArray = new String[] { "array-0", "array-1", "array-2" };
66 newArray = new String[] { "newArray-0", "newArray-1", "newArray-2" };
67
68 testList = new ArrayList<>();
69 testList.add("list-0");
70 testList.add("list-1");
71 testList.add("list-2");
72
73 newList = new ArrayList<>();
74 newList.add("newList-0");
75 newList.add("newList-1");
76 newList.add("newList-2");
77
78 arrayList = new ArrayList<>();
79 arrayList.add("arrayList-0");
80 arrayList.add("arrayList-1");
81 arrayList.add("arrayList-2");
82
83
84 bean = new IndexedTestBean();
85 bean.setStringArray(testArray);
86 bean.setStringList(testList);
87 bean.setArrayList(arrayList);
88 }
89
90
91
92
93 @AfterEach
94 public void tearDown() {
95 bean = null;
96 }
97
98
99
100
101 @Test
102 public void testArrayIndexedPropertyDescriptor() throws Exception {
103 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringArray");
104 assertNotNull(descriptor, "No Array Descriptor");
105 assertEquals(IndexedPropertyDescriptor.class, descriptor.getClass(), "Not IndexedPropertyDescriptor");
106 assertEquals(testArray.getClass(), descriptor.getPropertyType(), "PropertyDescriptor Type invalid");
107 }
108
109
110
111
112 @Test
113 public void testArrayIndexedReadMethod() throws Exception {
114 final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean.getPropertyDescriptor(bean, "stringArray");
115 assertNotNull(descriptor.getIndexedReadMethod(), "No Array Indexed Read Method");
116 }
117
118
119
120
121 @Test
122 public void testArrayIndexedWriteMethod() throws Exception {
123 final IndexedPropertyDescriptor descriptor = (IndexedPropertyDescriptor) propertyUtilsBean.getPropertyDescriptor(bean, "stringArray");
124 assertNotNull(descriptor.getIndexedWriteMethod(), "No Array Indexed Write Method");
125 }
126
127
128
129
130 @Test
131 public void testArrayListIndexedPropertyDescriptor() throws Exception {
132 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "arrayList");
133 assertNotNull(descriptor, "No ArrayList Descriptor");
134 if (Jira492IndexedListsSupport.supportsIndexedLists()) {
135 assertEquals(IndexedPropertyDescriptor.class, descriptor.getClass(), "Not IndexedPropertyDescriptor");
136 }
137 assertEquals(ArrayList.class, descriptor.getPropertyType(), "PropertyDescriptor Type invalid");
138 }
139
140
141
142
143 @Test
144 public void testArrayListReadMethod() throws Exception {
145 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "arrayList");
146 assertNotNull(descriptor.getReadMethod(), "No ArrayList Read Method");
147 }
148
149
150
151
152 @Test
153 public void testArrayListWriteMethod() throws Exception {
154 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "arrayList");
155 assertNotNull(descriptor.getWriteMethod(), "No ArrayList Write Method");
156 }
157
158
159
160
161 @Test
162 public void testArrayReadMethod() throws Exception {
163 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringArray");
164 assertNotNull(descriptor.getReadMethod(), "No Array Read Method");
165 }
166
167
168
169
170 @Test
171 public void testArrayWriteMethod() throws Exception {
172 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringArray");
173 assertNotNull(descriptor.getWriteMethod(), "No Array Write Method");
174 }
175
176
177
178
179 @Test
180 public void testGetArray() throws Exception {
181 assertEquals(testArray, propertyUtilsBean.getProperty(bean, "stringArray"));
182 }
183
184
185
186
187
188
189 @Test
190 public void testGetArrayAsString() throws Exception {
191 assertEquals("array-0", beanUtilsBean.getProperty(bean, "stringArray"));
192 }
193
194
195
196
197 @Test
198 public void testGetArrayItemA() throws Exception {
199 assertEquals("array-1", beanUtilsBean.getProperty(bean, "stringArray[1]"));
200 }
201
202
203
204
205 @Test
206 public void testGetArrayItemB() throws Exception {
207 assertEquals("array-1", beanUtilsBean.getIndexedProperty(bean, "stringArray", 1));
208 }
209
210
211
212
213 @Test
214 public void testGetArrayList() throws Exception {
215 assertEquals(arrayList, propertyUtilsBean.getProperty(bean, "arrayList"));
216 }
217
218
219
220
221
222
223 @Test
224 public void testGetList() throws Exception {
225 assertEquals(testList, propertyUtilsBean.getProperty(bean, "stringList"));
226 }
227
228
229
230
231
232
233 @Test
234 public void testGetListAsString() throws Exception {
235 assertEquals("list-0", beanUtilsBean.getProperty(bean, "stringList"));
236 }
237
238
239
240
241 @Test
242 public void testGetListItemA() throws Exception {
243 assertEquals("list-1", beanUtilsBean.getProperty(bean, "stringList[1]"));
244 }
245
246
247
248
249 @Test
250 public void testGetListItemB() throws Exception {
251 assertEquals("list-1", beanUtilsBean.getIndexedProperty(bean, "stringList", 1));
252 }
253
254
255
256
257 @Test
258 public void testListIndexedPropertyDescriptor() throws Exception {
259 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringList");
260 assertNotNull(descriptor, "No List Descriptor");
261 if (Jira492IndexedListsSupport.supportsIndexedLists()) {
262
263 assertEquals(IndexedPropertyDescriptor.class, descriptor.getClass(), "Not IndexedPropertyDescriptor");
264 }
265 assertEquals(List.class, descriptor.getPropertyType(), "PropertyDescriptor Type invalid");
266 }
267
268
269
270
271 @Test
272 public void testListIndexedReadMethod() throws Exception {
273 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringList");
274 assertNotNull(descriptor, "stringList descriptor not found");
275 assumeTrue(Jira492IndexedListsSupport.supportsIndexedLists(), "JDK does not support index bean properties on java.util.List");
276 assertNotNull(((IndexedPropertyDescriptor) descriptor).getIndexedReadMethod(), "No List Indexed Read Method");
277 }
278
279
280
281
282 @Test
283 public void testListIndexedWriteMethod() throws Exception {
284 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringList");
285 assertNotNull(descriptor, "stringList descriptor not found");
286 assumeTrue(Jira492IndexedListsSupport.supportsIndexedLists(), "JDK does not support index bean properties on java.util.List");
287 assertNotNull(((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod(), "No List Indexed Write Method");
288 }
289
290
291
292
293
294
295 @Test
296 public void testListReadMethod() throws Exception {
297 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringList");
298 assertNotNull(descriptor.getReadMethod(), "No List Read Method");
299 }
300
301
302
303
304
305
306 @Test
307 public void testListWriteMethod() throws Exception {
308 final PropertyDescriptor descriptor = propertyUtilsBean.getPropertyDescriptor(bean, "stringList");
309 assertNotNull(descriptor.getWriteMethod(), "No List Write Method");
310 }
311
312
313
314
315
316
317
318
319
320
321 @Test
322 public void testSetArray() throws Exception {
323 beanUtilsBean.setProperty(bean, "stringArray", newArray);
324 final Object value = bean.getStringArray();
325 assertEquals(newArray.getClass(), value.getClass(), "Type is different");
326 final String[] array = (String[]) value;
327 assertEquals(newArray.length, array.length, "Array Length is different");
328 for (int i = 0; i < array.length; i++) {
329 assertEquals(newArray[i], array[i], "Element " + i + " is different");
330 }
331 }
332
333
334
335
336 @Test
337 public void testSetArrayItemA() throws Exception {
338 beanUtilsBean.setProperty(bean, "stringArray[1]", "modified-1");
339 assertEquals("modified-1", bean.getStringArray(1));
340 }
341
342
343
344
345 @Test
346 public void testSetArrayItemB() throws Exception {
347 propertyUtilsBean.setIndexedProperty(bean, "stringArray", 1, "modified-1");
348 assertEquals("modified-1", bean.getStringArray(1));
349 }
350
351
352
353
354 @Test
355 public void testSetArrayList() throws Exception {
356 beanUtilsBean.setProperty(bean, "arrayList", newList);
357 final Object value = bean.getArrayList();
358 assertEquals(newList.getClass(), value.getClass(), "Type is different");
359 final List<?> list = (List<?>) value;
360 assertEquals(newList.size(), list.size(), "List size is different");
361 for (int i = 0; i < list.size(); i++) {
362 assertEquals(newList.get(i), list.get(i), "Element " + i + " is different");
363 }
364 }
365
366
367
368
369
370
371
372 @Test
373 public void testSetList() throws Exception {
374 beanUtilsBean.setProperty(bean, "stringList", newList);
375 final Object value = bean.getStringList();
376 assertEquals(newList.getClass(), value.getClass(), "Type is different");
377 final List<?> list = (List<?>) value;
378 assertEquals(newList.size(), list.size(), "List size is different");
379 for (int i = 0; i < list.size(); i++) {
380 assertEquals(newList.get(i), list.get(i), "Element " + i + " is different");
381 }
382 }
383
384
385
386
387 @Test
388 public void testSetListItemA() throws Exception {
389 beanUtilsBean.setProperty(bean, "stringList[1]", "modified-1");
390 assertEquals("modified-1", bean.getStringList(1));
391 }
392
393
394
395
396 @Test
397 public void testSetListItemB() throws Exception {
398 propertyUtilsBean.setIndexedProperty(bean, "stringList", 1, "modified-1");
399 assertEquals("modified-1", bean.getStringList(1));
400 }
401
402 }