1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.beanutils;
18
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.TreeMap;
26
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30
31
32
33
34
35
36 public class LazyDynaMapTestCase extends TestCase {
37
38 protected LazyDynaMap dynaMap = null;
39 protected String testProperty = "myProperty";
40 protected String testPropertyA = "myProperty-A";
41 protected String testPropertyB = "myProperty-B";
42 protected String testString1 = "myStringValue-1";
43 protected String testString2 = "myStringValue-2";
44 protected Integer testInteger1 = new Integer(30);
45 protected Integer testInteger2 = new Integer(40);
46 protected String testKey = "myKey";
47
48
49
50
51
52
53
54
55 public LazyDynaMapTestCase(final String name) {
56 super(name);
57 }
58
59
60
61
62
63
64 public static void main(final String[] args) {
65 junit.textui.TestRunner.run(suite());
66 }
67
68
69
70
71 public static Test suite() {
72 return (new TestSuite(LazyDynaMapTestCase.class));
73 }
74
75
76
77
78 @Override
79 public void setUp() throws Exception {
80 dynaMap = new LazyDynaMap();
81 dynaMap.setReturnNull(true);
82 }
83
84
85
86
87 @Override
88 public void tearDown() {
89 dynaMap = null;
90 }
91
92
93
94
95
96
97 public void testGeneral() {
98
99 assertEquals("Check DynaClass name", "TestBean", new LazyDynaMap("TestBean").getName());
100
101 }
102
103
104
105
106 public void testSimpleProperty() {
107
108
109 assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
110 assertNull("Check Value is null", dynaMap.get(testProperty));
111
112
113 dynaMap.set(testProperty, testInteger1);
114 assertEquals("Check First Value is correct", testInteger1, dynaMap.get(testProperty));
115 assertEquals("Check Property type is correct", Integer.class, dynaMap.getDynaProperty(testProperty).getType());
116
117
118 dynaMap.set(testProperty, testInteger2);
119 assertEquals("Check Second Value is correct", testInteger2, dynaMap.get(testProperty));
120
121
122 dynaMap.set(testProperty, testString1);
123 assertEquals("Check Third Value is correct", testString1, dynaMap.get(testProperty));
124
125 }
126
127
128
129
130 public void testSimplePropertyRestricted() {
131
132
133 dynaMap.setRestricted(true);
134 assertTrue("Check MutableDynaClass is restricted", dynaMap.isRestricted());
135
136
137 assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
138 assertNull("Check Value is null", dynaMap.get(testProperty));
139
140
141 try {
142 dynaMap.set(testProperty, testString1);
143 fail("expected IllegalArgumentException trying to add new property to restricted DynaClass");
144 } catch (final IllegalArgumentException expected) {
145
146 }
147
148 }
149
150
151
152
153 public void testMappedPropertyDefault() {
154
155
156 assertNull("Check Mapped Property doesn't exist", dynaMap.getDynaProperty(testProperty));
157 assertNull("Check Map is null", dynaMap.get(testProperty));
158 assertNull("Check Mapped Value is null", dynaMap.get(testProperty, testKey));
159
160
161 dynaMap.set(testProperty, testKey, testInteger1);
162 assertEquals("Check Mapped Property exists", HashMap.class, dynaMap.get(testProperty).getClass());
163 assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
164 assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((HashMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
165
166
167 dynaMap.set(testProperty, testKey, testInteger2);
168 assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
169 assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((HashMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
170 }
171
172
173
174
175 public void testMappedPropertyTreeMap() {
176
177
178 assertNull("Check Mapped Property doesn't exist", dynaMap.getDynaProperty(testProperty));
179 assertNull("Check Map is null", dynaMap.get(testProperty));
180
181
182 dynaMap.add(testProperty, TreeMap.class);
183 assertTrue("Check Property is mapped", dynaMap.getDynaProperty(testProperty).isMapped());
184 assertEquals("Check Property is correct type", TreeMap.class, dynaMap.getDynaProperty(testProperty).getType());
185 assertEquals("Check Mapped Property now exists", TreeMap.class, dynaMap.get(testProperty).getClass());
186
187
188 dynaMap.set(testProperty, testKey, testInteger1);
189 assertEquals("Check Mapped Property exists", TreeMap.class, dynaMap.get(testProperty).getClass());
190 assertEquals("Check First Mapped Value is correct(a)", testInteger1, dynaMap.get(testProperty, testKey));
191 assertEquals("Check First Mapped Value is correct(b)", testInteger1, ((TreeMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
192
193
194 dynaMap.set(testProperty, testKey, testInteger2);
195 assertEquals("Check Second Mapped Value is correct(a)", testInteger2, dynaMap.get(testProperty, testKey));
196 assertEquals("Check Second Mapped Value is correct(b)", testInteger2, ((TreeMap<?, ?>)dynaMap.get(testProperty)).get(testKey));
197 }
198
199
200
201
202 public void testMappedPropertyUtils() {
203
204 dynaMap.setReturnNull(false);
205
206
207 assertFalse("Check Mapped Property doesn't exist", dynaMap.isDynaProperty(testProperty));
208 assertNull("Check Map is null", dynaMap.get(testProperty));
209 assertNull("Check Mapped Value is null", dynaMap.get(testProperty, testKey));
210
211
212 try {
213 PropertyUtils.setProperty(dynaMap, testProperty+"("+testKey+")", testString1);
214 }
215 catch (final NoSuchMethodException ex) {
216 fail("testIndexedPropertyUtils threw "+ex);
217 }
218 catch (final InvocationTargetException ex) {
219 fail("testIndexedPropertyUtils threw "+ex);
220 }
221 catch (final IllegalAccessException ex) {
222 fail("testIndexedPropertyUtils threw "+ex);
223 }
224
225
226 assertEquals("Check Mapped Bean Value is correct", testString1, dynaMap.get(testProperty, testKey));
227
228 }
229
230
231
232
233 public void testMappedPropertyRestricted() {
234
235
236 dynaMap.setRestricted(true);
237 assertTrue("Check MutableDynaClass is restricted", dynaMap.isRestricted());
238
239
240 assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
241 assertNull("Check Value is null", dynaMap.get(testProperty));
242
243
244 try {
245 dynaMap.set(testProperty, testKey, testInteger1);
246 fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass");
247 } catch (final IllegalArgumentException expected) {
248
249 }
250
251 }
252
253
254
255
256 public void testMappedInvalidType() {
257 dynaMap.set(testProperty, new Integer(1));
258 assertFalse("Check Property is not mapped", dynaMap.getDynaProperty(testProperty).isMapped());
259 try {
260 dynaMap.set(testProperty, testKey, testInteger1);
261 fail("set(property, key, value) should have thrown IllegalArgumentException");
262 } catch (final IllegalArgumentException expected) {
263
264 }
265 }
266
267
268
269
270 public void testIndexedPropertyDefault() {
271
272 int index = 3;
273
274
275 assertNull("Check Indexed Property doesn't exist", dynaMap.getDynaProperty(testProperty));
276 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
277 assertNull("Check Indexed value is null", dynaMap.get(testProperty, index));
278
279
280 dynaMap.set(testProperty, index, testInteger1);
281 assertNotNull("Check Indexed Property is not null", dynaMap.get(testProperty));
282 assertEquals("Check Indexed Property is correct type", ArrayList.class, dynaMap.get(testProperty).getClass());
283 assertEquals("Check First Indexed Value is correct", testInteger1, dynaMap.get(testProperty, index));
284 assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((ArrayList<?>)dynaMap.get(testProperty)).size()));
285
286
287 index = index + 2;
288 dynaMap.set(testProperty, index, testString1);
289 assertEquals("Check Second Indexed Value is correct", testString1, dynaMap.get(testProperty, index));
290 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((ArrayList<?>)dynaMap.get(testProperty)).size()));
291 }
292
293
294
295
296 public void testIndexedLinkedList() {
297
298 int index = 3;
299
300
301 assertNull("Check Indexed Property doesn't exist", dynaMap.getDynaProperty(testProperty));
302 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
303
304
305 dynaMap.add(testProperty, LinkedList.class);
306 assertTrue("Check Property is indexed", dynaMap.getDynaProperty(testProperty).isIndexed());
307 assertEquals("Check Property is correct type", LinkedList.class, dynaMap.getDynaProperty(testProperty).getType());
308 assertEquals("Check Indexed Property now exists", LinkedList.class, dynaMap.get(testProperty).getClass());
309
310
311 dynaMap.set(testProperty, index, testString1);
312 assertEquals("Check Property type is correct", LinkedList.class, dynaMap.get(testProperty).getClass());
313 assertEquals("Check First Indexed Value is correct", testString1, dynaMap.get(testProperty, index));
314 assertEquals("Check First Array length is correct", new Integer(index+1), new Integer(((LinkedList<?>)dynaMap.get(testProperty)).size()));
315
316
317 index = index + 2;
318 dynaMap.set(testProperty, index, testInteger1);
319 assertEquals("Check Second Indexed Value is correct", testInteger1, dynaMap.get(testProperty, index));
320 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((LinkedList<?>)dynaMap.get(testProperty)).size()));
321 }
322
323
324
325
326 public void testIndexedPrimitiveArray() {
327
328 int index = 3;
329 final int[] primitiveArray = new int[0];
330
331
332 assertNull("Check Indexed Property doesn't exist", dynaMap.getDynaProperty(testProperty));
333 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
334
335
336 dynaMap.add(testProperty, primitiveArray.getClass());
337 assertEquals("Check Indexed Property exists", primitiveArray.getClass(), dynaMap.getDynaProperty(testProperty).getType());
338 assertTrue("Check Indexed Property exists", dynaMap.get(testProperty).getClass().isInstance(primitiveArray));
339
340
341 dynaMap.set(testProperty, index, testInteger1);
342 assertNotNull("Check Indexed Property is not null", dynaMap.get(testProperty));
343 assertEquals("Check Indexed Property is correct type", primitiveArray.getClass(), dynaMap.get(testProperty).getClass());
344 assertEquals("Check First Indexed Value is correct(a)", testInteger1, dynaMap.get(testProperty, index));
345 assertEquals("Check First Indexed Value is correct(b)", testInteger1, new Integer(((int[])dynaMap.get(testProperty))[index]));
346 assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((int[])dynaMap.get(testProperty)).length));
347
348
349 index = index + 2;
350 dynaMap.set(testProperty, index, testInteger2);
351 assertEquals("Check Second Indexed Value is correct(a)", testInteger2, dynaMap.get(testProperty, index));
352 assertEquals("Check Second Indexed Value is correct(b)", testInteger2, new Integer(((int[])dynaMap.get(testProperty))[index]));
353 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((int[])dynaMap.get(testProperty)).length));
354
355 }
356
357
358
359
360 public void testIndexedObjectArray() {
361
362 int index = 3;
363 final Object objectArray = new String[0];
364
365
366 assertNull("Check Indexed Property doesn't exist", dynaMap.getDynaProperty(testProperty));
367 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
368
369
370 dynaMap.add(testProperty, objectArray.getClass());
371 assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaMap.getDynaProperty(testProperty).getType());
372 assertTrue("Check Indexed Property exists", dynaMap.get(testProperty).getClass().isInstance(objectArray));
373
374
375 dynaMap.set(testProperty, index, testString1);
376 assertNotNull("Check Indexed Property is not null", dynaMap.get(testProperty));
377 assertEquals("Check Indexed Property is correct type", objectArray.getClass(), dynaMap.get(testProperty).getClass());
378 assertEquals("Check First Indexed Value is correct(a)", testString1, dynaMap.get(testProperty, index));
379 assertEquals("Check First Indexed Value is correct(b)", testString1, ((String[])dynaMap.get(testProperty))[index]);
380 assertEquals("Check Array length is correct", new Integer(index+1), new Integer(((String[])dynaMap.get(testProperty)).length));
381
382
383 index = index + 2;
384 dynaMap.set(testProperty, index, testString2);
385 assertEquals("Check Second Indexed Value is correct(a)", testString2, dynaMap.get(testProperty, index));
386 assertEquals("Check Second Indexed Value is correct(b)", testString2, ((String[])dynaMap.get(testProperty))[index]);
387 assertEquals("Check Second Array length is correct", new Integer(index+1), new Integer(((String[])dynaMap.get(testProperty)).length));
388 }
389
390
391
392
393 public void testIndexedDynaBeanArray() {
394
395 final int index = 3;
396 final Object objectArray = new LazyDynaBean[0];
397
398
399 assertNull("Check Indexed Property doesn't exist", dynaMap.getDynaProperty(testProperty));
400 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
401
402
403 dynaMap.add(testProperty, objectArray.getClass());
404 assertEquals("Check Indexed Property exists", objectArray.getClass(), dynaMap.getDynaProperty(testProperty).getType());
405 assertEquals("Check Indexed Property is correct type", objectArray.getClass(), dynaMap.get(testProperty).getClass());
406
407
408 for (int i = index; i >= 0; i--) {
409 assertEquals("Check Array Components initialized", LazyDynaBean.class, dynaMap.get(testProperty, index).getClass());
410 }
411
412 dynaMap.add(testPropertyB, objectArray.getClass());
413 final LazyDynaBean newBean = new LazyDynaBean();
414 newBean.set(testPropertyB, testString2);
415 dynaMap.set(testPropertyA, index, newBean);
416 assertEquals("Check Indexed Value is correct(a)", testString2, ((DynaBean)dynaMap.get(testPropertyA, index)).get(testPropertyB));
417
418 }
419
420
421
422
423 public void testIndexedPropertyUtils() {
424
425 final int index = 3;
426 dynaMap.setReturnNull(false);
427
428
429 assertFalse("Check Indexed Property doesn't exist", dynaMap.isDynaProperty(testProperty));
430 assertNull("Check Indexed Property is null", dynaMap.get(testProperty));
431 assertNull("Check Indexed value is null", dynaMap.get(testProperty, index));
432
433
434 try {
435 PropertyUtils.setProperty(dynaMap, testProperty+"["+index+"]", testString1);
436 }
437 catch (final NoSuchMethodException ex) {
438 fail("testIndexedPropertyUtils threw "+ex);
439 }
440 catch (final InvocationTargetException ex) {
441 fail("testIndexedPropertyUtils threw "+ex);
442 }
443 catch (final IllegalAccessException ex) {
444 fail("testIndexedPropertyUtils threw "+ex);
445 }
446
447
448 assertEquals("Check Indexed Bean Value is correct", testString1, dynaMap.get(testProperty, index));
449
450 }
451
452
453
454
455 public void testIndexedPropertyRestricted() {
456
457 final int index = 3;
458
459
460 dynaMap.setRestricted(true);
461 assertTrue("Check MutableDynaClass is restricted", dynaMap.isRestricted());
462
463
464 assertNull("Check Property doesn't exist", dynaMap.getDynaProperty(testProperty));
465 assertNull("Check Value is null", dynaMap.get(testProperty));
466
467
468 try {
469 dynaMap.set(testProperty, index, testInteger1);
470 fail("expected IllegalArgumentException trying to add new property to restricted MutableDynaClass");
471 } catch (final IllegalArgumentException expected) {
472
473 }
474
475 }
476
477
478
479
480 public void testIndexedInvalidType() {
481 final int index = 3;
482 dynaMap.set(testProperty, "Test String");
483 assertFalse("Check Property is not indexed", dynaMap.getDynaProperty(testProperty).isIndexed());
484 try {
485 dynaMap.set(testProperty, index, testString1);
486 fail("set(property, index, value) should have thrown IllegalArgumentException");
487 } catch (final IllegalArgumentException expected) {
488
489 }
490 }
491
492
493
494
495 public void testNewInstance() {
496
497
498
499 final LazyDynaMap orig = new LazyDynaMap(new TreeMap<String, Object>());
500 orig.set("indexProp", 0, "indexVal0");
501 orig.set("indexProp", 1, "indexVal1");
502 assertEquals("Index prop size", 2, ((List<?>)orig.get("indexProp")).size());
503
504 final LazyDynaMap newOne = (LazyDynaMap)orig.newInstance();
505 final Map<String, Object> newMap = newOne.getMap();
506 assertEquals("Check Map type", TreeMap.class, newMap.getClass());
507
508 final ArrayList<?> indexProp = (ArrayList<?>)newMap.get("indexProp");
509 assertNotNull("Indexed Prop missing", indexProp);
510 assertEquals("Index prop size", 0, indexProp.size());
511 }
512
513 }