001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.beanutils;
018:
019: import java.util.List;
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.TreeMap;
023: import java.util.ArrayList;
024: import java.util.LinkedList;
025: import java.io.ByteArrayInputStream;
026: import java.io.ByteArrayOutputStream;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import junit.framework.TestCase;
030: import junit.framework.Test;
031: import junit.framework.TestSuite;
032:
033: /**
034: * <p>Test Case for the <code>LazyDynaList</code>class.</p>
035: *
036: * @author Niall Pemberton
037: */
038: public class LazyDynaListTestCase extends TestCase {
039:
040: private static final String BASIC_PROP1 = "BasicDynaClass_Property1";
041: private static final String BASIC_PROP2 = "BasicDynaClass_Property2";
042:
043: protected DynaProperty[] properties = new DynaProperty[] {
044: new DynaProperty(BASIC_PROP1, String.class),
045: new DynaProperty(BASIC_PROP2, HashMap.class) };
046:
047: protected DynaClass treeMapDynaClass = new LazyDynaMap(
048: new TreeMap());
049: protected DynaClass hashMapDynaClass = new LazyDynaMap(
050: new HashMap());
051: protected DynaClass pojoDynaClass = new WrapDynaBean(new TestBean())
052: .getDynaClass();
053: protected DynaClass basicDynaClass = new BasicDynaClass("test",
054: BasicDynaBean.class, properties);
055:
056: // ---------------------------------------------------------- Constructors
057:
058: /**
059: * Construct a new instance of this test case.
060: *
061: * @param name Name of the test case
062: */
063: public LazyDynaListTestCase(String name) {
064: super (name);
065: }
066:
067: // -------------------------------------------------- Overall Test Methods
068:
069: /**
070: * Run thus Test
071: */
072: public static void main(String[] args) {
073: junit.textui.TestRunner.run(suite());
074: }
075:
076: /**
077: * Return the tests included in this test suite.
078: */
079: public static Test suite() {
080: return (new TestSuite(LazyDynaListTestCase.class));
081: }
082:
083: /**
084: * Set up instance variables required by this test case.
085: */
086: public void setUp() throws Exception {
087: }
088:
089: /**
090: * Tear down instance variables required by this test case.
091: */
092: public void tearDown() {
093: }
094:
095: // ------------------------------------------------ Individual Test Methods
096:
097: /**
098: * Test DynaBean Create
099: */
100: public void testDynaBeanDynaClass() {
101:
102: // Create LazyArrayList for DynaBeans
103: LazyDynaList list = new LazyDynaList(basicDynaClass);
104:
105: // test
106: dynaBeanTest(list, BasicDynaBean.class, basicDynaClass,
107: new BenchBean());
108: }
109:
110: /**
111: * Test DynaBean Create
112: */
113: public void testDynaBeanType() {
114:
115: // Create LazyArrayList for DynaBeans
116: LazyDynaList list = new LazyDynaList(LazyDynaBean.class);
117:
118: LazyDynaBean bean = new LazyDynaBean();
119: bean.set("prop1", "val");
120:
121: // test
122: dynaBeanTest(list, LazyDynaBean.class, bean.getDynaClass(),
123: new BenchBean());
124: }
125:
126: /**
127: * Test Map Create
128: */
129: public void testMapDynaClass() {
130:
131: // Create LazyArrayList for TreeMap's
132: LazyDynaList list = new LazyDynaList(treeMapDynaClass);
133:
134: // test
135: mapTest(list, TreeMap.class, new BenchBean());
136:
137: }
138:
139: /**
140: * Test Map Create
141: */
142: public void testMapType() {
143:
144: // Create LazyArrayList for HashMap's
145: LazyDynaList list = new LazyDynaList(HashMap.class);
146:
147: // test
148: mapTest(list, HashMap.class, new BenchBean());
149:
150: }
151:
152: /**
153: * Test Pojo Create
154: */
155: public void testPojoDynaClass() {
156:
157: // Create LazyArrayList for POJO's
158: LazyDynaList list = new LazyDynaList(pojoDynaClass);
159:
160: // test
161: pojoTest(list, TestBean.class, new BenchBean());
162:
163: }
164:
165: /**
166: * Test Pojo Create
167: */
168: public void testPojoType() {
169:
170: // Create LazyArrayList for POJO's
171: LazyDynaList list = new LazyDynaList(TestBean.class);
172:
173: // test
174: pojoTest(list, TestBean.class, new BenchBean());
175:
176: }
177:
178: /**
179: * Test Collection
180: */
181: public void testCollection(LazyDynaList list, Class testClass,
182: DynaClass testDynaClass, Object wrongBean) {
183:
184: // ----- Create Collection & Array of Maps -----
185: int size = 5;
186: List testList = new ArrayList(size);
187: TreeMap[] testArray = new TreeMap[size];
188: for (int i = 0; i < size; i++) {
189: testArray[i] = new TreeMap();
190: testArray[i].put("prop" + i, "val" + i);
191: testList.add(testArray[i]);
192: }
193:
194: // ----- Create LazyArrayList from Collection -----
195: LazyDynaList lazyList = new LazyDynaList(testList);
196: assertEquals("1. check size", size, lazyList.size());
197:
198: DynaBean[] dynaArray = lazyList.toDynaBeanArray();
199: TreeMap[] mapArray = (TreeMap[]) lazyList.toArray();
200:
201: // Check values
202: assertEquals("2. check size", size, dynaArray.length);
203: assertEquals("3. check size", size, mapArray.length);
204: for (int i = 0; i < size; i++) {
205: assertEquals("4." + i + " DynaBean error ", "val" + i,
206: dynaArray[i].get("prop" + i));
207: assertEquals("5." + i + " Map error ", "val" + i,
208: mapArray[i].get("prop" + i));
209: }
210:
211: // ----- Create LazyArrayList from Array -----
212: lazyList = new LazyDynaList(testArray);
213: assertEquals("6. check size", size, lazyList.size());
214:
215: dynaArray = lazyList.toDynaBeanArray();
216: mapArray = (TreeMap[]) lazyList.toArray();
217:
218: // Check values
219: assertEquals("7. check size", size, dynaArray.length);
220: assertEquals("8. check size", size, mapArray.length);
221: for (int i = 0; i < size; i++) {
222: assertEquals("9." + i + " DynaBean error ", "val" + i,
223: dynaArray[i].get("prop" + i));
224: assertEquals("10." + i + " Map error ", "val" + i,
225: mapArray[i].get("prop" + i));
226: }
227:
228: }
229:
230: /**
231: * Test DynaBean Create
232: */
233: private void dynaBeanTest(LazyDynaList list, Class testClass,
234: DynaClass testDynaClass, Object wrongBean) {
235:
236: // Test get(index) created correct DynaBean - Second
237: Object dynaBean = list.get(1);
238: assertNotNull("1. DynaBean Not Created", dynaBean);
239: assertEquals("2. Wrong Type", testClass, dynaBean.getClass());
240:
241: // Test toArray() creates correct Array - Second
242: Object array = list.toArray();
243: assertNotNull("3. Array Not Created", array);
244: assertEquals("4. Not DynaBean[]", testClass, array.getClass()
245: .getComponentType());
246: DynaBean[] dynaArray = (DynaBean[]) array;
247: assertEquals("5. Array Size Wrong", 2, dynaArray.length);
248:
249: // Test get(index) created correct DynaBean - Fourth
250: dynaBean = list.get(3);
251: assertNotNull("6. DynaBean Not Created", dynaBean);
252: assertEquals("7. Wrong type", testClass, dynaBean.getClass());
253:
254: // Test toArray() creates correct Array - Fourth
255: array = list.toArray();
256: assertNotNull("8. Array Not Created", array);
257: assertEquals("9. Not DynaBean[]", testClass, array.getClass()
258: .getComponentType());
259: dynaArray = (DynaBean[]) array;
260: assertEquals("10. Array Size Wrong", 4, dynaArray.length);
261:
262: // Test fail if different type added
263: try {
264: list.add(2, wrongBean);
265: fail("Expected IllegalArgumentException");
266: } catch (IllegalArgumentException ignore) {
267: // expected result
268: }
269:
270: // find a String property to set
271: String testProperty = findStringProperty(testDynaClass);
272: assertNotNull("Test Property Not Found", testProperty);
273: dynaArray = list.toDynaBeanArray();
274: for (int i = 0; i < dynaArray.length; i++) {
275: dynaArray[i].set(testProperty, "orig_pos" + i);
276: }
277:
278: // Create Collection
279: List collection = new ArrayList();
280: try {
281: collection.add(testDynaClass.newInstance());
282: collection.add(testDynaClass.newInstance());
283: collection.add(testDynaClass.newInstance());
284: } catch (Exception ex) {
285: fail("1. FAILED: " + ex);
286: }
287: int expectedSize = dynaArray.length + collection.size();
288: String origValue = (String) ((DynaBean) collection.get(0))
289: .get(testProperty);
290: ((DynaBean) collection.get(0)).set(testProperty, origValue
291: + "_updated_" + 0);
292: ((DynaBean) collection.get(1)).set(testProperty, origValue
293: + "_updated_" + 1);
294: ((DynaBean) collection.get(2)).set(testProperty, origValue
295: + "_updated_" + 2);
296:
297: // Test Insert - addAll(index, Collection)
298: list.addAll(1, collection);
299: dynaArray = list.toDynaBeanArray();
300:
301: // Check array after insert
302: dynaArray = list.toDynaBeanArray();
303: assertEquals("11. Array Size Wrong", expectedSize,
304: dynaArray.length);
305:
306: // Check Beans have inserted correctly - by checking the property values
307: assertEquals("12. Wrong Value", "orig_pos0", dynaArray[0]
308: .get(testProperty));
309: assertEquals("13. Wrong Value", origValue + "_updated_" + 0,
310: dynaArray[1].get(testProperty));
311: assertEquals("14. Wrong Value", origValue + "_updated_" + 1,
312: dynaArray[2].get(testProperty));
313: assertEquals("15. Wrong Value", origValue + "_updated_" + 2,
314: dynaArray[3].get(testProperty));
315: assertEquals("16. Wrong Value", "orig_pos1", dynaArray[4]
316: .get(testProperty));
317:
318: // Test Insert - add(index, Object)
319: try {
320: DynaBean extraElement = (DynaBean) testDynaClass
321: .newInstance();
322: extraElement.set(testProperty, "extraOne");
323: list.add(2, extraElement);
324: dynaArray = list.toDynaBeanArray();
325: assertEquals("17. Wrong Value",
326: origValue + "_updated_" + 0, dynaArray[1]
327: .get(testProperty));
328: assertEquals("18. Wrong Value", "extraOne", dynaArray[2]
329: .get(testProperty));
330: assertEquals("19. Wrong Value",
331: origValue + "_updated_" + 1, dynaArray[3]
332: .get(testProperty));
333: } catch (Exception ex) {
334: fail("2. FAILED: " + ex);
335: }
336:
337: }
338:
339: /**
340: * Test Map Create
341: */
342: private String findStringProperty(DynaClass dynaClass) {
343: DynaProperty[] properties = dynaClass.getDynaProperties();
344: for (int i = 0; i < properties.length; i++) {
345: if (properties[i].getType() == String.class) {
346: return properties[i].getName();
347: }
348: }
349: return null;
350: }
351:
352: /**
353: * Test Map Create
354: */
355: private void mapTest(LazyDynaList list, Class testClass,
356: Object wrongBean) {
357:
358: // Test get(index) created correct DynaBean - First
359: Object dynaBean = list.get(0);
360: assertNotNull("1. DynaBean Not Created", dynaBean);
361: assertEquals("2. Not LazyDynaMap", LazyDynaMap.class, dynaBean
362: .getClass());
363:
364: // Test get(index) created correct Map - First
365: Object map = ((LazyDynaMap) dynaBean).getMap();
366: assertNotNull("3. Map Not Created", map);
367: assertEquals("4. Wrong Map", testClass, map.getClass());
368:
369: // Test toArray() creates correct Array - First
370: Object array = list.toArray();
371: assertNotNull("5. Array Not Created", array);
372: assertEquals("6. Not Map[]", testClass, array.getClass()
373: .getComponentType());
374: Map[] mapArray = (Map[]) array;
375: assertEquals("7. Array Size Wrong", 1, mapArray.length);
376:
377: // Test get(index) created correct DynaBean - Third
378: dynaBean = list.get(2);
379: assertNotNull("8. DynaBean Not Created", dynaBean);
380: assertEquals("9. Not LazyDynaMap", LazyDynaMap.class, dynaBean
381: .getClass());
382:
383: // Test get(index) created correct Map - Third
384: map = ((LazyDynaMap) dynaBean).getMap();
385: assertNotNull("10. Map Not Created", map);
386: assertEquals("11. Wrong Map", testClass, map.getClass());
387:
388: // Test toArray() creates correct Array - Third
389: array = list.toArray();
390: assertNotNull("12. Array Not Created", array);
391: assertEquals("13. Not Map[]", testClass, array.getClass()
392: .getComponentType());
393: mapArray = (Map[]) array;
394: assertEquals("14. Array Size Wrong", 3, mapArray.length);
395:
396: // Test fail if different type added
397: try {
398: list.add(2, wrongBean);
399: fail("Expected IllegalArgumentException");
400: } catch (IllegalArgumentException ignore) {
401: // expected result
402: }
403:
404: }
405:
406: /**
407: * Test Pojo Create
408: */
409: private void pojoTest(LazyDynaList list, Class testClass,
410: Object wrongBean) {
411:
412: // Test get(index) created correct DynaBean - First
413: Object dynaBean = list.get(0);
414: assertNotNull("1. DynaBean Not Created", dynaBean);
415: assertEquals("2. Not WrapDynaBean", WrapDynaBean.class,
416: dynaBean.getClass());
417:
418: // Test get(index) created correct POJO - First
419: Object pojoBean = ((WrapDynaBean) dynaBean).getInstance();
420: assertNotNull("3. POJO Not Created", pojoBean);
421: assertEquals("4. Not WrapDynaBean", testClass, pojoBean
422: .getClass());
423:
424: // Test toArray() creates correct Array - First
425: Object array = list.toArray();
426: assertNotNull("5. Array Not Created", array);
427: assertEquals("6. Wrong array", testClass, array.getClass()
428: .getComponentType());
429: Object[] pojoArray = (Object[]) array;
430: assertEquals("7. Array Size Wrong", 1, pojoArray.length);
431:
432: // Test get(index) created correct DynaBean - Second
433: dynaBean = list.get(1);
434: assertNotNull("8. DynaBean Not Created", dynaBean);
435: assertEquals("9. Not WrapDynaBean", WrapDynaBean.class,
436: dynaBean.getClass());
437:
438: // Test get(index) created correct POJO - Second
439: pojoBean = ((WrapDynaBean) dynaBean).getInstance();
440: assertNotNull("10. POJO Not Created", pojoBean);
441: assertEquals("11. Not WrapDynaBean", testClass, pojoBean
442: .getClass());
443:
444: // Test toArray() creates correct Array - Second
445: array = list.toArray();
446: assertNotNull("12. Array Not Created", array);
447: assertEquals("13. Wrong array", testClass, array.getClass()
448: .getComponentType());
449: pojoArray = (Object[]) array;
450: assertEquals("14. Array Size Wrong", 2, pojoArray.length);
451:
452: // Test fail if different type added
453: try {
454: list.add(2, wrongBean);
455: fail("Expected IllegalArgumentException");
456: } catch (IllegalArgumentException ignore) {
457: // expected result
458: }
459:
460: }
461:
462: /**
463: * Test DynaBean serialization.
464: */
465: public void testSerializationDynaBean() {
466:
467: // Create LazyArrayList for DynaBeans
468: LazyDynaList target = new LazyDynaList(basicDynaClass);
469: BasicDynaBean bean = (BasicDynaBean) target.get(0);
470:
471: // Set a Property
472: assertNull("pre-set check", bean.get(BASIC_PROP1));
473: bean.set(BASIC_PROP1, "value1");
474: assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
475:
476: // Serialize/Deserialize
477: LazyDynaList result = (LazyDynaList) serializeDeserialize(
478: target, "DynaBean");
479: target = null;
480: bean = null;
481:
482: // Confirm property value
483: bean = (BasicDynaBean) result.get(0);
484: assertEquals("post-serialize check", "value1", bean
485: .get(BASIC_PROP1));
486:
487: }
488:
489: /**
490: * Test DynaBean serialization.
491: */
492: public void testSerializationLazyDynaBean() {
493:
494: // Create LazyArrayList for DynaBeans
495: LazyDynaList target = new LazyDynaList();
496: LazyDynaBean bean = (LazyDynaBean) target.get(0);
497:
498: // Set a Property
499: assertNull("pre-set check", bean.get(BASIC_PROP1));
500: bean.set(BASIC_PROP1, "value1");
501: assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
502:
503: // Serialize/Deserialize
504: LazyDynaList result = (LazyDynaList) serializeDeserialize(
505: target, "DynaBean");
506: target = null;
507: bean = null;
508:
509: // Confirm property value
510: bean = (LazyDynaBean) result.get(0);
511: assertEquals("post-serialize check", "value1", bean
512: .get(BASIC_PROP1));
513:
514: }
515:
516: /**
517: * Test Map serialization.
518: */
519: public void testSerializationMap() {
520:
521: // Create LazyArrayList for DynaBeans
522: LazyDynaList target = new LazyDynaList(treeMapDynaClass);
523: LazyDynaMap bean = (LazyDynaMap) target.get(0);
524:
525: // Set a Property
526: assertNull("pre-set check", bean.get(BASIC_PROP1));
527: bean.set(BASIC_PROP1, "value1");
528: assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
529:
530: // Serialize/Deserialize
531: LazyDynaList result = (LazyDynaList) serializeDeserialize(
532: target, "Map");
533: target = null;
534: bean = null;
535:
536: // Confirm property value
537: bean = (LazyDynaMap) result.get(0);
538: assertEquals("post-serialize check", "value1", bean
539: .get(BASIC_PROP1));
540:
541: }
542:
543: /**
544: * Test POJO (WrapDynaBean) serialization.
545: */
546: public void testSerializationPojo() {
547:
548: // Create LazyArrayList for DynaBeans
549: LazyDynaList target = new LazyDynaList(pojoDynaClass);
550: WrapDynaBean bean = (WrapDynaBean) target.get(0);
551:
552: // Set a Property
553: assertEquals("pre-set check", "This is a string", bean
554: .get("stringProperty"));
555: bean.set("stringProperty", "value1");
556: assertEquals("post-set check", "value1", bean
557: .get("stringProperty"));
558:
559: // Serialize/Deserialize
560: LazyDynaList result = (LazyDynaList) serializeDeserialize(
561: target, "POJO");
562: target = null;
563: bean = null;
564:
565: // Confirm property value
566: bean = (WrapDynaBean) result.get(0);
567: assertEquals("post-serialize check", "value1", bean
568: .get("stringProperty"));
569:
570: }
571:
572: /**
573: * Do serialization and deserialization.
574: */
575: private Object serializeDeserialize(Object target, String text) {
576:
577: // Serialize the test object
578: ByteArrayOutputStream baos = new ByteArrayOutputStream();
579: try {
580: ObjectOutputStream oos = new ObjectOutputStream(baos);
581: oos.writeObject(target);
582: oos.flush();
583: oos.close();
584: } catch (Exception e) {
585: fail(text + ": Exception during serialization: " + e);
586: }
587:
588: // Deserialize the test object
589: Object result = null;
590: try {
591: ByteArrayInputStream bais = new ByteArrayInputStream(baos
592: .toByteArray());
593: ObjectInputStream ois = new ObjectInputStream(bais);
594: result = ois.readObject();
595: bais.close();
596: } catch (Exception e) {
597: fail(text + ": Exception during deserialization: " + e);
598: }
599: return result;
600:
601: }
602:
603: }
|