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  
18  
19  package org.apache.commons.beanutils;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.ObjectInputStream;
24  import java.io.ObjectOutputStream;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  
30  /**
31   * <p>Test Case for the <code>WrapDynaBean</code> implementation class.
32   * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
33   * because the two classes provide similar levels of functionality.</p>
34   *
35   * @version $Id$
36   */
37  
38  public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase {
39  
40  
41      // ---------------------------------------------------- Instance Variables
42  
43  
44      // ---------------------------------------------------------- Constructors
45  
46  
47      /**
48       * Construct a new instance of this test case.
49       *
50       * @param name Name of the test case
51       */
52      public WrapDynaBeanTestCase(final String name) {
53  
54          super(name);
55  
56      }
57  
58  
59      // -------------------------------------------------- Overall Test Methods
60  
61  
62      /**
63       * Set up instance variables required by this test case.
64       */
65      @Override
66      public void setUp() throws Exception {
67  
68          bean = new WrapDynaBean(new TestBean());
69  
70      }
71  
72  
73      /**
74       * Return the tests included in this test suite.
75       */
76      public static Test suite() {
77  
78          return (new TestSuite(WrapDynaBeanTestCase.class));
79  
80      }
81  
82  
83      /**
84       * Tear down instance variables required by this test case.
85       */
86      @Override
87      public void tearDown() {
88  
89          bean = null;
90  
91      }
92  
93  
94  
95      // ------------------------------------------------ Individual Test Methods
96  
97  
98      /**
99       * The <code>set()</code> method.
100      */
101     public void testSimpleProperties() {
102 
103         checkSimplePropertyAccess();
104 
105     }
106 
107 
108     /**
109      * Helper method for testing whether basic access to properties works as
110      * expected.
111      */
112     private void checkSimplePropertyAccess() {
113         // Invalid getter
114         try {
115             bean.get("invalidProperty");
116             fail("Invalid get should have thrown IllegalArgumentException");
117         } catch (final IllegalArgumentException t) {
118             // Expected result
119         }
120 
121         // Invalid setter
122         try {
123             bean.set("invalidProperty", "XYZ");
124             fail("Invalid set should have thrown IllegalArgumentException");
125         } catch (final IllegalArgumentException t) {
126             // Expected result
127         }
128 
129         // Set up initial Value
130         String testValue = "Original Value";
131         final String testProperty = "stringProperty";
132         final TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance();
133         instance.setStringProperty(testValue);
134         assertEquals("Check String property", testValue, instance.getStringProperty());
135 
136         // Test Valid Get & Set
137         try {
138             testValue = "Some new value";
139             bean.set(testProperty, testValue);
140             assertEquals("Test Set", testValue, instance.getStringProperty());
141             assertEquals("Test Get", testValue, bean.get(testProperty));
142         } catch (final IllegalArgumentException t) {
143             fail("Get threw exception: " + t);
144         }
145     }
146 
147     /**
148      * The <code>set()</code> method.
149      */
150     public void testIndexedProperties() {
151 
152         // Invalid getter
153         try {
154             bean.get("invalidProperty", 0);
155             fail("Invalid get should have thrown IllegalArgumentException");
156         } catch (final IllegalArgumentException t) {
157             // Expected result
158         }
159 
160         // Invalid setter
161         try {
162             bean.set("invalidProperty", 0, "XYZ");
163             fail("Invalid set should have thrown IllegalArgumentException");
164         } catch (final IllegalArgumentException t) {
165             // Expected result
166         }
167 
168         // Set up initial Value
169         String testValue = "Original Value";
170         final String testProperty = "stringIndexed";
171         final TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance();
172         instance.setStringIndexed(0, testValue);
173         assertEquals("Check String property", testValue, instance.getStringIndexed(0));
174 
175         // Test Valid Get & Set
176         try {
177             testValue = "Some new value";
178             bean.set(testProperty, 0, testValue);
179             assertEquals("Test Set", testValue, instance.getStringIndexed(0));
180             assertEquals("Test Get", testValue, bean.get(testProperty, 0));
181         } catch (final IllegalArgumentException t) {
182             fail("Get threw exception: " + t);
183         }
184 
185     }
186 
187     /**
188      * The <code>contains()</code> method is not supported by the
189      * <code>WrapDynaBean</code> implementation class.
190      */
191     @Override
192     public void testMappedContains() {
193 
194         try {
195             assertTrue("Can see first key",
196                     bean.contains("mappedProperty", "First Key"));
197             fail("Should have thrown UnsupportedOperationException");
198         } catch (final UnsupportedOperationException t) {
199             // Expected result
200         } catch (final Throwable t) {
201             fail("Exception: " + t);
202         }
203 
204 
205         try {
206             assertTrue("Can not see unknown key",
207                     !bean.contains("mappedProperty", "Unknown Key"));
208             fail("Should have thrown UnsupportedOperationException");
209         } catch (final UnsupportedOperationException t) {
210             // Expected result
211         } catch (final Throwable t) {
212             fail("Exception: " + t);
213         }
214 
215     }
216 
217 
218     /**
219      * The <code>remove()</code> method is not supported by the
220      * <code>WrapDynaBean</code> implementation class.
221      */
222     @Override
223     public void testMappedRemove() {
224 
225         try {
226             assertTrue("Can see first key",
227                     bean.contains("mappedProperty", "First Key"));
228             bean.remove("mappedProperty", "First Key");
229             fail("Should have thrown UnsupportedOperationException");
230             //            assertTrue("Can not see first key",
231             //         !bean.contains("mappedProperty", "First Key"));
232         } catch (final UnsupportedOperationException t) {
233             // Expected result
234         } catch (final Throwable t) {
235             fail("Exception: " + t);
236         }
237 
238         try {
239             assertTrue("Can not see unknown key",
240                     !bean.contains("mappedProperty", "Unknown Key"));
241             bean.remove("mappedProperty", "Unknown Key");
242             fail("Should have thrown UnsupportedOperationException");
243             //            assertTrue("Can not see unknown key",
244             //         !bean.contains("mappedProperty", "Unknown Key"));
245         } catch (final UnsupportedOperationException t) {
246             // Expected result
247         } catch (final Throwable t) {
248             fail("Exception: " + t);
249         }
250 
251     }
252 
253     /** Tests getInstance method */
254     public void testGetInstance() {
255         final AlphaBean alphaBean = new AlphaBean("Now On Air... John Peel");
256         final WrapDynaBean dynaBean = new WrapDynaBean(alphaBean);
257         final Object wrappedInstance = dynaBean.getInstance();
258         assertTrue("Object type is AlphaBean", wrappedInstance instanceof AlphaBean);
259         final AlphaBean wrappedAlphaBean = (AlphaBean) wrappedInstance;
260         assertTrue("Same Object", wrappedAlphaBean == alphaBean);
261     }
262 
263     /** Tests the newInstance implementation for WrapDynaClass */
264     public void testNewInstance() throws Exception {
265         final WrapDynaClass dynaClass = WrapDynaClass.createDynaClass(AlphaBean.class);
266         final Object createdInstance = dynaClass.newInstance();
267         assertTrue("Object type is WrapDynaBean", createdInstance instanceof WrapDynaBean);
268         final WrapDynaBean dynaBean = (WrapDynaBean) createdInstance;
269         assertTrue("Object type is AlphaBean", dynaBean.getInstance() instanceof AlphaBean);
270     }
271 
272 
273     /**
274      * Serialization and deserialization tests.
275      * (WrapDynaBean is now serializable, although WrapDynaClass still is not)
276      */
277     @Override
278     public void testSerialization() {
279 
280         // Create a bean and set a value
281         final WrapDynaBean origBean = new WrapDynaBean(new TestBean());
282         final Integer newValue = new Integer(789);
283         assertEquals("origBean default", new Integer(123), origBean.get("intProperty"));
284         origBean.set("intProperty", newValue);
285         assertEquals("origBean new value", newValue, origBean.get("intProperty"));
286 
287         // Serialize/Deserialize & test value
288         final WrapDynaBean bean = (WrapDynaBean)serializeDeserialize(origBean, "First Test");
289         assertEquals("bean value", newValue, bean.get("intProperty"));
290 
291     }
292 
293     /**
294      * Do serialization and deserialization.
295      */
296     private Object serializeDeserialize(final Object target, final String text) {
297 
298         // Serialize the test object
299         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
300         try {
301             final ObjectOutputStream oos = new ObjectOutputStream(baos);
302             oos.writeObject(target);
303             oos.flush();
304             oos.close();
305         } catch (final Exception e) {
306             fail(text + ": Exception during serialization: " + e);
307         }
308 
309         // Deserialize the test object
310         Object result = null;
311         try {
312             final ByteArrayInputStream bais =
313                 new ByteArrayInputStream(baos.toByteArray());
314             final ObjectInputStream ois = new ObjectInputStream(bais);
315             result = ois.readObject();
316             bais.close();
317         } catch (final Exception e) {
318             fail(text + ": Exception during deserialization: " + e);
319         }
320         return result;
321 
322     }
323 
324     /**
325      * Tests whether a WrapDynaClass can be provided when constructing a bean.
326      */
327     public void testInitWithDynaClass() {
328         final WrapDynaClass clazz = WrapDynaClass.createDynaClass(TestBean.class);
329         bean = new WrapDynaBean(new TestBean(), clazz);
330         assertSame("Wrong DynaClass", clazz, bean.getDynaClass());
331         checkSimplePropertyAccess();
332     }
333 
334     /**
335      * Tests whether caching works for WrapDynaClass objects.
336      */
337     public void testGetWrapDynaClassFromCache() {
338         final WrapDynaClass clazz = WrapDynaClass.createDynaClass(TestBean.class);
339         assertSame("Instance not cached", clazz,
340                 WrapDynaClass.createDynaClass(TestBean.class));
341     }
342 
343     /**
344      * Tests whether the PropertyUtilsBean instance associated with a WrapDynaClass is
345      * taken into account when accessing an instance from the cache.
346      */
347     public void testGetWrapDynaClassFromCacheWithPropUtils() {
348         final WrapDynaClass clazz = WrapDynaClass.createDynaClass(TestBean.class);
349         final PropertyUtilsBean pu = new PropertyUtilsBean();
350         final WrapDynaClass clazz2 = WrapDynaClass.createDynaClass(TestBean.class, pu);
351         assertNotSame("Got same instance from cache", clazz, clazz2);
352     }
353 
354     /**
355      * Tests whether a custom PropertyUtilsBean instance can be used for introspection of
356      * bean properties.
357      */
358     public void testIntrospectionWithCustomPropUtils() {
359         final PropertyUtilsBean pu = new PropertyUtilsBean();
360         pu.addBeanIntrospector(new FluentPropertyBeanIntrospector());
361         final WrapDynaClass dynaClass = WrapDynaClass.createDynaClass(
362                 FluentIntrospectionTestBean.class, pu);
363         final FluentIntrospectionTestBean obj = new FluentIntrospectionTestBean();
364         bean = new WrapDynaBean(obj, dynaClass);
365         bean.set("fluentProperty", "testvalue");
366         assertEquals("Property not set", "testvalue", obj.getStringProperty());
367     }
368 }