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.*; 022import static org.junit.Assert.assertEquals; 023 024import java.util.Map; 025 026import org.apache.commons.beanutils2.testbeans.TestBean; 027import org.junit.*; 028 029public class CopyPropertiesTestCase 030{ 031 032 private TestBean source; 033 034 private ExtendedTestBean target; 035 036 @Before 037 public void setUp() 038 { 039 source = new TestBean(); 040 target = new ExtendedTestBean(); 041 } 042 043 @After 044 public void tearDown() 045 { 046 source = null; 047 target = null; 048 } 049 050 @Test( expected = NullPointerException.class ) 051 public void copyPropertiesToNull() 052 throws Exception 053 { 054 on( source ).copyPropertiesTo( null ); 055 } 056 057 @Test 058 public void copyPropertiesToTargetWithChangedScalarProperties() 059 throws Exception 060 { 061 changeScalarProperties( target ); 062 on( source ).copyPropertiesTo( target ); 063 // we can not simply call assertEquals because of the nested properties in TestBean 064 assertReadWritePropertiesEquals( source, target ); 065 assertExtendedPropertiesUnchanged(); 066 } 067 068 private void assertExtendedPropertiesUnchanged() 069 { 070 ExtendedTestBean expected = new ExtendedTestBean(); 071 assertEquals( expected.extendedBooleanProperty, target.extendedBooleanProperty ); 072 assertEquals( expected.extendedReadOnlyProperty, target.extendedReadOnlyProperty ); 073 assertEquals( expected.extendedStringProperty, target.extendedStringProperty ); 074 } 075 076 @Test 077 public void copyPropertiesToTargetWithChangedArrayProperties() 078 throws Exception 079 { 080 changeArrayProperties( target ); 081 on( source ).copyPropertiesTo( target ); 082 assertReadWritePropertiesEquals( source, target ); 083 assertExtendedPropertiesUnchanged(); 084 } 085 086 /** 087 * Makes sure that copy properties has the same effect as populate, if source and target are of the same type 088 */ 089 @Test 090 public void copyPropertiesToSameAsPopulate() 091 throws Exception 092 { 093 changeScalarProperties( source ); 094 changeArrayProperties( source ); 095 Map<String, Object> sourceProperties = on( source ).describe(); 096 097 TestBean targetForCopyProperties = new TestBean(); 098 TestBean targetForPopulate = new TestBean(); 099 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}