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.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          // we can not simply call assertEquals because of the nested properties in TestBean
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          // The value of writeOnlyProperty can not be read out from original, so it must have the
100         // original value
101         assertEquals( "writeOnlyProperty has been changed on clone!", defaults.getWriteOnlyPropertyValue(),
102                       clone.getWriteOnlyPropertyValue() );
103     }
104 
105     /**
106      * Makes sure, that the general contract of creating a shallow clone is not violated.
107      */
108     @Test
109     public void cloneIsShallow()
110         throws Exception
111     {
112         clone = on( original ).cloneBean();
113 
114         assertShallow( original, clone );
115     }
116 }