samples/gc/test/org/apidesign/gc/WeakListenersTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
parent 263 7e8e995065c5
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
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@273
    51
        } catch (OutOfMemoryError ex) {
jtulach@273
    52
            ex.printStackTrace();
jtulach@273
    53
            // sometimes the test fails on OOME, let's catch it
jtulach@273
    54
            // of course it cannot be garbage collected. That is OK.
jtulach@273
    55
            return;
jtulach@113
    56
        }
jtulach@113
    57
        fail("The listener cannot be GCed as it is held from long living JavaBean");
jtulach@113
    58
    }
jtulach@113
    59
jtulach@113
    60
    @Test
jtulach@113
    61
    public void listenViaWeakListener() {
jtulach@113
    62
        PropL listener = new PropL();
jtulach@113
    63
        
jtulach@113
    64
        PropertyChangeListener weakL = WeakListeners.create(PropertyChangeListener.class, listener, longLivingBean);
jtulach@113
    65
        longLivingBean.addPropertyChangeListener(weakL);
jtulach@113
    66
        longLivingBean.setBackground(Color.WHITE);
jtulach@113
    67
        assertEquals("background property change notified", "background", listener.propName);
jtulach@113
    68
        // BEGIN: gc.assertGC
jtulach@113
    69
        WeakReference<Object> ref = new WeakReference<Object>(listener);
jtulach@113
    70
        listener = null;
jtulach@113
    71
        NbTestCase.assertGC("This listener can disappear", ref);
jtulach@113
    72
        // END: gc.assertGC
jtulach@113
    73
    }
jtulach@113
    74
jtulach@113
    75
    
jtulach@113
    76
    private static class PropL implements PropertyChangeListener {
jtulach@113
    77
        String propName;
jtulach@113
    78
        public void propertyChange(PropertyChangeEvent evt) {
jtulach@113
    79
            propName = evt.getPropertyName();
jtulach@113
    80
        }
jtulach@113
    81
    }
jtulach@113
    82
}