KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanPropertyValueChangeClosureTest


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.beanutils;
18
19 import junit.framework.TestCase;
20
21
22 /**
23  * Test cases for <code>BeanPropertyValueChangeClosure</code>.
24  *
25  * @author Norm Deane
26  */

27 public class BeanPropertyValueChangeClosureTest extends TestCase {
28    
29     private static final Integer JavaDoc expectedIntegerValue = new Integer JavaDoc(123);
30     private static final Float JavaDoc expectedFloatValue = new Float JavaDoc(123.123f);
31     private static final Double JavaDoc expectedDoubleValue = new Double JavaDoc(567879.12344d);
32     private static final Boolean JavaDoc expectedBooleanValue = Boolean.TRUE;
33     private static final Byte JavaDoc expectedByteValue = new Byte JavaDoc("12");
34
35     /**
36      * Constructor for BeanPropertyValueChangeClosureTest.
37      *
38      * @param name Name of this test case.
39      */

40     public BeanPropertyValueChangeClosureTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     /**
45      * Test execute with simple float property and Float value.
46      */

47     public void testExecuteWithSimpleFloatPropertyAndFloatValue() {
48         TestBean testBean = new TestBean();
49         new BeanPropertyValueChangeClosure("floatProperty", expectedFloatValue).execute(testBean);
50         assertTrue(expectedFloatValue.floatValue() == testBean.getFloatProperty());
51     }
52
53     /**
54      * Test execute with simple float property and String value.
55      */

56     public void testExecuteWithSimpleFloatPropertyAndStringValue() {
57         try {
58             new BeanPropertyValueChangeClosure("floatProperty", "123").execute(new TestBean());
59             fail("Should have thrown an IllegalArgumentException");
60         } catch (IllegalArgumentException JavaDoc e) {
61             /* this is what we expect */
62         }
63     }
64
65     /**
66      * Test execute with simple float property and Double value.
67      */

68     public void testExecuteWithSimpleFloatPropertyAndDoubleValue() {
69         try {
70             new BeanPropertyValueChangeClosure("floatProperty", expectedDoubleValue).execute(new TestBean());
71             fail("Should have thrown an IllegalArgumentException");
72         } catch (IllegalArgumentException JavaDoc e) {
73             /* this is what we expect */
74         }
75     }
76
77     /**
78      * Test execute with simple float property and Integer value.
79      */

80     public void testExecuteWithSimpleFloatPropertyAndIntegerValue() {
81         TestBean testBean = new TestBean();
82         new BeanPropertyValueChangeClosure("floatProperty", expectedIntegerValue).execute(testBean);
83         assertTrue(expectedIntegerValue.floatValue() == testBean.getFloatProperty());
84     }
85
86     /**
87      * Test execute with simple double property and Double value.
88      */

89     public void testExecuteWithSimpleDoublePropertyAndDoubleValue() {
90         TestBean testBean = new TestBean();
91         new BeanPropertyValueChangeClosure("doubleProperty", expectedDoubleValue).execute(testBean);
92         assertTrue(expectedDoubleValue.doubleValue() == testBean.getDoubleProperty());
93     }
94
95     /**
96      * Test execute with simple double property and String value.
97      */

98     public void testExecuteWithSimpleDoublePropertyAndStringValue() {
99         try {
100             new BeanPropertyValueChangeClosure("doubleProperty", "123").execute(new TestBean());
101             fail("Should have thrown an IllegalArgumentException");
102         } catch (IllegalArgumentException JavaDoc e) {
103             /* this is what we expect */
104         }
105     }
106
107     /**
108      * Test execute with simple double property and Float value.
109      */

110     public void testExecuteWithSimpleDoublePropertyAndFloatValue() {
111         TestBean testBean = new TestBean();
112         new BeanPropertyValueChangeClosure("doubleProperty", expectedFloatValue).execute(testBean);
113         assertTrue(expectedFloatValue.doubleValue() == testBean.getDoubleProperty());
114     }
115
116     /**
117      * Test execute with simple double property and Integer value.
118      */

119     public void testExecuteWithSimpleDoublePropertyAndIntegerValue() {
120         TestBean testBean = new TestBean();
121         new BeanPropertyValueChangeClosure("doubleProperty", expectedIntegerValue).execute(testBean);
122         assertTrue(expectedIntegerValue.doubleValue() == testBean.getDoubleProperty());
123     }
124
125     /**
126      * Test execute with simple int property and Double value.
127      */

128     public void testExecuteWithSimpleIntPropertyAndDoubleValue() {
129         try {
130             new BeanPropertyValueChangeClosure("intProperty", expectedDoubleValue).execute(new TestBean());
131             fail("Should have thrown an IllegalArgumentException");
132         } catch (IllegalArgumentException JavaDoc e) {
133             /* this is what we expect */
134         }
135     }
136
137     /**
138      * Test execute with simple int property and String value.
139      */

140     public void testExecuteWithSimpleIntPropertyAndStringValue() {
141         try {
142             new BeanPropertyValueChangeClosure("intProperty", "123").execute(new TestBean());
143             fail("Should have thrown an IllegalArgumentException");
144         } catch (IllegalArgumentException JavaDoc e) {
145             /* this is what we expect */
146         }
147     }
148
149     /**
150      * Test execute with simple int property and Float value.
151      */

152     public void testExecuteWithSimpleIntPropertyAndFloatValue() {
153         try {
154             new BeanPropertyValueChangeClosure("intProperty", expectedFloatValue).execute(new TestBean());
155             fail("Should have thrown an IllegalArgumentException");
156         } catch (IllegalArgumentException JavaDoc e) {
157             /* this is what we expect */
158         }
159     }
160
161     /**
162      * Test execute with simple int property and Integer value.
163      */

164     public void testExecuteWithSimpleIntPropertyAndIntegerValue() {
165         TestBean testBean = new TestBean();
166         new BeanPropertyValueChangeClosure("intProperty", expectedIntegerValue).execute(testBean);
167         assertTrue(expectedIntegerValue.intValue() == testBean.getIntProperty());
168     }
169
170     /**
171      * Test execute with simple boolean property and Boolean value.
172      */

173     public void testExecuteWithSimpleBooleanPropertyAndBooleanValue() {
174         TestBean testBean = new TestBean();
175         new BeanPropertyValueChangeClosure("booleanProperty", expectedBooleanValue).execute(testBean);
176         assertTrue(expectedBooleanValue.booleanValue() == testBean.getBooleanProperty());
177     }
178
179     /**
180      * Test execute with simple boolean property and String value.
181      */

182     public void testExecuteWithSimpleBooleanPropertyAndStringValue() {
183         try {
184             new BeanPropertyValueChangeClosure("booleanProperty", "true").execute(new TestBean());
185             fail("Should have thrown an IllegalArgumentException");
186         } catch (IllegalArgumentException JavaDoc e) {
187             /* this is what we expect */
188         }
189     }
190
191     /**
192      * Test execute with simple byte property and Byte value.
193      */

194     public void testExecuteWithSimpleBytePropertyAndByteValue() {
195         TestBean testBean = new TestBean();
196         new BeanPropertyValueChangeClosure("byteProperty", expectedByteValue).execute(testBean);
197         assertTrue(expectedByteValue.byteValue() == testBean.getByteProperty());
198     }
199
200     /**
201      * Test execute with simple boolean property and String value.
202      */

203     public void testExecuteWithSimpleBytePropertyAndStringValue() {
204         try {
205             new BeanPropertyValueChangeClosure("byteProperty", "foo").execute(new TestBean());
206             fail("Should have thrown an IllegalArgumentException");
207         } catch (IllegalArgumentException JavaDoc e) {
208             /* this is what we expect */
209         }
210     }
211
212     /**
213      * Test execute with simple primitive property and null value.
214      */

215     public void testExecuteWithSimplePrimitivePropertyAndNullValue() {
216         try {
217             new BeanPropertyValueChangeClosure("intProperty", null).execute(new TestBean());
218             fail("Should have thrown an IllegalArgumentException");
219         } catch (NullPointerException JavaDoc e) {
220             /* this is what we expect */
221         }
222     }
223
224     /**
225      * Test execute with read only property.
226      */

227     public void testExecuteWithReadOnlyProperty() {
228         try {
229             new BeanPropertyValueChangeClosure("readOnlyProperty", "foo").execute(new TestBean());
230             fail("Should have thrown an IllegalArgumentException");
231         } catch (IllegalArgumentException JavaDoc e) {
232             /* this is what we expect */
233         }
234     }
235
236     /**
237      * Test execute with write only property.
238      */

239     public void testExecuteWithWriteOnlyProperty() {
240         TestBean testBean = new TestBean();
241         new BeanPropertyValueChangeClosure("writeOnlyProperty", "foo").execute(testBean);
242         assertEquals("foo", testBean.getWriteOnlyPropertyValue());
243     }
244
245     /**
246      * Test execute with a nested property.
247      */

248     public void testExecuteWithNestedProperty() {
249         TestBean testBean = new TestBean();
250         new BeanPropertyValueChangeClosure("nested.stringProperty", "bar").execute(testBean);
251         assertEquals("bar", testBean.getNested().getStringProperty());
252     }
253
254     /**
255      * Test execute with a nested property and null in the property path.
256      */

257     public void testExecuteWithNullInPropertyPath() {
258         try {
259             new BeanPropertyValueChangeClosure("anotherNested.stringProperty", "foo").execute(new TestBean());
260             fail("Should have thrown an IllegalArgumentException");
261         } catch (IllegalArgumentException JavaDoc e) {
262             /* this is what we expect */
263         }
264     }
265
266     /**
267      * Test execute with a nested property and null in the property path and ignoreNull = true.
268      */

269     public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
270         TestBean testBean = new TestBean();
271
272         // create a closure that will attempt to set a property on the null bean in the path
273
BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("anotherNested.stringProperty",
274                 "Should ignore exception", true);
275
276         try {
277             closure.execute(testBean);
278         } catch (IllegalArgumentException JavaDoc e) {
279             fail("Should have ignored the exception.");
280         }
281     }
282
283     /**
284      * Test execute with indexed property.
285      */

