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 ListenerListTest extends NbTestCase { 43 44 private static final boolean debug = false; 45 46 public ListenerListTest(java.lang.String 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 { 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 ("unchecked") 78 ListenerList<MyL> sll = (ListenerList<MyL>)mo.get(); 79 List <MyL> lla = ll.getListeners(); 80 List <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 , Serializable { 88 89 static final long serialVersionUID = 12345L; 90 91 static int cntr; 93 private int id = cntr++; 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 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 > { 120 121 ListenerList<T> listenerList; 122 123 List <T> imitation; 124 125 public ListenerListCouple() { 126 listenerList = new ListenerList<T>(); 127 imitation = new ArrayList <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 assertEquals(imitation.size(), listenerList.getListenerCount()); 145 int i = 0; 146 List <T> listeners = listenerList.getListeners(); 147 for (EventListener l : imitation) { 148 assertSame(l, listeners.get(i++)); 149 } 150 } 151 152 } 153 154 } 155 | Popular Tags |