From 8c3f191d5bc8ec86ad020dd077a4fab53b50693e Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 6 Sep 2003 16:53:23 +0000 Subject: [PATCH] Test ObservedBag and ObservedListIterator git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131136 13f79535-47bb-0310-9956-ffa450edef68 --- .../standard/StandardModificationEvent.java | 6 +- .../observed/ObservedTestHelper.java | 178 +++++++++++++++++- 2 files changed, 177 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/commons/collections/observed/standard/StandardModificationEvent.java b/src/java/org/apache/commons/collections/observed/standard/StandardModificationEvent.java index cc0506020..b9c7889af 100644 --- a/src/java/org/apache/commons/collections/observed/standard/StandardModificationEvent.java +++ b/src/java/org/apache/commons/collections/observed/standard/StandardModificationEvent.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/observed/standard/Attic/StandardModificationEvent.java,v 1.1 2003/09/03 23:54:26 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/observed/standard/Attic/StandardModificationEvent.java,v 1.2 2003/09/06 16:53:23 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -78,7 +78,7 @@ import org.apache.commons.collections.observed.ModificationHandler; * All objects used are the real objects from the method calls, not clones. * * @since Commons Collections 3.0 - * @version $Revision: 1.1 $ $Date: 2003/09/03 23:54:26 $ + * @version $Revision: 1.2 $ $Date: 2003/09/06 16:53:23 $ * * @author Stephen Colebourne */ @@ -167,6 +167,8 @@ public class StandardModificationEvent extends ModificationEvent { public Collection getChangeCollection() { if (object == null) { return Collections.EMPTY_LIST; + } else if (type == ModificationEventType.ADD_NCOPIES || type == ModificationEventType.REMOVE_NCOPIES) { + return Collections.singletonList(object); } else if (isType(ModificationEventType.GROUP_BULK)) { if (object instanceof Collection) { return (Collection) object; diff --git a/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java b/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java index 0b3fd05ac..7dc94a2a3 100644 --- a/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java +++ b/src/test/org/apache/commons/collections/observed/ObservedTestHelper.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/observed/Attic/ObservedTestHelper.java,v 1.1 2003/09/03 23:54:25 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/observed/Attic/ObservedTestHelper.java,v 1.2 2003/09/06 16:53:23 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -60,10 +60,10 @@ package org.apache.commons.collections.observed; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import junit.framework.Assert; -import org.apache.commons.collections.observed.ModificationEventType; import org.apache.commons.collections.observed.standard.StandardModificationEvent; import org.apache.commons.collections.observed.standard.StandardModificationHandler; import org.apache.commons.collections.observed.standard.StandardModificationListener; @@ -75,7 +75,7 @@ import org.apache.commons.collections.observed.standard.StandardPreModificationL * {@link ObservedCollection} implementations. * * @since Commons Collections 3.0 - * @version $Revision: 1.1 $ $Date: 2003/09/03 23:54:25 $ + * @version $Revision: 1.2 $ $Date: 2003/09/06 16:53:23 $ * * @author Stephen Colebourne */ @@ -173,7 +173,8 @@ public class ObservedTestHelper { doTestAddAllIndexed(factory); doTestRemoveIndexed(factory); doTestSetIndexed(factory); - // TODO: ITERATOR add/set + doTestIteratorAdd(factory); + doTestIteratorSet(factory); } public static void bulkTestObservedBag(ObservedFactory factory) { @@ -182,7 +183,8 @@ public class ObservedTestHelper { Assert.assertTrue(factory.createObservedCollection(new StandardModificationHandler()) instanceof ObservedBag); bulkTestObservedCollection(factory); - // TODO: bag nCopies + doTestAddNCopies(factory); + doTestRemoveNCopies(factory); } //----------------------------------------------------------------------- @@ -470,6 +472,88 @@ public class ObservedTestHelper { Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged()); } + //----------------------------------------------------------------------- + public static void doTestAddNCopies(ObservedFactory factory) { + ObservedBag coll = (ObservedBag) factory.createObservedCollection(LISTENER); + + coll.addAll(SIX_SEVEN_LIST); + LISTENER.preEvent = null; + LISTENER.postEvent = null; + Assert.assertEquals(2, coll.size()); + coll.add(EIGHT, 3); + Assert.assertEquals(5, coll.size()); + // pre + Assert.assertSame(coll, LISTENER.preEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.preEvent.getHandler()); + Assert.assertEquals(ModificationEventType.ADD_NCOPIES, LISTENER.preEvent.getType()); + Assert.assertEquals(-1, LISTENER.preEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.preEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.preEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(3, LISTENER.preEvent.getChangeRepeat()); + Assert.assertSame(null, LISTENER.preEvent.getResult()); + Assert.assertEquals(2, LISTENER.preEvent.getPreSize()); + Assert.assertEquals(2, LISTENER.preEvent.getPostSize()); + Assert.assertEquals(0, LISTENER.preEvent.getSizeChange()); + Assert.assertEquals(false, LISTENER.preEvent.isSizeChanged()); + // post + Assert.assertSame(coll, LISTENER.postEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.postEvent.getHandler()); + Assert.assertEquals(ModificationEventType.ADD_NCOPIES, LISTENER.postEvent.getType()); + Assert.assertEquals(-1, LISTENER.postEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(3, LISTENER.postEvent.getChangeRepeat()); + Assert.assertSame(Boolean.TRUE, LISTENER.postEvent.getResult()); + Assert.assertEquals(2, LISTENER.postEvent.getPreSize()); + Assert.assertEquals(5, LISTENER.postEvent.getPostSize()); + Assert.assertEquals(3, LISTENER.postEvent.getSizeChange()); + Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged()); + } + + //----------------------------------------------------------------------- + public static void doTestIteratorAdd(ObservedFactory factory) { + ObservedList coll = (ObservedList) factory.createObservedCollection(LISTENER); + + coll.addAll(SIX_SEVEN_LIST); + LISTENER.preEvent = null; + LISTENER.postEvent = null; + ListIterator it = coll.listIterator(); + Assert.assertEquals(2, coll.size()); + it.next(); + it.add(EIGHT); + Assert.assertEquals(3, coll.size()); + // pre + Assert.assertSame(coll, LISTENER.preEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.preEvent.getHandler()); + Assert.assertEquals(ModificationEventType.ADD_INDEXED, LISTENER.preEvent.getType()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.preEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.preEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeRepeat()); + Assert.assertSame(null, LISTENER.preEvent.getResult()); + Assert.assertEquals(2, LISTENER.preEvent.getPreSize()); + Assert.assertEquals(2, LISTENER.preEvent.getPostSize()); + Assert.assertEquals(0, LISTENER.preEvent.getSizeChange()); + Assert.assertEquals(false, LISTENER.preEvent.isSizeChanged()); + // post + Assert.assertSame(coll, LISTENER.postEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.postEvent.getHandler()); + Assert.assertEquals(ModificationEventType.ADD_INDEXED, LISTENER.postEvent.getType()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeRepeat()); + Assert.assertSame(null, LISTENER.postEvent.getResult()); + Assert.assertEquals(2, LISTENER.postEvent.getPreSize()); + Assert.assertEquals(3, LISTENER.postEvent.getPostSize()); + Assert.assertEquals(1, LISTENER.postEvent.getSizeChange()); + Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged()); + } + //----------------------------------------------------------------------- public static void doTestAddAll(ObservedFactory factory) { ObservedCollection coll = factory.createObservedCollection(LISTENER); @@ -677,6 +761,47 @@ public class ObservedTestHelper { Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged()); } + //----------------------------------------------------------------------- + public static void doTestRemoveNCopies(ObservedFactory factory) { + ObservedBag coll = (ObservedBag) factory.createObservedCollection(LISTENER); + + coll.add(SIX, 6); + coll.add(SEVEN, 7); + LISTENER.preEvent = null; + LISTENER.postEvent = null; + Assert.assertEquals(13, coll.size()); + coll.remove(SEVEN, 3); + Assert.assertEquals(10, coll.size()); + // pre + Assert.assertSame(coll, LISTENER.preEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.preEvent.getHandler()); + Assert.assertEquals(ModificationEventType.REMOVE_NCOPIES, LISTENER.preEvent.getType()); + Assert.assertEquals(-1, LISTENER.preEvent.getChangeIndex()); + Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(3, LISTENER.postEvent.getChangeRepeat()); + Assert.assertSame(null, LISTENER.preEvent.getResult()); + Assert.assertEquals(13, LISTENER.preEvent.getPreSize()); + Assert.assertEquals(13, LISTENER.preEvent.getPostSize()); + Assert.assertEquals(0, LISTENER.preEvent.getSizeChange()); + Assert.assertEquals(false, LISTENER.preEvent.isSizeChanged()); + // post + Assert.assertSame(coll, LISTENER.postEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.postEvent.getHandler()); + Assert.assertEquals(ModificationEventType.REMOVE_NCOPIES, LISTENER.postEvent.getType()); + Assert.assertEquals(-1, LISTENER.postEvent.getChangeIndex()); + Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(SEVEN, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(3, LISTENER.postEvent.getChangeRepeat()); + Assert.assertSame(Boolean.TRUE, LISTENER.postEvent.getResult()); + Assert.assertEquals(13, LISTENER.postEvent.getPreSize()); + Assert.assertEquals(10, LISTENER.postEvent.getPostSize()); + Assert.assertEquals(-3, LISTENER.postEvent.getSizeChange()); + Assert.assertEquals(true, LISTENER.postEvent.isSizeChanged()); + } + //----------------------------------------------------------------------- public static void doTestRemoveAll(ObservedFactory factory) { ObservedCollection coll = factory.createObservedCollection(LISTENER); @@ -862,4 +987,47 @@ public class ObservedTestHelper { Assert.assertEquals(false, LISTENER.postEvent.isSizeChanged()); } + //----------------------------------------------------------------------- + public static void doTestIteratorSet(ObservedFactory factory) { + ObservedList coll = (ObservedList) factory.createObservedCollection(LISTENER); + + coll.addAll(SIX_SEVEN_LIST); + LISTENER.preEvent = null; + LISTENER.postEvent = null; + ListIterator it = coll.listIterator(); + Assert.assertEquals(2, coll.size()); + it.next(); + it.next(); + it.set(EIGHT); + Assert.assertEquals(2, coll.size()); + // pre + Assert.assertSame(coll, LISTENER.preEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.preEvent.getHandler()); + Assert.assertEquals(ModificationEventType.SET_INDEXED, LISTENER.preEvent.getType()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeRepeat()); + Assert.assertSame(null, LISTENER.preEvent.getResult()); + Assert.assertEquals(2, LISTENER.preEvent.getPreSize()); + Assert.assertEquals(2, LISTENER.preEvent.getPostSize()); + Assert.assertEquals(0, LISTENER.preEvent.getSizeChange()); + Assert.assertEquals(false, LISTENER.preEvent.isSizeChanged()); + // post + Assert.assertSame(coll, LISTENER.postEvent.getSourceCollection()); + Assert.assertSame(coll.getHandler(), LISTENER.postEvent.getHandler()); + Assert.assertEquals(ModificationEventType.SET_INDEXED, LISTENER.postEvent.getType()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeIndex()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeObject()); + Assert.assertEquals(1, LISTENER.preEvent.getChangeCollection().size()); + Assert.assertSame(EIGHT, LISTENER.postEvent.getChangeCollection().iterator().next()); + Assert.assertEquals(1, LISTENER.postEvent.getChangeRepeat()); + Assert.assertSame(SEVEN, LISTENER.postEvent.getResult()); + Assert.assertEquals(2, LISTENER.postEvent.getPreSize()); + Assert.assertEquals(2, LISTENER.postEvent.getPostSize()); + Assert.assertEquals(0, LISTENER.postEvent.getSizeChange()); + Assert.assertEquals(false, LISTENER.postEvent.isSizeChanged()); + } + }