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