1 19 20 package org.openide.util; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.lang.ref.WeakReference ; 25 import java.util.Vector ; 26 import javax.naming.event.NamingEvent ; 27 import javax.naming.event.NamingExceptionEvent ; 28 import javax.naming.event.ObjectChangeListener ; 29 import org.netbeans.junit.NbTestCase; 30 31 public class WeakListenerTest extends NbTestCase { 32 33 public WeakListenerTest(String testName) { 34 super(testName); 35 } 36 37 public void testPrivateRemoveMethod() throws Exception { 38 PropChBean bean = new PropChBean(); 39 Listener listener = new Listener (); 40 PropertyChangeListener weakL = new PrivatePropL(listener, bean); 41 WeakReference ref = new WeakReference (listener); 42 43 bean.addPCL(weakL); 44 45 listener = null; 46 assertGC("Listener wasn't GCed", ref); 47 48 ref = new WeakReference (weakL); 49 weakL = null; 50 assertGC("WeakListener wasn't GCed", ref); 51 } 52 53 private static final class Listener 54 implements PropertyChangeListener , ObjectChangeListener { 55 public int cnt; 56 57 public void propertyChange(PropertyChangeEvent ev) { 58 cnt++; 59 } 60 61 public void namingExceptionThrown(NamingExceptionEvent evt) { 62 cnt++; 63 } 64 65 public void objectChanged(NamingEvent evt) { 66 cnt++; 67 } 68 } 70 private static class PropChBean { 71 private Vector listeners = new Vector (); 72 private void addPCL(PropertyChangeListener l) { listeners.add(l); } 73 private void removePCL(PropertyChangeListener l) { listeners.remove(l); } 74 } 76 private static class PrivatePropL extends WeakListener implements PropertyChangeListener { 77 78 public PrivatePropL(PropertyChangeListener orig, Object source) { 79 super(PropertyChangeListener .class, orig); 80 setSource(source); 81 } 82 83 protected String removeMethodName() { 84 return "removePCL"; } 86 87 89 public void propertyChange(PropertyChangeEvent evt) { 90 PropertyChangeListener l = (PropertyChangeListener ) super.get(evt); 91 if (l != null) l.propertyChange(evt); 92 } 93 } } 95 | Popular Tags |