1 16 17 package org.apache.commons.beanutils; 18 19 import junit.framework.TestCase; 20 21 22 27 public class BeanPropertyValueChangeClosureTest extends TestCase { 28 29 private static final Integer expectedIntegerValue = new Integer (123); 30 private static final Float expectedFloatValue = new Float (123.123f); 31 private static final Double expectedDoubleValue = new Double (567879.12344d); 32 private static final Boolean expectedBooleanValue = Boolean.TRUE; 33 private static final Byte expectedByteValue = new Byte ("12"); 34 35 40 public BeanPropertyValueChangeClosureTest(String name) { 41 super(name); 42 } 43 44 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 56 public void testExecuteWithSimpleFloatPropertyAndStringValue() { 57 try { 58 new BeanPropertyValueChangeClosure("floatProperty", "123").execute(new TestBean()); 59 fail("Should have thrown an IllegalArgumentException"); 60 } catch (IllegalArgumentException e) { 61 62 } 63 } 64 65 68 public void testExecuteWithSimpleFloatPropertyAndDoubleValue() { 69 try { 70 new BeanPropertyValueChangeClosure("floatProperty", expectedDoubleValue).execute(new TestBean()); 71 fail("Should have thrown an IllegalArgumentException"); 72 } catch (IllegalArgumentException e) { 73 74 } 75 } 76 77 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 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 98 public void testExecuteWithSimpleDoublePropertyAndStringValue() { 99 try { 100 new BeanPropertyValueChangeClosure("doubleProperty", "123").execute(new TestBean()); 101 fail("Should have thrown an IllegalArgumentException"); 102 } catch (IllegalArgumentException e) { 103 104 } 105 } 106 107 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 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 128 public void testExecuteWithSimpleIntPropertyAndDoubleValue() { 129 try { 130 new BeanPropertyValueChangeClosure("intProperty", expectedDoubleValue).execute(new TestBean()); 131 fail("Should have thrown an IllegalArgumentException"); 132 } catch (IllegalArgumentException e) { 133 134 } 135 } 136 137 140 public void testExecuteWithSimpleIntPropertyAndStringValue() { 141 try { 142 new BeanPropertyValueChangeClosure("intProperty", "123").execute(new TestBean()); 143 fail("Should have thrown an IllegalArgumentException"); 144 } catch (IllegalArgumentException e) { 145 146 } 147 } 148 149 152 public void testExecuteWithSimpleIntPropertyAndFloatValue() { 153 try { 154 new BeanPropertyValueChangeClosure("intProperty", expectedFloatValue).execute(new TestBean()); 155 fail("Should have thrown an IllegalArgumentException"); 156 } catch (IllegalArgumentException e) { 157 158 } 159 } 160 161 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 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 182 public void testExecuteWithSimpleBooleanPropertyAndStringValue() { 183 try { 184 new BeanPropertyValueChangeClosure("booleanProperty", "true").execute(new TestBean()); 185 fail("Should have thrown an IllegalArgumentException"); 186 } catch (IllegalArgumentException e) { 187 188 } 189 } 190 191 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 203 public void testExecuteWithSimpleBytePropertyAndStringValue() { 204 try { 205 new BeanPropertyValueChangeClosure("byteProperty", "foo").execute(new TestBean()); 206 fail("Should have thrown an IllegalArgumentException"); 207 } catch (IllegalArgumentException e) { 208 209 } 210 } 211 212 215 public void testExecuteWithSimplePrimitivePropertyAndNullValue() { 216 try { 217 new BeanPropertyValueChangeClosure("intProperty", null).execute(new TestBean()); 218 fail("Should have thrown an IllegalArgumentException"); 219 } catch (NullPointerException e) { 220 221 } 222 } 223 224 227 public void testExecuteWithReadOnlyProperty() { 228 try { 229 new BeanPropertyValueChangeClosure("readOnlyProperty", "foo").execute(new TestBean()); 230 fail("Should have thrown an IllegalArgumentException"); 231 } catch (IllegalArgumentException e) { 232 233 } 234 } 235 236 239 public void testExecuteWithWriteOnlyProperty() { 240 TestBean testBean = new TestBean(); 241 new BeanPropertyValueChangeClosure("writeOnlyProperty", "foo").execute(testBean); 242 assertEquals("foo", testBean.getWriteOnlyPropertyValue()); 243 } 244 245 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 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 e) { 262 263 } 264 } 265 266 269 public void testExecuteWithNullInPropertyPathAngIgnoreTrue() { 270 TestBean testBean = new TestBean(); 271 272 BeanPropertyValueChangeClosure closure = new BeanPropertyValueChangeClosure("anotherNested.stringProperty", 274 "Should ignore exception", true); 275 276 try { 277 closure.execute(testBean); 278 } catch (IllegalArgumentException e) { 279 fail("Should have ignored the exception."); 280 } 281 } 282 283 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 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 304 public void testExecuteWithSimpleStringProperty() { 305 TestBean testBean = new TestBean(); 306 new BeanPropertyValueChangeClosure("stringProperty", "barney").execute(testBean); 307 assertEquals("barney", testBean.getStringProperty()); 308 } 309 310 313 public void testExecuteWithInvalidPropertyName() { 314 try { 315 new BeanPropertyValueChangeClosure("bogusProperty", "foo").execute(new TestBean()); 316 fail("Should have thrown an IllegalArgumentException"); 317 } catch (IllegalArgumentException e) { 318 319 } 320 } 321 } 322 | Popular Tags |