    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
    18
    19  package org.apache.commons.beanutils;
    20
    21
    22  import java.io.ByteArrayInputStream;
    23  import java.io.ByteArrayOutputStream;
    24  import java.io.ObjectInputStream;
    25  import java.io.ObjectOutputStream;
    26
    27  import java.util.ArrayList;
    28  import java.util.HashMap;
    29  import java.util.List;
    30  import java.util.Map;
    31
    32  import junit.framework.TestCase;
    33  import junit.framework.Test;
    34  import junit.framework.TestSuite;
    35
    36
    37  /***
    38   * <p>Test Case for the <code>BasicDynaBean</code> implementation class.
    39   * These tests were based on the ones in <code>PropertyUtilsTestCase</code>
    40   * because the two classes provide similar levels of functionality.</p>
    41   *
    42   * @author Craig R. McClanahan
    43   * @version $Revision: 469743 $ $Date: 2006-11-01 01:27:40 +0000 (Wed, 01 Nov 2006) $
    44   */
    45
    46  public class BasicDynaBeanTestCase extends TestCase {
    47
    48
    49      // ---------------------------------------------------- Instance Variables
    50
    51
    52      /***
    53       * The basic test bean for each test.
    54       */
    55      protected DynaBean bean = null;
    56
    57
    58      /***
    59       * The set of property names we expect to have returned when calling
    60       * <code>getDynaProperties()</code>.  You should update this list
    61       * when new properties are added to TestBean.
    62       */
    63      protected final static String[] properties = {
    64          "booleanProperty",
    65          "booleanSecond",
    66          "doubleProperty",
    67          "floatProperty",
    68          "intArray",
    69          "intIndexed",
    70          "intProperty",
    71          "listIndexed",
    72          "longProperty",
    73          "mappedProperty",
    74          "mappedIntProperty",
    75          "nullProperty",
    76          "shortProperty",
    77          "stringArray",
    78          "stringIndexed",
    79          "stringProperty",
    80      };
    81
    82
    83      // ---------------------------------------------------------- Constructors
    84
    85
    86      /***
    87       * Construct a new instance of this test case.
    88       *
    89       * @param name Name of the test case
    90       */
    91      public BasicDynaBeanTestCase(String name) {
    92
    93          super(name);
    94
    95      }
    96
    97
    98      // -------------------------------------------------- Overall Test Methods
    99
    100
    101     /***
    102      * Set up instance variables required by this test case.
    103      */
    104     public void setUp() throws Exception {
    105
    106         // Instantiate a new DynaBean instance
    107         DynaClass dynaClass = createDynaClass();
    108         bean = dynaClass.newInstance();
    109
    110         // Initialize the DynaBean's property values (like TestBean)
    111         bean.set("booleanProperty", new Boolean(true));
    112         bean.set("booleanSecond", new Boolean(true));
    113         bean.set("doubleProperty", new Double(321.0));
    114         bean.set("floatProperty", new Float((float) 123.0));
    115         int intArray[] = { 0, 10, 20, 30, 40 };
    116         bean.set("intArray", intArray);
    117         int intIndexed[] = { 0, 10, 20, 30, 40 };
    118         bean.set("intIndexed", intIndexed);
    119         bean.set("intProperty", new Integer(123));
    120         List listIndexed = new ArrayList();
    121         listIndexed.add("String 0");
    122         listIndexed.add("String 1");
    123         listIndexed.add("String 2");
    124         listIndexed.add("String 3");
    125         listIndexed.add("String 4");
    126         bean.set("listIndexed", listIndexed);
    127         bean.set("longProperty", new Long(321));
    128         HashMap mappedProperty = new HashMap();
    129         mappedProperty.put("First Key", "First Value");
    130         mappedProperty.put("Second Key", "Second Value");
    131         bean.set("mappedProperty", mappedProperty);
    132         HashMap mappedIntProperty = new HashMap();
    133         mappedIntProperty.put("One", new Integer(1));
    134         mappedIntProperty.put("Two", new Integer(2));
    135         bean.set("mappedIntProperty", mappedIntProperty);
    136         // Property "nullProperty" is not initialized, so it should return null
    137         bean.set("shortProperty", new Short((short) 987));
    138         String stringArray[] =
    139                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
    140         bean.set("stringArray", stringArray);
    141         String stringIndexed[] =
    142                 { "String 0", "String 1", "String 2", "String 3", "String 4" };
    143         bean.set("stringIndexed", stringIndexed);
    144         bean.set("stringProperty", "This is a string");
    145
    146     }
    147
    148
    149     /***
    150      * Return the tests included in this test suite.
    151      */
    152     public static Test suite() {
    153
    154         return (new TestSuite(BasicDynaBeanTestCase.class));
    155
    156     }
    157
    158
    159     /***
    160      * Tear down instance variables required by this test case.
    161      */
    162     public void tearDown() {
    163
    164         bean = null;
    165
    166     }
    167
    168
    169
    170     // ------------------------------------------------ Individual Test Methods
    171
    172
    173     /***
    174      * Corner cases on getDynaProperty invalid arguments.
    175      */
    176     public void testGetDescriptorArguments() {
    177
    178         try {
    179             DynaProperty descriptor =
    180                     bean.getDynaClass().getDynaProperty("unknown");
    181             assertNull("Unknown property descriptor should be null",
    182                     descriptor);
    183         } catch (Throwable t) {
    184             fail("Threw " + t + " instead of returning null");
    185         }
    186
    187         try {
    188             bean.getDynaClass().getDynaProperty(null);
    189             fail("Should throw IllegalArgumentException");
    190         } catch (IllegalArgumentException e) {
    191             // Expected response
    192         } catch (Throwable t) {
    193             fail("Threw " + t + " instead of IllegalArgumentException");
    194         }
    195
    196     }
    197
    198
    199     /***
    200      * Positive getDynaProperty on property <code>booleanProperty</code>.
    201      */
    202     public void testGetDescriptorBoolean() {
    203
    204         testGetDescriptorBase("booleanProperty", Boolean.TYPE);
    205
    206     }
    207
    208
    209     /***
    210      * Positive getDynaProperty on property <code>doubleProperty</code>.
    211      */
    212     public void testGetDescriptorDouble() {
    213
    214         testGetDescriptorBase("doubleProperty", Double.TYPE);
    215
    216     }
    217
    218
    219     /***
    220      * Positive getDynaProperty on property <code>floatProperty</code>.
    221      */
    222     public void testGetDescriptorFloat() {
    223
    224         testGetDescriptorBase("floatProperty", Float.TYPE);
    225
    226     }
    227
    228
    229     /***
    230      * Positive getDynaProperty on property <code>intProperty</code>.
    231      */
    232     public void testGetDescriptorInt() {
    233
    234         testGetDescriptorBase("intProperty", Integer.TYPE);
    235
    236     }
    237
    238
    239     /***
    240      * Positive getDynaProperty on property <code>longProperty</code>.
    241      */
    242     public void testGetDescriptorLong() {
    243
    244         testGetDescriptorBase("longProperty", Long.TYPE);
    245
    246     }
    247
    248
    249     /***
    250      * Positive getDynaProperty on property <code>booleanSecond</code>
    251      * that uses an "is" method as the getter.
    252      */
    253     public void testGetDescriptorSecond() {
    254
    255         testGetDescriptorBase("booleanSecond", Boolean.TYPE);
    256
    257     }
    258
    259
    260     /***
    261      * Positive getDynaProperty on property <code>shortProperty</code>.
    262      */
    263     public void testGetDescriptorShort() {
    264
    265         testGetDescriptorBase("shortProperty", Short.TYPE);
    266
    267     }
    268
    269
    270     /***
    271      * Positive getDynaProperty on property <code>stringProperty</code>.
    272      */
    273     public void testGetDescriptorString() {
    274
    275         testGetDescriptorBase("stringProperty", String.class);
    276
    277     }
    278
    279
    280     /***
    281      * Positive test for getDynaPropertys().  Each property name
    282      * listed in <code>properties</code> should be returned exactly once.
    283      */
    284     public void testGetDescriptors() {
    285
    286         DynaProperty pd[] = bean.getDynaClass().getDynaProperties();
    287         assertNotNull("Got descriptors", pd);
    288         int count[] = new int[properties.length];
    289         for (int i = 0; i < pd.length; i++) {
    290             String name = pd[i].getName();
    291             for (int j = 0; j < properties.length; j++) {
    292                 if (name.equals(properties[j]))
    293                     count[j]++;
    294             }
    295         }
    296         for (int j = 0; j < properties.length; j++) {
    297             if (count[j] < 0)
    298                 fail("Missing property " + properties[j]);
    299             else if (count[j] > 1)
    300                 fail("Duplicate property " + properties[j]);
    301         }
    302
    303     }
    304
    305
    306     /***
    307      * Corner cases on getIndexedProperty invalid arguments.
    308      */
    309     public void testGetIndexedArguments() {
    310
    311         try {
    312             bean.get("intArray", -1);
    313             fail("Should throw IndexOutOfBoundsException");
    314         } catch (IndexOutOfBoundsException e) {
    315             // Expected response
    316         } catch (Throwable t) {
    317             fail("Threw " + t + " instead of IndexOutOfBoundsException");
    318         }
    319
    320
    321     }
    322
    323
    324     /***
    325      * Positive and negative tests on getIndexedProperty valid arguments.
    326      */
    327     public void testGetIndexedValues() {
    328
    329         Object value = null;
    330
    331         for (int i = 0; i < 5; i++) {
    332
    333             try {
    334                 value = bean.get("intArray", i);
    335                 assertNotNull("intArray returned value " + i, value);
    336                 assertTrue("intArray returned Integer " + i,
    337                         value instanceof Integer);
    338                 assertEquals("intArray returned correct " + i, i * 10,
    339                         ((Integer) value).intValue());
    340             } catch (Throwable t) {
    341                 fail("intArray " + i + " threw " + t);
    342             }
    343
    344             try {
    345                 value = bean.get("intIndexed", i);
    346                 assertNotNull("intIndexed returned value " + i, value);
    347                 assertTrue("intIndexed returned Integer " + i,
    348                         value instanceof Integer);
    349                 assertEquals("intIndexed returned correct " + i, i * 10,
    350                         ((Integer) value).intValue());
    351             } catch (Throwable t) {
    352                 fail("intIndexed " + i + " threw " + t);
    353             }
    354
    355             try {
    356                 value = bean.get("listIndexed", i);
    357                 assertNotNull("listIndexed returned value " + i, value);
    358                 assertTrue("list returned String " + i,
    359                         value instanceof String);
    360                 assertEquals("listIndexed returned correct " + i,
    361                         "String " + i, (String) value);
    362             } catch (Throwable t) {
    363                 fail("listIndexed " + i + " threw " + t);
    364             }
    365
    366             try {
    367                 value = bean.get("stringArray", i);
    368                 assertNotNull("stringArray returned value " + i, value);
    369                 assertTrue("stringArray returned String " + i,
    370                         value instanceof String);
    371                 assertEquals("stringArray returned correct " + i,
    372                         "String " + i, (String) value);
    373             } catch (Throwable t) {
    374                 fail("stringArray " + i + " threw " + t);
    375             }
    376
    377             try {
    378                 value = bean.get("stringIndexed", i);
    379                 assertNotNull("stringIndexed returned value " + i, value);
    380                 assertTrue("stringIndexed returned String " + i,
    381                         value instanceof String);
    382                 assertEquals("stringIndexed returned correct " + i,
    383                         "String " + i, (String) value);
    384             } catch (Throwable t) {
    385                 fail("stringIndexed " + i + " threw " + t);
    386             }
    387
    388         }
    389
    390
    391     }
    392
    393
    394     /***
    395      * Corner cases on getMappedProperty invalid arguments.
    396      */
    397     public void testGetMappedArguments() {
    398
    399
    400         try {
    401             Object value = bean.get("mappedProperty", "unknown");
    402             assertNull("Should not return a value", value);
    403         } catch (Throwable t) {
    404             fail("Threw " + t + " instead of returning null");
    405         }
    406
    407
    408     }
    409
    410
    411     /***
    412      * Positive and negative tests on getMappedProperty valid arguments.
    413      */
    414     public void testGetMappedValues() {
    415
    416         Object value = null;
    417
    418         try {
    419             value = bean.get("mappedProperty", "First Key");
    420             assertEquals("Can find first value", "First Value", value);
    421         } catch (Throwable t) {
    422             fail("Finding first value threw " + t);
    423         }
    424
    425         try {
    426             value = bean.get("mappedProperty", "Second Key");
    427             assertEquals("Can find second value", "Second Value", value);
    428         } catch (Throwable t) {
    429             fail("Finding second value threw " + t);
    430         }
    431
    432         try {
    433             value = bean.get("mappedProperty", "Third Key");
    434             assertNull("Can not find third value", value);
    435         } catch (Throwable t) {
    436             fail("Finding third value threw " + t);
    437         }
    438
    439     }
    440
    441
    442     /***
    443      * Corner cases on getSimpleProperty invalid arguments.
    444      */
    445     public void testGetSimpleArguments() {
    446
    447         try {
    448             bean.get(null);
    449             fail("Should throw IllegalArgumentException");
    450         } catch (IllegalArgumentException e) {
    451             // Expected response
    452         } catch (Throwable t) {
    453             fail("Threw " + t + " instead of IllegalArgumentException");
    454         }
    455
    456     }
    457
    458
    459     /***
    460      * Test getSimpleProperty on a boolean property.
    461      */
    462     public void testGetSimpleBoolean() {
    463
    464         try {
    465             Object value = bean.get("booleanProperty");
    466             assertNotNull("Got a value", value);
    467             assertTrue("Got correct type", (value instanceof Boolean));
    468             assertTrue("Got correct value",
    469                     ((Boolean) value).booleanValue() == true);
    470         } catch (Throwable e) {
    471             fail("Exception: " + e);
    472         }
    473
    474     }
    475
    476
    477     /***
    478      * Test getSimpleProperty on a double property.
    479      */
    480     public void testGetSimpleDouble() {
    481
    482         try {
    483             Object value = bean.get("doubleProperty");
    484             assertNotNull("Got a value", value);
    485             assertTrue("Got correct type", (value instanceof Double));
    486             assertEquals("Got correct value",
    487                     ((Double) value).doubleValue(),
    488                     321.0, 0.005);
    489         } catch (Throwable t) {
    490             fail("Exception: " + t);
    491         }
    492
    493     }
    494
    495
    496     /***
    497      * Test getSimpleProperty on a float property.
    498      */
    499     public void testGetSimpleFloat() {
    500
    501         try {
    502             Object value = bean.get("floatProperty");
    503             assertNotNull("Got a value", value);
    504             assertTrue("Got correct type", (value instanceof Float));
    505             assertEquals("Got correct value",
    506                     ((Float) value).floatValue(),
    507                     (float) 123.0,
    508                     (float) 0.005);
    509         } catch (Throwable t) {
    510             fail("Exception: " + t);
    511         }
    512
    513     }
    514
    515
    516     /***
    517      * Test getSimpleProperty on a int property.
    518      */
    519     public void testGetSimpleInt() {
    520
    521         try {
    522             Object value = bean.get("intProperty");
    523             assertNotNull("Got a value", value);
    524             assertTrue("Got correct type", (value instanceof Integer));
    525             assertEquals("Got correct value",
    526                     ((Integer) value).intValue(),
    527                     123);
    528         } catch (Throwable t) {
    529             fail("Exception: " + t);
    530         }
    531
    532     }
    533
    534
    535     /***
    536      * Test getSimpleProperty on a long property.
    537      */
    538     public void testGetSimpleLong() {
    539
    540         try {
    541             Object value = bean.get("longProperty");
    542             assertNotNull("Got a value", value);
    543             assertTrue("Got correct type", (value instanceof Long));
    544             assertEquals("Got correct value",
    545                     ((Long) value).longValue(),
    546                     321);
    547         } catch (Throwable t) {
    548             fail("Exception: " + t);
    549         }
    550
    551     }
    552
    553
    554     /***
    555      * Test getSimpleProperty on a short property.
    556      */
    557     public void testGetSimpleShort() {
    558
    559         try {
    560             Object value = bean.get("shortProperty");
    561             assertNotNull("Got a value", value);
    562             assertTrue("Got correct type", (value instanceof Short));
    563             assertEquals("Got correct value",
    564                     ((Short) value).shortValue(),
    565                     (short) 987);
    566         } catch (Throwable t) {
    567             fail("Exception: " + t);
    568         }
    569
    570     }
    571
    572
    573     /***
    574      * Test getSimpleProperty on a String property.
    575      */
    576     public void testGetSimpleString() {
    577
    578         try {
    579             Object value = bean.get("stringProperty");
    580             assertNotNull("Got a value", value);
    581             assertTrue("Got correct type", (value instanceof String));
    582             assertEquals("Got correct value",
    583                     (String) value,
    584                     "This is a string");
    585         } catch (Throwable t) {
    586             fail("Exception: " + t);
    587         }
    588
    589     }
    590
    591
    592     /***
    593      * Test <code>contains()</code> method for mapped properties.
    594      */
    595     public void testMappedContains() {
    596
    597         try {
    598             assertTrue("Can see first key",
    599                     bean.contains("mappedProperty", "First Key"));
    600         } catch (Throwable t) {
    601             fail("Exception: " + t);
    602         }
    603
    604
    605         try {
    606             assertTrue("Can not see unknown key",
    607                     !bean.contains("mappedProperty", "Unknown Key"));
    608         } catch (Throwable t) {
    609             fail("Exception: " + t);
    610         }
    611
    612     }
    613
    614
    615     /***
    616      * Test <code>remove()</code> method for mapped properties.
    617      */
    618     public void testMappedRemove() {
    619
    620         try {
    621             assertTrue("Can see first key",
    622                     bean.contains("mappedProperty", "First Key"));
    623             bean.remove("mappedProperty", "First Key");
    624             assertTrue("Can not see first key",
    625                     !bean.contains("mappedProperty", "First Key"));
    626         } catch (Throwable t) {
    627             fail("Exception: " + t);
    628         }
    629
    630         try {
    631             assertTrue("Can not see unknown key",
    632                     !bean.contains("mappedProperty", "Unknown Key"));
    633             bean.remove("mappedProperty", "Unknown Key");
    634             assertTrue("Can not see unknown key",
    635                     !bean.contains("mappedProperty", "Unknown Key"));
    636         } catch (Throwable t) {
    637             fail("Exception: " + t);
    638         }
    639
    640     }
    641
    642
    643     /***
    644      * Test serialization and deserialization.
    645      */
    646     public void testSerialization() {
    647
    648         // Serialize the test bean
    649         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    650         try {
    651             ObjectOutputStream oos = new ObjectOutputStream(baos);
    652             oos.writeObject(bean);
    653             oos.flush();
    654             oos.close();
    655         } catch (Exception e) {
    656             fail("Exception during serialization: " + e);
    657         }
    658
    659         // Deserialize the test bean
    660         try {
    661             bean = null;
    662             ByteArrayInputStream bais =
    663                 new ByteArrayInputStream(baos.toByteArray());
    664             ObjectInputStream ois = new ObjectInputStream(bais);
    665             bean = (DynaBean) ois.readObject();
    666             bais.close();
    667         } catch (Exception e) {
    668             fail("Exception during deserialization: " + e);
    669         }
    670
    671         // Confirm property values
    672         testGetDescriptorArguments();
    673         testGetDescriptorBoolean();
    674         testGetDescriptorDouble();
    675         testGetDescriptorFloat();
    676         testGetDescriptorInt();
    677         testGetDescriptorLong();
    678         testGetDescriptorSecond();
    679         testGetDescriptorShort();
    680         testGetDescriptorString();
    681         testGetDescriptors();
    682         testGetIndexedArguments();
    683         testGetIndexedValues();
    684         testGetMappedArguments();
    685         testGetMappedValues();
    686         testGetSimpleArguments();
    687         testGetSimpleBoolean();
    688         testGetSimpleDouble();
    689         testGetSimpleFloat();
    690         testGetSimpleInt();
    691         testGetSimpleLong();
    692         testGetSimpleShort();
    693         testGetSimpleString();
    694         testMappedContains();
    695         testMappedRemove();
    696
    697         // Ensure that we can create a new instance of the same DynaClass
    698         try {
    699             bean = bean.getDynaClass().newInstance();
    700         } catch (Exception e) {
    701             fail("Exception creating new instance: " + e);
    702         }
    703         testGetDescriptorArguments();
    704         testGetDescriptorBoolean();
    705         testGetDescriptorDouble();
    706         testGetDescriptorFloat();
    707         testGetDescriptorInt();
    708         testGetDescriptorLong();
    709         testGetDescriptorSecond();
    710         testGetDescriptorShort();
    711         testGetDescriptorString();
    712         testGetDescriptors();
    713
    714     }
    715
    716
    717     /***
    718      * Corner cases on setIndexedProperty invalid arguments.
    719      */
    720     public void testSetIndexedArguments() {
    721
    722         try {
    723             bean.set("intArray", -1, new Integer(0));
    724             fail("Should throw IndexOutOfBoundsException");
    725         } catch (IndexOutOfBoundsException e) {
    726             // Expected response
    727         } catch (Throwable t) {
    728             fail("Threw " + t + " instead of IndexOutOfBoundsException");
    729         }
    730
    731     }
    732
    733
    734     /***
    735      * Positive and negative tests on setIndexedProperty valid arguments.
    736      */
    737     public void testSetIndexedValues() {
    738
    739         Object value = null;
    740
    741         try {
    742             bean.set("intArray", 0, new Integer(1));
    743             value = (Integer) bean.get("intArray", 0);
    744             assertNotNull("Returned new value 0", value);
    745             assertTrue("Returned Integer new value 0",
    746                     value instanceof Integer);
    747             assertEquals("Returned correct new value 0", 1,
    748                     ((Integer) value).intValue());
    749         } catch (Throwable t) {
    750             fail("Threw " + t);
    751         }
    752
    753         try {
    754             bean.set("intIndexed", 1, new Integer(11));
    755             value = (Integer) bean.get("intIndexed", 1);
    756             assertNotNull("Returned new value 1", value);
    757             assertTrue("Returned Integer new value 1",
    758                     value instanceof Integer);
    759             assertEquals("Returned correct new value 1", 11,
    760                     ((Integer) value).intValue());
    761         } catch (Throwable t) {
    762             fail("Threw " + t);
    763         }
    764
    765         try {
    766             bean.set("listIndexed", 2, "New Value 2");
    767             value = (String) bean.get("listIndexed", 2);
    768             assertNotNull("Returned new value 2", value);
    769             assertTrue("Returned String new value 2",
    770                     value instanceof String);
    771             assertEquals("Returned correct new value 2", "New Value 2",
    772                     (String) value);
    773         } catch (Throwable t) {
    774             fail("Threw " + t);
    775         }
    776
    777         try {
    778             bean.set("stringArray", 3, "New Value 3");
    779             value = (String) bean.get("stringArray", 3);
    780             assertNotNull("Returned new value 3", value);
    781             assertTrue("Returned String new value 3",
    782                     value instanceof String);
    783             assertEquals("Returned correct new value 3", "New Value 3",
    784                     (String) value);
    785         } catch (Throwable t) {
    786             fail("Threw " + t);
    787         }
    788
    789         try {
    790             bean.set("stringIndexed", 4, "New Value 4");
    791             value = (String) bean.get("stringIndexed", 4);
    792             assertNotNull("Returned new value 4", value);
    793             assertTrue("Returned String new value 4",
    794                     value instanceof String);
    795             assertEquals("Returned correct new value 4", "New Value 4",
    796                     (String) value);
    797         } catch (Throwable t) {
    798             fail("Threw " + t);
    799         }
    800
    801
    802     }
    803
    804
    805     /***
    806      * Positive and negative tests on setMappedProperty valid arguments.
    807      */
    808     public void testSetMappedValues() {
    809
    810         try {
    811             bean.set("mappedProperty", "First Key", "New First Value");
    812             assertEquals("Can replace old value",
    813                     "New First Value",
    814                     (String) bean.get("mappedProperty", "First Key"));
    815         } catch (Throwable t) {
    816             fail("Finding fourth value threw " + t);
    817         }
    818
    819         try {
    820             bean.set("mappedProperty", "Fourth Key", "Fourth Value");
    821             assertEquals("Can set new value",
    822                     "Fourth Value",
    823                     (String) bean.get("mappedProperty", "Fourth Key"));
    824         } catch (Throwable t) {
    825             fail("Finding fourth value threw " + t);
    826         }
    827
    828
    829     }
    830
    831
    832     /***
    833      * Test setSimpleProperty on a boolean property.
    834      */
    835     public void testSetSimpleBoolean() {
    836
    837         try {
    838             boolean oldValue =
    839                     ((Boolean) bean.get("booleanProperty")).booleanValue();
    840             boolean newValue = !oldValue;
    841             bean.set("booleanProperty", new Boolean(newValue));
    842             assertTrue("Matched new value",
    843                     newValue ==
    844                     ((Boolean) bean.get("booleanProperty")).booleanValue());
    845         } catch (Throwable e) {
    846             fail("Exception: " + e);
    847         }
    848
    849     }
    850
    851
    852     /***
    853      * Test setSimpleProperty on a double property.
    854      */
    855     public void testSetSimpleDouble() {
    856
    857         try {
    858             double oldValue =
    859                     ((Double) bean.get("doubleProperty")).doubleValue();
    860             double newValue = oldValue + 1.0;
    861             bean.set("doubleProperty", new Double(newValue));
    862             assertEquals("Matched new value",
    863                     newValue,
    864                     ((Double) bean.get("doubleProperty")).doubleValue(),
    865                     0.005);
    866         } catch (Throwable e) {
    867             fail("Exception: " + e);
    868         }
    869
    870     }
    871
    872
    873     /***
    874      * Test setSimpleProperty on a float property.
    875      */
    876     public void testSetSimpleFloat() {
    877
    878         try {
    879             float oldValue =
    880                     ((Float) bean.get("floatProperty")).floatValue();
    881             float newValue = oldValue + (float) 1.0;
    882             bean.set("floatProperty", new Float(newValue));
    883             assertEquals("Matched new value",
    884                     newValue,
    885                     ((Float) bean.get("floatProperty")).floatValue(),
    886                     (float) 0.005);
    887         } catch (Throwable e) {
    888             fail("Exception: " + e);
    889         }
    890
    891     }
    892
    893
    894     /***
    895      * Test setSimpleProperty on a int property.
    896      */
    897     public void testSetSimpleInt() {
    898
    899         try {
    900             int oldValue =
    901                     ((Integer) bean.get("intProperty")).intValue();
    902             int newValue = oldValue + 1;
    903             bean.set("intProperty", new Integer(newValue));
    904             assertEquals("Matched new value",
    905                     newValue,
    906                     ((Integer) bean.get("intProperty")).intValue());
    907         } catch (Throwable e) {
    908             fail("Exception: " + e);
    909         }
    910
    911     }
    912
    913
    914     /***
    915      * Test setSimpleProperty on a long property.
    916      */
    917     public void testSetSimpleLong() {
    918
    919         try {
    920             long oldValue =
    921                     ((Long) bean.get("longProperty")).longValue();
    922             long newValue = oldValue + 1;
    923             bean.set("longProperty", new Long(newValue));
    924             assertEquals("Matched new value",
    925                     newValue,
    926                     ((Long) bean.get("longProperty")).longValue());
    927         } catch (Throwable e) {
    928             fail("Exception: " + e);
    929         }
    930
    931     }
    932
    933
    934     /***
    935      * Test setSimpleProperty on a short property.
    936      */
    937     public void testSetSimpleShort() {
    938
    939         try {
    940             short oldValue =
    941                     ((Short) bean.get("shortProperty")).shortValue();
    942             short newValue = (short) (oldValue + 1);
    943             bean.set("shortProperty", new Short(newValue));
    944             assertEquals("Matched new value",
    945                     newValue,
    946                     ((Short) bean.get("shortProperty")).shortValue());
    947         } catch (Throwable e) {
    948             fail("Exception: " + e);
    949         }
    950
    951     }
    952
    953
    954     /***
    955      * Test setSimpleProperty on a String property.
    956      */
    957     public void testSetSimpleString() {
    958
    959         try {
    960             String oldValue = (String) bean.get("stringProperty");
    961             String newValue = oldValue + " Extra Value";
    962             bean.set("stringProperty", newValue);
    963             assertEquals("Matched new value",
    964                     newValue,
    965                     (String) bean.get("stringProperty"));
    966         } catch (Throwable e) {
    967             fail("Exception: " + e);
    968         }
    969
    970     }
    971
    972
    973     // ------------------------------------------------------ Protected Methods
    974
    975
    976     /***
    977      * Create and return a <code>DynaClass</code> instance for our test
    978      * <code>DynaBean</code>.
    979      */
    980     protected DynaClass createDynaClass() {
    981
    982         int intArray[] = new int[0];
    983         String stringArray[] = new String[0];
    984
    985         DynaClass dynaClass = new BasicDynaClass
    986                 ("TestDynaClass", null,
    987                         new DynaProperty[]{
    988                             new DynaProperty("booleanProperty", Boolean.TYPE),
    989                             new DynaProperty("booleanSecond", Boolean.TYPE),
    990                             new DynaProperty("doubleProperty", Double.TYPE),
    991                             new DynaProperty("floatProperty", Float.TYPE),
    992                             new DynaProperty("intArray", intArray.getClass()),
    993                             new DynaProperty("intIndexed", intArray.getClass()),
    994                             new DynaProperty("intProperty", Integer.TYPE),
    995                             new DynaProperty("listIndexed", List.class),
    996                             new DynaProperty("longProperty", Long.TYPE),
    997                             new DynaProperty("mappedProperty", Map.class),
    998                             new DynaProperty("mappedIntProperty", Map.class),
    999                             new DynaProperty("nullProperty", String.class),
    1000                             new DynaProperty("shortProperty", Short.TYPE),
    1001                             new DynaProperty("stringArray", stringArray.getClass()),
    1002                             new DynaProperty("stringIndexed", stringArray.getClass()),
    1003                             new DynaProperty("stringProperty", String.class),
    1004                         });
    1005         return (dynaClass);
    1006
    1007     }
    1008
    1009
    1010     /***
    1011      * Base for testGetDescriptorXxxxx() series of tests.
    1012      *
    1013      * @param name Name of the property to be retrieved
    1014      * @param type Expected class type of this property
    1015      */
    1016     protected void testGetDescriptorBase(String name, Class type) {
    1017
    1018         try {
    1019             DynaProperty descriptor =
    1020                     bean.getDynaClass().getDynaProperty(name);
    1021             assertNotNull("Got descriptor", descriptor);
    1022             assertEquals("Got correct type", type, descriptor.getType());
    1023         } catch (Throwable t) {
    1024             fail("Threw an exception: " + t);
    1025         }
    1026
    1027     }
    1028
    1029
    1030 }