1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20 import junit.framework.TestCase;
21
22
23
24
25
26
27
28 public class BeanPropertyValueChangeClosureTestCase extends TestCase {
29
30 private static final Integer expectedIntegerValue = new Integer(123);
31 private static final Float expectedFloatValue = new Float(123.123f);
32 private static final Double expectedDoubleValue = new Double(567879.12344d);
33 private static final Boolean expectedBooleanValue = Boolean.TRUE;
34 private static final Byte expectedByteValue = new Byte("12");
35
36
37
38
39
40
41 public BeanPropertyValueChangeClosureTestCase(final String name) {
42 super(name);
43 }
44
45
46
47
48 public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
49 final TestBean testBean = new TestBean();
50 new BeanPropertyValueChangeClosure("floatProperty", expectedFloatValue).execute(testBean);
51 assertTrue(expectedFloatValue.floatValue() == testBean.getFloatProperty());
52 }
53
54
55
56
57 public void testExecuteWithSimpleFloatPropertyAndStringValue() {
58 try {
59 new BeanPropertyValueChangeClosure("floatProperty", "123").execute(new TestBean());
60 fail("Should have thrown an IllegalArgumentException");
61 } catch (final IllegalArgumentException e) {
62
63 }
64 }
65
66
67
68
69 public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
70 try {
71 new BeanPropertyValueChangeClosure("floatProperty", expectedDoubleValue).execute(new TestBean());
72 fail("Should have thrown an IllegalArgumentException");
73 } catch (final IllegalArgumentException e) {
74
75 }
76 }
77
78
79
80
81 public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
82 final TestBean testBean = new TestBean();
83 new BeanPropertyValueChangeClosure("floatProperty", expectedIntegerValue).execute(testBean);
84 assertTrue(expectedIntegerValue.floatValue() == testBean.getFloatProperty());
85 }
86
87
88
89
90 public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
91 final TestBean testBean = new TestBean();
92 new BeanPropertyValueChangeClosure("doubleProperty", expectedDoubleValue).execute(testBean);
93 assertTrue(expectedDoubleValue.doubleValue() == testBean.getDoubleProperty());
94 }
95
96
97
98
99 public void testExecuteWithSimpleDoublePropertyAndStringValue() {
100 try {
101 new BeanPropertyValueChangeClosure("doubleProperty", "123").execute(new TestBean());
102 fail("Should have thrown an IllegalArgumentException");
103 } catch (final IllegalArgumentException e) {
104
105 }
106 }
107
108
109
110
111 public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
112 final TestBean testBean = new TestBean();
113 new BeanPropertyValueChangeClosure("doubleProperty", expectedFloatValue).execute(testBean);
114 assertTrue(expectedFloatValue.doubleValue() == testBean.getDoubleProperty());
115 }
116
117
118
119
120 public void testExecuteWithSimpleDoublePropertyAndIntegerValue() {
121 final TestBean testBean = new TestBean();
122 new BeanPropertyValueChangeClosure("doubleProperty", expectedIntegerValue).execute(testBean);
123 assertTrue(expectedIntegerValue.doubleValue() == testBean.getDoubleProperty());
124 }
125
126
127
128
129 public void testExecuteWithSimpleIntPropertyAndDoubleValue() {
130 try {
131 new BeanPropertyValueChangeClosure("intProperty", expectedDoubleValue).execute(new TestBean());
132 fail("Should have thrown an IllegalArgumentException");
133 } catch (final IllegalArgumentException e) {
134
135 }
136 }
137
138
139
140
141 public void testExecuteWithSimpleIntPropertyAndStringValue() {
142 try {
143 new BeanPropertyValueChangeClosure("intProperty", "123").execute(new TestBean());
144 fail("Should have thrown an IllegalArgumentException");
145 } catch (final IllegalArgumentException e) {
146
147 }
148 }
149
150
151
152
153 public void testExecuteWithSimpleIntPropertyAndFloatValue() {
154 try {
155 new BeanPropertyValueChangeClosure("intProperty", expectedFloatValue).execute(new TestBean());
156 fail("Should have thrown an IllegalArgumentException");
157 } catch (final IllegalArgumentException e) {
158
159 }
160 }
161
162
163
164
165 public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
166 final TestBean testBean = new TestBean();
167 new BeanPropertyValueChangeClosure("intProperty", expectedIntegerValue).execute(testBean);
168 assertTrue(expectedIntegerValue.intValue() == testBean.getIntProperty());
169 }
170
171
172
173
174 public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() {
175 final TestBean testBean = new TestBean();
176 new BeanPropertyValueChangeClosure("booleanProperty", expectedBooleanValue).execute(testBean);
177 assertTrue(expectedBooleanValue.booleanValue() == testBean.getBooleanProperty());
178 }
179
180
181
182
183 public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
184 try {
185 new BeanPropertyValueChangeClosure("booleanProperty", "true").execute(new TestBean());
186 fail("Should have thrown an IllegalArgumentException");
187 } catch (final IllegalArgumentException e) {
188
189 }
190 }
191
192
193
194
195 public void testExecuteWithSimpleBytePropertyAndByteValue() {
196 final TestBean testBean = new TestBean();
197 new BeanPropertyValueChangeClosure("byteProperty", expectedByteValue).execute(testBean);
198 assertTrue(expectedByteValue.byteValue() == testBean.getByteProperty());
199 }
200
201
202
203
204 public void testExecuteWithSimpleBytePropertyAndStringValue() {
205 try {
206 new BeanPropertyValueChangeClosure("byteProperty", "foo").execute(new TestBean());
207 fail("Should have thrown an IllegalArgumentException");
208 } catch (final IllegalArgumentException e) {
209
210 }
211 }
212
213
214
215
216 public void testExecuteWithSimplePrimitivePropertyAndNullValue() {
217 try {
218 new BeanPropertyValueChangeClosure("intProperty", null).execute(new TestBean());
219 fail("Should have thrown an IllegalArgumentException");
220 } catch (final IllegalArgumentException e) {
221
222 }
223 }
224
225
226
227
228 public void testExecuteWithReadOnlyProperty() {
229 try {
230 new BeanPropertyValueChangeClosure("readOnlyProperty", "foo").execute(new TestBean());
231 fail("Should have thrown an IllegalArgumentException");
232 } catch (final IllegalArgumentException e) {
233
234 }
235 }
236
237
238
239
240 public void testExecuteWithWriteOnlyProperty() {
241 final TestBean testBean = new TestBean();
242 new BeanPropertyValueChangeClosure("writeOnlyProperty", "foo").execute(testBean);
243 assertEquals("foo", testBean.getWriteOnlyPropertyValue());
244 }
245
246
247
248
249 public void testExecuteWithNestedProperty() {
250 final TestBean testBean = new TestBean();
251 new BeanPropertyValueChangeClosure("nested.stringProperty", "bar").execute(testBean);
252 assertEquals("bar", testBean.getNested().getStringProperty());
253 }
254
255
256
257
258 public void testExecuteWithNullInPropertyPath() {
259 try {
260 new BeanPropertyValueChangeClosure("anotherNested.stringProperty", "foo").execute(new TestBean());
261 fail("Should have thrown an IllegalArgumentException");
262 } catch (final IllegalArgumentException e) {
263
264 }
265 }
266
267
268
269
270 public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
271 final TestBean testBean = new TestBean();
272
273
274 final BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("anotherNested.stringProperty",
275 "Should ignore exception", true);
276
277 try {
278 closure.execute(testBean);
279 } catch (final IllegalArgumentException e) {
280 fail("Should have ignored the exception.");
281 }
282 }
283
284
285
286
287 public void testExecuteWithIndexedProperty() {
288 final TestBean testBean = new TestBean();
289 new BeanPropertyValueChangeClosure("intIndexed[0]", expectedIntegerValue).execute(testBean);
290 assertTrue(expectedIntegerValue.intValue() == testBean.getIntIndexed(0));
291 }
292
293
294
295
296 public void testExecuteWithMappedProperty() {
297 final TestBean testBean = new TestBean();
298 new BeanPropertyValueChangeClosure("mappedProperty(fred)", "barney").execute(testBean);
299 assertEquals("barney", testBean.getMappedProperty("fred"));
300 }
301
302
303
304
305 public void testExecuteWithSimpleStringProperty() {
306 final TestBean testBean = new TestBean();
307 new BeanPropertyValueChangeClosure("stringProperty", "barney").execute(testBean);
308 assertEquals("barney", testBean.getStringProperty());
309 }
310
311
312
313
314 public void testExecuteWithInvalidPropertyName() {
315 try {
316 new BeanPropertyValueChangeClosure("bogusProperty", "foo").execute(new TestBean());
317 fail("Should have thrown an IllegalArgumentException");
318 } catch (final IllegalArgumentException e) {
319
320 }
321 }
322 }