View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * This test verifies that although BEANUTILS-492
37   * means {@link IndexedPropertyDescriptor}s are not
38   * returned for properties of type {@link List}, they
39   * can still be accessed as positional items.
40   *
41   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-492">BEANUTILS-492</a>
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          // Only first element survives as a String
97          assertEquals("item0", described.get("someList"));
98      }
99  
100     @Test
101     public void getPropertyType() throws Exception {
102         if (supportsIndexedLists()) {
103             // legacy behaviour (< Java 8)
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             // Java 7 or earlier? (BEANUTILS-492)
115             IndexedPropertyDescriptor indexed = (IndexedPropertyDescriptor) propDesc;
116             assertEquals(String.class, indexed.getIndexedReadMethod().getReturnType());
117         }
118     }
119 
120 
121 }