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: */package javax.swing.event;
021:
022: import java.beans.PropertyChangeEvent;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyChangeListenerProxy;
025: import java.beans.PropertyChangeSupport;
026: import java.beans.VetoableChangeListener;
027: import java.io.IOException;
028: import java.io.ObjectInputStream;
029: import java.io.ObjectOutputStream;
030: import java.io.Serializable;
031: import javax.swing.SwingTestCase;
032:
033: public class SwingPropertyChangeSupportTest extends SwingTestCase {
034: public static class FindableListener {
035: public String valueChangedKey = "";
036:
037: public Object valueChangedOld = null;
038:
039: public Object valueChangedNew = null;
040:
041: public void reset() {
042: valueChangedKey = "";
043: valueChangedOld = null;
044: valueChangedNew = null;
045: }
046:
047: public int findMe(final Object[] listenersArray) {
048: int found = 0;
049: for (int i = 0; i < listenersArray.length; i++) {
050: if (listenersArray[i] == this ) {
051: found++;
052: }
053: }
054: return found;
055: }
056: }
057:
058: public static class ConcreteVetoableChangeListener extends
059: FindableListener implements VetoableChangeListener {
060: public void vetoableChange(final PropertyChangeEvent evt) {
061: valueChangedKey = evt.getPropertyName();
062: valueChangedOld = evt.getOldValue();
063: valueChangedNew = evt.getNewValue();
064: }
065: };
066:
067: public class ConcretePropertyChangeListener extends
068: PropertyChangeController implements Serializable {
069: private static final long serialVersionUID = 1L;
070:
071: public ConcretePropertyChangeListener() {
072: super ();
073: }
074:
075: public int findMyProxy(final Object[] listenersArray,
076: final String property) {
077: int found = 0;
078: for (int i = 0; i < listenersArray.length; i++) {
079: Object curListener = listenersArray[i];
080: if (curListener instanceof PropertyChangeListenerProxy) {
081: PropertyChangeListenerProxy proxy = (PropertyChangeListenerProxy) curListener;
082: if (proxy.getListener() == this
083: && proxy.getPropertyName().equals(property)) {
084: found++;
085: }
086: }
087: }
088: return found;
089: }
090:
091: private void writeObject(final ObjectOutputStream outStream)
092: throws IOException {
093: outStream.defaultWriteObject();
094: }
095:
096: private void readObject(final ObjectInputStream inStream)
097: throws IOException, ClassNotFoundException {
098: inStream.defaultReadObject();
099: }
100: };
101:
102: public static class SerializableListener implements
103: PropertyChangeListener, Serializable {
104: private static final long serialVersionUID = 1L;
105:
106: private String name;
107:
108: public SerializableListener() {
109: super ();
110: name = "";
111: }
112:
113: public SerializableListener(final String name) {
114: super ();
115: this .name = name;
116: }
117:
118: private void writeObject(final ObjectOutputStream outStream)
119: throws IOException {
120: outStream.defaultWriteObject();
121: outStream.writeObject(name);
122: }
123:
124: private void readObject(final ObjectInputStream inStream)
125: throws IOException, ClassNotFoundException {
126: inStream.defaultReadObject();
127: name = (String) inStream.readObject();
128: }
129:
130: public void propertyChange(PropertyChangeEvent e) {
131: }
132: };
133:
134: protected Object panel;
135:
136: protected PropertyChangeSupport propertyChangeSupport;
137:
138: /*
139: * @see TestCase#setUp()
140: */
141: @Override
142: protected void setUp() throws Exception {
143: super .setUp();
144: panel = new SerializableListener("Panel");
145: propertyChangeSupport = new SwingPropertyChangeSupport(panel);
146: }
147:
148: /*
149: * @see TestCase#tearDown()
150: */
151: @Override
152: protected void tearDown() throws Exception {
153: panel = null;
154: propertyChangeSupport = null;
155: super .tearDown();
156: }
157:
158: /*
159: * Class under test for void firePropertyChange(String, Object, Object)
160: */
161: public void testFirePropertyChangeStringObjectObject() {
162: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
163: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
164: propertyChangeSupport.addPropertyChangeListener("first",
165: changeListener1);
166: propertyChangeSupport.addPropertyChangeListener("second",
167: changeListener2);
168: String oldValue = "old";
169: String newValue = "new";
170: propertyChangeSupport.firePropertyChange("first", oldValue,
171: newValue);
172: changeListener1.checkLastPropertyFired(panel, "first",
173: oldValue, newValue);
174: assertFalse(changeListener2.isChanged());
175: changeListener1.reset();
176: changeListener2.reset();
177: propertyChangeSupport.firePropertyChange("second", oldValue,
178: newValue);
179: assertFalse(changeListener1.isChanged());
180: changeListener2.checkLastPropertyFired(panel, "second",
181: oldValue, newValue);
182: changeListener1.reset();
183: changeListener2.reset();
184: propertyChangeSupport.addPropertyChangeListener("first",
185: changeListener2);
186: propertyChangeSupport.firePropertyChange("first", oldValue,
187: newValue);
188: changeListener1.checkLastPropertyFired(panel, "first",
189: oldValue, newValue);
190: changeListener2.checkLastPropertyFired(panel, "first",
191: oldValue, newValue);
192: changeListener1.reset();
193: changeListener2.reset();
194: propertyChangeSupport.removePropertyChangeListener("first",
195: changeListener2);
196: propertyChangeSupport
197: .addPropertyChangeListener(changeListener2);
198: propertyChangeSupport.firePropertyChange("first", oldValue,
199: newValue);
200: changeListener1.checkLastPropertyFired(panel, "first",
201: oldValue, newValue);
202: changeListener2.checkLastPropertyFired(panel, "first",
203: oldValue, newValue);
204: changeListener1.reset();
205: changeListener2.reset();
206: propertyChangeSupport.firePropertyChange("second", oldValue,
207: newValue);
208: assertFalse(changeListener1.isChanged());
209: changeListener2.checkLastPropertyFired(panel, "second",
210: oldValue, newValue);
211: changeListener1.reset();
212: changeListener2.reset();
213: propertyChangeSupport.firePropertyChange("second", newValue,
214: newValue);
215: assertFalse(changeListener1.isChanged());
216: assertFalse(changeListener2.isChanged());
217: changeListener1.reset();
218: changeListener2.reset();
219: propertyChangeSupport.firePropertyChange("second", null, null);
220: assertFalse(changeListener1.isChanged());
221: changeListener2.checkLastPropertyFired(panel, "second", null,
222: null);
223: }
224:
225: /*
226: * Class under test for void removePropertyChangeListener(String, PropertyChangeListener)
227: */
228: public void testRemovePropertyChangeListenerStringPropertyChangeListener() {
229: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
230: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
231: ConcretePropertyChangeListener changeListener3 = new ConcretePropertyChangeListener();
232: ConcretePropertyChangeListener changeListener4 = new ConcretePropertyChangeListener();
233: PropertyChangeListener[] propertyListeners = null;
234: propertyListeners = propertyChangeSupport
235: .getPropertyChangeListeners();
236: assertTrue(propertyListeners != null
237: && propertyListeners.length == 0);
238: propertyChangeSupport.addPropertyChangeListener("first",
239: changeListener1);
240: propertyChangeSupport.addPropertyChangeListener("second",
241: changeListener2);
242: propertyChangeSupport
243: .addPropertyChangeListener(changeListener3);
244: propertyChangeSupport.addPropertyChangeListener("first",
245: changeListener1);
246: propertyChangeSupport.addPropertyChangeListener("first",
247: changeListener2);
248: propertyChangeSupport
249: .addPropertyChangeListener(changeListener4);
250: propertyListeners = propertyChangeSupport
251: .getPropertyChangeListeners();
252: assertTrue(propertyListeners != null
253: && propertyListeners.length == 6);
254: propertyChangeSupport.removePropertyChangeListener("third",
255: changeListener3);
256: propertyListeners = propertyChangeSupport
257: .getPropertyChangeListeners();
258: assertTrue(propertyListeners != null
259: && propertyListeners.length == 6);
260: assertTrue(changeListener3.findMe(propertyListeners) == 1);
261: propertyChangeSupport.removePropertyChangeListener("first",
262: changeListener2);
263: propertyListeners = propertyChangeSupport
264: .getPropertyChangeListeners();
265: assertTrue(propertyListeners != null
266: && propertyListeners.length == 5);
267: assertTrue(changeListener2.findMyProxy(propertyListeners,
268: "second") == 1);
269: assertTrue(changeListener1.findMyProxy(propertyListeners,
270: "first") == 2);
271: propertyChangeSupport
272: .removePropertyChangeListener(changeListener1);
273: propertyListeners = propertyChangeSupport
274: .getPropertyChangeListeners();
275: assertTrue(propertyListeners != null
276: && propertyListeners.length == 5);
277: assertTrue(changeListener1.findMyProxy(propertyListeners,
278: "first") == 2);
279: propertyChangeSupport.removePropertyChangeListener("first",
280: changeListener1);
281: propertyListeners = propertyChangeSupport
282: .getPropertyChangeListeners();
283: assertTrue(propertyListeners != null
284: && propertyListeners.length == 4);
285: assertTrue(changeListener1.findMyProxy(propertyListeners,
286: "first") == 1);
287: propertyChangeSupport.removePropertyChangeListener("first",
288: changeListener1);
289: propertyListeners = propertyChangeSupport
290: .getPropertyChangeListeners();
291: assertTrue(propertyListeners != null
292: && propertyListeners.length == 3);
293: assertTrue(changeListener1.findMyProxy(propertyListeners,
294: "first") == 0);
295: }
296:
297: /*
298: * Class under test for void addPropertyChangeListener(String, PropertyChangeListener)
299: */
300: public void testAddPropertyChangeListenerStringPropertyChangeListener() {
301: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
302: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
303: PropertyChangeListener[] propertyListeners = null;
304: propertyListeners = propertyChangeSupport
305: .getPropertyChangeListeners();
306: assertTrue(propertyListeners != null
307: && propertyListeners.length == 0);
308: propertyChangeSupport.addPropertyChangeListener("first",
309: changeListener1);
310: propertyListeners = propertyChangeSupport
311: .getPropertyChangeListeners();
312: assertTrue(propertyListeners != null
313: && propertyListeners.length == 1);
314: assertTrue(changeListener1.findMyProxy(propertyListeners,
315: "first") == 1);
316: propertyChangeSupport.addPropertyChangeListener("second",
317: changeListener2);
318: propertyListeners = propertyChangeSupport
319: .getPropertyChangeListeners();
320: assertTrue(propertyListeners != null
321: && propertyListeners.length == 2);
322: assertTrue(changeListener1.findMyProxy(propertyListeners,
323: "first") == 1);
324: assertTrue(changeListener2.findMyProxy(propertyListeners,
325: "second") == 1);
326: propertyChangeSupport.addPropertyChangeListener("third",
327: changeListener2);
328: propertyChangeSupport
329: .addPropertyChangeListener(changeListener1);
330: propertyChangeSupport.addPropertyChangeListener("first",
331: changeListener1);
332: propertyListeners = propertyChangeSupport
333: .getPropertyChangeListeners();
334: assertTrue(propertyListeners != null
335: && propertyListeners.length == 5);
336: assertTrue(changeListener1.findMyProxy(propertyListeners,
337: "first") == 2);
338: assertTrue(changeListener2.findMyProxy(propertyListeners,
339: "second") == 1);
340: assertTrue(changeListener2.findMyProxy(propertyListeners,
341: "third") == 1);
342: }
343:
344: /*
345: * Class under test for PropertyChangeListener[] getPropertyChangeListeners(String)
346: */
347: public void testGetPropertyChangeListenersString() {
348: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
349: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
350: ConcretePropertyChangeListener changeListener3 = new ConcretePropertyChangeListener();
351: ConcretePropertyChangeListener changeListener4 = new ConcretePropertyChangeListener();
352: PropertyChangeListener[] propertyListeners = null;
353: propertyListeners = propertyChangeSupport
354: .getPropertyChangeListeners("first");
355: assertTrue(propertyListeners != null
356: && propertyListeners.length == 0);
357: propertyChangeSupport.addPropertyChangeListener("first",
358: changeListener1);
359: propertyChangeSupport.addPropertyChangeListener("second",
360: changeListener1);
361: propertyChangeSupport
362: .addPropertyChangeListener(changeListener2);
363: propertyChangeSupport
364: .addPropertyChangeListener(changeListener3);
365: propertyChangeSupport.addPropertyChangeListener("second",
366: changeListener4);
367: propertyListeners = propertyChangeSupport
368: .getPropertyChangeListeners("first");
369: assertTrue(propertyListeners != null
370: && propertyListeners.length == 1);
371: assertTrue(changeListener1.findMe(propertyListeners) == 1);
372: assertTrue(changeListener2.findMe(propertyListeners) == 0);
373: assertTrue(changeListener3.findMe(propertyListeners) == 0);
374: assertTrue(changeListener4.findMe(propertyListeners) == 0);
375: propertyListeners = propertyChangeSupport
376: .getPropertyChangeListeners("second");
377: assertTrue(propertyListeners != null
378: && propertyListeners.length == 2);
379: assertTrue(changeListener1.findMe(propertyListeners) == 1);
380: assertTrue(changeListener2.findMe(propertyListeners) == 0);
381: assertTrue(changeListener3.findMe(propertyListeners) == 0);
382: assertTrue(changeListener4.findMe(propertyListeners) == 1);
383: propertyListeners = propertyChangeSupport
384: .getPropertyChangeListeners("null");
385: assertTrue(propertyListeners != null
386: && propertyListeners.length == 0);
387: }
388:
389: /*
390: * Class under test for boolean hasListeners(String)
391: */
392: public void testHasListenersString() {
393: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
394: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
395: propertyChangeSupport.addPropertyChangeListener("first",
396: changeListener1);
397: assertTrue(propertyChangeSupport.hasListeners("first"));
398: assertFalse(propertyChangeSupport.hasListeners("second"));
399: assertFalse(propertyChangeSupport.hasListeners("third"));
400: assertFalse(propertyChangeSupport.hasListeners("forth"));
401: propertyChangeSupport.addPropertyChangeListener("second",
402: changeListener2);
403: assertTrue(propertyChangeSupport.hasListeners("first"));
404: assertTrue(propertyChangeSupport.hasListeners("second"));
405: assertFalse(propertyChangeSupport.hasListeners("third"));
406: assertFalse(propertyChangeSupport.hasListeners("forth"));
407: propertyChangeSupport.addPropertyChangeListener("third",
408: changeListener1);
409: assertTrue(propertyChangeSupport.hasListeners("first"));
410: assertTrue(propertyChangeSupport.hasListeners("second"));
411: assertTrue(propertyChangeSupport.hasListeners("third"));
412: assertFalse(propertyChangeSupport.hasListeners("forth"));
413: propertyChangeSupport
414: .addPropertyChangeListener(changeListener2);
415: assertTrue(propertyChangeSupport.hasListeners("first"));
416: assertTrue(propertyChangeSupport.hasListeners("second"));
417: assertTrue(propertyChangeSupport.hasListeners("third"));
418: assertTrue(propertyChangeSupport.hasListeners("forth"));
419: propertyChangeSupport.removePropertyChangeListener("first",
420: changeListener1);
421: assertTrue(propertyChangeSupport.hasListeners("first"));
422: assertTrue(propertyChangeSupport.hasListeners("second"));
423: assertTrue(propertyChangeSupport.hasListeners("third"));
424: assertTrue(propertyChangeSupport.hasListeners("forth"));
425: propertyChangeSupport.removePropertyChangeListener("first",
426: changeListener2);
427: assertTrue(propertyChangeSupport.hasListeners("first"));
428: assertTrue(propertyChangeSupport.hasListeners("second"));
429: assertTrue(propertyChangeSupport.hasListeners("third"));
430: assertTrue(propertyChangeSupport.hasListeners("forth"));
431: }
432:
433: /*
434: * Class under test for void removePropertyChangeListener(PropertyChangeListener)
435: */
436: public void testRemovePropertyChangeListenerPropertyChangeListener() {
437: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
438: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
439: ConcretePropertyChangeListener changeListener3 = new ConcretePropertyChangeListener();
440: ConcretePropertyChangeListener changeListener4 = new ConcretePropertyChangeListener();
441: PropertyChangeListener[] propertyListeners = null;
442: propertyListeners = propertyChangeSupport
443: .getPropertyChangeListeners();
444: assertTrue(propertyListeners != null
445: && propertyListeners.length == 0);
446: propertyChangeSupport
447: .addPropertyChangeListener(changeListener1);
448: propertyChangeSupport
449: .addPropertyChangeListener(changeListener2);
450: propertyChangeSupport
451: .addPropertyChangeListener(changeListener3);
452: propertyChangeSupport
453: .addPropertyChangeListener(changeListener1);
454: propertyChangeSupport.addPropertyChangeListener("first",
455: changeListener2);
456: propertyChangeSupport
457: .addPropertyChangeListener(changeListener4);
458: propertyListeners = propertyChangeSupport
459: .getPropertyChangeListeners();
460: assertTrue(propertyListeners != null
461: && propertyListeners.length == 6);
462: propertyChangeSupport
463: .removePropertyChangeListener(changeListener3);
464: propertyListeners = propertyChangeSupport
465: .getPropertyChangeListeners();
466: assertTrue(propertyListeners != null
467: && propertyListeners.length == 5);
468: assertTrue(changeListener1.findMe(propertyListeners) == 2);
469: assertTrue(changeListener3.findMe(propertyListeners) == 0);
470: propertyChangeSupport
471: .removePropertyChangeListener(changeListener1);
472: propertyListeners = propertyChangeSupport
473: .getPropertyChangeListeners();
474: assertTrue(propertyListeners != null
475: && propertyListeners.length == 4);
476: assertTrue(changeListener1.findMe(propertyListeners) == 1);
477: propertyChangeSupport.removePropertyChangeListener("first",
478: changeListener1);
479: propertyListeners = propertyChangeSupport
480: .getPropertyChangeListeners();
481: assertTrue(propertyListeners != null
482: && propertyListeners.length == 4);
483: assertTrue(changeListener1.findMe(propertyListeners) == 1);
484: propertyChangeSupport
485: .removePropertyChangeListener(changeListener2);
486: propertyListeners = propertyChangeSupport
487: .getPropertyChangeListeners();
488: assertTrue(propertyListeners != null
489: && propertyListeners.length == 3);
490: assertTrue(changeListener2.findMe(propertyListeners) == 0);
491: propertyChangeSupport
492: .removePropertyChangeListener(changeListener2);
493: propertyListeners = propertyChangeSupport
494: .getPropertyChangeListeners();
495: assertTrue(propertyListeners != null
496: && propertyListeners.length == 3);
497: assertTrue(changeListener2.findMe(propertyListeners) == 0);
498: }
499:
500: /*
501: * Class under test for void addPropertyChangeListener(PropertyChangeListener)
502: */
503: public void testAddPropertyChangeListenerPropertyChangeListener() {
504: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
505: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
506: ConcretePropertyChangeListener changeListener3 = new ConcretePropertyChangeListener();
507: ConcretePropertyChangeListener changeListener4 = new ConcretePropertyChangeListener();
508: PropertyChangeListener[] propertyListeners = null;
509: propertyListeners = propertyChangeSupport
510: .getPropertyChangeListeners();
511: assertTrue(propertyListeners != null
512: && propertyListeners.length == 0);
513: propertyChangeSupport
514: .addPropertyChangeListener(changeListener1);
515: propertyListeners = propertyChangeSupport
516: .getPropertyChangeListeners();
517: assertTrue(propertyListeners != null
518: && propertyListeners.length == 1);
519: assertTrue(changeListener1.findMe(propertyListeners) == 1);
520: propertyChangeSupport
521: .addPropertyChangeListener(changeListener2);
522: propertyListeners = propertyChangeSupport
523: .getPropertyChangeListeners();
524: assertTrue(propertyListeners != null
525: && propertyListeners.length == 2);
526: assertTrue(changeListener1.findMe(propertyListeners) == 1);
527: assertTrue(changeListener2.findMe(propertyListeners) == 1);
528: propertyChangeSupport
529: .addPropertyChangeListener(changeListener3);
530: propertyListeners = propertyChangeSupport
531: .getPropertyChangeListeners();
532: assertTrue(propertyListeners != null
533: && propertyListeners.length == 3);
534: assertTrue(changeListener1.findMe(propertyListeners) == 1);
535: assertTrue(changeListener2.findMe(propertyListeners) == 1);
536: assertTrue(changeListener3.findMe(propertyListeners) == 1);
537: propertyChangeSupport
538: .addPropertyChangeListener(changeListener1);
539: propertyChangeSupport
540: .addPropertyChangeListener(changeListener4);
541: propertyListeners = propertyChangeSupport
542: .getPropertyChangeListeners();
543: assertTrue(propertyListeners != null
544: && propertyListeners.length == 5);
545: assertTrue(changeListener1.findMe(propertyListeners) == 2);
546: assertTrue(changeListener2.findMe(propertyListeners) == 1);
547: assertTrue(changeListener3.findMe(propertyListeners) == 1);
548: assertTrue(changeListener4.findMe(propertyListeners) == 1);
549: }
550:
551: /*
552: * Class under test for PropertyChangeListener[] getPropertyChangeListeners()
553: */
554: public void testGetPropertyChangeListeners() {
555: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
556: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
557: ConcretePropertyChangeListener changeListener3 = new ConcretePropertyChangeListener();
558: ConcretePropertyChangeListener changeListener4 = new ConcretePropertyChangeListener();
559: PropertyChangeListener[] propertyListeners = null;
560: propertyListeners = propertyChangeSupport
561: .getPropertyChangeListeners();
562: assertTrue(propertyListeners != null
563: && propertyListeners.length == 0);
564: propertyChangeSupport.addPropertyChangeListener("first",
565: changeListener1);
566: propertyChangeSupport.addPropertyChangeListener("second",
567: changeListener1);
568: propertyChangeSupport
569: .addPropertyChangeListener(changeListener2);
570: propertyListeners = propertyChangeSupport
571: .getPropertyChangeListeners();
572: assertTrue(propertyListeners != null
573: && propertyListeners.length == 3);
574: assertTrue(changeListener1.findMe(propertyListeners) == 0);
575: assertTrue(changeListener2.findMe(propertyListeners) == 1);
576: assertTrue(changeListener3.findMe(propertyListeners) == 0);
577: assertTrue(changeListener4.findMe(propertyListeners) == 0);
578: propertyChangeSupport
579: .addPropertyChangeListener(changeListener3);
580: propertyChangeSupport.addPropertyChangeListener("second",
581: changeListener4);
582: propertyListeners = propertyChangeSupport
583: .getPropertyChangeListeners();
584: assertTrue(propertyListeners != null
585: && propertyListeners.length == 5);
586: assertTrue(changeListener1.findMe(propertyListeners) == 0);
587: assertTrue(changeListener2.findMe(propertyListeners) == 1);
588: assertTrue(changeListener3.findMe(propertyListeners) == 1);
589: assertTrue(changeListener4.findMe(propertyListeners) == 0);
590: }
591:
592: /*
593: * Class under test for void firePropertyChange(PropertyChangeEvent)
594: */
595: public void testFirePropertyChangePropertyChangeEvent() {
596: ConcretePropertyChangeListener changeListener1 = new ConcretePropertyChangeListener();
597: ConcretePropertyChangeListener changeListener2 = new ConcretePropertyChangeListener();
598: propertyChangeSupport.addPropertyChangeListener("first",
599: changeListener1);
600: propertyChangeSupport.addPropertyChangeListener("second",
601: changeListener2);
602: String oldValue = "old";
603: String newValue = "new";
604: propertyChangeSupport
605: .firePropertyChange(new PropertyChangeEvent(panel,
606: "first", oldValue, newValue));
607: changeListener1.checkLastPropertyFired(panel, "first",
608: oldValue, newValue);
609: assertFalse(changeListener2.isChanged());
610: changeListener1.reset();
611: changeListener2.reset();
612: propertyChangeSupport
613: .firePropertyChange(new PropertyChangeEvent(panel,
614: "second", oldValue, newValue));
615: assertFalse(changeListener1.isChanged());
616: changeListener2.checkLastPropertyFired(panel, "second",
617: oldValue, newValue);
618: changeListener1.reset();
619: changeListener2.reset();
620: propertyChangeSupport.addPropertyChangeListener("first",
621: changeListener2);
622: propertyChangeSupport
623: .firePropertyChange(new PropertyChangeEvent(panel,
624: "first", oldValue, newValue));
625: changeListener1.checkLastPropertyFired(panel, "first",
626: oldValue, newValue);
627: changeListener2.checkLastPropertyFired(panel, "first",
628: oldValue, newValue);
629: changeListener1.reset();
630: changeListener2.reset();
631: propertyChangeSupport.removePropertyChangeListener("first",
632: changeListener2);
633: propertyChangeSupport
634: .addPropertyChangeListener(changeListener2);
635: propertyChangeSupport
636: .firePropertyChange(new PropertyChangeEvent(panel,
637: "first", oldValue, newValue));
638: changeListener1.checkLastPropertyFired(panel, "first",
639: oldValue, newValue);
640: changeListener2.checkLastPropertyFired(panel, "first",
641: oldValue, newValue);
642: changeListener1.reset();
643: changeListener2.reset();
644: propertyChangeSupport
645: .firePropertyChange(new PropertyChangeEvent(panel,
646: "second", oldValue, newValue));
647: assertFalse(changeListener1.isChanged());
648: changeListener2.checkLastPropertyFired(panel, "second",
649: oldValue, newValue);
650: }
651:
652: public void testReadWriteObject() throws IOException,
653: ClassNotFoundException {
654: String name1 = "name1";
655: String name2 = "name2";
656: PropertyChangeListener changeListener1 = new SerializableListener(
657: name1);
658: PropertyChangeListener changeListener2 = new SerializableListener(
659: name2);
660: propertyChangeSupport.addPropertyChangeListener(name1,
661: changeListener1);
662: propertyChangeSupport.addPropertyChangeListener(name2,
663: changeListener2);
664: PropertyChangeSupport resurrectedList = (PropertyChangeSupport) serializeObject(propertyChangeSupport);
665: assertEquals(1, resurrectedList
666: .getPropertyChangeListeners(name1).length);
667: assertEquals(name1, ((SerializableListener) resurrectedList
668: .getPropertyChangeListeners(name1)[0]).name);
669: assertEquals(1, resurrectedList
670: .getPropertyChangeListeners(name2).length);
671: assertEquals(name2, ((SerializableListener) resurrectedList
672: .getPropertyChangeListeners(name2)[0]).name);
673: }
674: }
|