Code example for PropertyChangeSupport

Methods: addPropertyChangeListener, getPropertyChangeListeners, removePropertyChangeListener

0
     * Test the method removePropertyChangeListener(PropertyChangeListener, 
     * String) when a listener for the named property has been registered. 
     */ 
    public void testRemovePropertyChangeListener_PropertyChangeListener_String_PropertyRegistered() { 
        Object src = new Object();
        PropertyChangeSupport sup = new PropertyChangeSupport(src);
        PropertyChangeListener l1 = new MockPropertyChangeListener();
        sup.addPropertyChangeListener("myProp", l1);
        assertEquals(1, sup.getPropertyChangeListeners().length);
 
        sup.removePropertyChangeListener("myProp", l1);
        assertEquals(0, sup.getPropertyChangeListeners().length);
        assertEquals(0, sup.getPropertyChangeListeners("myProp").length);
 
        PropertyChangeListener l2 = new MockPropertyChangeListener();
        PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp",
                l2);
        sup.addPropertyChangeListener(l3);
        assertEquals(1, sup.getPropertyChangeListeners().length);
        sup.removePropertyChangeListener("myProp", l2);
        assertEquals(0, sup.getPropertyChangeListeners().length);
        assertEquals(0, sup.getPropertyChangeListeners("myProp").length);
    } 
 
    /* 
     * Test the method removePropertyChangeListener(PropertyChangeListener, 
     * String) with a non-registered listener parameter.