View Javadoc
1   package org.apache.commons.beanutils2.testbeans;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static org.junit.Assert.assertArrayEquals;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertSame;
23  
24  public final class TestBeanUtils
25  {
26      private TestBeanUtils()
27      {
28          // private constructor to prevent instantiation
29      }
30  
31      public static void changeArrayProperties( TestBean testBean )
32      {
33          int intArray[] = new int[] { 123, 456, 789 };
34          String stringArray[] = new String[] { "New String 0", "New String 1" };
35          testBean.setIntArray( intArray );
36          testBean.setStringArray( stringArray );
37      }
38  
39      public static void changeScalarProperties( TestBean testBean )
40      {
41          testBean.setBooleanProperty( false );
42          // booleanSecond is left at true
43          testBean.setByteProperty( (byte) 111 );
44          testBean.setDoubleProperty( 432.0 );
45          // floatProperty is left at 123.0
46          testBean.setIntProperty( 543 );
47          testBean.setLongProperty( 123456789l );
48          testBean.setNullProperty( "Non-null value" );
49          testBean.setShortProperty( (short) 654 );
50          // stringProperty is left at "This is a string"
51      }
52  
53      /**
54       * Asserts that all properties that are readable and writable on {@code actual} are equal to those on
55       * {@code expected}.
56       *
57       * @param expected the {@code TestBean} with the expected properties.
58       * @param actual the {@code TestBean} with the actual values.
59       */
60      public static void assertReadWritePropertiesEquals( TestBean expected, TestBean actual )
61      {
62          assertEquals( "Property 'anotherNested' is not equal!", expected.getAnotherNested(), actual.getAnotherNested() );
63          assertEquals( "Property 'booleanProperty' is not equal!", expected.getBooleanProperty(),
64                        actual.getBooleanProperty() );
65          assertEquals( "Property 'booleanSecond' is not equal!", expected.isBooleanSecond(), actual.isBooleanSecond() );
66          assertEquals( "Property 'byteProperty' is not equal!", expected.getByteProperty(), actual.getByteProperty() );
67          assertArrayEquals( "Property 'DateArrayProperty' is not equal!", expected.getDateArrayProperty(),
68                             actual.getDateArrayProperty() );
69          assertEquals( "Property 'dateProperty' is not equal!", expected.getDateProperty(), actual.getDateProperty() );
70          assertEquals( "Property 'doubleProperty' is not equal!", expected.getDoubleProperty(),
71                        actual.getDoubleProperty(), 0.0 );
72          assertArrayEquals( "Property 'dupProperty' is not equal!", expected.getDupProperty(), actual.getDupProperty() );
73          assertEquals( "Property 'floatProperty' is not equal!", expected.getFloatProperty(), actual.getFloatProperty(),
74                        0.0 );
75          assertArrayEquals( "Property 'intArrayProperty' is not equal!", expected.getIntArray(), actual.getIntArray() );
76          assertEquals( "Property 'intProperty' is not equal!", expected.getIntProperty(), actual.getIntProperty() );
77          assertEquals( "Property 'invalidBoolean' is not equal!", expected.getInvalidBoolean(),
78                        actual.getInvalidBoolean() );
79          assertEquals( "Property 'listIndexed' is not equal!", expected.getListIndexed(), actual.getListIndexed() );
80          assertEquals( "Property 'longProperty' is not equal!", expected.getLongProperty(), actual.getLongProperty() );
81          assertEquals( "Property 'mapProperty' is not equal!", expected.getMapProperty(), actual.getMapProperty() );
82          assertEquals( "Property 'nested' is not equal!", expected.getNested(), actual.getNested() );
83          assertEquals( "Property 'nullProperty' is not equal!", expected.getNullProperty(), actual.getNullProperty() );
84          assertEquals( "Property 'shortProperty' is not equal!", expected.getShortProperty(), actual.getShortProperty() );
85          assertArrayEquals( "Property 'stringArray' is not equal!", expected.getStringArray(), actual.getStringArray() );
86          assertEquals( "Property 'stringProperty' is not equal!", expected.getStringProperty(),
87                        actual.getStringProperty() );
88      }
89  
90      /**
91       * Asserts, that complex properties of {@code referenced} are the same as in {@code referencing} (that they are just
92       * referenced).
93       */
94      public static void assertShallow( TestBean referenced, TestBean referencing )
95      {
96          assertSame( referenced.getAnotherNested(), referencing.getAnotherNested() );
97          assertSame( referenced.getDateArrayProperty(), referencing.getDateArrayProperty() );
98          assertSame( referenced.getDateProperty(), referencing.getDateProperty() );
99          assertSame( referenced.getDateProperty(), referencing.getDateProperty() );
100         assertSame( referenced.getDupProperty(), referencing.getDupProperty() );
101         assertSame( referenced.getIntArray(), referencing.getIntArray() );
102         assertSame( referenced.getMapProperty(), referencing.getMapProperty() );
103         // mappedNested and nested are lazy initialized, so the following getXXX calls will create new instances on
104         // both, clone and original, causing the assertions to fail.
105         // assertSame(clone.getMappedNested(), original.getMappedNested());
106         // assertSame(clone.getNested(), original.getNested());
107         assertSame( referenced.getStringArray(), referencing.getStringArray() );
108         assertSame( referenced.getStringProperty(), referencing.getStringProperty() );
109     }
110 
111 }