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