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.apache.commons.beanutils2.bugs.other.Jira492IndexedListsSupport.supportsIndexedLists;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
22
23 import java.beans.IndexedPropertyDescriptor;
24 import java.beans.PropertyDescriptor;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.beanutils2.BeanUtilsBean;
30 import org.apache.commons.beanutils2.PropertyUtilsBean;
31 import org.apache.commons.beanutils2.bugs.other.Jira492IndexedListsSupport.IndexedBean;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38
39
40
41 public class Jira492Test {
42
43 private final BeanUtilsBean beanUtils = new BeanUtilsBean();
44
45 private final PropertyUtilsBean propertyUtils = new PropertyUtilsBean();
46
47 private IndexedBean bean;
48
49 @Test
50 public void describe() throws Exception {
51 final Map<String, String> described = beanUtils.describe(bean);
52
53 assertEquals("item0", described.get("someList"));
54 }
55
56 public void getArrayProperty() throws Exception {
57 final String[] arr = beanUtils.getArrayProperty(bean, "someList");
58 assertEquals(2, arr.length);
59 assertEquals("item0", arr[0]);
60 assertEquals("item1", arr[1]);
61 }
62
63 @Test
64 public void getIndexedProperty() throws Exception {
65 assertEquals("item0", beanUtils.getIndexedProperty(bean, "someList", 0));
66 assertEquals("item1", beanUtils.getIndexedProperty(bean, "someList[1]"));
67 }
68
69 @Test
70 public void getProperty() throws Exception {
71 assertEquals("item0", beanUtils.getProperty(bean, "someList"));
72 }
73
74 @Test
75 public void getPropertyDescriptor() throws Exception {
76 final PropertyDescriptor propDesc = propertyUtils.getPropertyDescriptor(bean, "someList");
77 if (supportsIndexedLists()) {
78
79 final IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propDesc;
80 assertEquals(String.class, indexed.getIndexedReadMethod().getReturnType());
81 }
82 }
83
84 @Test
85 public void getPropertySubScript() throws Exception {
86 assertEquals("item0", beanUtils.getProperty(bean, "someList[0]"));
87 assertEquals("item1", beanUtils.getProperty(bean, "someList[1]"));
88 }
89
90 @Test
91 public void getPropertyType() throws Exception {
92 if (supportsIndexedLists()) {
93
94 assertEquals(String.class, propertyUtils.getPropertyType(bean, "someList[0]"));
95 } else {
96 assertEquals(List.class, propertyUtils.getPropertyType(bean, "someList"));
97 }
98 }
99
100 @Test
101 public void getPropertyUnconverted() throws Exception {
102 final Object someList = propertyUtils.getProperty(bean, "someList");
103 assertInstanceOf(List.class, someList, "Did not retrieve list");
104 }
105
106 @BeforeEach
107 public void makeBean() {
108 bean = new IndexedBean();
109 bean.setSomeList(Arrays.asList("item0", "item1"));
110 }
111
112 @Test
113 public void setIndexedProperty() throws Exception {
114 beanUtils.setProperty(bean, "someList[1]", "item1-modified");
115 assertEquals("item1-modified", beanUtils.getIndexedProperty(bean, "someList", 1));
116 }
117
118 }