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  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.beans.IntrospectionException;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * <p>
32   * Test Case for the {@code MappedPropertyDescriptor}.
33   * </p>
34   */
35  public class MappedPropertyTest {
36  
37      /**
38       * Sets up instance variables required by this test case.
39       */
40      @BeforeEach
41      public void setUp() throws Exception {
42      }
43  
44      /**
45       * Tear down instance variables required by this test case.
46       */
47      @AfterEach
48      public void tearDown() {
49      }
50  
51      /**
52       * Test property with any two args
53       */
54      @Test
55      public void testAnyArgsProperty() throws Exception {
56          final String property = "anyMapped";
57          final Class<?> clazz = MappedPropertyTestBean.class;
58          final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
59          assertNull(desc.getMappedReadMethod(), "Getter is found");
60          assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
61      }
62  
63      /**
64       * Test boolean "is" method name
65       */
66      @Test
67      public void testBooleanMapped() throws Exception {
68          final String property = "mappedBoolean";
69          final Class<?> clazz = MappedPropertyTestBean.class;
70          final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
71          assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
72          assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
73      }
74  
75      /**
76       * Test Interface Inherited mapped property
77       */
78      @Test
79      public void testChildInterfaceMapped() throws Exception {
80          final String property = "mapproperty";
81          final Class<?> clazz = MappedPropertyChildInterface.class;
82          final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
83          assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
84          assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
85      }
86  
87      /**
88       * Test Mapped Property - Different Types
89       *
90       * Expect to find the getDifferentTypes() method, but not the setDifferentTypes() method because setDifferentTypes() sets and Integer, while
91       * getDifferentTypes() returns a Long.
92       */
93      @Test
94      public void testDifferentTypes() throws Exception {
95          final String property = "differentTypes";
96          final Class<?> clazz = MappedPropertyTestBean.class;
97          final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
98          assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
99          assertNull(desc.getMappedWriteMethod(), "Setter is found");
100     }
101 
102     /**
103      * Test valid method name
104      */
105     @Test
106     public void testFound() throws Exception {
107         final String property = "mapproperty";
108         final Class<?> clazz = MappedPropertyTestBean.class;
109         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
110         assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
111         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
112     }
113 
114     /**
115      * Test Interface with mapped property
116      */
117     @Test
118     public void testInterfaceMapped() throws Exception {
119         final String property = "mapproperty";
120         final Class<?> clazz = MappedPropertyTestInterface.class;
121         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
122         assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
123         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
124     }
125 
126     /**
127      * Test property not found in interface
128      */
129     @Test
130     public void testInterfaceNotFound() {
131         final String property = "XXXXXX";
132         final Class<?> clazz = MappedPropertyTestInterface.class;
133         assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
134     }
135 
136     /**
137      * Test Mapped Property - Invalid Getter
138      */
139     @Test
140     public void testInvalidGetter() throws Exception {
141         final String property = "invalidGetter";
142         final Class<?> clazz = MappedPropertyTestBean.class;
143         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
144         assertNull(desc.getMappedReadMethod(), "Getter is found");
145         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
146     }
147 
148     /**
149      * Test Mapped Property - Invalid Setter
150      */
151     @Test
152     public void testInvalidSetter() throws Exception {
153         final String property = "invalidSetter";
154         final Class<?> clazz = MappedPropertyTestBean.class;
155         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
156         assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
157         assertNull(desc.getMappedWriteMethod(), "Setter is found");
158     }
159 
160     /**
161      * Test Map getter
162      */
163     @Test
164     public void testMapGetter() throws Exception {
165         final MappedPropertyTestBean bean = new MappedPropertyTestBean();
166         final String testValue = "test value";
167         final String testKey = "testKey";
168         BeanUtils.setProperty(bean, "myMap(" + testKey + ")", "test value");
169         assertEquals(testValue, bean.getMyMap().get(testKey), "Map getter");
170     }
171 
172     /**
173      * Test Mapped Property - Getter only
174      */
175     @Test
176     public void testMappedGetterOnly() throws Exception {
177         final String property = "mappedGetterOnly";
178         final Class<?> clazz = MappedPropertyTestBean.class;
179         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
180         assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
181         assertNull(desc.getMappedWriteMethod(), "Setter is found");
182     }
183 
184     /**
185      * Test Mapped Property - Setter Only
186      */
187     @Test
188     public void testMappedSetterOnly() throws Exception {
189         final String property = "mappedSetterOnly";
190         final Class<?> clazz = MappedPropertyTestBean.class;
191         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
192         assertNull(desc.getMappedReadMethod(), "Getter is found");
193         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
194     }
195 
196     /**
197      * Test invalid method name
198      */
199     @Test
200     public void testNotFound() {
201         final String property = "xxxxxxx";
202         final Class<?> clazz = MappedPropertyTestBean.class;
203         assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
204     }
205 
206     /**
207      * Test property with two primitive args
208      */
209     @Test
210     public void testPrimitiveArgsProperty() throws Exception {
211         final String property = "mappedPrimitive";
212         final Class<?> clazz = MappedPropertyTestBean.class;
213         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
214         assertNull(desc.getMappedReadMethod(), "Getter is found");
215         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
216     }
217 
218     /**
219      * Test 'protected' mapped property
220      */
221     @Test
222     public void testProtected() {
223         final String property = "protectedProperty";
224         final Class<?> clazz = MappedPropertyTestBean.class;
225         assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
226     }
227 
228     /**
229      * Test 'protected' method in parent
230      */
231     @Test
232     public void testProtectedParentMethod() {
233         final String property = "protectedMapped";
234         final Class<?> clazz = MappedPropertyChildBean.class;
235         assertThrows(IntrospectionException.class, () -> new MappedPropertyDescriptor(property, clazz));
236     }
237 
238     /**
239      * Test 'public' method in parent
240      */
241     @Test
242     public void testPublicParentMethod() throws Exception {
243         final String property = "mapproperty";
244         final Class<?> clazz = MappedPropertyChildBean.class;
245         final MappedPropertyDescriptor desc = new MappedPropertyDescriptor(property, clazz);
246         assertNotNull(desc.getMappedReadMethod(), "Getter is missing");
247         assertNotNull(desc.getMappedWriteMethod(), "Setter is missing");
248     }
249 }