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