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
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29
30
31
32
33
34
35
36
37
38 public class WrapDynaBeanTestCase extends BasicDynaBeanTestCase {
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public WrapDynaBeanTestCase(final String name) {
53
54 super(name);
55
56 }
57
58
59
60
61
62
63
64
65 @Override
66 public void setUp() throws Exception {
67
68 bean = new WrapDynaBean(new TestBean());
69
70 }
71
72
73
74
75
76 public static Test suite() {
77
78 return (new TestSuite(WrapDynaBeanTestCase.class));
79
80 }
81
82
83
84
85
86 @Override
87 public void tearDown() {
88
89 bean = null;
90
91 }
92
93
94
95
96
97
98
99
100
101 public void testSimpleProperties() {
102
103 checkSimplePropertyAccess();
104
105 }
106
107
108
109
110
111
112 private void checkSimplePropertyAccess() {
113
114 try {
115 bean.get("invalidProperty");
116 fail("Invalid get should have thrown IllegalArgumentException");
117 } catch (final IllegalArgumentException t) {
118
119 }
120
121
122 try {
123 bean.set("invalidProperty", "XYZ");
124 fail("Invalid set should have thrown IllegalArgumentException");
125 } catch (final IllegalArgumentException t) {
126
127 }
128
129
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
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
149
150 public void testIndexedProperties() {
151
152
153 try {
154 bean.get("invalidProperty", 0);
155 fail("Invalid get should have thrown IllegalArgumentException");
156 } catch (final IllegalArgumentException t) {
157
158 }
159
160
161 try {
162 bean.set("invalidProperty", 0, "XYZ");
163 fail("Invalid set should have thrown IllegalArgumentException");
164 } catch (final IllegalArgumentException t) {
165
166 }
167
168
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
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
189
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
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
211 } catch (final Throwable t) {
212 fail("Exception: " + t);
213 }
214
215 }
216
217
218
219
220
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
231
232 } catch (final UnsupportedOperationException t) {
233
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
244
245 } catch (final UnsupportedOperationException t) {
246
247 } catch (final Throwable t) {
248 fail("Exception: " + t);
249 }
250
251 }
252
253
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
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
275
276
277 @Override
278 public void testSerialization() {
279
280
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
288 final WrapDynaBean bean = (WrapDynaBean)serializeDeserialize(origBean, "First Test");
289 assertEquals("bean value", newValue, bean.get("intProperty"));
290
291 }
292
293
294
295
296 private Object serializeDeserialize(final Object target, final String text) {
297
298
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
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
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
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
345
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
356
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 }