KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > editor > util > ListenerListTest


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.netbeans.lib.editor.util;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.EventListener JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Random JavaDoc;
31 import org.netbeans.junit.NbTest;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.junit.NbTestSuite;
34 import org.openide.util.io.NbMarshalledObject;
35
36
37 /**
38  * Test of ListenerList correctness.
39  *
40  * @author mmetelka
41  */

42 public class ListenerListTest extends NbTestCase {
43     
44     private static final boolean debug = false;
45     
46     public ListenerListTest(java.lang.String JavaDoc testName) {
47         super(testName);
48     }
49     
50     public void testAddAndRemoveListeners() {
51         ListenerListCouple<MyL> couple = new ListenerListCouple<MyL>();
52         MyL l1 = new MyL();
53         MyL l11 = new MyL();
54         MyL l2 = new MyL();
55         MyL l21 = new MyL();
56         MyL l3 = new MyL();
57         couple.add(l1);
58         couple.add(l2);
59         couple.add(l3);
60         couple.add(l21);
61         couple.add(l11);
62         couple.remove(l1);
63         couple.remove(l2);
64         couple.remove(l2);
65         couple.remove(l21);
66         couple.remove(l3);
67         couple.add(l3);
68     }
69     
70     public void testSerialization() throws Exception JavaDoc {
71         ListenerList<MyL> ll = new ListenerList<MyL>();
72         ll.add(new MyL());
73         ll.add(new MyL());
74         ll.add(new MyL());
75         
76         NbMarshalledObject mo = new NbMarshalledObject(ll);
77         @SuppressWarnings JavaDoc("unchecked")
78         ListenerList<MyL> sll = (ListenerList<MyL>)mo.get();
79         List JavaDoc<MyL> lla = ll.getListeners();
80         List JavaDoc<MyL> slla = sll.getListeners();
81         assertEquals(lla.size(), slla.size());
82         for (int i = lla.size() - 1; i >= 0; i--) {
83             assertEquals(lla.get(i), slla.get(i));
84         }
85     }
86     
87     private static final class MyL implements EventListener JavaDoc, Serializable JavaDoc {
88         
89         static final long serialVersionUID = 12345L;
90         
91         static int cntr; // static counter
92

93         private int id = cntr++; // should be restored during deserialization
94

95         private int notified;
96         
97         public void notifyChange() {
98             notified++;
99         }
100         
101         public int getNotified() {
102             return notified;
103         }
104         
105         public int hashCode() {
106             return id;
107         }
108         
109         public boolean equals(Object JavaDoc o) {
110             if (this == o)
111                 return true;
112             if (o instanceof MyL)
113                 return (id == ((MyL) o).id);
114             return false;
115         }
116         
117     }
118
119     private static final class ListenerListCouple<T extends EventListener JavaDoc> {
120         
121         ListenerList<T> listenerList;
122         
123         List JavaDoc<T> imitation;
124         
125         public ListenerListCouple() {
126             listenerList = new ListenerList<T>();
127             imitation = new ArrayList JavaDoc<T>();
128         }
129         
130         public void add(T listener) {
131             listenerList.add(listener);
132             imitation.add(listener);
133             checkListsEqual();
134         }
135         
136         public void remove(T listener) {
137             listenerList.remove(listener);
138             imitation.remove(listener);
139             checkListsEqual();
140         }
141         
142         public void checkListsEqual() {
143             // Check the same listeners are stored in imitation
144
assertEquals(imitation.size(), listenerList.getListenerCount());
145             int i = 0;
146             List JavaDoc<T> listeners = listenerList.getListeners();
147             for (EventListener JavaDoc l : imitation) {
148                 assertSame(l, listeners.get(i++));
149             }
150         }
151         
152     }
153     
154 }
155
Popular Tags