01: /*
02: * TestUtils.java
03: *
04: * Created on October 31, 2006, 9:08 AM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package org.jdesktop.test;
11:
12: import java.beans.PropertyChangeListener;
13: import java.beans.PropertyDescriptor;
14: import java.lang.reflect.Method;
15:
16: import junit.framework.Assert;
17:
18: /**
19: * Extends assert to get all the ease-of-use assert methods
20: * @author rbair
21: */
22: public final class TestUtils extends Assert {
23: private TestUtils() {
24: }
25:
26: public static void assertPropertyChangeNotification(Object bean,
27: String propertyName, Object expected) throws Exception {
28:
29: //add the property change listener
30: Method m = bean.getClass().getMethod(
31: "addPropertyChangeListener",
32: PropertyChangeListener.class);
33: PropertyChangeReport rpt = new PropertyChangeReport();
34: m.invoke(bean, rpt);
35: PropertyDescriptor pd = new PropertyDescriptor(propertyName,
36: bean.getClass());
37:
38: //the original bean value, before being set to 'expected'
39: Object originalValue = pd.getReadMethod().invoke(bean);
40:
41: //set the bean value to 'expected'
42: m = pd.getWriteMethod();
43: m.invoke(bean, expected);
44:
45: //get the new bean value, after being set to 'expected'
46: Object newValue = pd.getReadMethod().invoke(bean);
47:
48: if (newValue == originalValue
49: || (newValue != null && newValue.equals(originalValue))) {
50: //assert that we don't get an event, because newValue was the same
51: //as old value (should only get an event if they differ)
52: assertEquals(0, rpt.getEventCount());
53: } else {
54: //assert bean's property is newValue
55: assertEquals(expected, newValue);
56: //assert that there is exactly one event
57: assertEquals(1, rpt.getEventCount());
58: //assert that the event's property name is correct
59: assertEquals(propertyName, rpt.getLastEvent()
60: .getPropertyName());
61: //assert that the original value is the old value of the event
62: assertEquals(originalValue, rpt.getLastOldValue());
63: //assert that the expected value is the new value of the event
64: assertEquals(expected, rpt.getLastNewValue());
65: }
66: }
67: }
|