KickJava   Java API By Example, From Geeks To Geeks.

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


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 PriorityListenerList correctness.
39  *
40  * @author mmetelka
41  */

42 public class PriorityListenerListTest extends NbTestCase {
43     
44     private static final boolean debug = false;
45     
46     private static final int SET_RATIO_2 = 50;
47     
48     public PriorityListenerListTest(java.lang.String JavaDoc testName) {
49         super(testName);
50     }
51     
52     public void testAddAndRemoveListenersOnThreeLevels() {
53         int TEST_PRIORITY_1 = 0;
54         int TEST_PRIORITY_2 = 3;
55         int TEST_PRIORITY_3 = 2;
56         
57         PriorityListenerListCouple couple = new PriorityListenerListCouple();
58         L l1 = new L();
59         L l11 = new L();
60         L l2 = new L();
61         L l21 = new L();
62         L l3 = new L();
63         couple.add(l1, TEST_PRIORITY_1);
64         couple.add(l2, TEST_PRIORITY_2);
65         couple.add(l3, TEST_PRIORITY_3);
66         couple.add(l21, TEST_PRIORITY_2);
67         couple.add(l11, TEST_PRIORITY_1);
68         couple.remove(l1, TEST_PRIORITY_1);
69         couple.remove(l2, TEST_PRIORITY_1); // should do nothing
70
couple.remove(l2, TEST_PRIORITY_2);
71         couple.remove(l21, TEST_PRIORITY_2);
72         couple.remove(l3, TEST_PRIORITY_3); // should remove the levels 2 and 3
73
couple.checkLastPriority(1);
74         couple.add(l3, TEST_PRIORITY_3);
75     }
76     
77     public void testNegativePriorities() {
78         try {
79             PriorityListenerList<EventListener JavaDoc> ll = new PriorityListenerList<EventListener JavaDoc>();
80             ll.add(new L(), -1);
81             fail("Should not get here");
82         } catch (IndexOutOfBoundsException JavaDoc e) {
83             // Invalid priority properly catched
84
}
85
86         try {
87             PriorityListenerList<EventListener JavaDoc> ll = new PriorityListenerList<EventListener JavaDoc>();
88             ll.remove(new L(), -1);
89             fail("Should not get here");
90         } catch (IndexOutOfBoundsException JavaDoc e) {
91             // Invalid priority properly catched
92
}
93     }
94     
95     public void testSerialization() throws Exception JavaDoc {
96         PriorityListenerList<EventListener JavaDoc> ll = new PriorityListenerList<EventListener JavaDoc>();
97         ll.add(new L(), 3);
98         ll.add(new L(), 1);
99         ll.add(new L(), 1);
100         
101         NbMarshalledObject mo = new NbMarshalledObject(ll);
102         PriorityListenerList sll = (PriorityListenerList)mo.get();
103         EventListener JavaDoc[][] lla = ll.getListenersArray();
104         EventListener JavaDoc[][] slla = sll.getListenersArray();
105         assertEquals(lla.length, slla.length);
106         for (int priority = lla.length - 1; priority >= 0; priority--) {
107             assertEquals(lla[priority].length, slla[priority].length);
108         }
109     }
110     
111     private static final class L implements EventListener JavaDoc, Serializable JavaDoc {
112         
113         static final long serialVersionUID = 12345L;
114         
115         private int notified;
116         
117         public void notifyChange() {
118             notified++;
119         }
120         
121         public int getNotified() {
122             return notified;
123         }
124         
125     }
126
127     private static final class PriorityListenerListImitation extends HashMap JavaDoc<Integer JavaDoc,List JavaDoc<EventListener JavaDoc>> {
128
129         public synchronized void add(EventListener JavaDoc listener, int priority) {
130             assertTrue(priority >= 0);
131             // Add to begining so that fired as last (comply with PriorityListenerList)
132
getList(priority, true).add(0, listener);
133         }
134         
135         public synchronized void remove(EventListener JavaDoc listener, int priority) {
136             assertTrue(priority >= 0);
137             List JavaDoc<EventListener JavaDoc> l = getList(priority, false);
138             for (int i = l.size() - 1; i >= 0; i--) {
139                 if (l.get(i) == listener) {
140                     l.remove(i);
141                     break;
142                 }
143             }
144         }
145         
146         public synchronized List JavaDoc<EventListener JavaDoc> getList(int priority) {
147             return getList(priority, false);
148         }
149         
150         public synchronized void checkEquals(PriorityListenerList<EventListener JavaDoc> priorityListenerList) {
151             // Check the same listeners are stored in imitation
152
EventListener JavaDoc[][] listenersArray = priorityListenerList.getListenersArray();
153             for (int priority = listenersArray.length - 1; priority >= 0; priority--) {
154                 EventListener JavaDoc[] listeners = listenersArray[priority];
155                 for (int i = listeners.length - 1; i >= 0; i--) {
156                     assertTrue(getList(priority).get(i) == listeners[i]);
157                 }
158             }
159             
160             // Check there are no extra priorities in the imitation
161
for (Map.Entry JavaDoc<Integer JavaDoc,List JavaDoc<EventListener JavaDoc>> entry : entrySet()) {
162                 if (entry.getValue().size() > 0) {
163                     assertTrue (entry.getKey() < listenersArray.length);
164                 }
165             }
166         }
167         
168         private List JavaDoc<EventListener JavaDoc> getList(int priority, boolean forceCreation) {
169             List JavaDoc<EventListener JavaDoc> l = get(priority);
170             if (l == null) {
171                 if (forceCreation) {
172                     l = new ArrayList JavaDoc<EventListener JavaDoc>();
173                     put(priority, l);
174                 } else { // just getting the value
175
l = Collections.emptyList();
176                 }
177             }
178             return l;
179         }
180
181     }
182
183     private static final class PriorityListenerListCouple {
184         
185         PriorityListenerList<EventListener JavaDoc> priorityListenerList;
186         
187         PriorityListenerListImitation imitation;
188         
189         public PriorityListenerListCouple() {
190             priorityListenerList = new PriorityListenerList<EventListener JavaDoc>();
191             imitation = new PriorityListenerListImitation();
192         }
193         
194         public void add(EventListener JavaDoc listener, int priority) {
195             priorityListenerList.add(listener, priority);
196             imitation.add(listener, priority);
197             imitation.checkEquals(priorityListenerList);
198         }
199         
200         public void remove(EventListener JavaDoc listener, int priority) {
201             priorityListenerList.remove(listener, priority);
202             imitation.remove(listener, priority);
203             imitation.checkEquals(priorityListenerList);
204         }
205         
206         public void checkLastPriority(int priority) {
207             assertTrue(priorityListenerList.getListenersArray().length - 1 == priority);
208         }
209         
210     }
211     
212 }
213
Popular Tags