001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024: import java.io.Serializable;
025:
026: /**
027: * General purpose test bean for JUnit tests for the "beanutils" component.
028: *
029: * @author Craig R. McClanahan
030: * @author Rodney Waldhoff
031: * @version $Revision: 556237 $ $Date: 2007-07-14 08:27:18 +0100 (Sat, 14 Jul 2007) $
032: */
033:
034: public class TestBean implements Serializable {
035:
036: // ----------------------------------------------------------- Constructors
037:
038: public TestBean() {
039: listIndexed.add("String 0");
040: listIndexed.add("String 1");
041: listIndexed.add("String 2");
042: listIndexed.add("String 3");
043: listIndexed.add("String 4");
044: }
045:
046: public TestBean(String stringProperty) {
047: setStringProperty(stringProperty);
048: }
049:
050: public TestBean(float floatProperty) {
051: setFloatProperty(floatProperty);
052: }
053:
054: public TestBean(boolean booleanProperty) {
055: setBooleanProperty(booleanProperty);
056: }
057:
058: public TestBean(Boolean booleanSecond) {
059: setBooleanSecond(booleanSecond.booleanValue());
060: }
061:
062: public TestBean(float floatProperty, String stringProperty) {
063: setFloatProperty(floatProperty);
064: setStringProperty(stringProperty);
065: }
066:
067: public TestBean(boolean booleanProperty, String stringProperty) {
068: setBooleanProperty(booleanProperty);
069: setStringProperty(stringProperty);
070: }
071:
072: public TestBean(Boolean booleanSecond, String stringProperty) {
073: setBooleanSecond(booleanSecond.booleanValue());
074: setStringProperty(stringProperty);
075: }
076:
077: public TestBean(Integer intProperty) {
078: setIntProperty(intProperty.intValue());
079: }
080:
081: public TestBean(double doubleProperty) {
082: setDoubleProperty(doubleProperty);
083: }
084:
085: TestBean(int intProperty) {
086: setIntProperty(intProperty);
087: }
088:
089: protected TestBean(boolean booleanProperty, boolean booleanSecond,
090: String stringProperty) {
091: setBooleanProperty(booleanProperty);
092: setBooleanSecond(booleanSecond);
093: setStringProperty(stringProperty);
094: }
095:
096: public TestBean(List listIndexed) {
097: this .listIndexed = listIndexed;
098: }
099:
100: public TestBean(String[][] string2dArray) {
101: this .string2dArray = string2dArray;
102: }
103:
104: // ------------------------------------------------------------- Properties
105:
106: /**
107: * A boolean property.
108: */
109: private boolean booleanProperty = true;
110:
111: public boolean getBooleanProperty() {
112: return (booleanProperty);
113: }
114:
115: public void setBooleanProperty(boolean booleanProperty) {
116: this .booleanProperty = booleanProperty;
117: }
118:
119: /**
120: * A boolean property that uses an "is" method for the getter.
121: */
122: private boolean booleanSecond = true;
123:
124: public boolean isBooleanSecond() {
125: return (booleanSecond);
126: }
127:
128: public void setBooleanSecond(boolean booleanSecond) {
129: this .booleanSecond = booleanSecond;
130: }
131:
132: /**
133: * A byte property.
134: */
135: private byte byteProperty = (byte) 121;
136:
137: public byte getByteProperty() {
138: return (this .byteProperty);
139: }
140:
141: public void setByteProperty(byte byteProperty) {
142: this .byteProperty = byteProperty;
143: }
144:
145: /**
146: * A java.util.Date property.
147: */
148: private java.util.Date dateProperty;
149:
150: public java.util.Date getDateProperty() {
151: return dateProperty;
152: }
153:
154: public void setDateProperty(java.util.Date dateProperty) {
155: this .dateProperty = dateProperty;
156: }
157:
158: /**
159: * A java.util.Date property.
160: */
161: private java.util.Date[] dateArrayProperty;
162:
163: public java.util.Date[] getDateArrayProperty() {
164: return dateArrayProperty;
165: }
166:
167: public void setDateArrayProperty(java.util.Date[] dateArrayProperty) {
168: this .dateArrayProperty = dateArrayProperty;
169: }
170:
171: /**
172: * A double property.
173: */
174: private double doubleProperty = 321.0;
175:
176: public double getDoubleProperty() {
177: return (this .doubleProperty);
178: }
179:
180: public void setDoubleProperty(double doubleProperty) {
181: this .doubleProperty = doubleProperty;
182: }
183:
184: /**
185: * An "indexed property" accessible via both array and subscript
186: * based getters and setters.
187: */
188: private String[] dupProperty = { "Dup 0", "Dup 1", "Dup 2",
189: "Dup 3", "Dup 4" };
190:
191: public String[] getDupProperty() {
192: return (this .dupProperty);
193: }
194:
195: public String getDupProperty(int index) {
196: return (this .dupProperty[index]);
197: }
198:
199: public void setDupProperty(int index, String value) {
200: this .dupProperty[index] = value;
201: }
202:
203: public void setDupProperty(String[] dupProperty) {
204: this .dupProperty = dupProperty;
205: }
206:
207: /**
208: * A float property.
209: */
210: private float floatProperty = (float) 123.0;
211:
212: public float getFloatProperty() {
213: return (this .floatProperty);
214: }
215:
216: public void setFloatProperty(float floatProperty) {
217: this .floatProperty = floatProperty;
218: }
219:
220: /**
221: * An integer array property accessed as an array.
222: */
223: private int intArray[] = { 0, 10, 20, 30, 40 };
224:
225: public int[] getIntArray() {
226: return (this .intArray);
227: }
228:
229: public void setIntArray(int[] intArray) {
230: this .intArray = intArray;
231: }
232:
233: /**
234: * An integer array property accessed as an indexed property.
235: */
236: private int intIndexed[] = { 0, 10, 20, 30, 40 };
237:
238: public int getIntIndexed(int index) {
239: return (intIndexed[index]);
240: }
241:
242: public void setIntIndexed(int index, int value) {
243: intIndexed[index] = value;
244: }
245:
246: /**
247: * An integer property.
248: */
249: private int intProperty = 123;
250:
251: public int getIntProperty() {
252: return (this .intProperty);
253: }
254:
255: public void setIntProperty(int intProperty) {
256: this .intProperty = intProperty;
257: }
258:
259: /**
260: * A List property accessed as an indexed property.
261: */
262: private List listIndexed = new ArrayList();
263:
264: public List getListIndexed() {
265: return (listIndexed);
266: }
267:
268: /**
269: * A long property.
270: */
271: private long longProperty = 321;
272:
273: public long getLongProperty() {
274: return (this .longProperty);
275: }
276:
277: public void setLongProperty(long longProperty) {
278: this .longProperty = longProperty;
279: }
280:
281: /**
282: * A mapped property with only a getter and setter for a Map.
283: */
284: private Map mapProperty = null;
285:
286: public Map getMapProperty() {
287: // Create the map the very first time
288: if (mapProperty == null) {
289: mapProperty = new HashMap();
290: mapProperty.put("First Key", "First Value");
291: mapProperty.put("Second Key", "Second Value");
292: }
293: return (mapProperty);
294: }
295:
296: public void setMapProperty(Map mapProperty) {
297: // Create the map the very first time
298: if (mapProperty == null) {
299: mapProperty = new HashMap();
300: mapProperty.put("First Key", "First Value");
301: mapProperty.put("Second Key", "Second Value");
302: }
303: this .mapProperty = mapProperty;
304: }
305:
306: /**
307: * A mapped property that has String keys and Object values.
308: */
309: private HashMap mappedObjects = null;
310:
311: public Object getMappedObjects(String key) {
312: // Create the map the very first time
313: if (mappedObjects == null) {
314: mappedObjects = new HashMap();
315: mappedObjects.put("First Key", "First Value");
316: mappedObjects.put("Second Key", "Second Value");
317: }
318: return (mappedObjects.get(key));
319: }
320:
321: public void setMappedObjects(String key, Object value) {
322: // Create the map the very first time
323: if (mappedObjects == null) {
324: mappedObjects = new HashMap();
325: mappedObjects.put("First Key", "First Value");
326: mappedObjects.put("Second Key", "Second Value");
327: }
328: mappedObjects.put(key, value);
329: }
330:
331: /**
332: * A mapped property that has String keys and String values.
333: */
334: private HashMap mappedProperty = null;
335:
336: public String getMappedProperty(String key) {
337: // Create the map the very first time
338: if (mappedProperty == null) {
339: mappedProperty = new HashMap();
340: mappedProperty.put("First Key", "First Value");
341: mappedProperty.put("Second Key", "Second Value");
342: }
343: return ((String) mappedProperty.get(key));
344: }
345:
346: public void setMappedProperty(String key, String value) {
347: // Create the map the very first time
348: if (mappedProperty == null) {
349: mappedProperty = new HashMap();
350: mappedProperty.put("First Key", "First Value");
351: mappedProperty.put("Second Key", "Second Value");
352: }
353: mappedProperty.put(key, value);
354: }
355:
356: /**
357: * A mapped property that has String keys and int values.
358: */
359: private HashMap mappedIntProperty = null;
360:
361: public int getMappedIntProperty(String key) {
362: // Create the map the very first time
363: if (mappedIntProperty == null) {
364: mappedIntProperty = new HashMap();
365: mappedIntProperty.put("One", new Integer(1));
366: mappedIntProperty.put("Two", new Integer(2));
367: }
368: Integer x = (Integer) mappedIntProperty.get(key);
369: return ((x == null) ? 0 : x.intValue());
370: }
371:
372: public void setMappedIntProperty(String key, int value) {
373: mappedIntProperty.put(key, new Integer(value));
374: }
375:
376: /**
377: * A nested reference to another test bean (populated as needed).
378: */
379: private TestBean nested = null;
380:
381: public TestBean getNested() {
382: if (nested == null)
383: nested = new TestBean();
384: return (nested);
385: }
386:
387: /**
388: * Another nested reference to another test bean,
389: */
390: private TestBean anotherNested = null;
391:
392: public TestBean getAnotherNested() {
393: return anotherNested;
394: }
395:
396: public void setAnotherNested(TestBean anotherNested) {
397: this .anotherNested = anotherNested;
398: }
399:
400: /*
401: * Another nested reference to a bean containing mapp properties
402: */
403: public class MappedTestBean {
404: public void setValue(String key, String val) {
405: }
406:
407: public String getValue(String key) {
408: return "Mapped Value";
409: }
410: }
411:
412: private MappedTestBean mappedNested = null;
413:
414: public MappedTestBean getMappedNested() {
415: if (mappedNested == null) {
416: mappedNested = new MappedTestBean();
417: }
418: return mappedNested;
419: }
420:
421: /**
422: * A String property with an initial value of null.
423: */
424: private String nullProperty = null;
425:
426: public String getNullProperty() {
427: return (this .nullProperty);
428: }
429:
430: public void setNullProperty(String nullProperty) {
431: this .nullProperty = nullProperty;
432: }
433:
434: /**
435: * A read-only String property.
436: */
437: private String readOnlyProperty = "Read Only String Property";
438:
439: public String getReadOnlyProperty() {
440: return (this .readOnlyProperty);
441: }
442:
443: /**
444: * A short property.
445: */
446: private short shortProperty = (short) 987;
447:
448: public short getShortProperty() {
449: return (this .shortProperty);
450: }
451:
452: public void setShortProperty(short shortProperty) {
453: this .shortProperty = shortProperty;
454: }
455:
456: /**
457: * A String array property accessed as a String.
458: */
459: private String[] stringArray = { "String 0", "String 1",
460: "String 2", "String 3", "String 4" };
461:
462: public String[] getStringArray() {
463: return (this .stringArray);
464: }
465:
466: public void setStringArray(String[] stringArray) {
467: this .stringArray = stringArray;
468: }
469:
470: /**
471: * A String array property accessed as an indexed property.
472: */
473: private String[] stringIndexed = { "String 0", "String 1",
474: "String 2", "String 3", "String 4" };
475:
476: public String getStringIndexed(int index) {
477: return (stringIndexed[index]);
478: }
479:
480: public void setStringIndexed(int index, String value) {
481: stringIndexed[index] = value;
482: }
483:
484: private String[][] string2dArray = new String[][] {
485: new String[] { "1", "2", "3" },
486: new String[] { "4", "5", "6" } };
487:
488: public String[] getString2dArray(int index) {
489: return string2dArray[index];
490: }
491:
492: /**
493: * A String property.
494: */
495: private String stringProperty = "This is a string";
496:
497: public String getStringProperty() {
498: return (this .stringProperty);
499: }
500:
501: public void setStringProperty(String stringProperty) {
502: this .stringProperty = stringProperty;
503: }
504:
505: /**
506: * A write-only String property.
507: */
508: private String writeOnlyProperty = "Write Only String Property";
509:
510: public String getWriteOnlyPropertyValue() {
511: return (this .writeOnlyProperty);
512: }
513:
514: public void setWriteOnlyProperty(String writeOnlyProperty) {
515: this .writeOnlyProperty = writeOnlyProperty;
516: }
517:
518: // ------------------------------------------------------ Invalid Properties
519:
520: /**
521: * <p>An invalid property that has two boolean getters (getInvalidBoolean
522: * and isInvalidBoolean) plus a String setter (setInvalidBoolean). By the
523: * rules described in the JavaBeans Specification, this will be considered
524: * a read-only boolean property, using isInvalidBoolean() as the getter.</p>
525: */
526: private boolean invalidBoolean = false;
527:
528: public boolean getInvalidBoolean() {
529: return (this .invalidBoolean);
530: }
531:
532: public boolean isInvalidBoolean() {
533: return (this .invalidBoolean);
534: }
535:
536: public void setInvalidBoolean(String invalidBoolean) {
537: if ("true".equalsIgnoreCase(invalidBoolean)
538: || "yes".equalsIgnoreCase(invalidBoolean)
539: || "1".equalsIgnoreCase(invalidBoolean)) {
540: this .invalidBoolean = true;
541: } else {
542: this .invalidBoolean = false;
543: }
544: }
545:
546: // ------------------------------------------------------- Static Variables
547:
548: /**
549: * A static variable that is accessed and updated via static methods
550: * for MethodUtils testing.
551: */
552: private static int counter = 0;
553:
554: /**
555: * Return the current value of the counter.
556: */
557: public static int currentCounter() {
558:
559: return (counter);
560:
561: }
562:
563: /**
564: * Increment the current value of the counter by 1.
565: */
566: public static void incrementCounter() {
567:
568: incrementCounter(1);
569:
570: }
571:
572: /**
573: * Increment the current value of the counter by the specified amount.
574: *
575: * @param amount Amount to be added to the current counter
576: */
577: public static void incrementCounter(int amount) {
578:
579: counter += amount;
580:
581: }
582:
583: /**
584: * Increments the current value of the count by the
585: * specified amount * 2. It has the same name
586: * as the method above so as to test the looseness
587: * of getMethod.
588: */
589: public static void incrementCounter(Number amount) {
590: counter += 2 * amount.intValue();
591: }
592:
593: }
|