View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils;
19  
20  import junit.framework.TestCase;
21  
22  
23  /**
24   * Test cases for <code>BeanPropertyValueChangeClosure</code>.
25   *
26   * @version $Id$
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       * Constructor for BeanPropertyValueChangeClosureTest.
38       *
39       * @param name Name of this test case.
40       */
41      public BeanPropertyValueChangeClosureTestCase(final String name) {
42          super(name);
43      }
44  
45      /**
46       * Test execute with simple float property and Float value.
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       * Test execute with simple float property and String value.
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              /* this is what we expect */
63          }
64      }
65  
66      /**
67       * Test execute with simple float property and Double value.
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              /* this is what we expect */
75          }
76      }
77  
78      /**
79       * Test execute with simple float property and Integer value.
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       * Test execute with simple double property and Double value.
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       * Test execute with simple double property and String value.
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             /* this is what we expect */
105         }
106     }
107 
108     /**
109      * Test execute with simple double property and Float value.
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      * Test execute with simple double property and Integer value.
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      * Test execute with simple int property and Double value.
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             /* this is what we expect */
135         }
136     }
137 
138     /**
139      * Test execute with simple int property and String value.
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             /* this is what we expect */
147         }
148     }
149 
150     /**
151      * Test execute with simple int property and Float value.
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             /* this is what we expect */
159         }
160     }
161 
162     /**
163      * Test execute with simple int property and Integer value.
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      * Test execute with simple boolean property and Boolean value.
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      * Test execute with simple boolean property and String value.
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             /* this is what we expect */
189         }
190     }
191 
192     /**
193      * Test execute with simple byte property and Byte value.
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      * Test execute with simple boolean property and String value.
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             /* this is what we expect */
210         }
211     }
212 
213     /**
214      * Test execute with simple primitive property and null value.
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             /* this is what we expect */
222         }
223     }
224 
225     /**
226      * Test execute with read only property.
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             /* this is what we expect */
234         }
235     }
236 
237     /**
238      * Test execute with write only property.
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      * Test execute with a nested property.
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      * Test execute with a nested property and null in the property path.
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             /* this is what we expect */
264         }
265     }
266 
267     /**
268      * Test execute with a nested property and null in the property path and ignoreNull = true.
269      */
270     public void testExecuteWithNullInPropertyPathAngIgnoreTrue() {
271         final TestBean testBean = new TestBean();
272 
273         // create a closure that will attempt to set a property on the null bean in the path
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      * Test execute with indexed property.
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      * Test execute with mapped property.
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      * Test execute with a simple String property.
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      * Test execute with an invalid property name.
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             /* this is what we expect */
320         }
321     }
322 }