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 java.beans.PropertyDescriptor;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.beanutils.PropertyUtils;
26  
27  /**
28   * @version $Id$
29   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-357">https://issues.apache.org/jira/browse/BEANUTILS-357</a>
30   */
31  public class Jira357TestCase extends TestCase {
32  
33      /**
34       * Create a test case with the specified name.
35       *
36       * @param name The name of the test
37       */
38      public Jira357TestCase(final String name) {
39          super(name);
40      }
41  
42      /**
43       * Run the Test.
44       *
45       * @param args Arguments
46       */
47      public static void main(final String[] args) {
48          junit.textui.TestRunner.run(suite());
49      }
50  
51      /**
52       * Create a test suite for this test.
53       *
54       * @return a test suite
55       */
56      public static Test suite() {
57          return (new TestSuite(Jira357TestCase.class));
58      }
59  
60      /**
61       * Set up.
62       *
63       * @throws java.lang.Exception
64       */
65      @Override
66      protected void setUp() throws Exception {
67          super.setUp();
68      }
69  
70      /**
71       * Tear Down.
72       *
73       * @throws java.lang.Exception
74       */
75      @Override
76      protected void tearDown() throws Exception {
77          super.tearDown();
78      }
79  
80      /**
81       * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
82       */
83      public void testPropertyUtils_getPropertyDescriptors_Foo() throws Exception {
84          checkReadMethod("foo", ConcreteTestBean.class);
85      }
86  
87      /**
88       * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
89       */
90      public void testPropertyUtils_getPropertyDescriptors_Bar() throws Exception {
91  
92          // FIXME the isBar() method returning AbstractTestBean.class as the
93          //       declaring class instead of ConcreteTestBean.class
94          //       causing this test to fail - so its commented out for now
95          //checkReadMethod("bar", ConcreteTestBean.class);
96      }
97  
98      /**
99       * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
100      */
101     public void testPropertyUtils_getPropertyDescriptors_InnerClassProperty() throws Exception {
102         checkReadMethod("innerClassProperty", ConcreteTestBean.class);
103     }
104 
105     /**
106      * Test {@link PropertyUtils#getPropertyDescriptors(Class)}
107      */
108     private void checkReadMethod(final String propertyName, final Class<?> expectedDeclaringClass) throws Exception {
109 
110         PropertyDescriptor[] descriptors = null;
111         try {
112             descriptors = PropertyUtils.getPropertyDescriptors(ConcreteTestBean.class);
113         } catch (final Exception e) {
114             e.printStackTrace();
115             fail("Threw: " + e);
116         }
117 
118         // Test InnerClassProperty
119         final PropertyDescriptor descriptor = findDescriptor(propertyName, descriptors);
120         assertNotNull(propertyName + "descriptor", descriptor);
121         assertEquals(propertyName + " read method declaring class", expectedDeclaringClass, descriptor.getReadMethod().getDeclaringClass());
122     }
123 
124     /**
125      * Find a property descriptor.
126      */
127     private PropertyDescriptor findDescriptor(final String propertyName, final PropertyDescriptor[] descriptors) {
128         if (descriptors != null) {
129             for (PropertyDescriptor descriptor : descriptors) {
130                 if (propertyName.equals(descriptor.getName())) {
131                     return descriptor;
132                 }
133             }
134         }
135         return null; // not found
136     }
137 
138     /**
139      * Abstract test bean.
140      */
141     public abstract static class AbstractTestBean {
142 
143         public abstract String getFoo();
144         public abstract void setFoo(String foo);
145 
146         public abstract boolean isBar();
147         public abstract void setBar(boolean bar);
148 
149         public abstract AbstractTestBean.InnerClass getInnerClassProperty();
150 
151         /** Inner Class */
152         public abstract static class InnerClass {
153             private String firstName;
154             public String getInnerName() {
155                 return firstName;
156             }
157             public void setInnerName(final String firstName) {
158                 this.firstName = firstName;
159             }
160         }
161     }
162 
163     /**
164      * Concrete bean implementation.
165      */
166     public static class ConcreteTestBean extends AbstractTestBean {
167 
168         private String foo;
169         private boolean bar;
170         private ConcreteTestBean.InnerClass innerClassProperty;
171 
172         @Override
173         public String getFoo() {
174             return foo;
175         }
176         @Override
177         public void setFoo(final String foo) {
178             this.foo = foo;
179         }
180         @Override
181         public boolean isBar() {
182             return bar;
183         }
184         @Override
185         public void setBar(final boolean bar) {
186             this.bar = bar;
187         }
188         @Override
189         public ConcreteTestBean.InnerClass getInnerClassProperty() {
190             return innerClassProperty;
191         }
192         public void setInnerClassProperty(final ConcreteTestBean.InnerClass innerClassProperty) {
193             this.innerClassProperty = innerClassProperty;
194         }
195     }
196 }