0001: /*
0002: * Copyright 2003-2004 The Apache Software Foundation
0003: *
0004: * Licensed under the Apache License, Version 2.0 (the "License");
0005: * you may not use this file except in compliance with the License.
0006: * You may obtain a copy of the License at
0007: *
0008: * http://www.apache.org/licenses/LICENSE-2.0
0009: *
0010: * Unless required by applicable law or agreed to in writing, software
0011: * distributed under the License is distributed on an "AS IS" BASIS,
0012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013: * See the License for the specific language governing permissions and
0014: * limitations under the License.
0015: */
0016: package org.apache.commons.events.observable;
0017:
0018: import java.util.ArrayList;
0019: import java.util.Collection;
0020: import java.util.Iterator;
0021: import java.util.List;
0022: import java.util.ListIterator;
0023: import java.util.SortedSet;
0024:
0025: import junit.framework.Assert;
0026:
0027: import org.apache.commons.collections.SortedBag;
0028: import org.apache.commons.events.observable.standard.StandardModificationHandler;
0029: import org.apache.commons.events.observable.standard.StandardModificationListener;
0030: import org.apache.commons.events.observable.standard.StandardPostModificationEvent;
0031: import org.apache.commons.events.observable.standard.StandardPostModificationListener;
0032: import org.apache.commons.events.observable.standard.StandardPreModificationEvent;
0033: import org.apache.commons.events.observable.standard.StandardPreModificationListener;
0034:
0035: /**
0036: * Helper for testing
0037: * {@link ObservedCollection} implementations.
0038: *
0039: * @since Commons Events 1.0
0040: * @version $Revision: 155443 $ $Date: 2005-02-26 06:19:51 -0700 (Sat, 26 Feb 2005) $
0041: *
0042: * @author Stephen Colebourne
0043: */
0044: public class ObservedTestHelper extends Assert {
0045:
0046: public static Object NONE = new Object();
0047:
0048: public static Integer FIVE = new Integer(5);
0049: public static Integer SIX = new Integer(6);
0050: public static Integer SEVEN = new Integer(7);
0051: public static Integer EIGHT = new Integer(8);
0052: public static Integer NINE = new Integer(9);
0053: public static List SIX_SEVEN_LIST = new ArrayList();
0054: static {
0055: SIX_SEVEN_LIST.add(SIX);
0056: SIX_SEVEN_LIST.add(SEVEN);
0057: }
0058:
0059: public static class Listener implements
0060: StandardModificationListener {
0061: public StandardPreModificationEvent preEvent = null;
0062: public StandardPostModificationEvent postEvent = null;
0063:
0064: public void modificationOccurring(
0065: StandardPreModificationEvent event) {
0066: this .preEvent = event;
0067: }
0068:
0069: public void modificationOccurred(
0070: StandardPostModificationEvent event) {
0071: this .postEvent = event;
0072: }
0073: }
0074:
0075: public static class PreListener implements
0076: StandardPreModificationListener {
0077: public StandardPreModificationEvent preEvent = null;
0078:
0079: public void modificationOccurring(
0080: StandardPreModificationEvent event) {
0081: this .preEvent = event;
0082: }
0083: }
0084:
0085: public static class PostListener implements
0086: StandardPostModificationListener {
0087: public StandardPostModificationEvent postEvent = null;
0088:
0089: public void modificationOccurred(
0090: StandardPostModificationEvent event) {
0091: this .postEvent = event;
0092: }
0093: }
0094:
0095: public static interface ObservedFactory {
0096: ObservableCollection createObservedCollection();
0097:
0098: ObservableCollection createObservedCollection(Object listener);
0099: }
0100:
0101: public static final Listener LISTENER = new Listener();
0102: public static final Listener LISTENER2 = new Listener();
0103: public static final PreListener PRE_LISTENER = new PreListener();
0104: public static final PostListener POST_LISTENER = new PostListener();
0105:
0106: public ObservedTestHelper() {
0107: super ();
0108: }
0109:
0110: //-----------------------------------------------------------------------
0111: public static void bulkTestObservedCollection(
0112: ObservedFactory factory) {
0113: doTestFactoryPlain(factory);
0114: doTestFactoryWithListener(factory);
0115: doTestFactoryWithPreListener(factory);
0116: doTestFactoryWithPostListener(factory);
0117: doTestFactoryWithHandler(factory);
0118: doTestFactoryWithObject(factory);
0119: doTestFactoryWithNull(factory);
0120:
0121: doTestAddRemoveGetPreListeners(factory);
0122: doTestAddRemoveGetPostListeners(factory);
0123:
0124: doTestAdd(factory);
0125: doTestAddAll(factory);
0126: doTestClear(factory);
0127: doTestRemove(factory);
0128: doTestRemoveAll(factory);
0129: doTestRetainAll(factory);
0130: doTestRemoveIterated(factory);
0131: }
0132:
0133: public static void bulkTestObservedSet(ObservedFactory factory) {
0134: assertTrue(factory.createObservedCollection() instanceof ObservableSet);
0135: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableSet);
0136: assertTrue(factory
0137: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableSet);
0138:
0139: bulkTestObservedCollection(factory);
0140: }
0141:
0142: public static void bulkTestObservedSortedSet(ObservedFactory factory) {
0143: assertTrue(factory.createObservedCollection() instanceof ObservableSortedSet);
0144: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableSortedSet);
0145: assertTrue(factory
0146: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableSortedSet);
0147:
0148: bulkTestObservedCollection(factory);
0149: doTestSubSet(factory);
0150: doTestHeadSet(factory);
0151: doTestTailSet(factory);
0152: }
0153:
0154: public static void bulkTestObservedList(ObservedFactory factory) {
0155: assertTrue(factory.createObservedCollection() instanceof ObservableList);
0156: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableList);
0157: assertTrue(factory
0158: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableList);
0159:
0160: bulkTestObservedCollection(factory);
0161: doTestAddIndexed(factory);
0162: doTestAddAllIndexed(factory);
0163: doTestRemoveIndexed(factory);
0164: doTestSetIndexed(factory);
0165: doTestAddIterated(factory);
0166: doTestSetIterated(factory);
0167: doTestRemoveListIterated(factory);
0168: doTestSubList(factory);
0169: }
0170:
0171: public static void bulkTestObservedBag(ObservedFactory factory) {
0172: assertTrue(factory.createObservedCollection() instanceof ObservableBag);
0173: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableBag);
0174: assertTrue(factory
0175: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableBag);
0176:
0177: bulkTestObservedCollection(factory);
0178: doTestAddNCopies(factory);
0179: doTestRemoveNCopies(factory);
0180: }
0181:
0182: public static void bulkTestObservedSortedBag(ObservedFactory factory) {
0183: assertTrue(factory.createObservedCollection() instanceof ObservableSortedBag);
0184: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableSortedBag);
0185: assertTrue(factory
0186: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableSortedBag);
0187:
0188: bulkTestObservedCollection(factory);
0189: doTestAddNCopies(factory);
0190: doTestRemoveNCopies(factory);
0191: }
0192:
0193: public static void bulkTestObservedBuffer(ObservedFactory factory) {
0194: assertTrue(factory.createObservedCollection() instanceof ObservableBuffer);
0195: assertTrue(factory.createObservedCollection(LISTENER) instanceof ObservableBuffer);
0196: assertTrue(factory
0197: .createObservedCollection(new StandardModificationHandler()) instanceof ObservableBuffer);
0198:
0199: bulkTestObservedCollection(factory);
0200: doTestRemoveNext(factory);
0201: }
0202:
0203: //-----------------------------------------------------------------------
0204: public static void doTestFactoryPlain(ObservedFactory factory) {
0205: ObservableCollection coll = factory.createObservedCollection();
0206:
0207: assertNotNull(coll.getHandler());
0208: assertEquals(StandardModificationHandler.class, coll
0209: .getHandler().getClass());
0210: assertEquals(0,
0211: coll.getHandler().getPreModificationListeners().length);
0212: assertEquals(0, coll.getHandler()
0213: .getPostModificationListeners().length);
0214: }
0215:
0216: public static void doTestFactoryWithPreListener(
0217: ObservedFactory factory) {
0218: ObservableCollection coll = factory
0219: .createObservedCollection(PRE_LISTENER);
0220:
0221: assertNotNull(coll.getHandler());
0222: assertEquals(StandardModificationHandler.class, coll
0223: .getHandler().getClass());
0224: assertEquals(1,
0225: coll.getHandler().getPreModificationListeners().length);
0226: assertEquals(0, coll.getHandler()
0227: .getPostModificationListeners().length);
0228: assertSame(PRE_LISTENER, coll.getHandler()
0229: .getPreModificationListeners()[0]);
0230:
0231: PRE_LISTENER.preEvent = null;
0232: coll.add(SIX);
0233: assertTrue(PRE_LISTENER.preEvent != null);
0234: }
0235:
0236: public static void doTestFactoryWithPostListener(
0237: ObservedFactory factory) {
0238: ObservableCollection coll = factory
0239: .createObservedCollection(POST_LISTENER);
0240:
0241: assertNotNull(coll.getHandler());
0242: assertEquals(StandardModificationHandler.class, coll
0243: .getHandler().getClass());
0244: assertEquals(0,
0245: coll.getHandler().getPreModificationListeners().length);
0246: assertEquals(1, coll.getHandler()
0247: .getPostModificationListeners().length);
0248: assertSame(POST_LISTENER, coll.getHandler()
0249: .getPostModificationListeners()[0]);
0250:
0251: POST_LISTENER.postEvent = null;
0252: coll.add(SIX);
0253: assertTrue(POST_LISTENER.postEvent != null);
0254: }
0255:
0256: public static void doTestFactoryWithListener(ObservedFactory factory) {
0257: ObservableCollection coll = factory
0258: .createObservedCollection(LISTENER);
0259:
0260: assertNotNull(coll.getHandler());
0261: assertEquals(StandardModificationHandler.class, coll
0262: .getHandler().getClass());
0263: assertEquals(1,
0264: coll.getHandler().getPreModificationListeners().length);
0265: assertEquals(1, coll.getHandler()
0266: .getPostModificationListeners().length);
0267: assertSame(LISTENER, coll.getHandler()
0268: .getPreModificationListeners()[0]);
0269: assertSame(LISTENER, coll.getHandler()
0270: .getPostModificationListeners()[0]);
0271:
0272: LISTENER.preEvent = null;
0273: LISTENER.postEvent = null;
0274: coll.add(SIX);
0275: assertTrue(LISTENER.preEvent != null);
0276: assertTrue(LISTENER.postEvent != null);
0277: }
0278:
0279: public static void doTestFactoryWithHandler(ObservedFactory factory) {
0280: StandardModificationHandler handler = new StandardModificationHandler();
0281: ObservableCollection coll = factory
0282: .createObservedCollection(handler);
0283:
0284: assertNotNull(coll.getHandler());
0285: assertSame(handler, coll.getHandler());
0286: assertEquals(0,
0287: coll.getHandler().getPreModificationListeners().length);
0288: assertEquals(0, coll.getHandler()
0289: .getPostModificationListeners().length);
0290: }
0291:
0292: public static void doTestFactoryWithObject(ObservedFactory factory) {
0293: try {
0294: factory.createObservedCollection(new Object());
0295: fail();
0296: } catch (IllegalArgumentException ex) {
0297: }
0298: }
0299:
0300: public static void doTestFactoryWithNull(ObservedFactory factory) {
0301: ObservableCollection coll = factory
0302: .createObservedCollection(null);
0303:
0304: assertNotNull(coll.getHandler());
0305: assertEquals(StandardModificationHandler.class, coll
0306: .getHandler().getClass());
0307: assertEquals(0,
0308: coll.getHandler().getPreModificationListeners().length);
0309: assertEquals(0, coll.getHandler()
0310: .getPostModificationListeners().length);
0311: }
0312:
0313: //-----------------------------------------------------------------------
0314: public static void doTestAddRemoveGetPreListeners(
0315: ObservedFactory factory) {
0316: ObservableCollection coll = factory.createObservedCollection();
0317:
0318: assertEquals(0,
0319: coll.getHandler().getPreModificationListeners().length);
0320: coll.getHandler().addPreModificationListener(LISTENER);
0321: assertEquals(1,
0322: coll.getHandler().getPreModificationListeners().length);
0323: assertSame(LISTENER, coll.getHandler()
0324: .getPreModificationListeners()[0]);
0325:
0326: coll.getHandler().addPreModificationListener(LISTENER2);
0327: assertEquals(2,
0328: coll.getHandler().getPreModificationListeners().length);
0329: assertSame(LISTENER, coll.getHandler()
0330: .getPreModificationListeners()[0]);
0331: assertSame(LISTENER2, coll.getHandler()
0332: .getPreModificationListeners()[1]);
0333:
0334: coll.getHandler().removePreModificationListener(LISTENER);
0335: assertEquals(1,
0336: coll.getHandler().getPreModificationListeners().length);
0337: assertSame(LISTENER2, coll.getHandler()
0338: .getPreModificationListeners()[0]);
0339:
0340: coll.getHandler().removePreModificationListener(LISTENER); // check no error if not present
0341: assertEquals(1,
0342: coll.getHandler().getPreModificationListeners().length);
0343: assertSame(LISTENER2, coll.getHandler()
0344: .getPreModificationListeners()[0]);
0345:
0346: coll.getHandler().removePreModificationListener(LISTENER2);
0347: assertEquals(0,
0348: coll.getHandler().getPreModificationListeners().length);
0349:
0350: try {
0351: coll.getHandler().addPreModificationListener(new Object());
0352: fail();
0353: } catch (ClassCastException ex) {
0354: }
0355: }
0356:
0357: public static void doTestAddRemoveGetPostListeners(
0358: ObservedFactory factory) {
0359: ObservableCollection coll = factory.createObservedCollection();
0360:
0361: assertEquals(0, coll.getHandler()
0362: .getPostModificationListeners().length);
0363: coll.getHandler().addPostModificationListener(LISTENER);
0364: assertEquals(1, coll.getHandler()
0365: .getPostModificationListeners().length);
0366: assertSame(LISTENER, coll.getHandler()
0367: .getPostModificationListeners()[0]);
0368:
0369: coll.getHandler().addPostModificationListener(LISTENER2);
0370: assertEquals(2, coll.getHandler()
0371: .getPostModificationListeners().length);
0372: assertSame(LISTENER, coll.getHandler()
0373: .getPostModificationListeners()[0]);
0374: assertSame(LISTENER2, coll.getHandler()
0375: .getPostModificationListeners()[1]);
0376:
0377: coll.getHandler().removePostModificationListener(LISTENER);
0378: assertEquals(1, coll.getHandler()
0379: .getPostModificationListeners().length);
0380: assertSame(LISTENER2, coll.getHandler()
0381: .getPostModificationListeners()[0]);
0382:
0383: coll.getHandler().removePostModificationListener(LISTENER); // check no error if not present
0384: assertEquals(1, coll.getHandler()
0385: .getPostModificationListeners().length);
0386: assertSame(LISTENER2, coll.getHandler()
0387: .getPostModificationListeners()[0]);
0388:
0389: coll.getHandler().removePostModificationListener(LISTENER2);
0390: assertEquals(0, coll.getHandler()
0391: .getPostModificationListeners().length);
0392:
0393: try {
0394: coll.getHandler().addPostModificationListener(new Object());
0395: fail();
0396: } catch (ClassCastException ex) {
0397: }
0398: }
0399:
0400: //-----------------------------------------------------------------------
0401: public static void doTestAdd(ObservedFactory factory) {
0402: ObservableCollection coll = factory
0403: .createObservedCollection(LISTENER);
0404:
0405: LISTENER.preEvent = null;
0406: LISTENER.postEvent = null;
0407: assertEquals(0, coll.size());
0408: coll.add(SIX);
0409: assertEquals(1, coll.size());
0410: checkPrePost(coll, ModificationEventType.ADD, -1, 1, SIX, null,
0411: false, 0, 1);
0412:
0413: LISTENER.preEvent = null;
0414: LISTENER.postEvent = null;
0415: assertEquals(1, coll.size());
0416: coll.add(SEVEN);
0417: assertEquals(2, coll.size());
0418: checkPrePost(coll, ModificationEventType.ADD, -1, 1, SEVEN,
0419: null, false, 1, 2);
0420:
0421: if (coll instanceof SortedSet == false
0422: && coll instanceof SortedBag == false) {
0423: LISTENER.preEvent = null;
0424: LISTENER.postEvent = null;
0425: assertEquals(2, coll.size());
0426: coll.add(SIX_SEVEN_LIST);
0427: assertEquals(3, coll.size());
0428: // pre - don't use checkPrePost as it checks based on Collection
0429: assertSame(SIX_SEVEN_LIST, LISTENER.preEvent
0430: .getChangeObject());
0431: assertEquals(1, LISTENER.preEvent.getChangeCollection()
0432: .size());
0433: assertSame(SIX_SEVEN_LIST, LISTENER.preEvent
0434: .getChangeCollection().iterator().next());
0435: // post - don't use checkPrePost as it checks based on Collection
0436: assertSame(SIX_SEVEN_LIST, LISTENER.postEvent
0437: .getChangeObject());
0438: assertEquals(1, LISTENER.postEvent.getChangeCollection()
0439: .size());
0440: assertSame(SIX_SEVEN_LIST, LISTENER.postEvent
0441: .getChangeCollection().iterator().next());
0442: assertEquals(2, LISTENER.postEvent.getPreSize());
0443: assertEquals(3, LISTENER.postEvent.getPostSize());
0444: }
0445: }
0446:
0447: //-----------------------------------------------------------------------
0448: public static void doTestAddIndexed(ObservedFactory factory) {
0449: ObservableList coll = (ObservableList) factory
0450: .createObservedCollection(LISTENER);
0451:
0452: coll.addAll(SIX_SEVEN_LIST);
0453: LISTENER.preEvent = null;
0454: LISTENER.postEvent = null;
0455: assertEquals(2, coll.size());
0456: coll.add(1, EIGHT);
0457: assertEquals(3, coll.size());
0458: checkPrePost(coll, ModificationEventType.ADD_INDEXED, 1, 1,
0459: EIGHT, null, false, 2, 3);
0460: }
0461:
0462: //-----------------------------------------------------------------------
0463: public static void doTestAddNCopies(ObservedFactory factory) {
0464: ObservableBag coll = (ObservableBag) factory
0465: .createObservedCollection(LISTENER);
0466:
0467: coll.addAll(SIX_SEVEN_LIST);
0468: LISTENER.preEvent = null;
0469: LISTENER.postEvent = null;
0470: assertEquals(2, coll.size());
0471: coll.add(EIGHT, 3);
0472: assertEquals(5, coll.size());
0473: checkPrePost(coll, ModificationEventType.ADD_NCOPIES, -1, 3,
0474: EIGHT, null, false, 2, 5);
0475: }
0476:
0477: //-----------------------------------------------------------------------
0478: public static void doTestAddIterated(ObservedFactory factory) {
0479: ObservableList coll = (ObservableList) factory
0480: .createObservedCollection(LISTENER);
0481:
0482: coll.addAll(SIX_SEVEN_LIST);
0483: LISTENER.preEvent = null;
0484: LISTENER.postEvent = null;
0485: ListIterator it = coll.listIterator();
0486: assertEquals(2, coll.size());
0487: it.next();
0488: it.add(EIGHT);
0489: assertEquals(3, coll.size());
0490: checkPrePost(coll, ModificationEventType.ADD_ITERATED, 1, 1,
0491: EIGHT, null, false, 2, 3);
0492: }
0493:
0494: //-----------------------------------------------------------------------
0495: public static void doTestAddAll(ObservedFactory factory) {
0496: ObservableCollection coll = factory
0497: .createObservedCollection(LISTENER);
0498:
0499: LISTENER.preEvent = null;
0500: LISTENER.postEvent = null;
0501: assertEquals(0, coll.size());
0502: coll.addAll(SIX_SEVEN_LIST);
0503: assertEquals(2, coll.size());
0504: checkPrePost(coll, ModificationEventType.ADD_ALL, -1, 1,
0505: SIX_SEVEN_LIST, null, false, 0, 2);
0506: }
0507:
0508: //-----------------------------------------------------------------------
0509: public static void doTestAddAllIndexed(ObservedFactory factory) {
0510: ObservableList coll = (ObservableList) factory
0511: .createObservedCollection(LISTENER);
0512:
0513: coll.addAll(SIX_SEVEN_LIST);
0514: LISTENER.preEvent = null;
0515: LISTENER.postEvent = null;
0516: assertEquals(2, coll.size());
0517: coll.addAll(1, SIX_SEVEN_LIST);
0518: assertEquals(4, coll.size());
0519: checkPrePost(coll, ModificationEventType.ADD_ALL_INDEXED, 1, 1,
0520: SIX_SEVEN_LIST, null, false, 2, 4);
0521: }
0522:
0523: //-----------------------------------------------------------------------
0524: public static void doTestClear(ObservedFactory factory) {
0525: ObservableCollection coll = factory
0526: .createObservedCollection(LISTENER);
0527:
0528: coll.addAll(SIX_SEVEN_LIST);
0529: LISTENER.preEvent = null;
0530: LISTENER.postEvent = null;
0531: assertEquals(2, coll.size());
0532: coll.clear();
0533: assertEquals(0, coll.size());
0534: checkPrePost(coll, ModificationEventType.CLEAR, -1, 1, NONE,
0535: null, false, 2, 0);
0536:
0537: LISTENER.preEvent = null;
0538: LISTENER.postEvent = null;
0539: assertEquals(0, coll.size());
0540: coll.clear(); // already done this
0541: assertEquals(0, coll.size());
0542: assertTrue(LISTENER.preEvent != null);
0543: assertTrue(LISTENER.postEvent == null);
0544: }
0545:
0546: //-----------------------------------------------------------------------
0547: public static void doTestRemove(ObservedFactory factory) {
0548: ObservableCollection coll = factory
0549: .createObservedCollection(LISTENER);
0550:
0551: coll.addAll(SIX_SEVEN_LIST);
0552: LISTENER.preEvent = null;
0553: LISTENER.postEvent = null;
0554: assertEquals(2, coll.size());
0555: coll.remove(SEVEN);
0556: assertEquals(1, coll.size());
0557: checkPrePost(coll, ModificationEventType.REMOVE, -1, 1, SEVEN,
0558: SEVEN, false, 2, 1);
0559:
0560: LISTENER.preEvent = null;
0561: LISTENER.postEvent = null;
0562: assertEquals(1, coll.size());
0563: coll.remove(SEVEN); // already removed
0564: assertEquals(1, coll.size());
0565: assertTrue(LISTENER.preEvent != null);
0566: assertTrue(LISTENER.postEvent == null);
0567: }
0568:
0569: //-----------------------------------------------------------------------
0570: public static void doTestRemoveIndexed(ObservedFactory factory) {
0571: ObservableList coll = (ObservableList) factory
0572: .createObservedCollection(LISTENER);
0573:
0574: coll.addAll(SIX_SEVEN_LIST);
0575: LISTENER.preEvent = null;
0576: LISTENER.postEvent = null;
0577: assertEquals(2, coll.size());
0578: coll.remove(0);
0579: assertEquals(1, coll.size());
0580: checkPrePost(coll, ModificationEventType.REMOVE_INDEXED, 0, 1,
0581: NONE, SIX, false, 2, 1);
0582: }
0583:
0584: //-----------------------------------------------------------------------
0585: public static void doTestRemoveNCopies(ObservedFactory factory) {
0586: ObservableBag coll = (ObservableBag) factory
0587: .createObservedCollection(LISTENER);
0588:
0589: coll.add(SIX, 6);
0590: coll.add(SEVEN, 7);
0591: LISTENER.preEvent = null;
0592: LISTENER.postEvent = null;
0593: assertEquals(13, coll.size());
0594: coll.remove(SEVEN, 3);
0595: assertEquals(10, coll.size());
0596: checkPrePost(coll, ModificationEventType.REMOVE_NCOPIES, -1, 3,
0597: SEVEN, SEVEN, false, 13, 10);
0598: }
0599:
0600: //-----------------------------------------------------------------------
0601: public static void doTestRemoveNext(ObservedFactory factory) {
0602: ObservableBuffer coll = (ObservableBuffer) factory
0603: .createObservedCollection(LISTENER);
0604:
0605: coll.add(SIX);
0606: coll.add(SEVEN);
0607: LISTENER.preEvent = null;
0608: LISTENER.postEvent = null;
0609: assertEquals(2, coll.size());
0610: coll.remove();
0611: assertEquals(1, coll.size());
0612: checkPre(coll, ModificationEventType.REMOVE_NEXT, -1, 1, NONE,
0613: null, 2, null, -1);
0614: checkPost(coll, ModificationEventType.REMOVE_NEXT, -1, 1,
0615: SEVEN, SEVEN, 2, 1, null, -1);
0616: }
0617:
0618: //-----------------------------------------------------------------------
0619: public static void doTestRemoveIterated(ObservedFactory factory) {
0620: ObservableCollection coll = factory
0621: .createObservedCollection(LISTENER);
0622:
0623: coll.addAll(SIX_SEVEN_LIST);
0624: LISTENER.preEvent = null;
0625: LISTENER.postEvent = null;
0626: assertEquals(2, coll.size());
0627: Iterator it = coll.iterator();
0628: it.next();
0629: Object removed = it.next(); // store remove as iterator order may vary
0630: it.remove();
0631: assertEquals(1, coll.size());
0632: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 1, 1,
0633: removed, removed, true, 2, 1);
0634:
0635: LISTENER.preEvent = null;
0636: LISTENER.postEvent = null;
0637: assertEquals(1, coll.size());
0638: coll.remove(removed); // already removed
0639: assertEquals(1, coll.size());
0640: assertTrue(LISTENER.preEvent != null);
0641: assertTrue(LISTENER.postEvent == null);
0642: }
0643:
0644: public static void doTestRemoveListIterated(ObservedFactory factory) {
0645: ObservableList coll = (ObservableList) factory
0646: .createObservedCollection(LISTENER);
0647:
0648: coll.addAll(SIX_SEVEN_LIST);
0649: LISTENER.preEvent = null;
0650: LISTENER.postEvent = null;
0651: assertEquals(2, coll.size());
0652: ListIterator it = coll.listIterator();
0653: it.next();
0654: it.next();
0655: it.remove();
0656: assertEquals(1, coll.size());
0657: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 1, 1,
0658: SEVEN, SEVEN, true, 2, 1);
0659:
0660: LISTENER.preEvent = null;
0661: LISTENER.postEvent = null;
0662: assertEquals(1, coll.size());
0663: coll.remove(SEVEN); // already removed
0664: assertEquals(1, coll.size());
0665: assertTrue(LISTENER.preEvent != null);
0666: assertTrue(LISTENER.postEvent == null);
0667: }
0668:
0669: //-----------------------------------------------------------------------
0670: public static void doTestRemoveAll(ObservedFactory factory) {
0671: ObservableCollection coll = factory
0672: .createObservedCollection(LISTENER);
0673:
0674: coll.add(EIGHT);
0675: coll.addAll(SIX_SEVEN_LIST);
0676: LISTENER.preEvent = null;
0677: LISTENER.postEvent = null;
0678: assertEquals(3, coll.size());
0679: coll.removeAll(SIX_SEVEN_LIST);
0680: assertEquals(1, coll.size());
0681: checkPrePost(coll, ModificationEventType.REMOVE_ALL, -1, 1,
0682: SIX_SEVEN_LIST, null, false, 3, 1);
0683:
0684: LISTENER.preEvent = null;
0685: LISTENER.postEvent = null;
0686: assertEquals(1, coll.size());
0687: coll.removeAll(SIX_SEVEN_LIST); // already done this
0688: assertEquals(1, coll.size());
0689: assertTrue(LISTENER.preEvent != null);
0690: assertTrue(LISTENER.postEvent == null);
0691: }
0692:
0693: //-----------------------------------------------------------------------
0694: public static void doTestRetainAll(ObservedFactory factory) {
0695: ObservableCollection coll = factory
0696: .createObservedCollection(LISTENER);
0697:
0698: coll.add(EIGHT);
0699: coll.addAll(SIX_SEVEN_LIST);
0700: LISTENER.preEvent = null;
0701: LISTENER.postEvent = null;
0702: assertEquals(3, coll.size());
0703: coll.retainAll(SIX_SEVEN_LIST);
0704: assertEquals(2, coll.size());
0705: checkPrePost(coll, ModificationEventType.RETAIN_ALL, -1, 1,
0706: SIX_SEVEN_LIST, null, false, 3, 2);
0707:
0708: LISTENER.preEvent = null;
0709: LISTENER.postEvent = null;
0710: assertEquals(2, coll.size());
0711: coll.retainAll(SIX_SEVEN_LIST); // already done this
0712: assertEquals(2, coll.size());
0713: assertTrue(LISTENER.preEvent != null);
0714: assertTrue(LISTENER.postEvent == null);
0715: }
0716:
0717: //-----------------------------------------------------------------------
0718: public static void doTestSetIndexed(ObservedFactory factory) {
0719: ObservableList coll = (ObservableList) factory
0720: .createObservedCollection(LISTENER);
0721:
0722: coll.addAll(SIX_SEVEN_LIST);
0723: LISTENER.preEvent = null;
0724: LISTENER.postEvent = null;
0725: assertEquals(2, coll.size());
0726: coll.set(0, EIGHT);
0727: assertEquals(2, coll.size());
0728: checkPrePost(coll, ModificationEventType.SET_INDEXED, 0, 1,
0729: EIGHT, SIX, false, 2, 2);
0730: }
0731:
0732: //-----------------------------------------------------------------------
0733: public static void doTestSetIterated(ObservedFactory factory) {
0734: ObservableList coll = (ObservableList) factory
0735: .createObservedCollection(LISTENER);
0736:
0737: coll.addAll(SIX_SEVEN_LIST);
0738: LISTENER.preEvent = null;
0739: LISTENER.postEvent = null;
0740: ListIterator it = coll.listIterator();
0741: assertEquals(2, coll.size());
0742: it.next();
0743: it.next();
0744: it.set(EIGHT);
0745: assertEquals(2, coll.size());
0746: checkPrePost(coll, ModificationEventType.SET_ITERATED, 1, 1,
0747: EIGHT, SEVEN, true, 2, 2);
0748: }
0749:
0750: //-----------------------------------------------------------------------
0751: public static void doTestSubList(ObservedFactory factory) {
0752: ObservableList coll = (ObservableList) factory
0753: .createObservedCollection(LISTENER);
0754:
0755: coll.addAll(SIX_SEVEN_LIST);
0756: coll.add(EIGHT);
0757: coll.addAll(SIX_SEVEN_LIST);
0758: List subList = coll.subList(1, 4);
0759:
0760: LISTENER.preEvent = null;
0761: LISTENER.postEvent = null;
0762: assertEquals(3, subList.size());
0763: subList.add(EIGHT);
0764: assertEquals(4, subList.size());
0765: checkPrePost(coll, ModificationEventType.ADD, -1, 1, EIGHT,
0766: null, false, 5, 6, subList, 1);
0767:
0768: LISTENER.preEvent = null;
0769: LISTENER.postEvent = null;
0770: assertEquals(4, subList.size());
0771: subList.add(1, EIGHT);
0772: assertEquals(5, subList.size());
0773: checkPrePost(coll, ModificationEventType.ADD_INDEXED, 2, 1,
0774: EIGHT, null, false, 6, 7, subList, 1);
0775:
0776: LISTENER.preEvent = null;
0777: LISTENER.postEvent = null;
0778: assertEquals(5, subList.size());
0779: subList.set(3, SEVEN);
0780: assertEquals(5, subList.size());
0781: checkPrePost(coll, ModificationEventType.SET_INDEXED, 4, 1,
0782: SEVEN, SIX, false, 7, 7, subList, 1);
0783:
0784: LISTENER.preEvent = null;
0785: LISTENER.postEvent = null;
0786: assertEquals(5, subList.size());
0787: ListIterator it = subList.listIterator();
0788: it.next();
0789: it.remove();
0790: assertEquals(4, subList.size());
0791: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 1, 1,
0792: SEVEN, SEVEN, true, 7, 6, subList, 1);
0793:
0794: LISTENER.preEvent = null;
0795: LISTENER.postEvent = null;
0796: assertEquals(4, subList.size());
0797: it = subList.listIterator();
0798: it.next();
0799: it.next();
0800: it.next();
0801: it.set(EIGHT);
0802: assertEquals(4, subList.size());
0803: checkPrePost(coll, ModificationEventType.SET_ITERATED, 3, 1,
0804: EIGHT, SEVEN, true, 6, 6, subList, 1);
0805:
0806: LISTENER.preEvent = null;
0807: LISTENER.postEvent = null;
0808: assertEquals(4, subList.size());
0809: subList.clear();
0810: assertEquals(0, subList.size());
0811: checkPrePost(coll, ModificationEventType.CLEAR, -1, 1, NONE,
0812: null, false, 6, 2, subList, 1);
0813: }
0814:
0815: //-----------------------------------------------------------------------
0816: public static void doTestSubSet(ObservedFactory factory) {
0817: ObservableSortedSet coll = (ObservableSortedSet) factory
0818: .createObservedCollection(LISTENER);
0819:
0820: coll.add(SIX);
0821: coll.add(EIGHT);
0822: coll.add(NINE);
0823: SortedSet subSet = coll.subSet(SIX, NINE);
0824:
0825: LISTENER.preEvent = null;
0826: LISTENER.postEvent = null;
0827: assertEquals(2, subSet.size());
0828: subSet.add(SEVEN);
0829: assertEquals(3, subSet.size());
0830: checkPrePost(coll, ModificationEventType.ADD, -1, 1, SEVEN,
0831: null, false, 3, 4, subSet, -1);
0832:
0833: LISTENER.preEvent = null;
0834: LISTENER.postEvent = null;
0835: assertEquals(3, subSet.size());
0836: subSet.add(SEVEN);
0837: assertEquals(3, subSet.size());
0838: // post
0839: assertSame(null, LISTENER.postEvent);
0840:
0841: LISTENER.preEvent = null;
0842: LISTENER.postEvent = null;
0843: assertEquals(3, subSet.size());
0844: subSet.remove(SEVEN);
0845: assertEquals(2, subSet.size());
0846: checkPrePost(coll, ModificationEventType.REMOVE, -1, 1, SEVEN,
0847: SEVEN, false, 4, 3, subSet, -1);
0848:
0849: LISTENER.preEvent = null;
0850: LISTENER.postEvent = null;
0851: assertEquals(2, subSet.size());
0852: Iterator it = subSet.iterator();
0853: it.next();
0854: it.remove();
0855: assertEquals(1, subSet.size());
0856: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 0, 1,
0857: SIX, SIX, true, 3, 2, subSet, -1);
0858:
0859: LISTENER.preEvent = null;
0860: LISTENER.postEvent = null;
0861: assertEquals(1, subSet.size());
0862: subSet.clear();
0863: assertEquals(0, subSet.size());
0864: checkPrePost(coll, ModificationEventType.CLEAR, -1, 1, NONE,
0865: null, false, 2, 1, subSet, -1);
0866: }
0867:
0868: //-----------------------------------------------------------------------
0869: public static void doTestHeadSet(ObservedFactory factory) {
0870: ObservableSortedSet coll = (ObservableSortedSet) factory
0871: .createObservedCollection(LISTENER);
0872:
0873: coll.add(SIX);
0874: coll.add(EIGHT);
0875: coll.add(NINE);
0876: SortedSet subSet = coll.headSet(NINE);
0877:
0878: LISTENER.preEvent = null;
0879: LISTENER.postEvent = null;
0880: assertEquals(2, subSet.size());
0881: subSet.add(SEVEN);
0882: assertEquals(3, subSet.size());
0883: checkPrePost(coll, ModificationEventType.ADD, -1, 1, SEVEN,
0884: null, false, 3, 4, subSet, -1);
0885:
0886: LISTENER.preEvent = null;
0887: LISTENER.postEvent = null;
0888: assertEquals(3, subSet.size());
0889: subSet.add(SEVEN);
0890: assertEquals(3, subSet.size());
0891: // post
0892: assertSame(null, LISTENER.postEvent);
0893:
0894: LISTENER.preEvent = null;
0895: LISTENER.postEvent = null;
0896: assertEquals(3, subSet.size());
0897: subSet.remove(SEVEN);
0898: assertEquals(2, subSet.size());
0899: checkPrePost(coll, ModificationEventType.REMOVE, -1, 1, SEVEN,
0900: SEVEN, false, 4, 3, subSet, -1);
0901:
0902: LISTENER.preEvent = null;
0903: LISTENER.postEvent = null;
0904: assertEquals(2, subSet.size());
0905: Iterator it = subSet.iterator();
0906: it.next();
0907: it.remove();
0908: assertEquals(1, subSet.size());
0909: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 0, 1,
0910: SIX, SIX, true, 3, 2, subSet, -1);
0911:
0912: LISTENER.preEvent = null;
0913: LISTENER.postEvent = null;
0914: assertEquals(1, subSet.size());
0915: subSet.clear();
0916: assertEquals(0, subSet.size());
0917: checkPrePost(coll, ModificationEventType.CLEAR, -1, 1, NONE,
0918: null, false, 2, 1, subSet, -1);
0919: }
0920:
0921: //-----------------------------------------------------------------------
0922: public static void doTestTailSet(ObservedFactory factory) {
0923: ObservableSortedSet coll = (ObservableSortedSet) factory
0924: .createObservedCollection(LISTENER);
0925:
0926: coll.add(FIVE);
0927: coll.add(SIX);
0928: coll.add(EIGHT);
0929: SortedSet subSet = coll.tailSet(SIX);
0930:
0931: LISTENER.preEvent = null;
0932: LISTENER.postEvent = null;
0933: assertEquals(2, subSet.size());
0934: subSet.add(SEVEN);
0935: assertEquals(3, subSet.size());
0936: checkPrePost(coll, ModificationEventType.ADD, -1, 1, SEVEN,
0937: null, false, 3, 4, subSet, -1);
0938:
0939: LISTENER.preEvent = null;
0940: LISTENER.postEvent = null;
0941: assertEquals(3, subSet.size());
0942: subSet.add(SEVEN);
0943: assertEquals(3, subSet.size());
0944: // post
0945: assertSame(null, LISTENER.postEvent);
0946:
0947: LISTENER.preEvent = null;
0948: LISTENER.postEvent = null;
0949: assertEquals(3, subSet.size());
0950: subSet.remove(SEVEN);
0951: assertEquals(2, subSet.size());
0952: checkPrePost(coll, ModificationEventType.REMOVE, -1, 1, SEVEN,
0953: SEVEN, false, 4, 3, subSet, -1);
0954:
0955: LISTENER.preEvent = null;
0956: LISTENER.postEvent = null;
0957: assertEquals(2, subSet.size());
0958: Iterator it = subSet.iterator();
0959: it.next();
0960: it.remove();
0961: assertEquals(1, subSet.size());
0962: checkPrePost(coll, ModificationEventType.REMOVE_ITERATED, 0, 1,
0963: SIX, SIX, true, 3, 2, subSet, -1);
0964:
0965: LISTENER.preEvent = null;
0966: LISTENER.postEvent = null;
0967: assertEquals(1, subSet.size());
0968: subSet.clear();
0969: assertEquals(0, subSet.size());
0970: checkPrePost(coll, ModificationEventType.CLEAR, -1, 1, NONE,
0971: null, false, 2, 1, subSet, -1);
0972: }
0973:
0974: //-----------------------------------------------------------------------
0975: protected static void checkPrePost(ObservableCollection coll,
0976: int type, int changeIndex, int changeRepeat,
0977: Object changeObject, Object previous, boolean previousPre,
0978: int preSize, int postSize) {
0979: checkPrePost(coll, type, changeIndex, changeRepeat,
0980: changeObject, previous, previousPre, preSize, postSize,
0981: null, -1);
0982: }
0983:
0984: protected static void checkPrePost(ObservableCollection coll,
0985: int type, int changeIndex, int changeRepeat,
0986: Object changeObject, Object previous, boolean previousPre,
0987: int preSize, int postSize, Collection view, int viewOffset) {
0988:
0989: checkPre(coll, type, changeIndex, changeRepeat, changeObject,
0990: (previousPre ? previous : null), preSize, view,
0991: viewOffset);
0992: checkPost(coll, type, changeIndex, changeRepeat, changeObject,
0993: previous, preSize, postSize, view, viewOffset);
0994: }
0995:
0996: protected static void checkPre(ObservableCollection coll, int type,
0997: int changeIndex, int changeRepeat, Object changeObject,
0998: Object previous, int preSize, Collection view,
0999: int viewOffset) {
1000:
1001: assertSame(coll, LISTENER.preEvent.getObservedCollection());
1002: assertSame(coll.getHandler(), LISTENER.preEvent.getHandler());
1003: assertEquals(
1004: false,
1005: LISTENER.preEvent.getBaseCollection() instanceof ObservableCollection);
1006: assertEquals(type, LISTENER.preEvent.getType());
1007: assertEquals(changeIndex, LISTENER.preEvent.getChangeIndex());
1008: assertEquals(changeRepeat, LISTENER.preEvent.getChangeRepeat());
1009: if (changeObject == NONE) {
1010: assertSame(null, LISTENER.preEvent.getChangeObject());
1011: assertEquals(0, LISTENER.preEvent.getChangeCollection()
1012: .size());
1013: } else if (changeObject instanceof Collection) {
1014: assertSame(changeObject, LISTENER.preEvent
1015: .getChangeObject());
1016: assertSame(changeObject, LISTENER.preEvent
1017: .getChangeCollection());
1018: } else {
1019: assertSame(changeObject, LISTENER.preEvent
1020: .getChangeObject());
1021: assertEquals(1, LISTENER.preEvent.getChangeCollection()
1022: .size());
1023: assertSame(changeObject, LISTENER.preEvent
1024: .getChangeCollection().iterator().next());
1025: }
1026: assertSame(previous, LISTENER.preEvent.getPrevious());
1027: assertEquals(preSize, LISTENER.preEvent.getPreSize());
1028: assertEquals((view != null), LISTENER.preEvent.isView());
1029: assertEquals(viewOffset, LISTENER.preEvent.getViewOffset());
1030: assertSame(view, LISTENER.preEvent.getView());
1031: }
1032:
1033: protected static void checkPost(ObservableCollection coll,
1034: int type, int changeIndex, int changeRepeat,
1035: Object changeObject, Object previous, int preSize,
1036: int postSize, Collection view, int viewOffset) {
1037:
1038: // post
1039: assertSame(coll, LISTENER.postEvent.getObservedCollection());
1040: assertSame(coll.getHandler(), LISTENER.postEvent.getHandler());
1041: assertEquals(
1042: false,
1043: LISTENER.postEvent.getBaseCollection() instanceof ObservableCollection);
1044: assertEquals(type, LISTENER.postEvent.getType());
1045: assertEquals(changeIndex, LISTENER.postEvent.getChangeIndex());
1046: assertEquals(changeRepeat, LISTENER.postEvent.getChangeRepeat());
1047: if (changeObject == NONE) {
1048: assertSame(null, LISTENER.postEvent.getChangeObject());
1049: assertEquals(0, LISTENER.postEvent.getChangeCollection()
1050: .size());
1051: } else if (changeObject instanceof Collection) {
1052: assertSame(changeObject, LISTENER.postEvent
1053: .getChangeObject());
1054: assertSame(changeObject, LISTENER.postEvent
1055: .getChangeCollection());
1056: } else {
1057: assertSame(changeObject, LISTENER.postEvent
1058: .getChangeObject());
1059: assertEquals(1, LISTENER.postEvent.getChangeCollection()
1060: .size());
1061: assertSame(changeObject, LISTENER.postEvent
1062: .getChangeCollection().iterator().next());
1063: }
1064: assertSame(previous, LISTENER.postEvent.getPrevious());
1065: assertEquals(preSize, LISTENER.postEvent.getPreSize());
1066: assertEquals(postSize, LISTENER.postEvent.getPostSize());
1067: assertEquals(postSize != preSize, LISTENER.postEvent
1068: .isSizeChanged());
1069: assertEquals((view != null), LISTENER.postEvent.isView());
1070: assertEquals(viewOffset, LISTENER.postEvent.getViewOffset());
1071: assertSame(view, LISTENER.postEvent.getView());
1072:
1073: switch (type) {
1074: case ModificationEventType.ADD:
1075: case ModificationEventType.ADD_ALL:
1076: case ModificationEventType.ADD_ALL_INDEXED:
1077: case ModificationEventType.ADD_INDEXED:
1078: case ModificationEventType.ADD_ITERATED:
1079: case ModificationEventType.ADD_NCOPIES:
1080: assertEquals(true, LISTENER.preEvent.isTypeAdd());
1081: assertEquals(true, LISTENER.postEvent.isTypeAdd());
1082: break;
1083: default:
1084: assertEquals(false, LISTENER.preEvent.isTypeAdd());
1085: assertEquals(false, LISTENER.postEvent.isTypeAdd());
1086: break;
1087: }
1088: switch (type) {
1089: case ModificationEventType.REMOVE:
1090: case ModificationEventType.REMOVE_ALL:
1091: case ModificationEventType.REMOVE_INDEXED:
1092: case ModificationEventType.REMOVE_ITERATED:
1093: case ModificationEventType.REMOVE_NCOPIES:
1094: case ModificationEventType.REMOVE_NEXT:
1095: case ModificationEventType.RETAIN_ALL:
1096: case ModificationEventType.CLEAR:
1097: assertEquals(true, LISTENER.preEvent.isTypeReduce());
1098: assertEquals(true, LISTENER.postEvent.isTypeReduce());
1099: break;
1100: default:
1101: assertEquals(false, LISTENER.preEvent.isTypeReduce());
1102: assertEquals(false, LISTENER.postEvent.isTypeReduce());
1103: break;
1104: }
1105: switch (type) {
1106: case ModificationEventType.SET_INDEXED:
1107: case ModificationEventType.SET_ITERATED:
1108: assertEquals(true, LISTENER.preEvent.isTypeChange());
1109: assertEquals(true, LISTENER.postEvent.isTypeChange());
1110: break;
1111: default:
1112: assertEquals(false, LISTENER.preEvent.isTypeChange());
1113: assertEquals(false, LISTENER.postEvent.isTypeChange());
1114: break;
1115: }
1116: switch (type) {
1117: case ModificationEventType.ADD_ALL:
1118: case ModificationEventType.ADD_ALL_INDEXED:
1119: case ModificationEventType.REMOVE_ALL:
1120: case ModificationEventType.RETAIN_ALL:
1121: case ModificationEventType.CLEAR:
1122: assertEquals(true, LISTENER.preEvent.isTypeBulk());
1123: assertEquals(true, LISTENER.postEvent.isTypeBulk());
1124: break;
1125: default:
1126: assertEquals(false, LISTENER.preEvent.isTypeBulk());
1127: assertEquals(false, LISTENER.postEvent.isTypeBulk());
1128: break;
1129: }
1130: }
1131:
1132: }
|