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