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.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.fail;
21  import static org.junit.jupiter.api.Assumptions.assumeTrue;
22  
23  import java.beans.BeanInfo;
24  import java.beans.IndexedPropertyDescriptor;
25  import java.beans.IntrospectionException;
26  import java.beans.Introspector;
27  import java.beans.PropertyDescriptor;
28  import java.util.List;
29  
30  import org.apache.commons.beanutils2.PropertyUtils;
31  import org.junit.jupiter.api.BeforeAll;
32  import org.junit.jupiter.api.Test;
33  
34  final class FirstChildBean extends RootBean {
35  }
36  
37  /**
38   * getPropertyType return null on second descendant class
39   * <p>
40   * This test only work in Java 7 or earlier (See BEANUTILS-492) - as a weaker alternative, see {@link Jira422bTest}.
41   *
42   *
43   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-422">https://issues.apache.org/jira/browse/BEANUTILS-422</a>
44   */
45  public class Jira422Test {
46  
47      /**
48       * Detects BEANUTILS-492 in Java 8 or later
49       *
50       * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-492">BEANUTILS-492</a>
51       */
52      @BeforeAll
53      public static void assumeSupportsIndexedLists() throws IntrospectionException {
54          final BeanInfo beanInfo = Introspector.getBeanInfo(RootBean.class);
55          for (final PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
56              if (pd.getName().equals("file")) {
57                  assumeTrue(pd instanceof IndexedPropertyDescriptor, "BEANUTILS-492: IndexedPropertyDescriptor no longer supported for java.util.List");
58                  return;
59              }
60          }
61          fail("Could not find PropertyDescriptor for 'file'");
62      }
63  
64      @Test
65      public void testRootBean() throws Exception {
66          final RootBean bean = new FirstChildBean();
67          final Class<?> propertyType = PropertyUtils.getPropertyType(bean, "file[0]");
68          assertEquals(String.class.getName(), propertyType.getName());
69      }
70  
71      @Test
72      public void testSecondChildBean() throws Exception {
73          final RootBean bean = new SecondChildBean();
74          final Class<?> propertyType = PropertyUtils.getPropertyType(bean, "file[0]");
75          assertEquals(String.class.getName(), propertyType.getName());
76      }
77  
78  }
79  
80  @SuppressWarnings("rawtypes")
81  class RootBean {
82  
83      private List file;
84  
85      public List getFile() {
86          return file;
87      }
88  
89      public String getFile(final int i) {
90          return (String) file.get(i);
91      }
92  
93      @SuppressWarnings("unchecked")
94      public void setFile(final int i, final String file) {
95          this.file.set(i, file);
96      }
97  
98      public void setFile(final List file) {
99          this.file = file;
100     }
101 
102 }
103 
104 final class SecondChildBean extends RootBean {
105 }