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  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  
29  /***
30   * <p>Test Case for the <code>WrapDynaBean</code> implementation class.
31   * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
32   * because the two classes provide similar levels of functionality.</p>
33   *
34   * @author Craig R. McClanahan
35   * @version $Revision: 469737 $ $Date: 2006-11-01 01:16:55 +0000 (Wed, 01 Nov 2006) $
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(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      public void setUp() throws Exception {
66  
67          bean = new WrapDynaBean(new TestBean());
68  
69      }
70  
71  
72      /***
73       * Return the tests included in this test suite.
74       */
75      public static Test suite() {
76  
77          return (new TestSuite(WrapDynaBeanTestCase.class));
78  
79      }
80  
81  
82      /***
83       * Tear down instance variables required by this test case.
84       */
85      public void tearDown() {
86  
87          bean = null;
88  
89      }
90  
91  
92  
93      // ------------------------------------------------ Individual Test Methods
94  
95  
96      /***
97       * The <code>set()</code> method.
98       */
99      public void testSimpleProperties() {
100 
101         // Invalid getter
102         try {
103             Object result = bean.get("invalidProperty");
104             fail("Invalid get should have thrown IllegalArgumentException");
105         } catch (IllegalArgumentException t) {
106             ; // Expected result
107         }
108 
109         // Invalid setter
110         try {
111             bean.set("invalidProperty", "XYZ");
112             fail("Invalid set should have thrown IllegalArgumentException");
113         } catch (IllegalArgumentException t) {
114             ; // Expected result
115         }
116 
117         // Set up initial Value
118         String testValue = "Original Value";
119         String testProperty = "stringProperty";
120         TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance();
121         instance.setStringProperty(testValue);
122         assertEquals("Check String property", testValue, instance.getStringProperty());
123 
124         // Test Valid Get & Set
125         try {
126             testValue = "Some new value";
127             bean.set(testProperty, testValue);
128             assertEquals("Test Set", testValue, instance.getStringProperty());
129             assertEquals("Test Get", testValue, bean.get(testProperty));
130         } catch (IllegalArgumentException t) {
131             fail("Get threw exception: " + t);
132         }
133 
134     }
135 
136     /***
137      * The <code>set()</code> method.
138      */
139     public void testIndexedProperties() {
140 
141         // Invalid getter
142         try {
143             Object result = bean.get("invalidProperty", 0);
144             fail("Invalid get should have thrown IllegalArgumentException");
145         } catch (IllegalArgumentException t) {
146             ; // Expected result
147         }
148 
149         // Invalid setter
150         try {
151             bean.set("invalidProperty", 0, "XYZ");
152             fail("Invalid set should have thrown IllegalArgumentException");
153         } catch (IllegalArgumentException t) {
154             ; // Expected result
155         }
156 
157         // Set up initial Value
158         String testValue = "Original Value";
159         String testProperty = "stringIndexed";
160         TestBean instance = (TestBean)((WrapDynaBean)bean).getInstance();
161         instance.setStringIndexed(0, testValue);
162         assertEquals("Check String property", testValue, instance.getStringIndexed(0));
163 
164         // Test Valid Get & Set
165         try {
166             testValue = "Some new value";
167             bean.set(testProperty, 0, testValue);
168             assertEquals("Test Set", testValue, instance.getStringIndexed(0));
169             assertEquals("Test Get", testValue, bean.get(testProperty, 0));
170         } catch (IllegalArgumentException t) {
171             fail("Get threw exception: " + t);
172         }
173 
174     }
175 
176     /***
177      * The <code>contains()</code> method is not supported by the
178      * <code>WrapDynaBean</code> implementation class.
179      */
180     public void testMappedContains() {
181 
182         try {
183             assertTrue("Can see first key",
184                     bean.contains("mappedProperty", "First Key"));
185             fail("Should have thrown UnsupportedOperationException");
186         } catch (UnsupportedOperationException t) {
187             // Expected result
188         } catch (Throwable t) {
189             fail("Exception: " + t);
190         }
191 
192 
193         try {
194             assertTrue("Can not see unknown key",
195                     !bean.contains("mappedProperty", "Unknown Key"));
196             fail("Should have thrown UnsupportedOperationException");
197         } catch (UnsupportedOperationException t) {
198             // Expected result
199         } catch (Throwable t) {
200             fail("Exception: " + t);
201         }
202 
203     }
204 
205 
206     /***
207      * The <code>remove()</code> method is not supported by the
208      * <code>WrapDynaBean</code> implementation class.
209      */
210     public void testMappedRemove() {
211 
212         try {
213             assertTrue("Can see first key",
214                     bean.contains("mappedProperty", "First Key"));
215             bean.remove("mappedProperty", "First Key");
216             fail("Should have thrown UnsupportedOperationException");
217             //            assertTrue("Can not see first key",
218             //         !bean.contains("mappedProperty", "First Key"));
219         } catch (UnsupportedOperationException t) {
220             // Expected result
221         } catch (Throwable t) {
222             fail("Exception: " + t);
223         }
224 
225         try {
226             assertTrue("Can not see unknown key",
227                     !bean.contains("mappedProperty", "Unknown Key"));
228             bean.remove("mappedProperty", "Unknown Key");
229             fail("Should have thrown UnsupportedOperationException");
230             //            assertTrue("Can not see unknown key",
231             //         !bean.contains("mappedProperty", "Unknown Key"));
232         } catch (UnsupportedOperationException t) {
233             // Expected result
234         } catch (Throwable t) {
235             fail("Exception: " + t);
236         }
237 
238     }
239 
240     /*** Tests getInstance method */
241     public void testGetInstance() {
242         AlphaBean alphaBean = new AlphaBean("Now On Air... John Peel");
243         WrapDynaBean dynaBean = new WrapDynaBean(alphaBean);
244         Object wrappedInstance = dynaBean.getInstance();
245         assertTrue("Object type is AlphaBean", wrappedInstance instanceof AlphaBean);
246         AlphaBean wrappedAlphaBean = (AlphaBean) wrappedInstance;
247         assertTrue("Same Object", wrappedAlphaBean == alphaBean);
248     }
249 
250     /*** Tests the newInstance implementation for WrapDynaClass */
251     public void testNewInstance() throws Exception {
252         WrapDynaClass dynaClass = WrapDynaClass.createDynaClass(AlphaBean.class);
253         Object createdInstance = dynaClass.newInstance();
254         assertTrue("Object type is WrapDynaBean", createdInstance instanceof WrapDynaBean);
255         WrapDynaBean dynaBean = (WrapDynaBean) createdInstance;
256         assertTrue("Object type is AlphaBean", dynaBean.getInstance() instanceof AlphaBean);
257     }
258 
259 
260     /***
261      * Serialization and deserialization tests.
262      * (WrapDynaBean is now serializable, although WrapDynaClass still is not)
263      */
264     public void testSerialization() {
265 
266         // Create a bean and set a value
267         WrapDynaBean origBean = new WrapDynaBean(new TestBean());
268         Integer newValue = new Integer(789);
269         assertEquals("origBean default", new Integer(123), (Integer)origBean.get("intProperty"));
270         origBean.set("intProperty", newValue); 
271         assertEquals("origBean new value", newValue, (Integer)origBean.get("intProperty"));
272         
273         // Serialize/Deserialize & test value
274         WrapDynaBean bean = (WrapDynaBean)serializeDeserialize(origBean, "First Test");
275         assertEquals("bean value", newValue, (Integer)bean.get("intProperty"));
276         
277     }
278 
279     /***
280      * Do serialization and deserialization.
281      */
282     private Object serializeDeserialize(Object target, String text) {
283 
284         // Serialize the test object
285         ByteArrayOutputStream baos = new ByteArrayOutputStream();
286         try {
287             ObjectOutputStream oos = new ObjectOutputStream(baos);
288             oos.writeObject(target);
289             oos.flush();
290             oos.close();
291         } catch (Exception e) {
292             fail(text + ": Exception during serialization: " + e);
293         }
294 
295         // Deserialize the test object
296         Object result = null;
297         try {
298             ByteArrayInputStream bais =
299                 new ByteArrayInputStream(baos.toByteArray());
300             ObjectInputStream ois = new ObjectInputStream(bais);
301             result = ois.readObject();
302             bais.close();
303         } catch (Exception e) {
304             fail(text + ": Exception during deserialization: " + e);
305         }
306         return result;
307 
308     }
309 
310 }