1 7 package com.inversoft.beans.test; 8 9 10 import java.util.Iterator ; 11 12 import junit.framework.TestCase; 13 14 import com.inversoft.beans.BaseBeanProperty; 15 import com.inversoft.beans.BeanException; 16 import com.inversoft.beans.BeanProperty; 17 import com.inversoft.beans.ConversionEvent; 18 import com.inversoft.beans.ConversionListenerAdapter; 19 import com.inversoft.beans.IndexedBeanProperty; 20 import com.inversoft.beans.NestedBeanProperty; 21 import com.inversoft.beans.PropertyEvent; 22 import com.inversoft.util.typeconverter.TypeConversionException; 23 24 25 30 public class ListenerTest extends TestCase { 31 32 33 public ListenerTest(String name) { 34 super(name); 35 } 36 37 40 public void testListenerMethods() { 41 try { 42 BeanProperty prop = new BeanProperty("string1", Bean1.class); 43 ListenerHelper l = new ListenerHelper(); 44 ListenerHelper l2 = new ListenerHelper(); 45 prop.addPropertyListener(l); 46 prop.addPropertyListener(l2); 47 48 Iterator iter = prop.getPropertyListeners(); 49 assertTrue(iter.hasNext()); 50 assertSame(l, iter.next()); 51 assertTrue(iter.hasNext()); 52 assertSame(l2, iter.next()); 53 assertFalse(iter.hasNext()); 54 55 prop.removePropertyListener(l); 56 iter = prop.getPropertyListeners(); 57 assertTrue(iter.hasNext()); 58 assertSame(l2, iter.next()); 59 assertFalse(iter.hasNext()); 60 } catch (BeanException be) { 61 fail(be.toString()); 62 } 63 64 try { 65 BeanProperty prop = new BeanProperty("string1", Bean1.class); 66 ListenerHelper l = new ListenerHelper(); 67 ListenerHelper l2 = new ListenerHelper(); 68 prop.addConversionListener(l); 69 prop.addConversionListener(l2); 70 71 Iterator iter = prop.getConversionListeners(); 72 assertTrue(iter.hasNext()); 73 assertSame(l, iter.next()); 74 assertTrue(iter.hasNext()); 75 assertSame(l2, iter.next()); 76 assertFalse(iter.hasNext()); 77 78 prop.removeConversionListener(l); 79 iter = prop.getConversionListeners(); 80 assertTrue(iter.hasNext()); 81 assertSame(l2, iter.next()); 82 assertFalse(iter.hasNext()); 83 } catch (BeanException be) { 84 fail(be.toString()); 85 } 86 } 87 88 91 public void testBadSubClassEventType() { 92 BaseBeanProperty bp = new BaseBeanProperty() { 93 protected void initialize() throws BeanException { 94 } 95 96 public Object getPropertyValue(Object bean) throws BeanException { 97 try { 98 fireConversionEvent(42, bean, null, null); 99 fail("Should have failed"); 100 } catch (IllegalArgumentException iae) { 101 } 103 return null; 104 } 105 106 public void setPropertyValue(Object bean, Object value, 107 boolean convert) 108 throws BeanException, TypeConversionException { 109 try { 110 firePropertyEvent(42, bean, value, null, null); 111 fail("Should have failed"); 112 } catch (IllegalArgumentException iae) { 113 } 115 } 116 }; 117 ListenerHelper l = new ListenerHelper(); 118 bp.addPropertyListener(l); 119 bp.addConversionListener(l); 120 121 try { 122 bp.setPropertyValue(null, null, true); 123 bp.getPropertyValue(null); 124 } catch (BeanException be) { 125 fail(be.toString()); 126 } 127 } 128 129 132 public void testBeanPropertyGet() { 133 134 try { 135 final Bean1 bean = new Bean1(); 136 BeanProperty prop = new BeanProperty("string1", Bean1.class); 137 138 prop.addPropertyListener( new ListenerHelper() { 139 public void handleGet(PropertyEvent event) { 140 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 142 assertTrue("New value should be foo", event.getNewValue().equals("foo")); 143 assertTrue("Bean should be bean", event.getBean() == bean); 144 assertEquals("string1", event.getFullName()); 145 assertTrue("Property name should be string1", event.getPropertyName().equals("string1")); 146 assertTrue("Property type should be String", event.getPropertyType() == String .class); 147 assertTrue("Index should be -1", event.getIndex() == null); 148 } 149 } ); 150 151 bean.setString1("foo"); 152 prop.getPropertyValue(bean); 153 } catch (BeanException be) { 154 fail(be.toString()); 155 } 156 } 157 158 161 public void testBeanPropertySet() { 162 163 try { 164 final Bean1 bean = new Bean1(); 165 BeanProperty prop = new BeanProperty("string1", Bean1.class); 166 167 prop.addPropertyListener( new ListenerHelper() { 168 public void handleSet(PropertyEvent event) { 169 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 171 assertTrue("New value should be bar", event.getNewValue().equals("bar")); 172 assertTrue("Bean should be bean", event.getBean() == bean); 173 assertTrue("Property name should be string1", event.getPropertyName().equals("string1")); 174 assertTrue("Property type should be String", event.getPropertyType() == String .class); 175 assertTrue("Index should be -1", event.getIndex() == null); 176 } 177 } ); 178 179 bean.setString1("foo"); 180 prop.setPropertyValue(bean, "bar"); 181 } catch (BeanException be) { 182 fail(be.toString()); 183 } 184 } 185 186 189 public void testBeanPropertyConversion() { 190 191 try { 192 final Bean1 bean = new Bean1(); 193 BeanProperty prop = new BeanProperty("integer1", Bean1.class); 194 195 prop.addConversionListener( new ListenerHelper() { 196 public void handleSet(PropertyEvent event) { 197 assertTrue("Old value should be null", event.getOldValue() == null); 199 assertTrue("New value should be new Integer(\"1\")", event.getNewValue().equals(new Integer (1))); 200 assertTrue("Bean should be bean", event.getBean() == bean); 201 assertTrue("Property name should be integer1", event.getPropertyName().equals("integer1")); 202 assertTrue("Property type should be Integer", event.getPropertyType() == Integer .class); 203 assertTrue("Index should be -1", event.getIndex() == null); 204 } 205 206 public void handlePreConversion(ConversionEvent event) { 207 assertTrue("Old value should be new String(\"1\")", event.getOldValue().equals("1")); 209 assertTrue("New value should be new String(\"1\")", event.getNewValue().equals("1")); 210 assertTrue("Bean should be bean", event.getBean() == bean); 211 assertTrue("Property name should be integer1", event.getPropertyName().equals("integer1")); 212 assertTrue("Property type should be Integer", event.getPropertyType() == Integer .class); 213 } 214 215 public void handlePostConversion(ConversionEvent event) { 216 assertTrue("Old value should be new String(\"1\")", event.getOldValue().equals("1")); 218 assertTrue("New value should be new Integer(1)", event.getNewValue().equals(new Integer (1))); 219 assertTrue("Bean should be bean", event.getBean() == bean); 220 assertTrue("Property name should be integer1", event.getPropertyName().equals("integer1")); 221 assertTrue("Property type should be Integer", event.getPropertyType() == Integer .class); 222 } 223 } ); 224 225 prop.setPropertyValue(bean, "1", true); 226 } catch (BeanException be) { 227 fail(be.toString()); 228 } catch (TypeConversionException tce) { 229 fail(tce.toString()); 230 } 231 } 232 233 236 public void testBeanPropertyFailedConversion() { 237 238 try { 239 final Bean1 bean = new Bean1(); 240 BeanProperty prop = new BeanProperty("integer1", Bean1.class); 241 242 prop.addConversionListener( new ConversionListenerAdapter() { 243 public void handlePreConversion(ConversionEvent event) { 244 assertTrue("Old value should be new String(\"foo\")", event.getOldValue().equals("foo")); 246 assertTrue("New value should be new String(\"foo\")", event.getNewValue().equals("foo")); 247 assertTrue("Bean should be bean", event.getBean() == bean); 248 assertTrue("Property name should be integer1", event.getPropertyName().equals("integer1")); 249 assertTrue("Property type should be Integer", event.getPropertyType() == Integer .class); 250 } 251 252 public void handleFailedConversion(ConversionEvent event) { 253 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 255 assertTrue("New value should be foo", event.getNewValue().equals("foo")); 256 assertTrue("Bean should be bean", event.getBean() == bean); 257 assertTrue("Property name should be integer1", event.getPropertyName().equals("integer1")); 258 assertTrue("Property type should be Integer", event.getPropertyType() == Integer .class); 259 } 260 } ); 261 262 prop.setPropertyValue(bean, "foo", true); 263 } catch (TypeConversionException tce) { 264 assertTrue("Should have a root cause of NumberFormatException", 265 tce.getCause() instanceof NumberFormatException ); 266 } catch (BeanException be) { 268 fail(be.toString()); 269 } 270 } 271 272 275 public void testIndexedBeanPropertyGet() { 276 277 try { 278 final Bean1 bean = new Bean1(); 279 IndexedBeanProperty prop = new IndexedBeanProperty("stringIndexed", Bean1.class); 280 281 prop.addPropertyListener( new ListenerHelper() { 282 public void handleGet(PropertyEvent event) { 283 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 285 assertTrue("New value should be foo", event.getNewValue().equals("foo")); 286 assertTrue("Bean should be bean", event.getBean() == bean); 287 assertTrue("Property name should be stringIndexed", event.getPropertyName().equals("stringIndexed")); 288 assertTrue("Property type should be String", event.getPropertyType() == String .class); 289 assertEquals("Index should be 0", new Integer (0), event.getIndex()); 290 } 291 } ); 292 293 bean.setStringIndexed(0, "foo"); 294 prop.getPropertyValue(bean, 0); 295 } catch (BeanException be) { 296 fail(be.toString()); 297 } 298 } 299 300 303 public void testIndexedBeanPropertySet() { 304 305 try { 306 final Bean1 bean = new Bean1(); 307 IndexedBeanProperty prop = new IndexedBeanProperty("stringIndexed", Bean1.class); 308 309 prop.addPropertyListener( new ListenerHelper() { 310 public void handleSet(PropertyEvent event) { 311 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 313 assertTrue("New value should be bar", event.getNewValue().equals("bar")); 314 assertTrue("Bean should be bean", event.getBean() == bean); 315 assertTrue("Property name should be stringIndexed", event.getPropertyName().equals("stringIndexed")); 316 assertTrue("Property type should be String", event.getPropertyType() == String .class); 317 assertEquals("Index should be 0", new Integer (0), event.getIndex()); 318 } 319 } ); 320 321 bean.setStringIndexed(0, "foo"); 322 prop.setPropertyValue(bean, 0, "bar"); 323 } catch (BeanException be) { 324 fail(be.toString()); 325 } 326 } 327 328 331 public void testNestedBeanPropertyGet() { 332 333 try { 334 final Bean1 bean1 = new Bean1(); 335 final Bean2 bean2 = new Bean2(); 336 final Bean3 bean3 = new Bean3(); 337 bean1.setProperty1(bean2); 338 bean2.setProperty2(bean3); 339 bean3.setProperty3("foo"); 340 NestedBeanProperty prop = new NestedBeanProperty("property1.property2.property3", Bean1.class); 341 342 prop.addPropertyListener( new ListenerHelper() { 343 public void handleGet(PropertyEvent event) { 344 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 346 assertTrue("New value should be foo", event.getNewValue().equals("foo")); 347 assertTrue("Property name should be property1.property2.property3", event.getPropertyName().equals("property1.property2.property3")); 349 assertTrue("Index should be -1", event.getIndex() == null); 350 } 351 } ); 352 353 prop.getPropertyValue(bean1); 354 } catch (BeanException be) { 355 fail(be.toString()); 356 } 357 } 358 359 362 public void testNestedBeanPropertySet() { 363 364 try { 365 final Bean1 bean = new Bean1(); 366 NestedBeanProperty prop = new NestedBeanProperty("property1.property2.property3", Bean1.class); 367 368 prop.addPropertyListener( new ListenerHelper() { 369 public void handleSet(PropertyEvent event) { 370 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 372 assertTrue("New value should be bar", event.getNewValue().equals("bar")); 373 assertTrue("Property name should be property1.property2.property3", event.getPropertyName().equals("property1.property2.property3")); 375 assertTrue("Index should be -1", event.getIndex() == null); 376 assertEquals("property1.property2.property3", event.getFullName()); 377 } 378 } ); 379 380 bean.setProperty1(new Bean2()); 381 bean.getProperty1().setProperty2(new Bean3()); 382 bean.getProperty1().getProperty2().setProperty3("foo"); 383 prop.setPropertyValue(bean, "bar", true); 384 } catch (BeanException be) { 385 fail(be.toString()); 386 } catch (TypeConversionException tce) { 387 fail(tce.toString()); 388 } 389 } 390 391 394 public void testNestedBeanPropertyConversion() { 395 396 try { 397 final Bean1 bean = new Bean1(); 398 NestedBeanProperty prop = new NestedBeanProperty("property1.property2.integer3", Bean1.class); 399 400 prop.addConversionListener( new ConversionListenerAdapter() { 401 public void handleSet(PropertyEvent event) { 402 assertTrue("Old value should be null", event.getOldValue() == null); 404 assertTrue("New value should be new Integer(1)", event.getNewValue().equals(new Integer (1))); 405 assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3")); 407 assertTrue("Index should be -1", event.getIndex() == null); 408 } 409 410 public void handlePreConversion(ConversionEvent event) { 411 assertTrue("Old value should be new String(\"1\")", event.getOldValue().equals("1")); 413 assertTrue("New value should be new String(\"1\")", event.getNewValue().equals("1")); 414 assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3")); 416 } 417 418 public void handlePostConversion(ConversionEvent event) { 419 assertTrue("Old value should be new String(\"1\")", event.getOldValue().equals("1")); 421 assertTrue("New value should be new Integer(1)", event.getNewValue().equals(new Integer (1))); 422 assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3")); 424 } 425 } ); 426 427 prop.setPropertyValue(bean, "1", true); 428 } catch (BeanException be) { 429 fail(be.toString()); 430 } catch (TypeConversionException tce) { 431 fail(tce.toString()); 432 } 433 } 434 435 438 public void testNestedBeanPropertyFailedConvert() { 439 440 try { 441 final Bean1 bean = new Bean1(); 442 NestedBeanProperty prop = new NestedBeanProperty("property1.property2.integer3", Bean1.class); 443 444 prop.addConversionListener( new ConversionListenerAdapter() { 445 public void handlePreConversion(ConversionEvent event) { 446 assertTrue("Old value should be new String(\"foo\")", event.getOldValue().equals("foo")); 448 assertTrue("New value should be new String(\"foo\")", event.getNewValue().equals("foo")); 449 assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3")); 451 } 452 453 public void handleFailedConversion(ConversionEvent event) { 454 assertTrue("Old value should be foo", event.getOldValue().equals("foo")); 456 assertTrue("New value should be foo", event.getNewValue().equals("foo")); 457 assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3")); 459 } 460 } ); 461 462 prop.setPropertyValue(bean, "foo", true); 463 } catch (TypeConversionException tce) { 464 assertTrue("Should have a root cause of NumberFormatException", 465 tce.getCause() instanceof NumberFormatException ); 466 } catch (BeanException be) { 468 fail(be.toString()); 469 } 470 } 471 } | Popular Tags |