Code example for PropertyChangeSupport

Methods: getPropertyChangeListeners, removePropertyChangeListener

0
     * Test the method addPropertyChangeListener(PropertyChangeListener, 
     * String) with a normal listener parameter and property name parameter. 
     */ 
    public void testAddPropertyChangeListener_PropertyChangeListener_String_Normal() { 
        Object src = new Object();
        PropertyChangeSupport sup = new PropertyChangeSupport(src);
        PropertyChangeListener l1 = new MockPropertyChangeListener();
        PropertyChangeListener l2 = new MockPropertyChangeListener();
        PropertyChangeListener l3 = new PropertyChangeListenerProxy("myProp",
                l2);
        PropertyChangeListener[] listeners;
 
        sup.addPropertyChangeListener("myProp2", l1);
 
        listeners = sup.getPropertyChangeListeners();
        assertEquals(1, listeners.length);
        assertSame(l1, ((PropertyChangeListenerProxy) listeners[0])
                .getListener());
 
        sup.removePropertyChangeListener(listeners[0]);
        sup.addPropertyChangeListener("myProp3", l3);
        listeners = sup.getPropertyChangeListeners();
        assertEquals(1, listeners.length);
        // pay attention to this recursive proxy 
        assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0])
                .getListener());
        assertNotSame(l3, listeners[0]);
        assertSame(
                l2,
                ((PropertyChangeListenerProxy) ((PropertyChangeListenerProxy) listeners[0])
                        .getListener()).getListener());
 
        listeners = sup.getPropertyChangeListeners("myProp");
        assertEquals(0, listeners.length);
 
        listeners = sup.getPropertyChangeListeners("myProp3");
        assertEquals(1, listeners.length);
        // pay attention to this recursive proxy 
        assertNotSame(l3, ((PropertyChangeListenerProxy) listeners[0])
                .getListener());
        assertNotSame(l3, listeners[0]);