1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
124
125
126 @Test
127 public void copyPropertiesToTargetIsShallow() throws Exception {
128 on( source ).copyPropertiesTo( target );
129
130 assertShallow(source, target);
131 }
132
133 @SuppressWarnings( "unused" )
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 }