1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
42
43
44
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
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
94
95
96 /***
97 * The <code>set()</code> method.
98 */
99 public void testSimpleProperties() {
100
101
102 try {
103 Object result = bean.get("invalidProperty");
104 fail("Invalid get should have thrown IllegalArgumentException");
105 } catch (IllegalArgumentException t) {
106 ;
107 }
108
109
110 try {
111 bean.set("invalidProperty", "XYZ");
112 fail("Invalid set should have thrown IllegalArgumentException");
113 } catch (IllegalArgumentException t) {
114 ;
115 }
116
117
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
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
142 try {
143 Object result = bean.get("invalidProperty", 0);
144 fail("Invalid get should have thrown IllegalArgumentException");
145 } catch (IllegalArgumentException t) {
146 ;
147 }
148
149
150 try {
151 bean.set("invalidProperty", 0, "XYZ");
152 fail("Invalid set should have thrown IllegalArgumentException");
153 } catch (IllegalArgumentException t) {
154 ;
155 }
156
157
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
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
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
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
218
219 } catch (UnsupportedOperationException t) {
220
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
231
232 } catch (UnsupportedOperationException t) {
233
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
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
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
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
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 }