    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.util.ArrayList;
    23  import java.util.HashMap;
    24  import java.util.List;
    25  import java.util.Map;
    26  import java.io.Serializable;
    27
    28
    29  /***
    30   * General purpose test bean for JUnit tests for the "beanutils" component.
    31   *
    32   * @author Craig R. McClanahan
    33   * @author Rodney Waldhoff
    34   * @version $Revision: 556237 $ $Date: 2007-07-14 08:27:18 +0100 (Sat, 14 Jul 2007) $
    35   */
    36
    37  public class TestBean implements Serializable {
    38
    39      // ----------------------------------------------------------- Constructors
    40
    41      public TestBean() {
    42          listIndexed.add("String 0");
    43          listIndexed.add("String 1");
    44          listIndexed.add("String 2");
    45          listIndexed.add("String 3");
    46          listIndexed.add("String 4");
    47      }
    48
    49      public TestBean(String stringProperty) {
    50          setStringProperty(stringProperty);
    51      }
    52
    53      public TestBean(float floatProperty) {
    54          setFloatProperty(floatProperty);
    55      }
    56
    57      public TestBean(boolean booleanProperty) {
    58          setBooleanProperty(booleanProperty);
    59      }
    60
    61      public TestBean(Boolean booleanSecond) {
    62          setBooleanSecond(booleanSecond.booleanValue());
    63      }
    64
    65      public TestBean(float floatProperty, String stringProperty) {
    66          setFloatProperty(floatProperty);
    67          setStringProperty(stringProperty);
    68      }
    69
    70      public TestBean(boolean booleanProperty, String stringProperty) {
    71          setBooleanProperty(booleanProperty);
    72          setStringProperty(stringProperty);
    73      }
    74
    75      public TestBean(Boolean booleanSecond, String stringProperty) {
    76          setBooleanSecond(booleanSecond.booleanValue());
    77          setStringProperty(stringProperty);
    78      }
    79
    80      public TestBean(Integer intProperty) {
    81          setIntProperty(intProperty.intValue());
    82      }
    83
    84     public TestBean(double doubleProperty) {
    85         setDoubleProperty(doubleProperty);
    86     }
    87
    88      TestBean(int intProperty) {
    89          setIntProperty(intProperty);
    90      }
    91
    92      protected TestBean(boolean booleanProperty, boolean booleanSecond, String stringProperty) {
    93          setBooleanProperty(booleanProperty);
    94          setBooleanSecond(booleanSecond);
    95          setStringProperty(stringProperty);
    96      }
    97
    98      public TestBean(List listIndexed) {
    99          this.listIndexed = listIndexed;
    100     }
    101
    102     public TestBean(String[][] string2dArray) {
    103         this.string2dArray = string2dArray;
    104     }
    105
    106     // ------------------------------------------------------------- Properties
    107
    108
    109     /***
    110      * A boolean property.
    111      */
    112     private boolean booleanProperty = true;
    113
    114     public boolean getBooleanProperty() {
    115         return (booleanProperty);
    116     }
    117
    118     public void setBooleanProperty(boolean booleanProperty) {
    119         this.booleanProperty = booleanProperty;
    120     }
    121
    122
    123     /***
    124      * A boolean property that uses an "is" method for the getter.
    125      */
    126     private boolean booleanSecond = true;
    127
    128     public boolean isBooleanSecond() {
    129         return (booleanSecond);
    130     }
    131
    132     public void setBooleanSecond(boolean booleanSecond) {
    133         this.booleanSecond = booleanSecond;
    134     }
    135
    136
    137     /***
    138      * A byte property.
    139      */
    140     private byte byteProperty = (byte) 121;
    141
    142     public byte getByteProperty() {
    143         return (this.byteProperty);
    144     }
    145
    146     public void setByteProperty(byte byteProperty) {
    147         this.byteProperty = byteProperty;
    148     }
    149
    150
    151     /***
    152      * A java.util.Date property.
    153      */
    154     private java.util.Date dateProperty;
    155
    156     public java.util.Date getDateProperty() {
    157         return dateProperty;
    158     }
    159
    160     public void setDateProperty(java.util.Date dateProperty) {
    161         this.dateProperty = dateProperty;
    162     }
    163
    164     /***
    165      * A java.util.Date property.
    166      */
    167     private java.util.Date[] dateArrayProperty;
    168
    169     public java.util.Date[] getDateArrayProperty() {
    170         return dateArrayProperty;
    171     }
    172
    173     public void setDateArrayProperty(java.util.Date[] dateArrayProperty) {
    174         this.dateArrayProperty = dateArrayProperty;
    175     }
    176
    177     /***
    178      * A double property.
    179      */
    180     private double doubleProperty = 321.0;
    181
    182     public double getDoubleProperty() {
    183         return (this.doubleProperty);
    184     }
    185
    186     public void setDoubleProperty(double doubleProperty) {
    187         this.doubleProperty = doubleProperty;
    188     }
    189
    190
    191     /***
    192      * An "indexed property" accessible via both array and subscript
    193      * based getters and setters.
    194      */
    195     private String[] dupProperty =
    196     { "Dup 0", "Dup 1", "Dup 2", "Dup 3", "Dup 4" };
    197
    198     public String[] getDupProperty() {
    199         return (this.dupProperty);
    200     }
    201
    202     public String getDupProperty(int index) {
    203         return (this.dupProperty[index]);
    204     }
    205
    206     public void setDupProperty(int index, String value) {
    207         this.dupProperty[index] = value;
    208     }
    209
    210     public void setDupProperty(String[] dupProperty) {
    211         this.dupProperty = dupProperty;
    212     }
    213
    214
    215     /***
    216      * A float property.
    217      */
    218     private float floatProperty = (float) 123.0;
    219
    220     public float getFloatProperty() {
    221         return (this.floatProperty);
    222     }
    223
    224     public void setFloatProperty(float floatProperty) {
    225         this.floatProperty = floatProperty;
    226     }
    227
    228
    229     /***
    230      * An integer array property accessed as an array.
    231      */
    232     private int intArray[] = { 0, 10, 20, 30, 40 };
    233
    234     public int[] getIntArray() {
    235         return (this.intArray);
    236     }
    237
    238     public void setIntArray(int[] intArray) {
    239         this.intArray = intArray;
    240     }
    241
    242
    243     /***
    244      * An integer array property accessed as an indexed property.
    245      */
    246     private int intIndexed[] = { 0, 10, 20, 30, 40 };
    247
    248     public int getIntIndexed(int index) {
    249         return (intIndexed[index]);
    250     }
    251
    252     public void setIntIndexed(int index, int value) {
    253         intIndexed[index] = value;
    254     }
    255
    256
    257     /***
    258      * An integer property.
    259      */
    260     private int intProperty = 123;
    261
    262     public int getIntProperty() {
    263         return (this.intProperty);
    264     }
    265
    266     public void setIntProperty(int intProperty) {
    267         this.intProperty = intProperty;
    268     }
    269
    270
    271     /***
    272      * A List property accessed as an indexed property.
    273      */
    274     private List listIndexed = new ArrayList();
    275
    276     public List getListIndexed() {
    277         return (listIndexed);
    278     }
    279
    280
    281     /***
    282      * A long property.
    283      */
    284     private long longProperty = 321;
    285
    286     public long getLongProperty() {
    287         return (this.longProperty);
    288     }
    289
    290     public void setLongProperty(long longProperty) {
    291         this.longProperty = longProperty;
    292     }
    293
    294
    295     /***
    296      * A mapped property with only a getter and setter for a Map.
    297      */
    298     private Map mapProperty = null;
    299
    300     public Map getMapProperty() {
    301         // Create the map the very first time
    302         if (mapProperty == null) {
    303             mapProperty = new HashMap();
    304             mapProperty.put("First Key", "First Value");
    305             mapProperty.put("Second Key", "Second Value");
    306         }
    307         return (mapProperty);
    308     }
    309
    310     public void setMapProperty(Map mapProperty) {
    311         // Create the map the very first time
    312         if (mapProperty == null) {
    313             mapProperty = new HashMap();
    314             mapProperty.put("First Key", "First Value");
    315             mapProperty.put("Second Key", "Second Value");
    316         }
    317         this.mapProperty = mapProperty;
    318     }
    319
    320
    321     /***
    322      * A mapped property that has String keys and Object values.
    323      */
    324     private HashMap mappedObjects = null;
    325
    326     public Object getMappedObjects(String key) {
    327         // Create the map the very first time
    328         if (mappedObjects == null) {
    329             mappedObjects = new HashMap();
    330             mappedObjects.put("First Key", "First Value");
    331             mappedObjects.put("Second Key", "Second Value");
    332         }
    333         return (mappedObjects.get(key));
    334     }
    335
    336     public void setMappedObjects(String key, Object value) {
    337         // Create the map the very first time
    338         if (mappedObjects == null) {
    339             mappedObjects = new HashMap();
    340             mappedObjects.put("First Key", "First Value");
    341             mappedObjects.put("Second Key", "Second Value");
    342         }
    343         mappedObjects.put(key, value);
    344     }
    345
    346
    347     /***
    348      * A mapped property that has String keys and String values.
    349      */
    350     private HashMap mappedProperty = null;
    351
    352     public String getMappedProperty(String key) {
    353         // Create the map the very first time
    354         if (mappedProperty == null) {
    355             mappedProperty = new HashMap();
    356             mappedProperty.put("First Key", "First Value");
    357             mappedProperty.put("Second Key", "Second Value");
    358         }
    359         return ((String) mappedProperty.get(key));
    360     }
    361
    362     public void setMappedProperty(String key, String value) {
    363         // Create the map the very first time
    364         if (mappedProperty == null) {
    365             mappedProperty = new HashMap();
    366             mappedProperty.put("First Key", "First Value");
    367             mappedProperty.put("Second Key", "Second Value");
    368         }
    369         mappedProperty.put(key, value);
    370     }
    371
    372
    373     /***
    374      * A mapped property that has String keys and int values.
    375      */
    376     private HashMap mappedIntProperty = null;
    377
    378     public int getMappedIntProperty(String key) {
    379         // Create the map the very first time
    380         if (mappedIntProperty == null) {
    381             mappedIntProperty = new HashMap();
    382             mappedIntProperty.put("One", new Integer(1));
    383             mappedIntProperty.put("Two", new Integer(2));
    384         }
    385         Integer x = (Integer) mappedIntProperty.get(key);
    386         return ((x == null) ? 0 : x.intValue());
    387     }
    388
    389     public void setMappedIntProperty(String key, int value) {
    390         mappedIntProperty.put(key, new Integer(value));
    391     }
    392
    393
    394     /***
    395      * A nested reference to another test bean (populated as needed).
    396      */
    397     private TestBean nested = null;
    398
    399     public TestBean getNested() {
    400         if (nested == null)
    401             nested = new TestBean();
    402         return (nested);
    403     }
    404
    405    /***
    406     * Another nested reference to another test bean,
    407     */
    408    private TestBean anotherNested = null;
    409
    410    public TestBean getAnotherNested() {
    411       return anotherNested;
    412    }
    413
    414    public void setAnotherNested( TestBean anotherNested ) {
    415       this.anotherNested = anotherNested;
    416    }
    417
    418     /*
    419      * Another nested reference to a bean containing mapp properties
    420      */
    421     public class MappedTestBean {
    422         public void setValue(String key,String val) { }
    423         public String getValue(String key) { return "Mapped Value"; }
    424     }
    425
    426     private MappedTestBean mappedNested = null;
    427
    428     public MappedTestBean getMappedNested() {
    429         if (mappedNested == null)
    430         {
    431             mappedNested = new MappedTestBean();
    432         }
    433         return mappedNested;
    434     }
    435
    436     /***
    437      * A String property with an initial value of null.
    438      */
    439     private String nullProperty = null;
    440
    441     public String getNullProperty() {
    442         return (this.nullProperty);
    443     }
    444
    445     public void setNullProperty(String nullProperty) {
    446         this.nullProperty = nullProperty;
    447     }
    448
    449
    450     /***
    451      * A read-only String property.
    452      */
    453     private String readOnlyProperty = "Read Only String Property";
    454
    455     public String getReadOnlyProperty() {
    456         return (this.readOnlyProperty);
    457     }
    458
    459
    460     /***
    461      * A short property.
    462      */
    463     private short shortProperty = (short) 987;
    464
    465     public short getShortProperty() {
    466         return (this.shortProperty);
    467     }
    468
    469     public void setShortProperty(short shortProperty) {
    470         this.shortProperty = shortProperty;
    471     }
    472
    473
    474     /***
    475      * A String array property accessed as a String.
    476      */
    477     private String[] stringArray =
    478             { "String 0", "String 1", "String 2", "String 3", "String 4" };
    479
    480     public String[] getStringArray() {
    481         return (this.stringArray);
    482     }
    483
    484     public void setStringArray(String[] stringArray) {
    485         this.stringArray = stringArray;
    486     }
    487
    488
    489     /***
    490      * A String array property accessed as an indexed property.
    491      */
    492     private String[] stringIndexed =
    493             { "String 0", "String 1", "String 2", "String 3", "String 4" };
    494
    495     public String getStringIndexed(int index) {
    496         return (stringIndexed[index]);
    497     }
    498
    499     public void setStringIndexed(int index, String value) {
    500         stringIndexed[index] = value;
    501     }
    502
    503     private String[][] string2dArray = new String[][] {new String[] {"1", "2", "3"}, new String[] {"4","5","6"}};
    504     public String[] getString2dArray(int index) {
    505         return string2dArray[index];
    506     }
    507
    508     /***
    509      * A String property.
    510      */
    511     private String stringProperty = "This is a string";
    512
    513     public String getStringProperty() {
    514         return (this.stringProperty);
    515     }
    516
    517     public void setStringProperty(String stringProperty) {
    518         this.stringProperty = stringProperty;
    519     }
    520
    521
    522     /***
    523      * A write-only String property.
    524      */
    525     private String writeOnlyProperty = "Write Only String Property";
    526
    527     public String getWriteOnlyPropertyValue() {
    528         return (this.writeOnlyProperty);
    529     }
    530
    531     public void setWriteOnlyProperty(String writeOnlyProperty) {
    532         this.writeOnlyProperty = writeOnlyProperty;
    533     }
    534
    535
    536     // ------------------------------------------------------ Invalid Properties
    537
    538
    539     /***
    540      * <p>An invalid property that has two boolean getters (getInvalidBoolean
    541      * and isInvalidBoolean) plus a String setter (setInvalidBoolean).  By the
    542      * rules described in the JavaBeans Specification, this will be considered
    543      * a read-only boolean property, using isInvalidBoolean() as the getter.</p>
    544      */
    545     private boolean invalidBoolean = false;
    546
    547     public boolean getInvalidBoolean() {
    548 	return (this.invalidBoolean);
    549     }
    550
    551     public boolean isInvalidBoolean() {
    552 	return (this.invalidBoolean);
    553     }
    554
    555     public void setInvalidBoolean(String invalidBoolean) {
    556 	if ("true".equalsIgnoreCase(invalidBoolean) ||
    557 	    "yes".equalsIgnoreCase(invalidBoolean) ||
    558 	    "1".equalsIgnoreCase(invalidBoolean)) {
    559 	    this.invalidBoolean = true;
    560 	} else {
    561 	    this.invalidBoolean = false;
    562 	}
    563     }
    564
    565
    566
    567     // ------------------------------------------------------- Static Variables
    568
    569
    570     /***
    571      * A static variable that is accessed and updated via static methods
    572      * for MethodUtils testing.
    573      */
    574     private static int counter = 0;
    575
    576
    577     /***
    578      * Return the current value of the counter.
    579      */
    580     public static int currentCounter() {
    581
    582         return (counter);
    583
    584     }
    585
    586
    587     /***
    588      * Increment the current value of the counter by 1.
    589      */
    590     public static void incrementCounter() {
    591
    592         incrementCounter(1);
    593
    594     }
    595
    596     /***
    597      * Increment the current value of the counter by the specified amount.
    598      *
    599      * @param amount Amount to be added to the current counter
    600      */
    601     public static void incrementCounter(int amount) {
    602
    603         counter += amount;
    604
    605     }
    606
    607     /***
    608      * Increments the current value of the count by the
    609      * specified amount * 2. It has the same name
    610      * as the method above so as to test the looseness
    611      * of getMethod.
    612      */
    613     public static void incrementCounter(Number amount) {
    614         counter += 2 * amount.intValue();
    615     }
    616
    617 }