View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.test;
6   
7   import java.beans.PropertyChangeEvent;
8   import java.beans.PropertyChangeListener;
9   import java.util.ArrayList;
10  import java.util.HashMap;
11  import java.util.Iterator;
12  
13  import junit.framework.Assert;
14  
15  /***
16   * This class checks to see that properties it receives
17   * via <code>PropertyChangeListener</code> are expected.
18   * If any unexpected property is received then a JUnit
19   * assertion failure is thrown.
20   * After all properties have been tested then check to make sure
21   * that each property as been received at least once.  If a property
22   * has not been received then a JUnit assertion failure is thrown.
23   *
24   * @author  Barrie Treloar
25   * @revision $Revision: 1.1 $
26   */
27  public class PropertyChangeListenerTester implements PropertyChangeListener {
28  
29      /*** The list of all properties to check. */
30      private ArrayList properties = new ArrayList();
31  
32      /*** Contains a count of how many times a property sent notification. */
33      private HashMap propertyNotificationCount = new HashMap();
34  
35      /***
36       * Create a <code>PropertyChangeListenerTester</code>.
37       */
38      public PropertyChangeListenerTester() {
39      }
40  
41      public final void addProperty(final String aProperty) {
42          properties.add(aProperty);
43      }
44  
45      public final void start() {
46          propertyNotificationCount.clear();
47      }
48  
49      public final void stop() {
50          ArrayList propertiesMissingNotification = new ArrayList();
51          Iterator iter = properties.iterator();
52          while (iter.hasNext()) {
53              String property = (String)iter.next();
54              if (propertyNotificationCount.get(property) == null) {
55                  propertiesMissingNotification.add(property);
56              }
57          }
58          if (!propertiesMissingNotification.isEmpty()) {
59              Assert.fail("Expected property notifications from the following properties: " + propertiesMissingNotification);
60          }
61      }
62  
63      public final void propertyChange(final PropertyChangeEvent evt) {
64          Integer currentCount = (Integer)propertyNotificationCount.get(evt.getPropertyName());
65          if (currentCount == null) {
66              currentCount = new Integer(1);
67          }
68          else {
69              currentCount = new Integer(currentCount.intValue() + 1);
70          }
71          propertyNotificationCount.put(evt.getPropertyName(), currentCount);
72      }
73  
74  }