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.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.apache.commons.beanutils2.testbeans.TestBean;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 public class PopulateTestCase
35 {
36
37 private TestBean target;
38 private TestBean defaults;
39
40 private Map<String, Object> properties;
41
42 @Before
43 public void setUp()
44 {
45 target = new TestBean();
46 defaults = new TestBean();
47 properties = new HashMap<String, Object>();
48 }
49
50 @After
51 public void tearDown()
52 {
53 target = null;
54 defaults = null;
55 properties = null;
56 }
57
58 @Test
59 public void populateEmptry()
60 throws Exception
61 {
62 on( target ).populate( properties );
63 assertTrue( target.equals( defaults ) );
64 }
65
66 @Test( expected = NullPointerException.class )
67 public void populateNull()
68 throws Exception
69 {
70 on( target ).populate( null );
71 }
72
73 @Test
74 public void populateWithNullKey()
75 throws Exception
76 {
77 properties.put( null, new Object() );
78 on( target ).populate( properties );
79 assertTrue( target.equals( defaults ) );
80 }
81
82 @Test
83 public void populateNullValue()
84 throws Exception
85 {
86 target.setStringProperty( "Hello World!" );
87 properties.put( "stringProperty", null );
88 on( target ).populate( properties );
89 assertNull( target.getStringProperty() );
90 }
91
92 @Test
93 public void populateUnknownKey()
94 throws Exception
95 {
96 properties.put( "unkown", "String value" );
97 on( target ).populate( properties );
98 assertEquals( defaults, target ) ;
99 }
100
101 @Test( expected = IllegalArgumentException.class )
102 public void populateNullToPrimitive()
103 throws Exception
104 {
105 properties.put( "intProperty", null );
106 on( target ).populate( properties );
107 }
108
109 @Test
110 public void populatePropertyArrays()
111 throws Exception
112 {
113 setUpArrayProperties();
114 on( target ).populate( properties );
115 validateArrayPropertiesOnTarget();
116 }
117
118 private void setUpArrayProperties()
119 {
120 int intArray[] = new int[] { 123, 456, 789 };
121 String stringArray[] = new String[] { "New String 0", "New String 1" };
122 properties.put( "intArray", intArray );
123 properties.put( "stringArray", stringArray );
124 }
125
126 private void validateArrayPropertiesOnTarget()
127 {
128 int[] expectedInts = (int[]) properties.get( "intArray" );
129 assertArrayEquals( expectedInts, target.getIntArray() );
130
131 String[] expectedStrings = (String[]) properties.get( "stringArray" );
132 assertArrayEquals( expectedStrings, target.getStringArray() );
133 }
134
135 @Test
136 public void populateScalar()
137 throws Exception
138 {
139 setUpScalarProperties();
140 on( target ).populate( properties );
141 validateScalarPropertiesOnTarget();
142 }
143
144 private void setUpScalarProperties()
145 {
146 properties.put( "booleanProperty", false );
147
148 properties.put( "byteProperty", (byte) 111 );
149 properties.put( "doubleProperty", 432.0 );
150
151 properties.put( "intProperty", 543 );
152 properties.put( "longProperty", 123456789l );
153 properties.put( "nullProperty", "Non-null value" );
154 properties.put( "shortProperty", (short) 654 );
155
156 properties.put( "writeOnlyProperty", "New writeOnlyProperty value" );
157 properties.put( "readOnlyProperty", "New readOnlyProperty value" );
158 }
159
160 private void validateScalarPropertiesOnTarget()
161 {
162 boolean expectedBoolean = (Boolean) properties.get( "booleanProperty" );
163
164 boolean expectedBooleanSecond = defaults.isBooleanSecond();
165 byte expectedByte = (Byte) properties.get( "byteProperty" );
166 double expectedDouble = (Double) properties.get( "doubleProperty" );
167
168 float expectedFloat = defaults.getFloatProperty();
169 int expectedInt = (Integer) properties.get( "intProperty" );
170 long expectedLong = (Long) properties.get( "longProperty" );
171 String expectedNull = (String) properties.get( "nullProperty" );
172 short expectedShort = (Short) properties.get( "shortProperty" );
173 String expectedString = defaults.getStringProperty();
174 String expectedWriteOnly = (String) properties.get( "writeOnlyProperty" );
175 String expectedReadOnly = defaults.getReadOnlyProperty();
176
177 assertEquals( "booleanProperty has the wrong value!", expectedBoolean, target.getBooleanProperty() );
178 assertEquals( "booleanSecond has the wrong value!", expectedBooleanSecond, target.isBooleanSecond() );
179 assertEquals( "byteProperty has the wrong value!", expectedByte, target.getByteProperty() );
180 assertEquals( "doubleProperty has the wrong value!", expectedDouble, target.getDoubleProperty(), 0.005 );
181 assertEquals( "floatProperty has the wrong value!", expectedFloat, target.getFloatProperty(), (float) 0.005 );
182 assertEquals( "intProperty has the wrong value!", expectedInt, target.getIntProperty() );
183 assertEquals( "longProperty has the wrong value!", expectedLong, target.getLongProperty() );
184 assertEquals( "nullProperty has the wrong value!", expectedNull, target.getNullProperty() );
185 assertEquals( "shortProperty has the wrong value!", expectedShort, target.getShortProperty() );
186 assertEquals( "stringProperty has the wrong value!", expectedString, target.getStringProperty() );
187 assertEquals( "writeOnlyProperty has the wrong value!", expectedWriteOnly,
188 target.getWriteOnlyPropertyValue() );
189 assertEquals( "readOnlyProperty has the wrong value!", expectedReadOnly,
190 target.getReadOnlyProperty() );
191 }
192
193
194
195
196 @Test
197 public void populateReadOnly()
198 throws Exception
199 {
200 properties.put( "readOnlyProperty", "Some Value" );
201 on( target ).populate( properties );
202 assertTrue( target.equals( defaults ) );
203 }
204
205
206
207
208 @Test
209 public void populateReadOnlyAndOther()
210 throws Exception
211 {
212 properties.put( "readOnlyProperty", "Some value" );
213 properties.put( "stringProperty", "Some other value" );
214 on( target ).populate( properties );
215
216 defaults.setStringProperty( "Some other value" );
217 assertTrue( target.equals( defaults ) );
218 }
219 }