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