View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.fail;
26  
27  import java.beans.IntrospectionException;
28  
29  import org.apache.commons.beanutils2.testbeans.MappedPropertyChildBean;
30  import org.apache.commons.beanutils2.testbeans.MappedPropertyChildInterface;
31  import org.apache.commons.beanutils2.testbeans.MappedPropertyTestBean;
32  import org.apache.commons.beanutils2.testbeans.MappedPropertyTestInterface;
33  import org.junit.Ignore;
34  import org.junit.Test;
35  
36  /**
37   * <p>
38   * Test Case for the <code>MappedPropertyDescriptor</code>.
39   * </p>
40   * Extracted from BeanUtils1
41   */
42  public class MappedPropertyTestCase
43  {
44  
45      @Test( expected = IntrospectionException.class )
46      public void testConstructorNullClass()
47          throws Exception
48      {
49          new MappedPropertyDescriptor( null, MappedPropertyTestBean.class );
50      }
51  
52      @Test( expected = IntrospectionException.class )
53      public void testConstructorEmptyStringClass()
54          throws Exception
55      {
56          new MappedPropertyDescriptor( "", MappedPropertyTestBean.class );
57      }
58  
59      @Test( expected = IntrospectionException.class )
60      public void testConstructorNullNullNull()
61          throws Exception
62      {
63          new MappedPropertyDescriptor( null, null, null );
64      }
65  
66      @Test( expected = IntrospectionException.class )
67      public void testConstructorEmptyStringNullNull()
68          throws Exception
69      {
70          new MappedPropertyDescriptor( "", null, null );
71      }
72  
73      /**
74       * Test valid method name
75       */
76      @Test
77      public void testFound()
78      {
79          String property = "mapproperty";
80          Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
81          try
82          {
83              MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
84              assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
85              assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
86          }
87          catch ( Exception ex )
88          {
89              fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
90          }
91      }
92  
93      /**
94       * Test boolean "is" method name
95       */
96      @Test
97      public void testBooleanMapped()
98      {
99          String property = "mappedBoolean";
100         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
101         try
102         {
103             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
104             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
105             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
106         }
107         catch ( Exception ex )
108         {
109             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
110         }
111     }
112 
113     /**
114      * Test invalid method name
115      */
116     @Test
117     public void testNotFound()
118     {
119         String property = "xxxxxxx";
120         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
121         try
122         {
123             new MappedPropertyDescriptor( property, clazz );
124             fail( "Property '" + property + "' found in " + clazz.getName() );
125         }
126         catch ( Exception ex )
127         {
128             // expected result
129         }
130     }
131 
132     /**
133      * Test Mapped Property - Getter only
134      */
135     @Test
136     public void testMappedGetterOnly()
137     {
138         String property = "mappedGetterOnly";
139         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
140         try
141         {
142             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
143             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
144             assertNull( "Setter is found", desc.getMappedWriteMethod() );
145         }
146         catch ( Exception ex )
147         {
148             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
149         }
150     }
151 
152     /**
153      * Test Mapped Property - Setter Only
154      */
155     @Test
156     public void testMappedSetterOnly()
157     {
158         String property = "mappedSetterOnly";
159         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
160         try
161         {
162             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
163             assertNull( "Getter is found", desc.getMappedReadMethod() );
164             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
165         }
166         catch ( Exception ex )
167         {
168             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
169         }
170     }
171 
172     /**
173      * Test Mapped Property - Invalid Setter
174      */
175     @Test
176     public void testInvalidSetter()
177     {
178         String property = "invalidSetter";
179         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
180         try
181         {
182             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
183             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
184             assertNull( "Setter is found", desc.getMappedWriteMethod() );
185         }
186         catch ( Exception ex )
187         {
188             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
189         }
190     }
191 
192     /**
193      * Test Mapped Property - Invalid Getter
194      */
195     @Test
196     public void testInvalidGetter()
197     {
198         String property = "invalidGetter";
199         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
200         try
201         {
202             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
203             assertNull( "Getter is found", desc.getMappedReadMethod() );
204             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
205         }
206         catch ( Exception ex )
207         {
208             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
209         }
210     }
211 
212     /**
213      * Test Mapped Property - Different Types Expect to find the getDifferentTypes() method, but not the
214      * setDifferentTypes() method because setDifferentTypes() sets and Integer, while getDifferentTypes() returns a
215      * Long.
216      */
217     @Test
218     public void testDifferentTypes()
219     {
220         String property = "differentTypes";
221         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
222         try
223         {
224             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
225             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
226             assertNull( "Setter is found", desc.getMappedWriteMethod() );
227         }
228         catch ( Exception ex )
229         {
230             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
231         }
232     }
233 
234     /**
235      * Test Mpa getter
236      */
237     @Test
238     @Ignore // TODO review this test
239     public void testMapGetter()
240     {
241         MappedPropertyTestBean bean = new MappedPropertyTestBean();
242         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
243         String property = "myMap";
244         try
245         {
246             String testValue = "test value";
247             String testKey = "testKey";
248             // BeanUtils.setProperty( bean, "myMap(" + testKey + ")", "test value" );
249             assertEquals( "Map getter", testValue, bean.getMyMap().get( testKey ) );
250         }
251         catch ( Exception ex )
252         {
253             fail( "Test set mapped property failed: " + ex );
254         }
255     }
256 
257     /**
258      * Test property with any two args
259      */
260     @Test
261     public void testAnyArgsProperty()
262     {
263         String property = "anyMapped";
264         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
265         try
266         {
267             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
268             assertNull( "Getter is found", desc.getMappedReadMethod() );
269             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
270         }
271         catch ( Exception ex )
272         {
273             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
274         }
275     }
276 
277     /**
278      * Test property with two primitve args
279      */
280     @Test
281     public void testPrimitiveArgsProperty()
282     {
283         String property = "mappedPrimitive";
284         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
285         try
286         {
287             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
288             assertNull( "Getter is found", desc.getMappedReadMethod() );
289             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
290         }
291         catch ( Exception ex )
292         {
293             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
294         }
295     }
296 
297     /**
298      * Test 'protected' mapped property
299      */
300     @Test( expected = IntrospectionException.class )
301     public void testProtected()
302         throws IntrospectionException
303     {
304         String property = "protectedProperty";
305         Class<MappedPropertyTestBean> clazz = MappedPropertyTestBean.class;
306         new MappedPropertyDescriptor( property, clazz );
307     }
308 
309     /**
310      * Test 'public' method in parent
311      */
312     @Test
313     public void testPublicParentMethod()
314     {
315         String property = "mapproperty";
316         Class<MappedPropertyChildBean> clazz = MappedPropertyChildBean.class;
317         try
318         {
319             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
320             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
321             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
322         }
323         catch ( Exception ex )
324         {
325             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
326         }
327     }
328 
329     /**
330      * Test 'protected' method in parent
331      */
332     @Test( expected = IntrospectionException.class )
333     public void testProtectedParentMethod()
334         throws IntrospectionException
335     {
336         String property = "protectedMapped";
337         Class<MappedPropertyChildBean> clazz = MappedPropertyChildBean.class;
338         new MappedPropertyDescriptor( property, clazz );
339     }
340 
341     /**
342      * Test Interface with mapped property
343      */
344     @Test
345     public void testInterfaceMapped()
346     {
347         String property = "mapproperty";
348         Class<MappedPropertyTestInterface> clazz = MappedPropertyTestInterface.class;
349         try
350         {
351             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
352             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
353             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
354         }
355         catch ( Exception ex )
356         {
357             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
358         }
359     }
360 
361     /**
362      * Test property not found in interface
363      */
364     @Test( expected = IntrospectionException.class )
365     public void testInterfaceNotFound()
366         throws IntrospectionException
367     {
368         String property = "XXXXXX";
369         Class<MappedPropertyTestInterface> clazz = MappedPropertyTestInterface.class;
370         new MappedPropertyDescriptor( property, clazz );
371     }
372 
373     /**
374      * Test Interface Inherited mapped property
375      */
376     @Test
377     public void testChildInterfaceMapped()
378     {
379         String property = "mapproperty";
380         Class<MappedPropertyChildInterface> clazz = MappedPropertyChildInterface.class;
381         try
382         {
383             MappedPropertyDescriptor desc = new MappedPropertyDescriptor( property, clazz );
384             assertNotNull( "Getter is missing", desc.getMappedReadMethod() );
385             assertNotNull( "Setter is missing", desc.getMappedWriteMethod() );
386         }
387         catch ( Exception ex )
388         {
389             fail( "Property '" + property + "' Not Found in " + clazz.getName() + ": " + ex );
390         }
391     }
392 }