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