KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > WeakListenerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Vector JavaDoc;
26 import javax.naming.event.NamingEvent JavaDoc;
27 import javax.naming.event.NamingExceptionEvent JavaDoc;
28 import javax.naming.event.ObjectChangeListener JavaDoc;
29 import org.netbeans.junit.NbTestCase;
30
31 public class WeakListenerTest extends NbTestCase {
32
33     public WeakListenerTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     public void testPrivateRemoveMethod() throws Exception JavaDoc {
38         PropChBean bean = new PropChBean();
39         Listener JavaDoc listener = new Listener JavaDoc();
40         PropertyChangeListener JavaDoc weakL = new PrivatePropL(listener, bean);
41         WeakReference JavaDoc ref = new WeakReference JavaDoc(listener);
42         
43         bean.addPCL(weakL);
44         
45         listener = null;
46         assertGC("Listener wasn't GCed", ref);
47         
48         ref = new WeakReference JavaDoc(weakL);
49         weakL = null;
50         assertGC("WeakListener wasn't GCed", ref);
51     }
52     
53     private static final class Listener
54             implements PropertyChangeListener JavaDoc, ObjectChangeListener JavaDoc {
55         public int cnt;
56         
57         public void propertyChange(PropertyChangeEvent JavaDoc ev) {
58             cnt++;
59         }
60         
61         public void namingExceptionThrown(NamingExceptionEvent JavaDoc evt) {
62             cnt++;
63         }
64         
65         public void objectChanged(NamingEvent JavaDoc evt) {
66             cnt++;
67         }
68     } // end of Listener
69

70     private static class PropChBean {
71         private Vector JavaDoc listeners = new Vector JavaDoc();
72         private void addPCL(PropertyChangeListener JavaDoc l) { listeners.add(l); }
73         private void removePCL(PropertyChangeListener JavaDoc l) { listeners.remove(l); }
74     } // End of PropChBean class
75

76     private static class PrivatePropL extends WeakListener implements PropertyChangeListener JavaDoc {
77         
78         public PrivatePropL(PropertyChangeListener JavaDoc orig, Object JavaDoc source) {
79             super(PropertyChangeListener JavaDoc.class, orig);
80             setSource(source);
81         }
82         
83         protected String JavaDoc removeMethodName() {
84             return "removePCL"; // NOI18N
85
}
86         
87         // ---- PropertyChangeListener implementation
88

89         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
90             PropertyChangeListener JavaDoc l = (PropertyChangeListener JavaDoc) super.get(evt);
91             if (l != null) l.propertyChange(evt);
92         }
93     } // End of PrivatePropL class
94
}
95
Popular Tags