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