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.assertReadWritePropertiesEquals;
22 import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.assertShallow;
23 import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.changeArrayProperties;
24 import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.changeScalarProperties;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import org.apache.commons.beanutils2.testbeans.TestBean;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 public class CloneTestCase
34 {
35
36 private TestBean original;
37 private TestBean defaults;
38
39 private TestBean clone;
40
41 @Before
42 public void setUp()
43 {
44 original = new TestBean();
45 defaults = new TestBean();
46 }
47
48 @After
49 public void tearDown()
50 {
51 original = null;
52 defaults = null;
53 clone = null;
54 }
55
56 @Test
57 public void cloneTestBean()
58 throws Exception
59 {
60 clone = on( defaults ).cloneBean();
61 assertNotNull( "Clone is null!", clone );
62
63 assertReadWritePropertiesEquals( defaults, clone );
64 }
65
66 @Test
67 public void cloneWithChangedScalarProperties()
68 throws Exception
69 {
70 changeScalarProperties( original );
71
72 clone = on( original ).cloneBean();
73 assertNotNull( "Clone is null!", clone );
74
75 assertReadWritePropertiesEquals( original, clone );
76 }
77
78 @Test
79 public void cloneWithChangedArrayProperties()
80 throws Exception
81 {
82 changeArrayProperties( original );
83
84 clone = on( original ).cloneBean();
85 assertNotNull( clone );
86 assertReadWritePropertiesEquals( original, clone );
87 }
88
89 @Test
90 public void cloneWithChangedWriteOnlyProperty()
91 throws Exception
92 {
93 original.setWriteOnlyProperty( "New writeOnlyProperty value" );
94
95 clone = on( original ).cloneBean();
96 assertNotNull( "Clone is null!", clone );
97 assertReadWritePropertiesEquals( original, clone );
98
99
100
101 assertEquals( "writeOnlyProperty has been changed on clone!", defaults.getWriteOnlyPropertyValue(),
102 clone.getWriteOnlyPropertyValue() );
103 }
104
105
106
107
108 @Test
109 public void cloneIsShallow()
110 throws Exception
111 {
112 clone = on( original ).cloneBean();
113
114 assertShallow( original, clone );
115 }
116 }