samples/gc/test/org/apidesign/gc/WeakListenersTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 18:51:38 +0200
changeset 263 7e8e995065c5
parent 113 f2b6057a3376
child 273 cfc5a3fd5ae8
permissions -rw-r--r--
Tests of all modules are executed and can fail the build
jtulach@113
     1
package org.apidesign.gc;
jtulach@113
     2
jtulach@113
     3
import java.awt.Color;
jtulach@113
     4
import java.beans.PropertyChangeEvent;
jtulach@113
     5
import java.beans.PropertyChangeListener;
jtulach@113
     6
import java.lang.ref.WeakReference;
jtulach@113
     7
import javax.swing.JPanel;
jtulach@263
     8
import junit.framework.AssertionFailedError;
jtulach@113
     9
import org.junit.After;
jtulach@113
    10
import org.junit.AfterClass;
jtulach@113
    11
import org.junit.Before;
jtulach@113
    12
import org.junit.BeforeClass;
jtulach@113
    13
import org.junit.Test;
jtulach@113
    14
import org.netbeans.junit.NbTestCase;
jtulach@113
    15
import static org.junit.Assert.*;
jtulach@113
    16
jtulach@113
    17
public class WeakListenersTest {
jtulach@113
    18
jtulach@113
    19
    private static JPanel longLivingBean;
jtulach@113
    20
    
jtulach@113
    21
    public WeakListenersTest() {
jtulach@113
    22
    }
jtulach@113
    23
jtulach@113
    24
    @BeforeClass
jtulach@113
    25
    public static void setUpClass() throws Exception {
jtulach@113
    26
        longLivingBean = new JPanel();
jtulach@113
    27
    }
jtulach@113
    28
    
jtulach@113
    29
    @Before
jtulach@113
    30
    public void setUp() {
jtulach@113
    31
        longLivingBean.setBackground(Color.BLACK);
jtulach@113
    32
    }
jtulach@113
    33
jtulach@113
    34
    @Test
jtulach@113
    35
    public void listenWithoutWeakReference() {
jtulach@113
    36
        PropL listener = new PropL();
jtulach@113
    37
        longLivingBean.addPropertyChangeListener(listener);
jtulach@113
    38
        longLivingBean.setBackground(Color.WHITE);
jtulach@113
    39
        assertEquals("background property change notified", "background", listener.propName);
jtulach@113
    40
        WeakReference<Object> ref = new WeakReference<Object>(listener);
jtulach@113
    41
        listener = null;
jtulach@113
    42
        try {
jtulach@113
    43
            NbTestCase.assertGC(
jtulach@113
    44
                "Of course, this listener cannot disappear, because it is held from long living JavaBean", 
jtulach@113
    45
                ref
jtulach@113
    46
            );
jtulach@263
    47
        } catch (AssertionFailedError ex) {
jtulach@113
    48
            ex.printStackTrace();
jtulach@263
    49
            // of course it cannot be garbage collected. That is OK.
jtulach@113
    50
            return;
jtulach@113
    51
        }
jtulach@113
    52
        fail("The listener cannot be GCed as it is held from long living JavaBean");
jtulach@113
    53
    }
jtulach@113
    54
jtulach@113
    55
    @Test
jtulach@113
    56
    public void listenViaWeakListener() {
jtulach@113
    57
        PropL listener = new PropL();
jtulach@113
    58
        
jtulach@113
    59
        PropertyChangeListener weakL = WeakListeners.create(PropertyChangeListener.class, listener, longLivingBean);
jtulach@113
    60
        longLivingBean.addPropertyChangeListener(weakL);
jtulach@113
    61
        longLivingBean.setBackground(Color.WHITE);
jtulach@113
    62
        assertEquals("background property change notified", "background", listener.propName);
jtulach@113
    63
        // BEGIN: gc.assertGC
jtulach@113
    64
        WeakReference<Object> ref = new WeakReference<Object>(listener);
jtulach@113
    65
        listener = null;
jtulach@113
    66
        NbTestCase.assertGC("This listener can disappear", ref);
jtulach@113
    67
        // END: gc.assertGC
jtulach@113
    68
    }
jtulach@113
    69
jtulach@113
    70
    
jtulach@113
    71
    private static class PropL implements PropertyChangeListener {
jtulach@113
    72
        String propName;
jtulach@113
    73
        public void propertyChange(PropertyChangeEvent evt) {
jtulach@113
    74
            propName = evt.getPropertyName();
jtulach@113
    75
        }
jtulach@113
    76
    }
jtulach@113
    77
}