001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.binding.tests;
032:
033: import java.util.Iterator;
034: import java.util.LinkedList;
035: import java.util.List;
036: import java.util.ListIterator;
037:
038: import javax.swing.event.ListDataEvent;
039: import javax.swing.event.ListDataListener;
040:
041: import junit.framework.TestCase;
042:
043: import com.jgoodies.binding.list.ArrayListModel;
044: import com.jgoodies.binding.list.LinkedListModel;
045: import com.jgoodies.binding.list.ObservableList;
046: import com.jgoodies.binding.tests.event.ListDataReport;
047:
048: /**
049: * A test case for classes {@link ArrayListModel} and {@link LinkedListModel}.
050: *
051: * @author Karsten Lentzsch
052: * @version $Revision: 1.17 $
053: */
054: public final class ObservableListTest extends TestCase {
055:
056: /**
057: * Tests the constructor that accepts a collection as initial content.
058: */
059: public void testListConstructor() {
060: new ArrayListModel<String>(createEmptyArrayListModel());
061: new LinkedListModel<String>(createEmptyArrayListModel());
062:
063: new ArrayListModel<String>(createSingleElementArrayListModel());
064: new LinkedListModel<String>(
065: createSingleElementLinkedListModel());
066:
067: new ArrayListModel<String>(createFilledArrayListModel());
068: new LinkedListModel<String>(createFilledLinkedListModel());
069: }
070:
071: /**
072: * Tests a single add operation on different observable lists.
073: */
074: public void testAdd() {
075: testAdd(createEmptyArrayListModel());
076: testAdd(createEmptyLinkedListModel());
077:
078: testAdd(createSingleElementArrayListModel());
079: testAdd(createSingleElementLinkedListModel());
080:
081: testAdd(createFilledArrayListModel());
082: testAdd(createFilledLinkedListModel());
083: }
084:
085: /**
086: * Tests a single indexed add operation on different observable lists.
087: */
088: public void testAddIndexed() {
089: testAddIndexed(createEmptyArrayListModel(), 0);
090: testAddIndexed(createEmptyLinkedListModel(), 0);
091:
092: testAddIndexed(createSingleElementArrayListModel(), 0);
093: testAddIndexed(createSingleElementLinkedListModel(), 0);
094: testAddIndexed(createSingleElementArrayListModel(), 1);
095: testAddIndexed(createSingleElementLinkedListModel(), 1);
096:
097: int size1 = createFilledArrayListModel().size();
098: for (int i = 0; i <= size1; i++) {
099: testAddIndexed(createFilledArrayListModel(), i);
100: }
101: int size2 = createFilledLinkedListModel().size();
102: for (int i = 0; i <= size2; i++) {
103: testAddIndexed(createFilledLinkedListModel(), i);
104: }
105: }
106:
107: /**
108: * Tests a multiple add operation on different combinations of observable lists.
109: */
110: public void testAddAll() {
111: testAddAll(createEmptyArrayListModel(),
112: createEmptyArrayListModel());
113: testAddAll(createEmptyLinkedListModel(),
114: createEmptyArrayListModel());
115: testAddAll(createEmptyArrayListModel(),
116: createSingleElementArrayListModel());
117: testAddAll(createEmptyLinkedListModel(),
118: createSingleElementArrayListModel());
119: testAddAll(createEmptyArrayListModel(),
120: createFilledArrayListModel());
121: testAddAll(createEmptyLinkedListModel(),
122: createFilledArrayListModel());
123:
124: testAddAll(createSingleElementArrayListModel(),
125: createEmptyArrayListModel());
126: testAddAll(createSingleElementLinkedListModel(),
127: createEmptyArrayListModel());
128: testAddAll(createSingleElementArrayListModel(),
129: createSingleElementArrayListModel());
130: testAddAll(createSingleElementLinkedListModel(),
131: createSingleElementArrayListModel());
132: testAddAll(createSingleElementArrayListModel(),
133: createFilledArrayListModel());
134: testAddAll(createSingleElementLinkedListModel(),
135: createFilledArrayListModel());
136:
137: testAddAll(createFilledArrayListModel(),
138: createEmptyArrayListModel());
139: testAddAll(createFilledLinkedListModel(),
140: createEmptyArrayListModel());
141: testAddAll(createFilledArrayListModel(),
142: createSingleElementArrayListModel());
143: testAddAll(createFilledLinkedListModel(),
144: createSingleElementArrayListModel());
145: testAddAll(createFilledArrayListModel(),
146: createFilledArrayListModel());
147: testAddAll(createFilledLinkedListModel(),
148: createFilledLinkedListModel());
149: }
150:
151: /**
152: * Tests indexed multiple add operations on different combinations of
153: * observable lists.
154: */
155: public void testAddAllIndexed() {
156: testAddAllIndexed(createEmptyArrayListModel(),
157: createFilledArrayListModel(), 0);
158: testAddAllIndexed(createEmptyLinkedListModel(),
159: createFilledArrayListModel(), 0);
160:
161: testAddAllIndexed(createSingleElementArrayListModel(),
162: createFilledArrayListModel(), 0);
163: testAddAllIndexed(createSingleElementLinkedListModel(),
164: createFilledArrayListModel(), 0);
165: testAddAllIndexed(createSingleElementArrayListModel(),
166: createFilledArrayListModel(), 1);
167: testAddAllIndexed(createSingleElementLinkedListModel(),
168: createFilledArrayListModel(), 1);
169:
170: int size1 = createFilledArrayListModel().size();
171: for (int i = 0; i <= size1; i++) {
172: testAddAllIndexed(createFilledArrayListModel(),
173: createFilledArrayListModel(), i);
174: }
175: int size2 = createFilledLinkedListModel().size();
176: for (int i = 0; i <= size2; i++) {
177: testAddAllIndexed(createFilledLinkedListModel(),
178: createFilledArrayListModel(), i);
179: }
180: }
181:
182: /**
183: * Tests the #clear operation on different observable lists.
184: */
185: public void testClear() {
186: testClear(createEmptyArrayListModel());
187: testClear(createEmptyLinkedListModel());
188:
189: testClear(createSingleElementArrayListModel());
190: testClear(createSingleElementLinkedListModel());
191:
192: testClear(createFilledArrayListModel());
193: testClear(createFilledLinkedListModel());
194: }
195:
196: /**
197: * Tests the #remove(int) operation on different observable lists.
198: */
199: public void testRemoveIndex() {
200: testRemoveIndex(createEmptyArrayListModel(), -1);
201: testRemoveIndex(createEmptyLinkedListModel(), -1);
202: testRemoveIndex(createEmptyArrayListModel(), 0);
203: testRemoveIndex(createEmptyLinkedListModel(), 0);
204: testRemoveIndex(createEmptyArrayListModel(), 1);
205: testRemoveIndex(createEmptyLinkedListModel(), 1);
206:
207: testRemoveIndex(createSingleElementArrayListModel(), -1);
208: testRemoveIndex(createSingleElementLinkedListModel(), -1);
209: testRemoveIndex(createSingleElementArrayListModel(), 0);
210: testRemoveIndex(createSingleElementLinkedListModel(), 0);
211: testRemoveIndex(createSingleElementArrayListModel(), 1);
212: testRemoveIndex(createSingleElementLinkedListModel(), 1);
213:
214: int size1 = createFilledArrayListModel().size();
215: for (int i = -1; i <= size1; i++) {
216: testRemoveIndex(createFilledArrayListModel(), i);
217: }
218: int size2 = createFilledLinkedListModel().size();
219: for (int i = -1; i <= size2; i++) {
220: testRemoveIndex(createFilledLinkedListModel(), i);
221: }
222: }
223:
224: /**
225: * Tests the #remove(Object) operation on different observable lists.
226: */
227: public void testRemoveObject() {
228: testRemoveObject(createEmptyArrayListModel(), -1);
229: testRemoveObject(createEmptyLinkedListModel(), -1);
230: testRemoveObject(createEmptyArrayListModel(), 0);
231: testRemoveObject(createEmptyLinkedListModel(), 0);
232: testRemoveObject(createEmptyArrayListModel(), 1);
233: testRemoveObject(createEmptyLinkedListModel(), 1);
234:
235: testRemoveObject(createSingleElementArrayListModel(), -1);
236: testRemoveObject(createSingleElementLinkedListModel(), -1);
237: testRemoveObject(createSingleElementArrayListModel(), 0);
238: testRemoveObject(createSingleElementLinkedListModel(), 0);
239: testRemoveObject(createSingleElementArrayListModel(), 1);
240: testRemoveObject(createSingleElementLinkedListModel(), 1);
241:
242: int size1 = createFilledArrayListModel().size();
243: for (int i = -1; i <= size1; i++) {
244: testRemoveObject(createFilledArrayListModel(), i);
245: }
246: int size2 = createFilledLinkedListModel().size();
247: for (int i = -1; i <= size2; i++) {
248: testRemoveObject(createFilledLinkedListModel(), i);
249: }
250: }
251:
252: /**
253: * Tests #removeAll on different combinations of observable lists.
254: */
255: public void testRemoveAll() {
256: testRemoveAll(createEmptyArrayListModel(),
257: createEmptyArrayListModel());
258: testRemoveAll(createEmptyLinkedListModel(),
259: createEmptyArrayListModel());
260: testRemoveAll(createEmptyArrayListModel(),
261: createSingleElementArrayListModel());
262: testRemoveAll(createEmptyLinkedListModel(),
263: createSingleElementArrayListModel());
264: testRemoveAll(createEmptyArrayListModel(),
265: createFilledArrayListModel());
266: testRemoveAll(createEmptyLinkedListModel(),
267: createFilledArrayListModel());
268:
269: testRemoveAll(createSingleElementArrayListModel(),
270: createEmptyArrayListModel());
271: testRemoveAll(createSingleElementLinkedListModel(),
272: createEmptyArrayListModel());
273: testRemoveAll(createSingleElementArrayListModel(),
274: createSingleElementArrayListModel());
275: testRemoveAll(createSingleElementLinkedListModel(),
276: createSingleElementArrayListModel());
277: testRemoveAll(createSingleElementArrayListModel(),
278: createFilledArrayListModel());
279: testRemoveAll(createSingleElementLinkedListModel(),
280: createFilledArrayListModel());
281:
282: testRemoveAll(createFilledArrayListModel(),
283: createEmptyArrayListModel());
284: testRemoveAll(createFilledLinkedListModel(),
285: createEmptyArrayListModel());
286: testRemoveAll(createFilledArrayListModel(),
287: createSingleElementArrayListModel());
288: testRemoveAll(createFilledLinkedListModel(),
289: createSingleElementArrayListModel());
290: testRemoveAll(createFilledArrayListModel(),
291: createFilledArrayListModel());
292: testRemoveAll(createFilledLinkedListModel(),
293: createFilledLinkedListModel());
294: }
295:
296: /**
297: * Tests #retainAll on different combinations of observable lists.
298: */
299: public void testRetainAll() {
300: testRetainAll(createEmptyArrayListModel(),
301: createEmptyArrayListModel());
302: testRetainAll(createEmptyLinkedListModel(),
303: createEmptyArrayListModel());
304: testRetainAll(createEmptyArrayListModel(),
305: createSingleElementArrayListModel());
306: testRetainAll(createEmptyLinkedListModel(),
307: createSingleElementArrayListModel());
308: testRetainAll(createEmptyArrayListModel(),
309: createFilledArrayListModel());
310: testRetainAll(createEmptyLinkedListModel(),
311: createFilledArrayListModel());
312:
313: testRetainAll(createSingleElementArrayListModel(),
314: createEmptyArrayListModel());
315: testRetainAll(createSingleElementLinkedListModel(),
316: createEmptyArrayListModel());
317: testRetainAll(createSingleElementArrayListModel(),
318: createSingleElementArrayListModel());
319: testRetainAll(createSingleElementLinkedListModel(),
320: createSingleElementArrayListModel());
321: testRetainAll(createSingleElementArrayListModel(),
322: createFilledArrayListModel());
323: testRetainAll(createSingleElementLinkedListModel(),
324: createFilledArrayListModel());
325:
326: testRetainAll(createFilledArrayListModel(),
327: createEmptyArrayListModel());
328: testRetainAll(createFilledLinkedListModel(),
329: createEmptyArrayListModel());
330: testRetainAll(createFilledArrayListModel(),
331: createSingleElementArrayListModel());
332: testRetainAll(createFilledLinkedListModel(),
333: createSingleElementArrayListModel());
334: testRetainAll(createFilledArrayListModel(),
335: createFilledArrayListModel());
336: testRetainAll(createFilledLinkedListModel(),
337: createFilledLinkedListModel());
338: }
339:
340: /**
341: * Tests the #set operation on different observable lists.
342: */
343: public void testSet() {
344: testSet(createEmptyArrayListModel(), 0);
345: testSet(createEmptyLinkedListModel(), 0);
346:
347: testSet(createSingleElementArrayListModel(), 0);
348: testSet(createSingleElementLinkedListModel(), 0);
349: testSet(createSingleElementArrayListModel(), 1);
350: testSet(createSingleElementLinkedListModel(), 1);
351:
352: int size1 = createFilledArrayListModel().size();
353: for (int i = 0; i < size1; i++) {
354: testSet(createFilledArrayListModel(), i);
355: }
356: int size2 = createFilledLinkedListModel().size();
357: for (int i = 0; i < size2; i++) {
358: testSet(createFilledLinkedListModel(), i);
359: }
360: }
361:
362: /**
363: * Tests the list iterator's add operation on different observable lists.
364: */
365: public void testListIteratorAdd() {
366: testListIteratorAdd(createEmptyArrayListModel(), 0, 0);
367: testListIteratorAdd(createEmptyLinkedListModel(), 0, 0);
368:
369: testListIteratorAdd(createSingleElementArrayListModel(), 0, 0);
370: testListIteratorAdd(createSingleElementArrayListModel(), 0, 1);
371: testListIteratorAdd(createSingleElementArrayListModel(), 1, 1);
372: testListIteratorAdd(createSingleElementLinkedListModel(), 0, 0);
373: testListIteratorAdd(createSingleElementLinkedListModel(), 0, 1);
374: testListIteratorAdd(createSingleElementLinkedListModel(), 1, 1);
375:
376: int size1 = createFilledArrayListModel().size();
377: for (int i = 0; i <= size1; i++) {
378: for (int j = i; j <= size1; j++) {
379: testListIteratorAdd(createFilledArrayListModel(), i, j);
380: }
381: }
382: int size2 = createFilledLinkedListModel().size();
383: for (int i = 0; i <= size2; i++) {
384: for (int j = i; j <= size2; j++) {
385: testListIteratorAdd(createFilledLinkedListModel(), i, j);
386: }
387: }
388: }
389:
390: /**
391: * Tests the list iterator's remove operation on different observable lists.
392: */
393: public void testListIteratorRemove() {
394: testListIteratorRemove(createEmptyArrayListModel(), 0, 0);
395: testListIteratorRemove(createEmptyLinkedListModel(), 0, 0);
396:
397: testListIteratorRemove(createSingleElementArrayListModel(), 0,
398: 0);
399: testListIteratorRemove(createSingleElementArrayListModel(), 0,
400: 1);
401: testListIteratorRemove(createSingleElementArrayListModel(), 1,
402: 1);
403: testListIteratorRemove(createSingleElementLinkedListModel(), 0,
404: 0);
405: testListIteratorRemove(createSingleElementLinkedListModel(), 0,
406: 1);
407: testListIteratorRemove(createSingleElementLinkedListModel(), 1,
408: 1);
409:
410: int size1 = createFilledArrayListModel().size();
411: for (int i = 0; i < size1; i++) {
412: for (int j = i; j < size1; j++) {
413: testListIteratorRemove(createFilledArrayListModel(), i,
414: j);
415: }
416: }
417: int size2 = createFilledLinkedListModel().size();
418: for (int i = 0; i < size2; i++) {
419: for (int j = i; j < size2; j++) {
420: testListIteratorRemove(createFilledLinkedListModel(),
421: i, j);
422: }
423: }
424: }
425:
426: /**
427: * Tests the iterator's remove operation on different observable lists.
428: */
429: public void testIteratorRemove() {
430: testIteratorRemove(createEmptyArrayListModel(), 0);
431: testIteratorRemove(createEmptyLinkedListModel(), 0);
432:
433: testIteratorRemove(createSingleElementArrayListModel(), 0);
434: testIteratorRemove(createSingleElementArrayListModel(), 1);
435: testIteratorRemove(createSingleElementLinkedListModel(), 0);
436: testIteratorRemove(createSingleElementLinkedListModel(), 1);
437:
438: int size1 = createFilledArrayListModel().size();
439: for (int i = 0; i < size1; i++) {
440: testIteratorRemove(createFilledArrayListModel(), i);
441: }
442: int size2 = createFilledLinkedListModel().size();
443: for (int i = 0; i < size2; i++) {
444: testIteratorRemove(createFilledLinkedListModel(), i);
445: }
446: }
447:
448: // Test the Event Listener List ******************************************
449:
450: public void testGetListDataListenersWithoutListeners() {
451: ListDataListener[] listeners = createEmptyArrayListModel()
452: .getListDataListeners();
453: assertEquals(
454: "The list of ArrayListModel's ListDataListeners is empty.",
455: 0, listeners.length);
456: listeners = createEmptyLinkedListModel().getListDataListeners();
457: assertEquals(
458: "The list of LinkedListModel's ListDataListeners is empty.",
459: 0, listeners.length);
460: }
461:
462: // Test Implementations **************************************************
463:
464: private void testAdd(ObservableList<String> list) {
465: ListDataReport listDataReport = new ListDataReport();
466: list.addListDataListener(listDataReport);
467:
468: list.add("OneAdded");
469: assertEquals("An element has been added.", 1, listDataReport
470: .eventCountAdd());
471: assertEquals("One event has been fired.", 1, listDataReport
472: .eventCount());
473:
474: list.add("TwoAdded");
475: assertEquals("Another element has been added.", 2,
476: listDataReport.eventCountAdd());
477: assertEquals("Another event has been fired.", 2, listDataReport
478: .eventCountAdd());
479: }
480:
481: private void testAddIndexed(ObservableList<String> list, int index) {
482: ListDataReport listDataReport = new ListDataReport();
483: list.addListDataListener(listDataReport);
484:
485: list.add(index, "OneAdded");
486: assertEquals("An element has been added.", 1, listDataReport
487: .eventCountAdd());
488: assertEquals("One event has been fired.", 1, listDataReport
489: .eventCount());
490: assertEquals("Proper index0 (first add).", index,
491: listDataReport.lastEvent().getIndex0());
492: assertEquals("Proper index1 (first add).", index,
493: listDataReport.lastEvent().getIndex1());
494:
495: list.add(index, "TwoAdded");
496: assertEquals("Another element has been added.", 2,
497: listDataReport.eventCountAdd());
498: assertEquals("Another event has been fired.", 2, listDataReport
499: .eventCount());
500: assertEquals("Proper index0 (second add).", index,
501: listDataReport.lastEvent().getIndex0());
502: assertEquals("Proper index1 (second add).", index,
503: listDataReport.lastEvent().getIndex1());
504: }
505:
506: private <E> void testAddAll(ObservableList<E> list,
507: List<E> additions) {
508: ListDataReport listDataReport = new ListDataReport();
509: list.addListDataListener(listDataReport);
510:
511: int addCount = additions.size();
512: int eventCount = additions.isEmpty() ? 0 : 1;
513: int addIndex0 = list.size();
514: int addIndex1 = addIndex0 + addCount - 1;
515: list.addAll(additions);
516: assertEquals("Elements have been added.", eventCount,
517: listDataReport.eventCountAdd());
518: assertEquals("One event has been fired.", eventCount,
519: listDataReport.eventCount());
520: if (!additions.isEmpty()) {
521: assertEquals("Proper index0 (first addAll).", addIndex0,
522: listDataReport.lastEvent().getIndex0());
523: assertEquals("Proper index1 (first addAll).", addIndex1,
524: listDataReport.lastEvent().getIndex1());
525: }
526:
527: addIndex0 = list.size();
528: addIndex1 = addIndex0 + addCount - 1;
529: list.addAll(additions);
530: assertEquals("More elements have been added.", 2 * eventCount,
531: listDataReport.eventCountAdd());
532: assertEquals("Another event has been fired.", 2 * eventCount,
533: listDataReport.eventCountAdd());
534: if (!additions.isEmpty()) {
535: assertEquals("Proper index0 (second addAll).", addIndex0,
536: listDataReport.lastEvent().getIndex0());
537: assertEquals("Proper index1 (second addAll).", addIndex1,
538: listDataReport.lastEvent().getIndex1());
539: }
540: }
541:
542: private <E> void testAddAllIndexed(ObservableList<E> list,
543: List<E> additions, int index) {
544: ListDataReport listDataReport = new ListDataReport();
545: list.addListDataListener(listDataReport);
546:
547: int addCount = additions.size();
548: int eventCount = additions.isEmpty() ? 0 : 1;
549: int addIndex0 = index;
550: int addIndex1 = addIndex0 + addCount - 1;
551: list.addAll(index, additions);
552: assertEquals("Elements have been added.", eventCount,
553: listDataReport.eventCountAdd());
554: assertEquals("One event has been fired.", eventCount,
555: listDataReport.eventCount());
556: if (!additions.isEmpty()) {
557: assertEquals("Proper index0 (first addAll).", addIndex0,
558: listDataReport.lastEvent().getIndex0());
559: assertEquals("Proper index1 (first addAll).", addIndex1,
560: listDataReport.lastEvent().getIndex1());
561: }
562:
563: addIndex0 = index;
564: addIndex1 = addIndex0 + addCount - 1;
565: list.addAll(index, additions);
566: assertEquals("More elements have been added.", 2 * eventCount,
567: listDataReport.eventCountAdd());
568: assertEquals("Another event has been fired.", 2 * eventCount,
569: listDataReport.eventCountAdd());
570: if (!additions.isEmpty()) {
571: assertEquals("Proper index0 (second addAll).", addIndex0,
572: listDataReport.lastEvent().getIndex0());
573: assertEquals("Proper index1 (second addAll).", addIndex1,
574: listDataReport.lastEvent().getIndex1());
575: }
576: }
577:
578: private <E> void testClear(ObservableList<E> list) {
579: ListDataReport listDataReport = new ListDataReport();
580: list.addListDataListener(listDataReport);
581:
582: int eventCount = list.isEmpty() ? 0 : 1;
583: int index0 = 0;
584: int index1 = list.size() - 1;
585: list.clear();
586: assertEquals("All elements have been removed.", eventCount,
587: listDataReport.eventCountRemove());
588: assertEquals("One event has been fired.", eventCount,
589: listDataReport.eventCount());
590: if (!list.isEmpty()) {
591: assertEquals("Proper index0 (first clear).", index0,
592: listDataReport.lastEvent().getIndex0());
593: assertEquals("Proper index1 (first clear).", index1,
594: listDataReport.lastEvent().getIndex1());
595: }
596:
597: list.clear();
598: assertEquals("No further elements have been removed.",
599: eventCount, listDataReport.eventCount());
600: }
601:
602: private <E> void testRemoveIndex(ObservableList<E> list, int index) {
603: ListDataReport listDataReport = new ListDataReport();
604: list.addListDataListener(listDataReport);
605:
606: if (index < 0 || index >= list.size()) {
607: try {
608: list.remove(index);
609: fail("IndexOutOfBoundsException expected when removing index "
610: + index);
611: } catch (IndexOutOfBoundsException e) {
612: // Do nothing; this is the expected behavior.
613: }
614: } else {
615: list.remove(index);
616: assertEquals("An element has been removed.", 1,
617: listDataReport.eventCountRemove());
618: assertEquals("One event has been fired.", 1, listDataReport
619: .eventCount());
620: assertEquals("Proper index0 (first remove).", index,
621: listDataReport.lastEvent().getIndex0());
622: assertEquals("Proper index1 (first remove).", index,
623: listDataReport.lastEvent().getIndex1());
624: }
625:
626: if (index < 0 || index >= list.size()) {
627: try {
628: list.remove(index);
629: fail("IndexOutOfBoundsException expected when removing index "
630: + index);
631: } catch (IndexOutOfBoundsException e) {
632: // Do nothing; this is the expected behavior.
633: }
634: } else {
635: list.remove(index);
636: assertEquals("Another element has been removed.", 2,
637: listDataReport.eventCountRemove());
638: assertEquals("Another event has been fired.", 2,
639: listDataReport.eventCount());
640: assertEquals("Proper index0 (second remove).", index,
641: listDataReport.lastEvent().getIndex0());
642: assertEquals("Proper index1 (second remove).", index,
643: listDataReport.lastEvent().getIndex1());
644: }
645: }
646:
647: private <E> void testRemoveObject(ObservableList<E> list, int index) {
648: ListDataReport listDataReport = new ListDataReport();
649: list.addListDataListener(listDataReport);
650:
651: boolean contained = (index >= 0) && (index < list.size());
652: Object object = contained ? list.get(index)
653: : "AnObjectNotContainedInTheCollection";
654:
655: boolean removed = list.remove(object);
656: assertEquals("Index != -1 iff the list changed.", contained,
657: removed);
658: if (removed) {
659: assertEquals("An element has been removed.", 1,
660: listDataReport.eventCountRemove());
661: assertEquals("One event has been fired.", 1, listDataReport
662: .eventCount());
663: assertEquals("Proper index0 (first remove).", index,
664: listDataReport.lastEvent().getIndex0());
665: assertEquals("Proper index1 (first remove).", index,
666: listDataReport.lastEvent().getIndex1());
667: }
668: }
669:
670: private <E> void testRemoveAll(ObservableList<E> list,
671: List<E> removals) {
672: ListDataReport listDataReport = new ListDataReport();
673: list.addListDataListener(listDataReport);
674:
675: List<Integer> indicesToRemove = new LinkedList<Integer>();
676: List<E> testRemoveList = new LinkedList<E>(list);
677: for (E e : removals) {
678: int index = testRemoveList.indexOf(e);
679: if (index != -1) {
680: indicesToRemove.add(Integer.valueOf(index));
681: testRemoveList.remove(index);
682: }
683: }
684:
685: int oldSize = list.size();
686: list.removeAll(removals);
687: int newSize = list.size();
688: int removedCount = oldSize - newSize;
689:
690: assertEquals(
691: "Removed element count and number of removal indices.",
692: removedCount, indicesToRemove.size());
693: assertEquals("Elements have been removed.", removedCount,
694: listDataReport.eventCountRemove());
695: assertEquals("One event has been fired.", removedCount,
696: listDataReport.eventCount());
697:
698: // Check the indices fired by the ObservableList
699: Iterator<Integer> indexIterator = indicesToRemove.iterator();
700: for (ListDataEvent event : listDataReport.eventList()) {
701: int index = indexIterator.next().intValue();
702: assertEquals("Proper index0 (removeAll).", index, event
703: .getIndex0());
704: assertEquals("Proper index1 (removeAll).", index, event
705: .getIndex1());
706: }
707: }
708:
709: private <E> void testRetainAll(ObservableList<E> list,
710: List<E> retains) {
711: ListDataReport listDataReport = new ListDataReport();
712: list.addListDataListener(listDataReport);
713:
714: int oldSize = list.size();
715: list.retainAll(retains);
716: int newSize = list.size();
717: int eventCount = oldSize - newSize;
718:
719: assertEquals("Elements have been removed.", eventCount,
720: listDataReport.eventCountRemove());
721: assertEquals("One event has been fired.", eventCount,
722: listDataReport.eventCount());
723: }
724:
725: private void testSet(ObservableList<String> list, int index) {
726: ListDataReport listDataReport = new ListDataReport();
727: list.addListDataListener(listDataReport);
728:
729: if (index >= list.size()) {
730: try {
731: list.set(index, "newValue1");
732: fail("IndexOutOfBoundsException expected when removing index "
733: + index);
734: } catch (IndexOutOfBoundsException e) {
735: // Do nothing; this is the expected behavior.
736: }
737: } else {
738: list.set(index, "newValue1");
739: assertEquals("An element has been changed.", 1,
740: listDataReport.eventCountChange());
741: assertEquals("One event has been fired.", 1, listDataReport
742: .eventCount());
743: assertEquals("Proper index0 (first change).", index,
744: listDataReport.lastEvent().getIndex0());
745: assertEquals("Proper index1 (first change).", index,
746: listDataReport.lastEvent().getIndex1());
747: }
748:
749: if (index >= list.size()) {
750: try {
751: list.set(index, "newValue2");
752: fail("IndexOutOfBoundsException expected when removing index "
753: + index);
754: } catch (IndexOutOfBoundsException e) {
755: // Do nothing; this is the expected behavior.
756: }
757: } else {
758: list.set(index, "newValue2");
759: assertEquals("Another element has been changed.", 2,
760: listDataReport.eventCountChange());
761: assertEquals("Another event has been fired.", 2,
762: listDataReport.eventCount());
763: assertEquals("Proper index0 (second change).", index,
764: listDataReport.lastEvent().getIndex0());
765: assertEquals("Proper index1 (second change).", index,
766: listDataReport.lastEvent().getIndex1());
767: }
768: }
769:
770: private void testListIteratorAdd(ObservableList<String> list,
771: int iteratorIndex, int elementIndex) {
772: ListDataReport listDataReport = new ListDataReport();
773: list.addListDataListener(listDataReport);
774:
775: ListIterator<String> listIterator = list
776: .listIterator(iteratorIndex);
777: int relativeElementIndex = elementIndex - iteratorIndex;
778: for (int i = 0; i < relativeElementIndex; i++) {
779: listIterator.next();
780: }
781: listIterator.add("Added by ListIterator");
782:
783: assertEquals("An element has been added.", 1, listDataReport
784: .eventCountAdd());
785: assertEquals("One event has been fired.", 1, listDataReport
786: .eventCount());
787: assertEquals("Proper index0.", elementIndex, listDataReport
788: .lastEvent().getIndex0());
789: assertEquals("Proper index1.", elementIndex, listDataReport
790: .lastEvent().getIndex1());
791: }
792:
793: private <E> void testListIteratorRemove(ObservableList<E> list,
794: int iteratorIndex, int elementIndex) {
795: ListDataReport listDataReport = new ListDataReport();
796: list.addListDataListener(listDataReport);
797:
798: ListIterator<E> listIterator = list.listIterator(iteratorIndex);
799: int relativeElementIndex = elementIndex - iteratorIndex;
800: for (int i = 0; i <= relativeElementIndex
801: && listIterator.hasNext(); i++) {
802: listIterator.next();
803: }
804: if (elementIndex < list.size()) {
805: listIterator.remove();
806: assertEquals("An element has been removed.", 1,
807: listDataReport.eventCountRemove());
808: assertEquals("One event has been fired.", 1, listDataReport
809: .eventCount());
810: assertEquals("Proper index0.", elementIndex, listDataReport
811: .lastEvent().getIndex0());
812: assertEquals("Proper index1.", elementIndex, listDataReport
813: .lastEvent().getIndex1());
814: }
815: }
816:
817: private <E> void testIteratorRemove(ObservableList<E> list,
818: int elementIndex) {
819: ListDataReport listDataReport = new ListDataReport();
820: list.addListDataListener(listDataReport);
821:
822: Iterator<E> iterator = list.iterator();
823: for (int i = 0; i <= elementIndex && iterator.hasNext(); i++) {
824: iterator.next();
825: }
826: if (elementIndex < list.size()) {
827: iterator.remove();
828: assertEquals("An element has been removed.", 1,
829: listDataReport.eventCountRemove());
830: assertEquals("One event has been fired.", 1, listDataReport
831: .eventCount());
832: assertEquals("Proper index0.", elementIndex, listDataReport
833: .lastEvent().getIndex0());
834: assertEquals("Proper index1.", elementIndex, listDataReport
835: .lastEvent().getIndex1());
836: }
837: }
838:
839: // Creating Observable Lists *********************************************
840:
841: private ArrayListModel<String> createEmptyArrayListModel() {
842: return new ArrayListModel<String>();
843: }
844:
845: private LinkedListModel<String> createEmptyLinkedListModel() {
846: return new LinkedListModel<String>();
847: }
848:
849: private ArrayListModel<String> createSingleElementArrayListModel() {
850: ArrayListModel<String> list = new ArrayListModel<String>();
851: list.add("One");
852: return list;
853: }
854:
855: private LinkedListModel<String> createSingleElementLinkedListModel() {
856: LinkedListModel<String> list = new LinkedListModel<String>();
857: list.add("One");
858: return list;
859: }
860:
861: private ArrayListModel<String> createFilledArrayListModel() {
862: ArrayListModel<String> list = new ArrayListModel<String>();
863: list.add("One");
864: list.add("Two");
865: list.add("Three");
866: list.add("Four");
867: list.add("Five");
868: list.add("Six");
869: return list;
870: }
871:
872: private LinkedListModel<String> createFilledLinkedListModel() {
873: LinkedListModel<String> list = new LinkedListModel<String>();
874: list.add("One");
875: list.add("Two");
876: list.add("Three");
877: list.add("Four");
878: list.add("Five");
879: list.add("Six");
880: return list;
881: }
882:
883: }
|