View Javadoc
1   package org.apache.commons.beanutils2;
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.apache.commons.beanutils2.BeanUtils.on;
21  import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.*;
22  import static org.junit.Assert.assertEquals;
23  
24  import java.util.Map;
25  
26  import org.apache.commons.beanutils2.testbeans.TestBean;
27  import org.junit.*;
28  
29  public class CopyPropertiesTestCase
30  {
31  
32      private TestBean source;
33  
34      private ExtendedTestBean target;
35  
36      @Before
37      public void setUp()
38      {
39          source = new TestBean();
40          target = new ExtendedTestBean();
41      }
42  
43      @After
44      public void tearDown()
45      {
46          source = null;
47          target = null;
48      }
49  
50      @Test( expected = NullPointerException.class )
51      public void copyPropertiesToNull()
52          throws Exception
53      {
54          on( source ).copyPropertiesTo( null );
55      }
56  
57      @Test
58      public void copyPropertiesToTargetWithChangedScalarProperties()
59          throws Exception
60      {
61          changeScalarProperties( target );
62          on( source ).copyPropertiesTo( target );
63          // we can not simply call assertEquals because of the nested properties in TestBean
64          assertReadWritePropertiesEquals( source, target );
65          assertExtendedPropertiesUnchanged();
66      }
67  
68      private void assertExtendedPropertiesUnchanged()
69      {
70          ExtendedTestBean expected = new ExtendedTestBean();
71          assertEquals( expected.extendedBooleanProperty, target.extendedBooleanProperty );
72          assertEquals( expected.extendedReadOnlyProperty, target.extendedReadOnlyProperty );
73          assertEquals( expected.extendedStringProperty, target.extendedStringProperty );
74      }
75  
76      @Test
77      public void copyPropertiesToTargetWithChangedArrayProperties()
78          throws Exception
79      {
80          changeArrayProperties( target );
81          on( source ).copyPropertiesTo( target );
82          assertReadWritePropertiesEquals( source, target );
83          assertExtendedPropertiesUnchanged();
84      }
85  
86      /**
87       * Makes sure that copy properties has the same effect as populate, if source and target are of the same type
88       */
89      @Test
90      public void copyPropertiesToSameAsPopulate()
91          throws Exception
92      {
93          changeScalarProperties( source );
94          changeArrayProperties( source );
95          Map<String, Object> sourceProperties = on( source ).describe();
96  
97          TestBean targetForCopyProperties = new TestBean();
98          TestBean targetForPopulate = new TestBean();
99  
100         on( source ).copyPropertiesTo( targetForCopyProperties );
101         on( targetForPopulate ).populate( sourceProperties );
102 
103         assertReadWritePropertiesEquals( source, targetForCopyProperties );
104         assertReadWritePropertiesEquals( source, targetForPopulate );
105         assertReadWritePropertiesEquals( targetForCopyProperties, targetForPopulate );
106     }
107 
108     /**
109      * Makes sure, that coping properties does not change the source
110      */
111     @Test
112     public void copyPropertiesToTargetNotChangingSource()
113         throws Exception
114     {
115         Map<String, Object> propertiesBefore = on( source ).describe();
116         on( source ).copyPropertiesTo( target );
117         Map<String, Object> propertiesAfter = on( source ).describe();
118 
119         assertEquals( propertiesBefore, propertiesAfter );
120     }
121 
122     /**
123      * Makes sure, that properties are copied shallow. In other words, that complex properties like nested beans will
124      * only be referenced from target and not copied.
125      */
126     @Test
127     public void copyPropertiesToTargetIsShallow() throws Exception {
128         on( source ).copyPropertiesTo( target );
129 
130         assertShallow(source, target);
131     }
132 
133     @SuppressWarnings( "unused" ) // unused methods invoked via introspection
134     private static class ExtendedTestBean
135         extends TestBean
136     {
137 
138         private static final long serialVersionUID = 4854743778089894313L;
139 
140         private boolean extendedBooleanProperty = true;
141 
142         private final String extendedReadOnlyProperty = "extended read only property";
143 
144         private String extendedStringProperty = "extended string property";
145 
146         public boolean getExtendedBooleanProperty()
147         {
148             return extendedBooleanProperty;
149         }
150 
151         public void setExtendedBooleanProperty( boolean booleanProperty )
152         {
153             extendedBooleanProperty = booleanProperty;
154         }
155 
156         public String getExtendedReadOnlyProperty()
157         {
158             return extendedReadOnlyProperty;
159         }
160 
161         public String getExtendedStringProperty()
162         {
163             return extendedStringProperty;
164         }
165 
166         public void setExtendedStringProperty( String stringProperty )
167         {
168             extendedStringProperty = stringProperty;
169         }
170 
171     }
172 
173 }