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;
18  
19  import java.beans.PropertyDescriptor;
20  import java.lang.reflect.Method;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Test class for {@code BeanIntrospectionData}.
28   *
29   * @version $Id: BeanIntrospectionDataTestCase.html 1048600 2019-08-14 01:36:40Z chtompki $
30   */
31  public class BeanIntrospectionDataTestCase extends TestCase {
32      /** Constant for the test bean class. */
33      private static final Class<?> BEAN_CLASS = FluentIntrospectionTestBean.class;
34  
35      /** Constant for the name of the test property. */
36      private static final String TEST_PROP = "fluentGetProperty";
37  
38      /**
39       * Creates an array with property descriptors for the test bean class.
40       *
41       * @return the array with property descriptors
42       */
43      private static PropertyDescriptor[] fetchDescriptors() {
44          final PropertyUtilsBean pub = new PropertyUtilsBean();
45          pub.removeBeanIntrospector(SuppressPropertiesBeanIntrospector.SUPPRESS_CLASS);
46          pub.addBeanIntrospector(new FluentPropertyBeanIntrospector());
47          return pub.getPropertyDescriptors(BEAN_CLASS);
48      }
49  
50      /**
51       * Creates a test instance which is initialized with default property descriptors.
52       *
53       * @return the test instance
54       */
55      private static BeanIntrospectionData setUpData() {
56          return new BeanIntrospectionData(fetchDescriptors());
57      }
58  
59      /**
60       * Returns the property descriptor for the test property.
61       *
62       * @param bid the data object
63       * @return the test property descriptor
64       */
65      private static PropertyDescriptor fetchTestDescriptor(final BeanIntrospectionData bid) {
66          return bid.getDescriptor(TEST_PROP);
67      }
68  
69      /**
70       * Tests whether a write method can be queried if it is defined in the descriptor.
71       */
72      public void testGetWriteMethodDefined() {
73          final BeanIntrospectionData data = setUpData();
74          final PropertyDescriptor pd = fetchTestDescriptor(data);
75          assertNotNull("No write method", pd.getWriteMethod());
76          assertEquals("Wrong write method", pd.getWriteMethod(),
77                  data.getWriteMethod(BEAN_CLASS, pd));
78      }
79  
80      /**
81       * Tests whether a write method can be queried that is currently not available in the
82       * property descriptor.
83       */
84      public void testGetWriteMethodUndefined() throws Exception {
85          final BeanIntrospectionData data = setUpData();
86          final PropertyDescriptor pd = fetchTestDescriptor(data);
87          final Method writeMethod = pd.getWriteMethod();
88          pd.setWriteMethod(null);
89          assertEquals("Wrong write method", writeMethod,
90                  data.getWriteMethod(BEAN_CLASS, pd));
91          assertEquals("Method not set in descriptor", writeMethod, pd.getWriteMethod());
92      }
93  
94      /**
95       * Tests getWriteMethod() if the method cannot be resolved. (This is a corner case
96       * which should normally not happen in practice.)
97       */
98      public void testGetWriteMethodNonExisting() throws Exception {
99          final PropertyDescriptor pd = new PropertyDescriptor(TEST_PROP,
100                 BEAN_CLASS.getMethod("getFluentGetProperty"), BEAN_CLASS.getMethod(
101                         "setFluentGetProperty", String.class));
102         final Map<String, String> methods = new HashMap<String, String>();
103         methods.put(TEST_PROP, "hashCode");
104         final BeanIntrospectionData data = new BeanIntrospectionData(
105                 new PropertyDescriptor[] { pd }, methods);
106         pd.setWriteMethod(null);
107         assertNull("Got a write method", data.getWriteMethod(BEAN_CLASS, pd));
108     }
109 
110     /**
111      * Tests getWriteMethod() for a property for which no write method is known.
112      */
113     public void testGetWriteMethodUnknown() {
114         final BeanIntrospectionData data = setUpData();
115         final PropertyDescriptor pd = data.getDescriptor("class");
116         assertNull("Got a write method", data.getWriteMethod(BEAN_CLASS, pd));
117     }
118 }