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 junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  /**
24   * <p>Test Case for the <code>MappedPropertyDescriptor</code>.</p>
25   *
26   * @version $Id$
27   */
28  public class MappedPropertyTestCase extends TestCase {
29  
30      // ---------------------------------------------------------- Constructors
31  
32      /**
33       * Construct a new instance of this test case.
34       *
35       * @param name Name of the test case
36       */
37      public MappedPropertyTestCase(final String name) {
38          super(name);
39      }
40  
41      // -------------------------------------------------- Overall Test Methods
42  
43      /**
44       * Run this Test
45       */
46      public static void main(final String[] args) {
47        junit.textui.TestRunner.run(suite());
48      }
49  
50      /**
51       * Set up instance variables required by this test case.
52       */
53      @Override
54      public void setUp() throws Exception {
55      }
56  
57      /**
58       * Return the tests included in this test suite.
59       */
60      public static Test suite() {
61          return (new TestSuite(MappedPropertyTestCase.class));
62      }
63  
64      /**
65       * Tear down instance variables required by this test case.
66       */
67      @Override
68      public void tearDown() {
69      }
70  
71      // ------------------------------------------------ Individual Test Methods
72  
73      /**
74       * Test valid method name
75       */
76      public void testFound() {
77          final String property = "mapproperty";
78          final Class<?> clazz = MappedPropertyTestBean.class;
79          try {
80              final MappedPropertyDescriptor desc
81                  = new MappedPropertyDescriptor(property, clazz);
82              assertNotNull("Getter is missing", desc.getMappedReadMethod());
83              assertNotNull("Setter is missing", desc.getMappedWriteMethod());
84          } catch (final Exception ex) {
85              fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
86          }
87      }
88  
89      /**
90       * Test boolean "is" method name
91       */
92      public void testBooleanMapped() {
93          final String property = "mappedBoolean";
94          final Class<?> clazz = MappedPropertyTestBean.class;
95          try {
96              final MappedPropertyDescriptor desc
97                  = new MappedPropertyDescriptor(property, clazz);
98              assertNotNull("Getter is missing", desc.getMappedReadMethod());
99              assertNotNull("Setter is missing", desc.getMappedWriteMethod());
100         } catch (final Exception ex) {
101             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
102         }
103     }
104 
105     /**
106      * Test invalid method name
107      */
108     public void testNotFound() {
109         final String property = "xxxxxxx";
110         final Class<?> clazz = MappedPropertyTestBean.class;
111         try {
112             new MappedPropertyDescriptor(property, clazz);
113             fail("Property '" + property + "' found in " + clazz.getName());
114         } catch (final Exception ex) {
115             // expected result
116         }
117     }
118 
119     /**
120      * Test Mapped Property - Getter only
121      */
122     public void testMappedGetterOnly() {
123         final String property = "mappedGetterOnly";
124         final Class<?> clazz = MappedPropertyTestBean.class;
125         try {
126             final MappedPropertyDescriptor desc
127                 = new MappedPropertyDescriptor(property, clazz);
128             assertNotNull("Getter is missing", desc.getMappedReadMethod());
129             assertNull("Setter is found", desc.getMappedWriteMethod());
130         } catch (final Exception ex) {
131             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
132         }
133     }
134 
135     /**
136      * Test Mapped Property - Setter Only
137      */
138     public void testMappedSetterOnly() {
139         final String property = "mappedSetterOnly";
140         final Class<?> clazz = MappedPropertyTestBean.class;
141         try {
142             final MappedPropertyDescriptor desc
143                 = new MappedPropertyDescriptor(property, clazz);
144             assertNull("Getter is found", desc.getMappedReadMethod());
145             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
146         } catch (final Exception ex) {
147             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
148         }
149     }
150 
151     /**
152      * Test Mapped Property - Invalid Setter
153      */
154     public void testInvalidSetter() {
155         final String property = "invalidSetter";
156         final Class<?> clazz = MappedPropertyTestBean.class;
157         try {
158             final MappedPropertyDescriptor desc
159                 = new MappedPropertyDescriptor(property, clazz);
160             assertNotNull("Getter is missing", desc.getMappedReadMethod());
161             assertNull("Setter is found", desc.getMappedWriteMethod());
162         } catch (final Exception ex) {
163             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
164         }
165     }
166 
167     /**
168      * Test Mapped Property - Invalid Getter
169      */
170     public void testInvalidGetter() {
171         final String property = "invalidGetter";
172         final Class<?> clazz = MappedPropertyTestBean.class;
173         try {
174             final MappedPropertyDescriptor desc
175                 = new MappedPropertyDescriptor(property, clazz);
176             assertNull("Getter is found", desc.getMappedReadMethod());
177             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
178         } catch (final Exception ex) {
179             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
180         }
181     }
182 
183     /**
184      * Test Mapped Property - Different Types
185      *
186      * Expect to find the getDifferentTypes() method, but not
187      * the setDifferentTypes() method because setDifferentTypes()
188      * sets and Integer, while getDifferentTypes() returns a Long.
189      */
190     public void testDifferentTypes() {
191         final String property = "differentTypes";
192         final Class<?> clazz = MappedPropertyTestBean.class;
193         try {
194             final MappedPropertyDescriptor desc
195                 = new MappedPropertyDescriptor(property, clazz);
196             assertNotNull("Getter is missing", desc.getMappedReadMethod());
197             assertNull("Setter is found", desc.getMappedWriteMethod());
198         } catch (final Exception ex) {
199             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
200         }
201     }
202 
203     /**
204      * Test Map getter
205      */
206     public void testMapGetter() {
207         final MappedPropertyTestBean bean = new MappedPropertyTestBean();
208         try {
209             final String testValue = "test value";
210             final String testKey   = "testKey";
211             BeanUtils.setProperty(bean, "myMap("+testKey+")", "test value");
212             assertEquals("Map getter", testValue, bean.getMyMap().get(testKey));
213         } catch (final Exception ex) {
214             fail("Test set mapped property failed: " + ex);
215         }
216     }
217 
218 
219     /**
220      * Test property with any two args
221      */
222     public void testAnyArgsProperty() {
223         final String property = "anyMapped";
224         final Class<?> clazz = MappedPropertyTestBean.class;
225         try {
226             final MappedPropertyDescriptor desc
227                 = new MappedPropertyDescriptor(property, clazz);
228             assertNull("Getter is found", desc.getMappedReadMethod());
229             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
230         } catch (final Exception ex) {
231             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
232         }
233     }
234 
235     /**
236      * Test property with two primitive args
237      */
238     public void testPrimitiveArgsProperty() {
239         final String property = "mappedPrimitive";
240         final Class<?> clazz = MappedPropertyTestBean.class;
241         try {
242             final MappedPropertyDescriptor desc
243                 = new MappedPropertyDescriptor(property, clazz);
244             assertNull("Getter is found", desc.getMappedReadMethod());
245             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
246         } catch (final Exception ex) {
247             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
248         }
249     }
250 
251     /**
252      * Test 'protected' mapped property
253      */
254     public void testProtected() {
255         final String property = "protectedProperty";
256         final Class<?> clazz = MappedPropertyTestBean.class;
257         try {
258             new MappedPropertyDescriptor(property, clazz);
259             fail("Property '" + property + "' found in " + clazz.getName());
260         } catch (final Exception ex) {
261             // expected result
262         }
263     }
264 
265 
266     /**
267      * Test 'public' method in parent
268      */
269     public void testPublicParentMethod() {
270         final String property = "mapproperty";
271         final Class<?> clazz = MappedPropertyChildBean.class;
272         try {
273             final MappedPropertyDescriptor desc
274                 = new MappedPropertyDescriptor(property, clazz);
275             assertNotNull("Getter is missing", desc.getMappedReadMethod());
276             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
277         } catch (final Exception ex) {
278             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
279         }
280     }
281 
282     /**
283      * Test 'protected' method in parent
284      */
285     public void testProtectedParentMethod() {
286         final String property = "protectedMapped";
287         final Class<?> clazz = MappedPropertyChildBean.class;
288         try {
289             new MappedPropertyDescriptor(property, clazz);
290             fail("Property '" + property + "' found in " + clazz.getName());
291         } catch (final Exception ex) {
292         }
293     }
294 
295 
296     /**
297      * Test Interface with mapped property
298      */
299     public void testInterfaceMapped() {
300         final String property = "mapproperty";
301         final Class<?> clazz = MappedPropertyTestInterface.class;
302         try {
303             final MappedPropertyDescriptor desc
304                 = new MappedPropertyDescriptor(property, clazz);
305             assertNotNull("Getter is missing", desc.getMappedReadMethod());
306             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
307         } catch (final Exception ex) {
308             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
309         }
310     }
311 
312     /**
313      * Test property not found in interface
314      */
315     public void testInterfaceNotFound() {
316         final String property = "XXXXXX";
317         final Class<?> clazz = MappedPropertyTestInterface.class;
318         try {
319             new MappedPropertyDescriptor(property, clazz);
320             fail("Property '" + property + "' found in " + clazz.getName());
321         } catch (final Exception ex) {
322         }
323     }
324 
325     /**
326      * Test Interface Inherited mapped property
327      */
328     public void testChildInterfaceMapped() {
329         final String property = "mapproperty";
330         final Class<?> clazz = MappedPropertyChildInterface.class;
331         try {
332             final MappedPropertyDescriptor desc
333                 = new MappedPropertyDescriptor(property, clazz);
334             assertNotNull("Getter is missing", desc.getMappedReadMethod());
335             assertNotNull("Setter is missing", desc.getMappedWriteMethod());
336         } catch (final Exception ex) {
337             fail("Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex);
338         }
339     }
340 }