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    *      https://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.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   * This test verifies that although BEANUTILS-492 means {@link IndexedPropertyDescriptor}s are not returned for properties of type {@link List}, they can still
37   * be accessed as positional items.
38   *
39   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-492">BEANUTILS-492</a>
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          // Only first element survives as a String
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              // Java 7 or earlier? (BEANUTILS-492)
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              // legacy behavior (< Java 8)
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 }