View Javadoc

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: 687190 $ $Date: 2008-08-19 18:51:19 -0400 (Tue, 19 Aug 2008) $
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 another test bean,
420     */
421    private DynaBean nestedDynaBean = null;
422     
423    public DynaBean getNestedDynaBean() {
424       return nestedDynaBean;
425    }
426     
427    public void setNestedDynaBean(DynaBean nestedDynaBean) {
428       this.nestedDynaBean = nestedDynaBean;
429    }
430    
431     /*
432      * Another nested reference to a bean containing mapp properties
433      */
434     public class MappedTestBean {
435         public void setValue(String key,String val) { }
436         public String getValue(String key) { return "Mapped Value"; }
437     }
438 
439     private MappedTestBean mappedNested = null;
440 
441     public MappedTestBean getMappedNested() {
442         if (mappedNested == null)
443         {
444             mappedNested = new MappedTestBean();
445         }
446         return mappedNested;
447     }
448 
449     /**
450      * A String property with an initial value of null.
451      */
452     private String nullProperty = null;
453 
454     public String getNullProperty() {
455         return (this.nullProperty);
456     }
457 
458     public void setNullProperty(String nullProperty) {
459         this.nullProperty = nullProperty;
460     }
461 
462 
463     /**
464      * A read-only String property.
465      */
466     private String readOnlyProperty = "Read Only String Property";
467 
468     public String getReadOnlyProperty() {
469         return (this.readOnlyProperty);
470     }
471 
472 
473     /**
474      * A short property.
475      */
476     private short shortProperty = (short) 987;
477 
478     public short getShortProperty() {
479         return (this.shortProperty);
480     }
481 
482     public void setShortProperty(short shortProperty) {
483         this.shortProperty = shortProperty;
484     }
485 
486 
487     /**
488      * A String array property accessed as a String.
489      */
490     private String[] stringArray =
491             { "String 0", "String 1", "String 2", "String 3", "String 4" };
492 
493     public String[] getStringArray() {
494         return (this.stringArray);
495     }
496 
497     public void setStringArray(String[] stringArray) {
498         this.stringArray = stringArray;
499     }
500 
501 
502     /**
503      * A String array property accessed as an indexed property.
504      */
505     private String[] stringIndexed =
506             { "String 0", "String 1", "String 2", "String 3", "String 4" };
507 
508     public String getStringIndexed(int index) {
509         return (stringIndexed[index]);
510     }
511 
512     public void setStringIndexed(int index, String value) {
513         stringIndexed[index] = value;
514     }
515 
516     private String[][] string2dArray = new String[][] {new String[] {"1", "2", "3"}, new String[] {"4","5","6"}};
517     public String[] getString2dArray(int index) {
518         return string2dArray[index];
519     }
520 
521     /**
522      * A String property.
523      */
524     private String stringProperty = "This is a string";
525 
526     public String getStringProperty() {
527         return (this.stringProperty);
528     }
529 
530     public void setStringProperty(String stringProperty) {
531         this.stringProperty = stringProperty;
532     }
533 
534 
535     /**
536      * A write-only String property.
537      */
538     private String writeOnlyProperty = "Write Only String Property";
539 
540     public String getWriteOnlyPropertyValue() {
541         return (this.writeOnlyProperty);
542     }
543 
544     public void setWriteOnlyProperty(String writeOnlyProperty) {
545         this.writeOnlyProperty = writeOnlyProperty;
546     }
547 
548 
549     // ------------------------------------------------------ Invalid Properties
550 
551 
552     /**
553      * <p>An invalid property that has two boolean getters (getInvalidBoolean
554      * and isInvalidBoolean) plus a String setter (setInvalidBoolean).  By the
555      * rules described in the JavaBeans Specification, this will be considered
556      * a read-only boolean property, using isInvalidBoolean() as the getter.</p>
557      */
558     private boolean invalidBoolean = false;
559 
560     public boolean getInvalidBoolean() {
561         return (this.invalidBoolean);
562     }
563 
564     public boolean isInvalidBoolean() {
565         return (this.invalidBoolean);
566     }
567 
568     public void setInvalidBoolean(String invalidBoolean) {
569         if ("true".equalsIgnoreCase(invalidBoolean) ||
570             "yes".equalsIgnoreCase(invalidBoolean) ||
571             "1".equalsIgnoreCase(invalidBoolean)) {
572             this.invalidBoolean = true;
573         } else {
574             this.invalidBoolean = false;
575         }
576     }
577 
578 
579 
580     // ------------------------------------------------------- Static Variables
581 
582 
583     /**
584      * A static variable that is accessed and updated via static methods
585      * for MethodUtils testing.
586      */
587     private static int counter = 0;
588 
589 
590     /**
591      * Return the current value of the counter.
592      */
593     public static int currentCounter() {
594 
595         return (counter);
596 
597     }
598 
599 
600     /**
601      * Increment the current value of the counter by 1.
602      */
603     public static void incrementCounter() {
604 
605         incrementCounter(1);
606 
607     }
608 
609     /**
610      * Increment the current value of the counter by the specified amount.
611      *
612      * @param amount Amount to be added to the current counter
613      */
614     public static void incrementCounter(int amount) {
615 
616         counter += amount;
617 
618     }
619 
620     /**
621      * Increments the current value of the count by the 
622      * specified amount * 2. It has the same name 
623      * as the method above so as to test the looseness 
624      * of getMethod. 
625      */
626     public static void incrementCounter(Number amount) {
627         counter += 2 * amount.intValue();
628     }
629 
630 }