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