1 19 20 package org.netbeans.lib.editor.util; 21 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.EventListener ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Random ; 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 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 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); couple.remove(l2, TEST_PRIORITY_2); 71 couple.remove(l21, TEST_PRIORITY_2); 72 couple.remove(l3, TEST_PRIORITY_3); couple.checkLastPriority(1); 74 couple.add(l3, TEST_PRIORITY_3); 75 } 76 77 public void testNegativePriorities() { 78 try { 79 PriorityListenerList<EventListener > ll = new PriorityListenerList<EventListener >(); 80 ll.add(new L(), -1); 81 fail("Should not get here"); 82 } catch (IndexOutOfBoundsException e) { 83 } 85 86 try { 87 PriorityListenerList<EventListener > ll = new PriorityListenerList<EventListener >(); 88 ll.remove(new L(), -1); 89 fail("Should not get here"); 90 } catch (IndexOutOfBoundsException e) { 91 } 93 } 94 95 public void testSerialization() throws Exception { 96 PriorityListenerList<EventListener > ll = new PriorityListenerList<EventListener >(); 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 [][] lla = ll.getListenersArray(); 104 EventListener [][] 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 , Serializable { 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 <Integer ,List <EventListener >> { 128 129 public synchronized void add(EventListener listener, int priority) { 130 assertTrue(priority >= 0); 131 getList(priority, true).add(0, listener); 133 } 134 135 public synchronized void remove(EventListener listener, int priority) { 136 assertTrue(priority >= 0); 137 List <EventListener > 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 <EventListener > getList(int priority) { 147 return getList(priority, false); 148 } 149 150 public synchronized void checkEquals(PriorityListenerList<EventListener > priorityListenerList) { 151 EventListener [][] listenersArray = priorityListenerList.getListenersArray(); 153 for (int priority = listenersArray.length - 1; priority >= 0; priority--) { 154 EventListener [] 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 for (Map.Entry <Integer ,List <EventListener >> entry : entrySet()) { 162 if (entry.getValue().size() > 0) { 163 assertTrue (entry.getKey() < listenersArray.length); 164 } 165 } 166 } 167 168 private List <EventListener > getList(int priority, boolean forceCreation) { 169 List <EventListener > l = get(priority); 170 if (l == null) { 171 if (forceCreation) { 172 l = new ArrayList <EventListener >(); 173 put(priority, l); 174 } else { l = Collections.emptyList(); 176 } 177 } 178 return l; 179 } 180 181 } 182 183 private static final class PriorityListenerListCouple { 184 185 PriorityListenerList<EventListener > priorityListenerList; 186 187 PriorityListenerListImitation imitation; 188 189 public PriorityListenerListCouple() { 190 priorityListenerList = new PriorityListenerList<EventListener >(); 191 imitation = new PriorityListenerListImitation(); 192 } 193 194 public void add(EventListener listener, int priority) { 195 priorityListenerList.add(listener, priority); 196 imitation.add(listener, priority); 197 imitation.checkEquals(priorityListenerList); 198 } 199 200 public void remove(EventListener 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 |