001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Alexander T. Simbirtsev
019: * @version $Revision$
020: * Created on 21.09.2004
021:
022: */package javax.swing.event;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030: import java.io.Serializable;
031: import java.util.EventListener;
032: import javax.swing.SwingTestCase;
033:
034: public class EventListenerListTest extends SwingTestCase {
035: static class ConcreteListener implements EventListener {
036: protected String name = null;
037:
038: public ConcreteListener() {
039: }
040:
041: public ConcreteListener(final String name) {
042: this .name = name;
043: }
044:
045: @Override
046: public String toString() {
047: return name;
048: }
049:
050: public int findMe(final Object[] listenersArray,
051: final Class<?> myClass) {
052: int found = 0;
053: for (int i = 0; i < listenersArray.length; i += 2) {
054: if (listenersArray[i] == myClass
055: && listenersArray[i + 1] == this ) {
056: found++;
057: }
058: }
059: return found;
060: }
061:
062: public int findMe(final EventListener[] listenersArray) {
063: int found = 0;
064: for (int i = 0; i < listenersArray.length; i++) {
065: if (listenersArray[i] == this ) {
066: found++;
067: }
068: }
069: return found;
070: }
071: }
072:
073: static class ConcreteSerializableListener extends ConcreteListener
074: implements Serializable {
075: private static final long serialVersionUID = 1L;
076:
077: public ConcreteSerializableListener() {
078: }
079:
080: public ConcreteSerializableListener(final String name) {
081: super (name);
082: }
083:
084: private void writeObject(final ObjectOutputStream outStream)
085: throws IOException {
086: outStream.defaultWriteObject();
087: outStream.writeObject(name);
088: }
089:
090: private void readObject(final ObjectInputStream inStream)
091: throws IOException, ClassNotFoundException {
092: inStream.defaultReadObject();
093: name = (String) inStream.readObject();
094: }
095: }
096:
097: protected EventListenerList list = null;
098:
099: public static void main(final String[] args) {
100: junit.textui.TestRunner.run(EventListenerListTest.class);
101: }
102:
103: /*
104: * @see TestCase#setUp()
105: */
106: @Override
107: protected void setUp() throws Exception {
108: super .setUp();
109: list = new EventListenerList();
110: }
111:
112: /*
113: * @see TestCase#tearDown()
114: */
115: @Override
116: protected void tearDown() throws Exception {
117: super .tearDown();
118: }
119:
120: public void testRemove() {
121: ConcreteListener listener1 = new ConcreteListener("1");
122: ConcreteListener listener2 = new ConcreteListener("2");
123: ConcreteListener listener3 = new ConcreteListener("3");
124: list.add(ConcreteListener.class, listener1);
125: list.add(ConcreteListener.class, listener2);
126: list.add(ConcreteListener.class, listener3);
127: assertTrue(list.getListenerCount() == 3);
128: list.remove(ConcreteListener.class, listener2);
129: assertTrue(list.getListenerCount() == 2);
130: list.remove(ConcreteListener.class, listener1);
131: assertTrue(list.getListenerCount() == 1);
132: list.add(ConcreteListener.class, listener2);
133: list.add(EventListener.class, listener1);
134: list.add(EventListener.class, listener3);
135: assertTrue(list.getListenerCount() == 4);
136: list.remove(ConcreteListener.class, listener1);
137: assertTrue(list.getListenerCount() == 4);
138: list.remove(ConcreteListener.class, listener2);
139: assertTrue(list.getListenerCount() == 3);
140: list.remove(ConcreteListener.class, listener3);
141: assertTrue(list.getListenerCount() == 2);
142: list.remove(EventListener.class, listener3);
143: assertTrue(list.getListenerCount() == 1);
144: list.remove(EventListener.class, null);
145: list.remove(EventListener.class, listener1);
146: assertTrue(list.getListenerCount() == 0);
147: }
148:
149: public void testAdd() {
150: ConcreteListener listener1 = new ConcreteListener("1");
151: ConcreteListener listener2 = new ConcreteListener("2");
152: ConcreteListener listener3 = new ConcreteListener("3");
153: Object[] listenersList = null;
154: list.add(ConcreteListener.class, listener1);
155: assertTrue(list.getListenerCount() == 1);
156: listenersList = list.getListenerList();
157: assertTrue(listener1.findMe(listenersList,
158: ConcreteListener.class) == 1);
159: list.add(ConcreteListener.class, listener2);
160: assertTrue(list.getListenerCount() == 2);
161: listenersList = list.getListenerList();
162: assertTrue(listener1.findMe(listenersList,
163: ConcreteListener.class) == 1);
164: assertTrue(listener2.findMe(listenersList,
165: ConcreteListener.class) == 1);
166: list.add(ConcreteListener.class, listener3);
167: assertTrue(list.getListenerCount() == 3);
168: listenersList = list.getListenerList();
169: assertTrue(listener1.findMe(listenersList,
170: ConcreteListener.class) == 1);
171: assertTrue(listener2.findMe(listenersList,
172: ConcreteListener.class) == 1);
173: assertTrue(listener3.findMe(listenersList,
174: ConcreteListener.class) == 1);
175: list.add(ConcreteListener.class, listener1);
176: assertTrue(list.getListenerCount() == 4);
177: listenersList = list.getListenerList();
178: assertTrue(listener1.findMe(listenersList,
179: ConcreteListener.class) == 2);
180: assertTrue(listener2.findMe(listenersList,
181: ConcreteListener.class) == 1);
182: assertTrue(listener3.findMe(listenersList,
183: ConcreteListener.class) == 1);
184: list.add(EventListener.class, listener1);
185: list.add(EventListener.class, listener2);
186: assertTrue(list.getListenerCount() == 6);
187: listenersList = list.getListenerList();
188: assertTrue(listener1.findMe(listenersList,
189: ConcreteListener.class) == 2);
190: assertTrue(listener1.findMe(listenersList, EventListener.class) == 1);
191: assertTrue(listener2.findMe(listenersList,
192: ConcreteListener.class) == 1);
193: assertTrue(listener2.findMe(listenersList, EventListener.class) == 1);
194: assertTrue(listener3.findMe(listenersList,
195: ConcreteListener.class) == 1);
196: }
197:
198: public void testGetListeners() {
199: ConcreteListener listener1 = new ConcreteListener("1");
200: ConcreteListener listener2 = new ConcreteListener("2");
201: ConcreteListener listener3 = new ConcreteListener("3");
202: EventListener[] listenersList = null;
203: listenersList = list.getListeners(ConcreteListener.class);
204: assertTrue(listener1.findMe(listenersList) == 0);
205: list.add(ConcreteListener.class, listener1);
206: listenersList = list.getListeners(ConcreteListener.class);
207: assertTrue(listener1.findMe(listenersList) == 1);
208: list.add(ConcreteListener.class, listener2);
209: listenersList = list.getListeners(ConcreteListener.class);
210: assertTrue(listener1.findMe(listenersList) == 1);
211: assertTrue(listener2.findMe(listenersList) == 1);
212: list.add(ConcreteListener.class, listener3);
213: listenersList = list.getListeners(ConcreteListener.class);
214: assertTrue(listener1.findMe(listenersList) == 1);
215: assertTrue(listener2.findMe(listenersList) == 1);
216: assertTrue(listener3.findMe(listenersList) == 1);
217: list.add(ConcreteListener.class, listener3);
218: list.add(EventListener.class, listener1);
219: list.add(EventListener.class, listener2);
220: assertTrue(list.getListenerCount() == 6);
221: listenersList = list.getListeners(EventListener.class);
222: assertTrue(listener1.findMe(listenersList) == 1);
223: assertTrue(listener2.findMe(listenersList) == 1);
224: assertTrue(listener3.findMe(listenersList) == 0);
225: listenersList = list.getListeners(ConcreteListener.class);
226: assertTrue(listener1.findMe(listenersList) == 1);
227: assertTrue(listener2.findMe(listenersList) == 1);
228: assertTrue(listener3.findMe(listenersList) == 2);
229: assertEquals(listenersList[3], listener1);
230: assertEquals(listenersList[1], listener3);
231: }
232:
233: /*
234: * Class under test for String toString()
235: */
236: public void testToString() {
237: ConcreteListener listener1 = new ConcreteListener("1");
238: ConcreteListener listener2 = new ConcreteListener("2");
239: ConcreteListener listener3 = new ConcreteListener("3");
240: list.add(ConcreteListener.class, listener1);
241: list.add(ConcreteListener.class, listener2);
242: list.add(ConcreteListener.class, listener3);
243: assertFalse(list.toString() == null
244: && list.toString().equals(""));
245: }
246:
247: /*
248: * this function is being tested by testAdd()
249: */
250: public void testGetListenerList() {
251: }
252:
253: /*
254: * Class under test for int getListenerCount(Class)
255: */
256: public void testGetListenerCountClass() {
257: ConcreteListener listener1 = new ConcreteListener("1");
258: ConcreteListener listener2 = new ConcreteListener("2");
259: ConcreteListener listener3 = new ConcreteListener("3");
260: assertTrue(list.getListenerCount(ConcreteListener.class) == 0);
261: list.add(ConcreteListener.class, listener1);
262: list.add(ConcreteListener.class, listener2);
263: list.add(ConcreteListener.class, listener3);
264: assertTrue(list.getListenerCount(ConcreteListener.class) == 3);
265: list.add(EventListener.class, listener1);
266: list.add(EventListener.class, listener2);
267: list.add(ConcreteListener.class, listener1);
268: assertTrue(list.getListenerCount(ConcreteListener.class) == 4);
269: assertTrue(list.getListenerCount(EventListener.class) == 2);
270: }
271:
272: /*
273: * Class under test for int getListenerCount()
274: */
275: public void testGetListenerCount() {
276: ConcreteListener listener1 = new ConcreteListener("1");
277: ConcreteListener listener2 = new ConcreteListener("2");
278: ConcreteListener listener3 = new ConcreteListener("3");
279: assertTrue(list.getListenerCount() == 0);
280: list.add(ConcreteListener.class, listener1);
281: list.add(ConcreteListener.class, listener2);
282: list.add(ConcreteListener.class, listener3);
283: assertTrue(list.getListenerCount() == 3);
284: list.add(ConcreteListener.class, listener1);
285: assertTrue(list.getListenerCount() == 4);
286: list.add(EventListener.class, listener1);
287: list.add(EventListener.class, listener2);
288: assertTrue(list.getListenerCount() == 6);
289: }
290:
291: public void testWriteObject() throws IOException {
292: ConcreteListener listener1 = new ConcreteSerializableListener(
293: "1");
294: ConcreteSerializableListener listener2 = new ConcreteSerializableListener(
295: "2");
296: ConcreteListener listener3 = new ConcreteListener("3");
297: list.add(ConcreteListener.class, listener1);
298: list.add(ConcreteSerializableListener.class, listener2);
299: list.add(EventListener.class, listener3);
300: ByteArrayOutputStream fo = new ByteArrayOutputStream();
301: ObjectOutputStream so = new ObjectOutputStream(fo);
302: so.writeObject(list);
303: so.flush();
304: }
305:
306: public void testReadObject() throws IOException,
307: ClassNotFoundException {
308: ConcreteListener listener1 = new ConcreteSerializableListener(
309: "1");
310: ConcreteListener listener2 = new ConcreteSerializableListener(
311: "2");
312: ConcreteListener listener3 = new ConcreteListener("3");
313: list.add(ConcreteListener.class, listener1);
314: list.add(ConcreteListener.class, listener3);
315: list.add(EventListener.class, listener2);
316: ByteArrayOutputStream fo = new ByteArrayOutputStream();
317: ObjectOutputStream so = new ObjectOutputStream(fo);
318: so.writeObject(list);
319: so.flush();
320: InputStream fi = new ByteArrayInputStream(fo.toByteArray());
321: ObjectInputStream si = new ObjectInputStream(fi);
322: EventListenerList resurrectedList = (EventListenerList) si
323: .readObject();
324: assertTrue(resurrectedList.getListeners(ConcreteListener.class).length == 1);
325: assertTrue((resurrectedList
326: .getListeners(ConcreteListener.class)[0]).name
327: .equals("1"));
328: assertTrue(resurrectedList.getListeners(EventListener.class).length == 1);
329: assertTrue(((ConcreteListener) resurrectedList
330: .getListeners(EventListener.class)[0]).name.equals("2"));
331: }
332: }
|