001package org.apache.commons.beanutils2; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020import static org.apache.commons.beanutils2.BeanUtils.on; 021import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.assertReadWritePropertiesEquals; 022import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.assertShallow; 023import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.changeArrayProperties; 024import static org.apache.commons.beanutils2.testbeans.TestBeanUtils.changeScalarProperties; 025import static org.junit.Assert.assertEquals; 026import static org.junit.Assert.assertNotNull; 027 028import org.apache.commons.beanutils2.testbeans.TestBean; 029import org.junit.After; 030import org.junit.Before; 031import org.junit.Test; 032 033public class CloneTestCase 034{ 035 036 private TestBean original; 037 private TestBean defaults; 038 039 private TestBean clone; 040 041 @Before 042 public void setUp() 043 { 044 original = new TestBean(); 045 defaults = new TestBean(); 046 } 047 048 @After 049 public void tearDown() 050 { 051 original = null; 052 defaults = null; 053 clone = null; 054 } 055 056 @Test 057 public void cloneTestBean() 058 throws Exception 059 { 060 clone = on( defaults ).cloneBean(); 061 assertNotNull( "Clone is null!", clone ); 062 // we can not simply call assertEquals because of the nested properties in TestBean 063 assertReadWritePropertiesEquals( defaults, clone ); 064 } 065 066 @Test 067 public void cloneWithChangedScalarProperties() 068 throws Exception 069 { 070 changeScalarProperties( original ); 071 072 clone = on( original ).cloneBean(); 073 assertNotNull( "Clone is null!", clone ); 074 075 assertReadWritePropertiesEquals( original, clone ); 076 } 077 078 @Test 079 public void cloneWithChangedArrayProperties() 080 throws Exception 081 { 082 changeArrayProperties( original ); 083 084 clone = on( original ).cloneBean(); 085 assertNotNull( clone ); 086 assertReadWritePropertiesEquals( original, clone ); 087 } 088 089 @Test 090 public void cloneWithChangedWriteOnlyProperty() 091 throws Exception 092 { 093 original.setWriteOnlyProperty( "New writeOnlyProperty value" ); 094 095 clone = on( original ).cloneBean(); 096 assertNotNull( "Clone is null!", clone ); 097 assertReadWritePropertiesEquals( original, clone ); 098 099 // 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}