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