KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > beans > test > ListenerTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.beans.test;
8
9
10 import java.util.Iterator JavaDoc;
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 /**
26  * This class is the testing class for the listeners of the events
27  * fired from the bean properties
28  * @author Brian Pontarelli
29  */

30 public class ListenerTest extends TestCase {
31
32     /** Constructs a new test instance */
33     public ListenerTest(String JavaDoc name) {
34         super(name);
35     }
36
37     /**
38      * Tests the bean property listener methods from BaseBeanProperty
39      */

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 JavaDoc 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 JavaDoc 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     /**
89      * Tests that sub-classes can not fire incorrect types of events.
90      */

91     public void testBadSubClassEventType() {
92         BaseBeanProperty bp = new BaseBeanProperty() {
93             protected void initialize() throws BeanException {
94             }
95
96             public Object JavaDoc getPropertyValue(Object JavaDoc bean) throws BeanException {
97                 try {
98                     fireConversionEvent(42, bean, null, null);
99                     fail("Should have failed");
100                 } catch (IllegalArgumentException JavaDoc iae) {
101                     // Expected
102
}
103                 return null;
104             }
105
106             public void setPropertyValue(Object JavaDoc bean, Object JavaDoc 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 JavaDoc iae) {
113                     // Expected
114
}
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     /**
130      * Tests the bean property get
131      */

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                         //System.err.println("Got get event 1");
141
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 JavaDoc.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     /**
159      * Tests the bean property set
160      */

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                         //System.err.println("Got set event 1");
170
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 JavaDoc.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     /**
187      * Tests the bean property conversion listener
188      */

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                         //System.err.println("Got conversion set event 1");
198
assertTrue("Old value should be null", event.getOldValue() == null);
199                         assertTrue("New value should be new Integer(\"1\")", event.getNewValue().equals(new Integer JavaDoc(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 JavaDoc.class);
203                         assertTrue("Index should be -1", event.getIndex() == null);
204                     }
205
206                     public void handlePreConversion(ConversionEvent event) {
207                         //System.err.println("Got pre-conversion event 1");
208
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 JavaDoc.class);
213                     }
214
215                     public void handlePostConversion(ConversionEvent event) {
216                         //System.err.println("Got post-conversion event 1");
217
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 JavaDoc(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 JavaDoc.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     /**
234      * Tests the bean property failed conversion listener
235      */

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                         //System.err.println("Got pre-conversion event 2");
245
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 JavaDoc.class);
250                     }
251
252                     public void handleFailedConversion(ConversionEvent event) {
253                         //System.err.println("Got bad-conversion event 1");
254
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 JavaDoc.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 JavaDoc);
266             //System.err.println(tce.toString());
267
} catch (BeanException be) {
268             fail(be.toString());
269         }
270     }
271
272     /**
273      * Tests the indexed bean property get
274      */

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                         //System.err.println("Got indexed get event 1");
284
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 JavaDoc.class);
289                         assertEquals("Index should be 0", new Integer JavaDoc(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     /**
301      * Tests the indexed bean property set
302      */

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                         //System.err.println("Got indexed set event 1");
312
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 JavaDoc.class);
317                         assertEquals("Index should be 0", new Integer JavaDoc(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     /**
329      * Tests the nested bean property conversion
330      */

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                         //System.err.println("Got nested get event 1");
345
assertTrue("Old value should be foo", event.getOldValue().equals("foo"));
346                         assertTrue("New value should be foo", event.getNewValue().equals("foo"));
347                         //assertTrue("Bean should be bean", event.getBean() == bean);
348
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     /**
360      * Tests the bean property set listener for nested properties
361      */

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                         //System.err.println("Got nested set event 1");
371
assertTrue("Old value should be foo", event.getOldValue().equals("foo"));
372                         assertTrue("New value should be bar", event.getNewValue().equals("bar"));
373                         //assertTrue("Bean should be bean", event.getBean() == bean);
374
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     /**
392      * Tests the nested bean property conversion
393      */

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                         //System.err.println("Got nested conversion set event 1");
403
assertTrue("Old value should be null", event.getOldValue() == null);
404                         assertTrue("New value should be new Integer(1)", event.getNewValue().equals(new Integer JavaDoc(1)));
405                         //assertTrue("Bean should be bean", event.getBean() == bean);
406
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                         //System.err.println("Got nested pre-conversion event 1");
412
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("Bean should be bean", event.getBean() == bean);
415
assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3"));
416                     }
417
418                     public void handlePostConversion(ConversionEvent event) {
419                         //System.err.println("Got nested post-conversion event 1");
420
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 JavaDoc(1)));
422                         //assertTrue("Bean should be bean", event.getBean() == bean);
423
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     /**
436      * Tests the nested bean property failed conversion
437      */

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                         //System.err.println("Got nested pre-conversion event 2");
447
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("Bean should be bean", event.getBean() == bean);
450
assertTrue("Property name should be property1.property2.integer3", event.getPropertyName().equals("property1.property2.integer3"));
451                     }
452
453                     public void handleFailedConversion(ConversionEvent event) {
454                         //System.err.println("Got bad-conversion event 1");
455
assertTrue("Old value should be foo", event.getOldValue().equals("foo"));
456                         assertTrue("New value should be foo", event.getNewValue().equals("foo"));
457                         //assertTrue("Bean should be bean", event.getBean() == bean);
458
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 JavaDoc);
466             //System.err.println(tce.toString());
467
} catch (BeanException be) {
468             fail(be.toString());
469         }
470     }
471 }
Popular Tags