    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  package org.apache.commons.beanutils;
    18
    19  import java.util.List;
    20  import java.util.Map;
    21  import java.util.HashMap;
    22  import java.util.TreeMap;
    23  import java.util.ArrayList;
    24  import java.util.LinkedList;
    25  import java.io.ByteArrayInputStream;
    26  import java.io.ByteArrayOutputStream;
    27  import java.io.ObjectInputStream;
    28  import java.io.ObjectOutputStream;
    29  import junit.framework.TestCase;
    30  import junit.framework.Test;
    31  import junit.framework.TestSuite;
    32
    33  /***
    34   * <p>Test Case for the <code>LazyDynaList</code>class.</p>
    35   *
    36   * @author Niall Pemberton
    37   */
    38  public class LazyDynaListTestCase extends TestCase {
    39
    40      private static final String BASIC_PROP1 = "BasicDynaClass_Property1";
    41      private static final String BASIC_PROP2 = "BasicDynaClass_Property2";
    42
    43      protected DynaProperty[] properties = new DynaProperty[] {
    44                                                 new DynaProperty(BASIC_PROP1, String.class),
    45                                                 new DynaProperty(BASIC_PROP2, HashMap.class)};
    46
    47      protected DynaClass treeMapDynaClass = new LazyDynaMap(new TreeMap());
    48      protected DynaClass hashMapDynaClass = new LazyDynaMap(new HashMap());
    49      protected DynaClass pojoDynaClass = new WrapDynaBean(new TestBean()).getDynaClass();
    50      protected DynaClass basicDynaClass = new BasicDynaClass("test", BasicDynaBean.class, properties);
    51
    52      // ---------------------------------------------------------- Constructors
    53
    54      /***
    55       * Construct a new instance of this test case.
    56       *
    57       * @param name Name of the test case
    58       */
    59      public LazyDynaListTestCase(String name) {
    60          super(name);
    61      }
    62
    63      // -------------------------------------------------- Overall Test Methods
    64
    65      /***
    66       * Run thus Test
    67       */
    68      public static void main(String[] args) {
    69          junit.textui.TestRunner.run(suite());
    70      }
    71
    72      /***
    73       * Return the tests included in this test suite.
    74       */
    75      public static Test suite() {
    76          return (new TestSuite(LazyDynaListTestCase.class));
    77      }
    78
    79      /***
    80       * Set up instance variables required by this test case.
    81       */
    82      public void setUp() throws Exception {
    83      }
    84
    85      /***
    86       * Tear down instance variables required by this test case.
    87       */
    88      public void tearDown() {
    89      }
    90
    91      // ------------------------------------------------ Individual Test Methods
    92
    93      /***
    94       * Test DynaBean Create
    95       */
    96      public void testDynaBeanDynaClass() {
    97
    98          // Create LazyArrayList for DynaBeans
    99          LazyDynaList list = new LazyDynaList(basicDynaClass);
    100
    101         // test
    102         dynaBeanTest(list, BasicDynaBean.class, basicDynaClass, new BenchBean());
    103     }
    104
    105     /***
    106      * Test DynaBean Create
    107      */
    108     public void testDynaBeanType() {
    109
    110         // Create LazyArrayList for DynaBeans
    111         LazyDynaList list = new LazyDynaList(LazyDynaBean.class);
    112
    113
    114         LazyDynaBean bean = new LazyDynaBean();
    115         bean.set("prop1", "val");
    116
    117         // test
    118         dynaBeanTest(list, LazyDynaBean.class, bean.getDynaClass(), new BenchBean());
    119     }
    120
    121     /***
    122      * Test Map Create
    123      */
    124     public void testMapDynaClass() {
    125
    126         // Create LazyArrayList for TreeMap's
    127         LazyDynaList list = new LazyDynaList(treeMapDynaClass);
    128
    129         // test
    130         mapTest(list, TreeMap.class, new BenchBean());
    131
    132     }
    133
    134     /***
    135      * Test Map Create
    136      */
    137     public void testMapType() {
    138
    139         // Create LazyArrayList for HashMap's
    140         LazyDynaList list = new LazyDynaList(HashMap.class);
    141
    142         // test
    143         mapTest(list, HashMap.class, new BenchBean());
    144
    145     }
    146
    147     /***
    148      * Test Pojo Create
    149      */
    150     public void testPojoDynaClass() {
    151
    152         // Create LazyArrayList for POJO's
    153         LazyDynaList list = new LazyDynaList(pojoDynaClass);
    154
    155         // test
    156         pojoTest(list, TestBean.class, new BenchBean());
    157
    158     }
    159
    160     /***
    161      * Test Pojo Create
    162      */
    163     public void testPojoType() {
    164
    165         // Create LazyArrayList for POJO's
    166         LazyDynaList list = new LazyDynaList(TestBean.class);
    167
    168         // test
    169         pojoTest(list, TestBean.class, new BenchBean());
    170
    171     }
    172
    173     /***
    174      * Test Collection
    175      */
    176     public void testCollection(LazyDynaList list, Class testClass, DynaClass testDynaClass, Object wrongBean) {
    177
    178         // ----- Create Collection & Array of Maps -----
    179         int size = 5;
    180         List testList = new ArrayList(size);
    181         TreeMap[] testArray = new TreeMap[size];
    182         for (int i = 0; i < size; i++) {
    183             testArray[i] = new TreeMap();
    184             testArray[i].put("prop"+i, "val"+i);
    185             testList.add(testArray[i]);
    186         }
    187
    188
    189         // ----- Create LazyArrayList from Collection -----
    190         LazyDynaList lazyList = new LazyDynaList(testList);
    191         assertEquals("1. check size", size, lazyList.size());
    192
    193         DynaBean[] dynaArray = lazyList.toDynaBeanArray();
    194         TreeMap[]  mapArray  = (TreeMap[])lazyList.toArray();
    195
    196         // Check values
    197         assertEquals("2. check size", size, dynaArray.length);
    198         assertEquals("3. check size", size, mapArray.length);
    199         for (int i = 0; i < size; i++) {
    200             assertEquals("4."+i+" DynaBean error ", "val"+i, dynaArray[i].get("prop"+i));
    201             assertEquals("5."+i+" Map error ", "val"+i, mapArray[i].get("prop"+i));
    202         }
    203
    204
    205
    206         // ----- Create LazyArrayList from Array -----
    207         lazyList = new LazyDynaList(testArray);
    208         assertEquals("6. check size", size, lazyList.size());
    209
    210         dynaArray = lazyList.toDynaBeanArray();
    211         mapArray  = (TreeMap[])lazyList.toArray();
    212
    213         // Check values
    214         assertEquals("7. check size", size, dynaArray.length);
    215         assertEquals("8. check size", size, mapArray.length);
    216         for (int i = 0; i < size; i++) {
    217             assertEquals("9."+i+" DynaBean error ", "val"+i, dynaArray[i].get("prop"+i));
    218             assertEquals("10."+i+" Map error ", "val"+i, mapArray[i].get("prop"+i));
    219         }
    220
    221     }
    222
    223
    224     /***
    225      * Test DynaBean Create
    226      */
    227     private void dynaBeanTest(LazyDynaList list, Class testClass, DynaClass testDynaClass, Object wrongBean) {
    228
    229         // Test get(index) created correct DynaBean - Second
    230         Object dynaBean = list.get(1);
    231         assertNotNull("1. DynaBean Not Created", dynaBean);
    232         assertEquals("2. Wrong Type", testClass, dynaBean.getClass());
    233
    234         // Test toArray() creates correct Array - Second
    235         Object array = list.toArray();
    236         assertNotNull("3. Array Not Created", array);
    237         assertEquals("4. Not DynaBean[]", testClass, array.getClass().getComponentType());
    238         DynaBean[] dynaArray = (DynaBean[])array;
    239         assertEquals("5. Array Size Wrong", 2, dynaArray.length);
    240
    241         // Test get(index) created correct DynaBean - Fourth
    242         dynaBean = list.get(3);
    243         assertNotNull("6. DynaBean Not Created", dynaBean);
    244         assertEquals("7. Wrong type", testClass, dynaBean.getClass());
    245
    246         // Test toArray() creates correct Array - Fourth
    247         array = list.toArray();
    248         assertNotNull("8. Array Not Created", array);
    249         assertEquals("9. Not DynaBean[]", testClass, array.getClass().getComponentType());
    250         dynaArray = (DynaBean[])array;
    251         assertEquals("10. Array Size Wrong", 4, dynaArray.length);
    252
    253         // Test fail if different type added
    254         try {
    255             list.add(2, wrongBean);
    256             fail("Expected IllegalArgumentException");
    257         } catch(IllegalArgumentException ignore) {
    258             // expected result
    259         }
    260
    261
    262         // find a String property to set
    263         String testProperty = findStringProperty(testDynaClass);
    264         assertNotNull("Test Property Not Found", testProperty);
    265         dynaArray = list.toDynaBeanArray();
    266         for (int i = 0; i < dynaArray.length; i++) {
    267             dynaArray[i].set(testProperty, "orig_pos"+i);
    268         }
    269
    270         // Create Collection
    271         List collection = new ArrayList();
    272         try {
    273             collection.add(testDynaClass.newInstance());
    274             collection.add(testDynaClass.newInstance());
    275             collection.add(testDynaClass.newInstance());
    276         } catch(Exception ex) {
    277             fail("1. FAILED: " + ex);
    278         }
    279         int expectedSize = dynaArray.length + collection.size();
    280         String origValue = (String)((DynaBean)collection.get(0)).get(testProperty);
    281         ((DynaBean)collection.get(0)).set(testProperty, origValue+"_updated_"+0);
    282         ((DynaBean)collection.get(1)).set(testProperty, origValue+"_updated_"+1);
    283         ((DynaBean)collection.get(2)).set(testProperty, origValue+"_updated_"+2);
    284
    285         // Test Insert - addAll(index, Collection)
    286         list.addAll(1, collection);
    287         dynaArray = list.toDynaBeanArray();
    288
    289         // Check array after insert
    290         dynaArray = list.toDynaBeanArray();
    291         assertEquals("11. Array Size Wrong", expectedSize, dynaArray.length);
    292
    293         // Check Beans have inserted correctly - by checking the property values
    294         assertEquals("12. Wrong Value", "orig_pos0",             dynaArray[0].get(testProperty));
    295         assertEquals("13. Wrong Value", origValue+"_updated_"+0, dynaArray[1].get(testProperty));
    296         assertEquals("14. Wrong Value", origValue+"_updated_"+1, dynaArray[2].get(testProperty));
    297         assertEquals("15. Wrong Value", origValue+"_updated_"+2, dynaArray[3].get(testProperty));
    298         assertEquals("16. Wrong Value", "orig_pos1",             dynaArray[4].get(testProperty));
    299
    300
    301         // Test Insert - add(index, Object)
    302         try {
    303             DynaBean extraElement = (DynaBean)testDynaClass.newInstance();
    304             extraElement.set(testProperty, "extraOne");
    305             list.add(2, extraElement);
    306             dynaArray = list.toDynaBeanArray();
    307             assertEquals("17. Wrong Value", origValue+"_updated_"+0, dynaArray[1].get(testProperty));
    308             assertEquals("18. Wrong Value", "extraOne",              dynaArray[2].get(testProperty));
    309             assertEquals("19. Wrong Value", origValue+"_updated_"+1, dynaArray[3].get(testProperty));
    310         } catch(Exception ex) {
    311             fail("2. FAILED: " + ex);
    312         }
    313
    314     }
    315
    316     /***
    317      * Test Map Create
    318      */
    319     private String findStringProperty(DynaClass dynaClass) {
    320         DynaProperty[] properties = dynaClass.getDynaProperties();
    321         for (int i = 0; i < properties.length; i++) {
    322             if (properties[i].getType() == String.class) {
    323                 return properties[i].getName();
    324             }
    325         }
    326         return null;
    327     }
    328
    329
    330
    331     /***
    332      * Test Map Create
    333      */
    334     private void mapTest(LazyDynaList list, Class testClass, Object wrongBean) {
    335
    336         // Test get(index) created correct DynaBean - First
    337         Object dynaBean = list.get(0);
    338         assertNotNull("1. DynaBean Not Created", dynaBean);
    339         assertEquals("2. Not LazyDynaMap", LazyDynaMap.class, dynaBean.getClass());
    340
    341         // Test get(index) created correct Map - First
    342         Object map = ((LazyDynaMap)dynaBean).getMap();
    343         assertNotNull("3. Map Not Created", map);
    344         assertEquals("4. Wrong Map", testClass, map.getClass());
    345
    346         // Test toArray() creates correct Array - First
    347         Object array = list.toArray();
    348         assertNotNull("5. Array Not Created", array);
    349         assertEquals("6. Not Map[]", testClass, array.getClass().getComponentType());
    350         Map[] mapArray = (Map[])array;
    351         assertEquals("7. Array Size Wrong", 1, mapArray.length);
    352
    353         // Test get(index) created correct DynaBean - Third
    354         dynaBean = list.get(2);
    355         assertNotNull("8. DynaBean Not Created", dynaBean);
    356         assertEquals("9. Not LazyDynaMap", LazyDynaMap.class, dynaBean.getClass());
    357
    358         // Test get(index) created correct Map - Third
    359         map = ((LazyDynaMap)dynaBean).getMap();
    360         assertNotNull("10. Map Not Created", map);
    361         assertEquals("11. Wrong Map", testClass, map.getClass());
    362
    363         // Test toArray() creates correct Array - Third
    364         array = list.toArray();
    365         assertNotNull("12. Array Not Created", array);
    366         assertEquals("13. Not Map[]", testClass, array.getClass().getComponentType());
    367         mapArray = (Map[])array;
    368         assertEquals("14. Array Size Wrong", 3, mapArray.length);
    369
    370         // Test fail if different type added
    371         try {
    372             list.add(2, wrongBean);
    373             fail("Expected IllegalArgumentException");
    374         } catch(IllegalArgumentException ignore) {
    375             // expected result
    376         }
    377
    378     }
    379
    380     /***
    381      * Test Pojo Create
    382      */
    383     private void pojoTest(LazyDynaList list, Class testClass, Object wrongBean) {
    384
    385         // Test get(index) created correct DynaBean - First
    386         Object dynaBean = list.get(0);
    387         assertNotNull("1. DynaBean Not Created", dynaBean);
    388         assertEquals("2. Not WrapDynaBean", WrapDynaBean.class, dynaBean.getClass());
    389
    390         // Test get(index) created correct POJO - First
    391         Object pojoBean = ((WrapDynaBean)dynaBean).getInstance();
    392         assertNotNull("3. POJO Not Created", pojoBean);
    393         assertEquals("4. Not WrapDynaBean", testClass, pojoBean.getClass());
    394
    395         // Test toArray() creates correct Array - First
    396         Object array = list.toArray();
    397         assertNotNull("5. Array Not Created", array);
    398         assertEquals("6. Wrong array", testClass, array.getClass().getComponentType());
    399         Object[] pojoArray = (Object[])array;
    400         assertEquals("7. Array Size Wrong", 1, pojoArray.length);
    401
    402         // Test get(index) created correct DynaBean - Second
    403         dynaBean = list.get(1);
    404         assertNotNull("8. DynaBean Not Created", dynaBean);
    405         assertEquals("9. Not WrapDynaBean", WrapDynaBean.class, dynaBean.getClass());
    406
    407         // Test get(index) created correct POJO - Second
    408         pojoBean = ((WrapDynaBean)dynaBean).getInstance();
    409         assertNotNull("10. POJO Not Created", pojoBean);
    410         assertEquals("11. Not WrapDynaBean", testClass, pojoBean.getClass());
    411
    412         // Test toArray() creates correct Array - Second
    413         array = list.toArray();
    414         assertNotNull("12. Array Not Created", array);
    415         assertEquals("13. Wrong array", testClass, array.getClass().getComponentType());
    416         pojoArray = (Object[])array;
    417         assertEquals("14. Array Size Wrong", 2, pojoArray.length);
    418
    419         // Test fail if different type added
    420         try {
    421             list.add(2, wrongBean);
    422             fail("Expected IllegalArgumentException");
    423         } catch(IllegalArgumentException ignore) {
    424             // expected result
    425         }
    426
    427     }
    428
    429     /***
    430      * Test DynaBean serialization.
    431      */
    432     public void testSerializationDynaBean() {
    433
    434         // Create LazyArrayList for DynaBeans
    435         LazyDynaList target = new LazyDynaList(basicDynaClass);
    436         BasicDynaBean bean = (BasicDynaBean)target.get(0);
    437
    438         // Set a Property
    439         assertNull("pre-set check", bean.get(BASIC_PROP1));
    440         bean.set(BASIC_PROP1, "value1");
    441         assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
    442
    443         // Serialize/Deserialize
    444         LazyDynaList result = (LazyDynaList)serializeDeserialize(target, "DynaBean");
    445         target = null;
    446         bean = null;
    447
    448         // Confirm property value
    449         bean = (BasicDynaBean)result.get(0);
    450         assertEquals("post-serialize check", "value1", bean.get(BASIC_PROP1));
    451
    452     }
    453
    454     /***
    455      * Test DynaBean serialization.
    456      */
    457     public void testSerializationLazyDynaBean() {
    458
    459         // Create LazyArrayList for DynaBeans
    460         LazyDynaList target = new LazyDynaList();
    461         LazyDynaBean bean = (LazyDynaBean)target.get(0);
    462
    463         // Set a Property
    464         assertNull("pre-set check", bean.get(BASIC_PROP1));
    465         bean.set(BASIC_PROP1, "value1");
    466         assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
    467
    468         // Serialize/Deserialize
    469         LazyDynaList result = (LazyDynaList)serializeDeserialize(target, "DynaBean");
    470         target = null;
    471         bean = null;
    472
    473         // Confirm property value
    474         bean = (LazyDynaBean)result.get(0);
    475         assertEquals("post-serialize check", "value1", bean.get(BASIC_PROP1));
    476
    477     }
    478
    479     /***
    480      * Test Map serialization.
    481      */
    482     public void testSerializationMap() {
    483
    484         // Create LazyArrayList for DynaBeans
    485         LazyDynaList target = new LazyDynaList(treeMapDynaClass);
    486         LazyDynaMap bean = (LazyDynaMap)target.get(0);
    487
    488         // Set a Property
    489         assertNull("pre-set check", bean.get(BASIC_PROP1));
    490         bean.set(BASIC_PROP1, "value1");
    491         assertEquals("post-set check", "value1", bean.get(BASIC_PROP1));
    492
    493         // Serialize/Deserialize
    494         LazyDynaList result = (LazyDynaList)serializeDeserialize(target, "Map");
    495         target = null;
    496         bean = null;
    497
    498         // Confirm property value
    499         bean = (LazyDynaMap)result.get(0);
    500         assertEquals("post-serialize check", "value1", bean.get(BASIC_PROP1));
    501
    502     }
    503
    504     /***
    505      * Test POJO (WrapDynaBean) serialization.
    506      */
    507     public void testSerializationPojo() {
    508
    509         // Create LazyArrayList for DynaBeans
    510         LazyDynaList target = new LazyDynaList(pojoDynaClass);
    511         WrapDynaBean bean = (WrapDynaBean)target.get(0);
    512
    513         // Set a Property
    514         assertEquals("pre-set check", "This is a string", bean.get("stringProperty"));
    515         bean.set("stringProperty", "value1");
    516         assertEquals("post-set check", "value1", bean.get("stringProperty"));
    517
    518         // Serialize/Deserialize
    519         LazyDynaList result = (LazyDynaList)serializeDeserialize(target, "POJO");
    520         target = null;
    521         bean = null;
    522
    523         // Confirm property value
    524         bean = (WrapDynaBean)result.get(0);
    525         assertEquals("post-serialize check", "value1", bean.get("stringProperty"));
    526
    527     }
    528
    529     /***
    530      * Do serialization and deserialization.
    531      */
    532     private Object serializeDeserialize(Object target, String text) {
    533
    534         // Serialize the test object
    535         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    536         try {
    537             ObjectOutputStream oos = new ObjectOutputStream(baos);
    538             oos.writeObject(target);
    539             oos.flush();
    540             oos.close();
    541         } catch (Exception e) {
    542             fail(text + ": Exception during serialization: " + e);
    543         }
    544
    545         // Deserialize the test object
    546         Object result = null;
    547         try {
    548             ByteArrayInputStream bais =
    549                 new ByteArrayInputStream(baos.toByteArray());
    550             ObjectInputStream ois = new ObjectInputStream(bais);
    551             result = ois.readObject();
    552             bais.close();
    553         } catch (Exception e) {
    554             fail(text + ": Exception during deserialization: " + e);
    555         }
    556         return result;
    557
    558     }
    559
    560 }