KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > TestBean


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26
27 /**
28  * General purpose test bean for JUnit tests for the "beanutils" component.
29  *
30  * @author Craig R. McClanahan
31  * @author Rodney Waldhoff
32  * @version $Revision: 1.1 $ $Date: 2004/05/10 20:01:16 $
33  */

34
35 public class TestBean {
36
37     // ----------------------------------------------------------- Constructors
38

39     public TestBean() {
40     }
41
42     public TestBean(String JavaDoc stringProperty) {
43         setStringProperty(stringProperty);
44     }
45
46     public TestBean(float floatProperty) {
47         setFloatProperty(floatProperty);
48     }
49
50     public TestBean(boolean booleanProperty) {
51         setBooleanProperty(booleanProperty);
52     }
53
54     public TestBean(Boolean JavaDoc booleanSecond) {
55         setBooleanSecond(booleanSecond.booleanValue());
56     }
57
58     public TestBean(float floatProperty, String JavaDoc stringProperty) {
59         setFloatProperty(floatProperty);
60         setStringProperty(stringProperty);
61     }
62
63     public TestBean(boolean booleanProperty, String JavaDoc stringProperty) {
64         setBooleanProperty(booleanProperty);
65         setStringProperty(stringProperty);
66     }
67
68     public TestBean(Boolean JavaDoc booleanSecond, String JavaDoc stringProperty) {
69         setBooleanSecond(booleanSecond.booleanValue());
70         setStringProperty(stringProperty);
71     }
72
73     public TestBean(Integer JavaDoc intProperty) {
74         setIntProperty(intProperty.intValue());
75     }
76
77    public TestBean(double doubleProperty) {
78        setDoubleProperty(doubleProperty);
79    }
80    
81     TestBean(int intProperty) {
82         setIntProperty(intProperty);
83     }
84
85     protected TestBean(boolean booleanProperty, boolean booleanSecond, String JavaDoc stringProperty) {
86         setBooleanProperty(booleanProperty);
87         setBooleanSecond(booleanSecond);
88         setStringProperty(stringProperty);
89     }
90
91     // ------------------------------------------------------------- Properties
92

93
94     /**
95      * A boolean property.
96      */

97     private boolean booleanProperty = true;
98
99     public boolean getBooleanProperty() {
100         return (booleanProperty);
101     }
102
103     public void setBooleanProperty(boolean booleanProperty) {
104         this.booleanProperty = booleanProperty;
105     }
106
107
108     /**
109      * A boolean property that uses an "is" method for the getter.
110      */

111     private boolean booleanSecond = true;
112
113     public boolean isBooleanSecond() {
114         return (booleanSecond);
115     }
116
117     public void setBooleanSecond(boolean booleanSecond) {
118         this.booleanSecond = booleanSecond;
119     }
120
121
122     /**
123      * A byte property.
124      */

125     private byte byteProperty = (byte) 121;
126
127     public byte getByteProperty() {
128         return (this.byteProperty);
129     }
130
131     public void setByteProperty(byte byteProperty) {
132         this.byteProperty = byteProperty;
133     }
134
135
136     /**
137      * A double property.
138      */

139     private double doubleProperty = 321.0;
140
141     public double getDoubleProperty() {
142         return (this.doubleProperty);
143     }
144
145     public void setDoubleProperty(double doubleProperty) {
146         this.doubleProperty = doubleProperty;
147     }
148
149
150     /**
151      * An "indexed property" accessible via both array and subscript
152      * based getters and setters.
153      */

154     private String JavaDoc dupProperty[] =
155     { "Dup 0", "Dup 1", "Dup 2", "Dup 3", "Dup 4" };
156
157     public String JavaDoc[] getDupProperty() {
158         return (this.dupProperty);
159     }
160
161     public String JavaDoc getDupProperty(int index) {
162         return (this.dupProperty[index]);
163     }
164
165     public void setDupProperty(int index, String JavaDoc value) {
166         this.dupProperty[index] = value;
167     }
168
169     public void setDupProperty(String JavaDoc dupProperty[]) {
170         this.dupProperty = dupProperty;
171     }
172
173
174     /**
175      * A float property.
176      */

177     private float floatProperty = (float) 123.0;
178
179     public float getFloatProperty() {
180         return (this.floatProperty);
181     }
182
183     public void setFloatProperty(float floatProperty) {
184         this.floatProperty = floatProperty;
185     }
186
187
188     /**
189      * An integer array property accessed as an array.
190      */

191     private int intArray[] = { 0, 10, 20, 30, 40 };
192
193     public int[] getIntArray() {
194         return (this.intArray);
195     }
196
197     public void setIntArray(int intArray[]) {
198         this.intArray = intArray;
199     }
200
201
202     /**
203      * An integer array property accessed as an indexed property.
204      */

205     private int intIndexed[] = { 0, 10, 20, 30, 40 };
206
207     public int getIntIndexed(int index) {
208         return (intIndexed[index]);
209     }
210
211     public void setIntIndexed(int index, int value) {
212         intIndexed[index] = value;
213     }
214
215
216     /**
217      * An integer property.
218      */

219     private int intProperty = 123;
220
221     public int getIntProperty() {
222         return (this.intProperty);
223     }
224
225     public void setIntProperty(int intProperty) {
226         this.intProperty = intProperty;
227     }
228
229
230     /**
231      * A List property accessed as an indexed property.
232      */

233     private static List JavaDoc listIndexed = new ArrayList JavaDoc();
234
235     static {
236         listIndexed.add("String 0");
237         listIndexed.add("String 1");
238         listIndexed.add("String 2");
239         listIndexed.add("String 3");
240         listIndexed.add("String 4");
241     }
242
243     public List JavaDoc getListIndexed() {
244         return (listIndexed);
245     }
246
247
248     /**
249      * A long property.
250      */

251     private long longProperty = 321;
252
253     public long getLongProperty() {
254         return (this.longProperty);
255     }
256
257     public void setLongProperty(long longProperty) {
258         this.longProperty = longProperty;
259     }
260
261
262     /**
263      * A mapped property with only a getter and setter for a Map.
264      */

265     private Map JavaDoc mapProperty = null;
266
267     public Map JavaDoc getMapProperty() {
268         // Create the map the very first time
269
if (mapProperty == null) {
270             mapProperty = new HashMap JavaDoc();
271             mapProperty.put("First Key", "First Value");
272             mapProperty.put("Second Key", "Second Value");
273         }
274         return (mapProperty);
275     }
276
277     public void setMapProperty(Map JavaDoc mapProperty) {
278         // Create the map the very first time
279
if (mapProperty == null) {
280             mapProperty = new HashMap JavaDoc();
281             mapProperty.put("First Key", "First Value");
282             mapProperty.put("Second Key", "Second Value");
283         }
284         this.mapProperty = mapProperty;
285     }
286
287
288     /**
289      * A mapped property that has String keys and Object values.
290      */

291     private HashMap JavaDoc mappedObjects = null;
292
293     public Object JavaDoc getMappedObjects(String JavaDoc key) {
294         // Create the map the very first time
295
if (mappedObjects == null) {
296             mappedObjects = new HashMap JavaDoc();
297             mappedObjects.put("First Key", "First Value");
298             mappedObjects.put("Second Key", "Second Value");
299         }
300         return (mappedObjects.get(key));
301     }
302
303     public void setMappedObjects(String JavaDoc key, Object JavaDoc value) {
304         // Create the map the very first time
305
if (mappedObjects == null) {
306             mappedObjects = new HashMap JavaDoc();
307             mappedObjects.put("First Key", "First Value");
308             mappedObjects.put("Second Key", "Second Value");
309         }
310         mappedObjects.put(key, value);
311     }
312
313
314     /**
315      * A mapped property that has String keys and String values.
316      */

317     private HashMap JavaDoc mappedProperty = null;
318
319     public String JavaDoc getMappedProperty(String JavaDoc key) {
320         // Create the map the very first time
321
if (mappedProperty == null) {
322             mappedProperty = new HashMap JavaDoc();
323             mappedProperty.put("First Key", "First Value");
324             mappedProperty.put("Second Key", "Second Value");
325         }
326         return ((String JavaDoc) mappedProperty.get(key));
327     }
328
329     public void setMappedProperty(String JavaDoc key, String JavaDoc value) {
330         // Create the map the very first time
331
if (mappedProperty == null) {
332             mappedProperty = new HashMap JavaDoc();
333             mappedProperty.put("First Key", "First Value");
334             mappedProperty.put("Second Key", "Second Value");
335         }
336         mappedProperty.put(key, value);
337     }
338
339
340     /**
341      * A mapped property that has String keys and int values.
342      */

343     private HashMap JavaDoc mappedIntProperty = null;
344
345     public int getMappedIntProperty(String JavaDoc key) {
346         // Create the map the very first time
347
if (mappedProperty == null) {
348             mappedProperty = new HashMap JavaDoc();
349             mappedProperty.put("One", new Integer JavaDoc(1));
350             mappedProperty.put("Two", new Integer JavaDoc(2));
351         }
352         Integer JavaDoc x = (Integer JavaDoc) mappedIntProperty.get(key);
353         return ((x == null) ? 0 : x.intValue());
354     }
355
356     public void setMappedIntProperty(String JavaDoc key, int value) {
357         mappedIntProperty.put(key, new Integer JavaDoc(value));
358     }
359
360
361     /**
362      * A nested reference to another test bean (populated as needed).
363      */

364     private TestBean nested = null;
365
366     public TestBean getNested() {
367         if (nested == null)
368             nested = new TestBean();
369         return (nested);
370     }
371
372    /**
373     * Another nested reference to another test bean,
374     */

375    private TestBean anotherNested = null;
376     
377    public TestBean getAnotherNested() {
378       return anotherNested;
379    }
380     
381    public void setAnotherNested( TestBean anotherNested ) {
382       this.anotherNested = anotherNested;
383    }
384    
385     /*
386      * Another nested reference to a bean containing mapp properties
387      */

388     class MappedTestBean {
389         public void setValue(String JavaDoc key,String JavaDoc val) { }
390         public String JavaDoc getValue(String JavaDoc key) { return "Mapped Value"; }
391     }
392
393     private MappedTestBean mappedNested = null;
394
395     public MappedTestBean getMappedNested() {
396         if (mappedNested == null)
397         {
398             mappedNested = new MappedTestBean();
399         }
400         return mappedNested;
401     }
402
403     /**
404      * A String property with an initial value of null.
405      */

406     private String JavaDoc nullProperty = null;
407
408     public String JavaDoc getNullProperty() {
409         return (this.nullProperty);
410     }
411
412     public void setNullProperty(String JavaDoc nullProperty) {
413         this.nullProperty = nullProperty;
414     }
415
416
417     /**
418      * A read-only String property.
419      */

420     private String JavaDoc readOnlyProperty = "Read Only String Property";
421
422     public String JavaDoc getReadOnlyProperty() {
423         return (this.readOnlyProperty);
424     }
425
426
427     /**
428      * A short property.
429      */

430     private short shortProperty = (short) 987;
431
432     public short getShortProperty() {
433         return (this.shortProperty);
434     }
435
436     public void setShortProperty(short shortProperty) {
437         this.shortProperty = shortProperty;
438     }
439
440
441     /**
442      * A String array property accessed as a String.
443      */

444     private String JavaDoc stringArray[] =
445             { "String 0", "String 1", "String 2", "String 3", "String 4" };
446
447     public String JavaDoc[] getStringArray() {
448         return (this.stringArray);
449     }
450
451     public void setStringArray(String JavaDoc stringArray[]) {
452         this.stringArray = stringArray;
453     }
454
455
456     /**
457      * A String array property accessed as an indexed property.
458      */

459     private String JavaDoc stringIndexed[] =
460             { "String 0", "String 1", "String 2", "String 3", "String 4" };
461
462     public String JavaDoc getStringIndexed(int index) {
463         return (stringIndexed[index]);
464     }
465
466     public void setStringIndexed(int index, String JavaDoc value) {
467         stringIndexed[index] = value;
468     }
469
470
471     /**
472      * A String property.
473      */

474     private String JavaDoc stringProperty = "This is a string";
475
476     public String JavaDoc getStringProperty() {
477         return (this.stringProperty);
478     }
479
480     public void setStringProperty(String JavaDoc stringProperty) {
481         this.stringProperty = stringProperty;
482     }
483
484
485     /**
486      * A write-only String property.
487      */

488     private String JavaDoc writeOnlyProperty = "Write Only String Property";
489
490     public String JavaDoc getWriteOnlyPropertyValue() {
491         return (this.writeOnlyProperty);
492     }
493
494     public void setWriteOnlyProperty(String JavaDoc writeOnlyProperty) {
495         this.writeOnlyProperty = writeOnlyProperty;
496     }
497
498
499     // ------------------------------------------------------ Invalid Properties
500

501
502     /**
503      * <p>An invalid property that has two boolean getters (getInvalidBoolean
504      * and isInvalidBoolean) plus a String setter (setInvalidBoolean). By the
505      * rules described in the JavaBeans Specification, this will be considered
506      * a read-only boolean property, using isInvalidBoolean() as the getter.</p>
507      */

508     private boolean invalidBoolean = false;
509
510     public boolean getInvalidBoolean() {
511     return (this.invalidBoolean);
512     }
513
514     public boolean isInvalidBoolean() {
515     return (this.invalidBoolean);
516     }
517
518     public void setInvalidBoolean(String JavaDoc invalidBoolean) {
519     if ("true".equalsIgnoreCase(invalidBoolean) ||
520         "yes".equalsIgnoreCase(invalidBoolean) ||
521         "1".equalsIgnoreCase(invalidBoolean)) {
522         this.invalidBoolean = true;
523     } else {
524         this.invalidBoolean = false;
525     }
526     }
527
528
529
530     // ------------------------------------------------------- Static Variables
531

532
533     /**
534      * A static variable that is accessed and updated via static methods
535      * for MethodUtils testing.
536      */

537     private static int counter = 0;
538
539
540     /**
541      * Return the current value of the counter.
542      */

543     public static int currentCounter() {
544
545         return (counter);
546
547     }
548
549
550     /**
551      * Increment the current value of the counter by 1.
552      */

553     public static void incrementCounter() {
554
555         incrementCounter(1);
556
557     }
558
559
560     /**
561      * Increment the current value of the counter by the specified amount.
562      *
563      * @param amount Amount to be added to the current counter
564      */

565     public static void incrementCounter(int amount) {
566
567         counter += amount;
568
569     }
570
571
572 }
573
Popular Tags