286     public void testExecuteWithIndexedProperty() {
287         TestBean testBean = new TestBean();
288         new BeanPropertyValueChangeClosure("intIndexed[0]", expectedIntegerValue).execute(testBean);
289         assertTrue(expectedIntegerValue.intValue() == testBean.getIntIndexed(0));
290     }
291
292     /**
293      * Test execute with mapped property.
294      */

295     public void testExecuteWithMappedProperty() {
296         TestBean testBean = new TestBean();
297         new BeanPropertyValueChangeClosure("mappedProperty(fred)", "barney").execute(testBean);
298         assertEquals("barney", testBean.getMappedProperty("fred"));
299     }
300
301     /**
302      * Test execute with a simple String property.
303      */

304     public void testExecuteWithSimpleStringProperty() {
305         TestBean testBean = new TestBean();
306         new BeanPropertyValueChangeClosure("stringProperty", "barney").execute(testBean);
307         assertEquals("barney", testBean.getStringProperty());
308     }
309
310     /**
311      * Test execute with an invalid property name.
312      */

313     public void testExecuteWithInvalidPropertyName() {
314         try {
315             new BeanPropertyValueChangeClosure("bogusProperty", "foo").execute(new TestBean());
316             fail("Should have thrown an IllegalArgumentException");
317         } catch (IllegalArgumentException JavaDoc e) {
318             /* this is what we expect */
319         }
320     }
321 }
322
Popular Tags