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.assertNotNull;
21  
22  import java.beans.PropertyDescriptor;
23  
24  import org.apache.commons.beanutils2.PropertyUtils;
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-357">https://issues.apache.org/jira/browse/BEANUTILS-357</a>
31   */
32  public class Jira357Test {
33  
34      /**
35       * Abstract test bean.
36       */
37      public abstract static class AbstractTestBean {
38  
39          /** Inner Class */
40          public abstract static class InnerClass {
41              private String firstName;
42  
43              public String getInnerName() {
44                  return firstName;
45              }
46  
47              public void setInnerName(final String firstName) {
48                  this.firstName = firstName;
49              }
50          }
51  
52          public abstract String getFoo();
53  
54          public abstract AbstractTestBean.InnerClass getInnerClassProperty();
55  
56          public abstract boolean isBar();
57  
58          public abstract void setBar(boolean bar);
59  
60          public abstract void setFoo(String foo);
61      }
62  
63      /**
64       * Concrete bean implementation.
65       */
66      public static class ConcreteTestBean extends AbstractTestBean {
67  
68          private String foo;
69          private boolean bar;
70          private ConcreteTestBean.InnerClass innerClassProperty;
71  
72          @Override
73          public String getFoo() {
74              return foo;
75          }
76  
77          @Override
78          public ConcreteTestBean.InnerClass getInnerClassProperty() {
79              return innerClassProperty;
80          }
81  
82          @Override
83          public boolean isBar() {
84              return bar;
85          }
86  
87          @Override
88          public void setBar(final boolean bar) {
89              this.bar = bar;
90          }
91  
92          @Override
93          public void setFoo(final String foo) {
94              this.foo = foo;
95          }
96  
97          public void setInnerClassProperty(final ConcreteTestBean.InnerClass innerClassProperty) {
98              this.innerClassProperty = innerClassProperty;
99          }
100     }
101 
102     /**
103      * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
104      */
105     private void checkReadMethod(final String propertyName, final Class<?> expectedDeclaringClass) throws Exception {
106 
107         PropertyDescriptor[] descriptors = null;
108         descriptors = PropertyUtils.getPropertyDescriptors(ConcreteTestBean.class);
109 
110         // Test InnerClassProperty
111         final PropertyDescriptor descriptor = findDescriptor(propertyName, descriptors);
112         assertNotNull(descriptor, propertyName + "descriptor");
113         assertEquals(expectedDeclaringClass, descriptor.getReadMethod().getDeclaringClass(), propertyName + " read method declaring class");
114     }
115 
116     /**
117      * Find a property descriptor.
118      */
119     private PropertyDescriptor findDescriptor(final String propertyName, final PropertyDescriptor[] descriptors) {
120         if (descriptors != null) {
121             for (final PropertyDescriptor descriptor : descriptors) {
122                 if (propertyName.equals(descriptor.getName())) {
123                     return descriptor;
124                 }
125             }
126         }
127         return null; // not found
128     }
129 
130     /**
131      * Sets up.
132      *
133      * @throws Exception
134      */
135     @BeforeEach
136     protected void setUp() throws Exception {
137     }
138 
139     /**
140      * Tear Down.
141      *
142      * @throws Exception
143      */
144     @AfterEach
145     protected void tearDown() throws Exception {
146     }
147 
148     /**
149      * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
150      */
151     @Test
152     public void testPropertyUtils_getPropertyDescriptors_Bar() throws Exception {
153         checkReadMethod("bar", ConcreteTestBean.class);
154     }
155 
156     /**
157      * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
158      */
159     @Test
160     public void testPropertyUtils_getPropertyDescriptors_Foo() throws Exception {
161         checkReadMethod("foo", ConcreteTestBean.class);
162     }
163 
164     /**
165      * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
166      */
167     @Test
168     public void testPropertyUtils_getPropertyDescriptors_InnerClassProperty() throws Exception {
169         checkReadMethod("innerClassProperty", ConcreteTestBean.class);
170     }
171 }