From cca7a505a3fa58d0859be101362ceda668700dee Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Fri, 31 Jul 2015 19:39:18 +1200 Subject: [PATCH] No effective change - code cleanup - while to foreach on BeanList etc --- .../java/com/avaje/ebean/common/BeanList.java | 24 +- .../java/com/avaje/ebean/common/BeanMap.java | 15 +- .../java/com/avaje/ebean/common/BeanSet.java | 42 ++-- .../avaje/ebean/common/ModifyCollection.java | 30 ++- .../com/avaje/ebean/common/ModifyList.java | 10 +- .../com/avaje/ebean/common/BeanListTest.java | 233 ++++++++++++++++++ .../com/avaje/ebean/common/BeanMapTest.java | 194 +++++++++++++++ .../com/avaje/ebean/common/BeanSetTest.java | 233 ++++++++++++++++++ 8 files changed, 723 insertions(+), 58 deletions(-) create mode 100644 src/test/java/com/avaje/ebean/common/BeanListTest.java create mode 100644 src/test/java/com/avaje/ebean/common/BeanMapTest.java create mode 100644 src/test/java/com/avaje/ebean/common/BeanSetTest.java diff --git a/src/main/java/com/avaje/ebean/common/BeanList.java b/src/main/java/com/avaje/ebean/common/BeanList.java index ff90b581f..5805be523 100644 --- a/src/main/java/com/avaje/ebean/common/BeanList.java +++ b/src/main/java/com/avaje/ebean/common/BeanList.java @@ -343,41 +343,41 @@ public final class BeanList extends AbstractBeanCollection implements List return list.remove(o); } - public boolean removeAll(Collection c) { + public boolean removeAll(Collection beans) { checkReadOnly(); init(); if (modifyRemoveListening) { boolean changed = false; - Iterator it = c.iterator(); - while (it.hasNext()) { - Object o = it.next(); - if (list.remove(o)) { - modifyRemoval(o); + for (Object bean : beans) { + if (list.remove(bean)) { + // register this bean as having been removed + modifyRemoval(bean); changed = true; } } return changed; } - return list.removeAll(c); + return list.removeAll(beans); } - public boolean retainAll(Collection c) { + public boolean retainAll(Collection retainBeans) { checkReadOnly(); init(); if (modifyRemoveListening) { boolean changed = false; Iterator it = list.iterator(); while (it.hasNext()) { - Object o = it.next(); - if (!c.contains(o)) { + Object bean = it.next(); + if (!retainBeans.contains(bean)) { + // removing this bean it.remove(); - modifyRemoval(o); + modifyRemoval(bean); changed = true; } } return changed; } - return list.retainAll(c); + return list.retainAll(retainBeans); } public E set(int index, E element) { diff --git a/src/main/java/com/avaje/ebean/common/BeanMap.java b/src/main/java/com/avaje/ebean/common/BeanMap.java index 7afd7dc39..74b0aa1bf 100644 --- a/src/main/java/com/avaje/ebean/common/BeanMap.java +++ b/src/main/java/com/avaje/ebean/common/BeanMap.java @@ -195,9 +195,9 @@ public final class BeanMap extends AbstractBeanCollection implements Ma checkReadOnly(); initClear(); if (modifyRemoveListening) { - for (K key : map.keySet()) { - E o = map.remove(key); - modifyRemoval(o); + // add all beans to the removal list + for (E bean : map.values()) { + modifyRemoval(bean); } } map.clear(); @@ -249,9 +249,12 @@ public final class BeanMap extends AbstractBeanCollection implements Ma checkReadOnly(); init(); if (modifyListening) { - Object o = map.put(key, value); - modifyAddition(value); - modifyRemoval(o); + Object oldBean = map.put(key, value); + if (value != oldBean) { + // register the add of the new and the removal of the old + modifyAddition(value); + modifyRemoval(oldBean); + } } return map.put(key, value); } diff --git a/src/main/java/com/avaje/ebean/common/BeanSet.java b/src/main/java/com/avaje/ebean/common/BeanSet.java index f5ab59345..346db7272 100644 --- a/src/main/java/com/avaje/ebean/common/BeanSet.java +++ b/src/main/java/com/avaje/ebean/common/BeanSet.java @@ -192,32 +192,29 @@ public final class BeanSet extends AbstractBeanCollection implements Set c) { + public boolean addAll(Collection addCollection) { checkReadOnly(); init(); if (modifyAddListening) { boolean changed = false; - Iterator it = c.iterator(); - while (it.hasNext()) { - E o = it.next(); - if (set.add(o)) { - modifyAddition(o); + for (E bean : addCollection) { + if (set.add(bean)) { + // register the addition of the bean + modifyAddition(bean); changed = true; } } return changed; } - return set.addAll(c); + return set.addAll(addCollection); } public void clear() { checkReadOnly(); initClear(); if (modifyRemoveListening) { - Iterator it = set.iterator(); - while (it.hasNext()) { - E e = it.next(); - modifyRemoval(e); + for (E bean : set) { + modifyRemoval(bean); } } set.clear(); @@ -262,41 +259,40 @@ public final class BeanSet extends AbstractBeanCollection implements Set c) { + public boolean removeAll(Collection beans) { checkReadOnly(); init(); if (modifyRemoveListening) { boolean changed = false; - Iterator it = c.iterator(); - while (it.hasNext()) { - Object o = it.next(); - if (set.remove(o)) { - modifyRemoval(o); + for (Object bean : beans) { + if (set.remove(bean)) { + modifyRemoval(bean); changed = true; } } return changed; } - return set.removeAll(c); + return set.removeAll(beans); } - public boolean retainAll(Collection c) { + public boolean retainAll(Collection beans) { checkReadOnly(); init(); if (modifyRemoveListening) { boolean changed = false; Iterator it = set.iterator(); while (it.hasNext()) { - Object o = it.next(); - if (!c.contains(o)) { + Object bean = it.next(); + if (!beans.contains(bean)) { + // not retaining this bean so add it to the removal list it.remove(); - modifyRemoval(o); + modifyRemoval(bean); changed = true; } } return changed; } - return set.retainAll(c); + return set.retainAll(beans); } public int size() { diff --git a/src/main/java/com/avaje/ebean/common/ModifyCollection.java b/src/main/java/com/avaje/ebean/common/ModifyCollection.java index 0de249bef..c948be71b 100644 --- a/src/main/java/com/avaje/ebean/common/ModifyCollection.java +++ b/src/main/java/com/avaje/ebean/common/ModifyCollection.java @@ -40,9 +40,7 @@ class ModifyCollection implements Collection { public boolean addAll(Collection collection) { boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - E o = it.next(); + for (E o : collection) { if (c.add(o)) { owner.modifyAddition(o); changed = true; @@ -51,27 +49,33 @@ class ModifyCollection implements Collection { return changed; } + @Override public void clear() { c.clear(); } + @Override public boolean contains(Object o) { return c.contains(o); } + @Override public boolean containsAll(Collection collection) { return c.containsAll(collection); } + @Override public boolean isEmpty() { return c.isEmpty(); } + @Override public Iterator iterator() { Iterator it = c.iterator(); return new ModifyIterator(owner, it); } + @Override public boolean remove(Object o) { if (c.remove(o)) { owner.modifyRemoval(o); @@ -80,41 +84,45 @@ class ModifyCollection implements Collection { return false; } + @Override public boolean removeAll(Collection collection) { boolean changed = false; - Iterator it = collection.iterator(); - while (it.hasNext()) { - Object o = (Object) it.next(); - if (c.remove(o)) { - owner.modifyRemoval(o); + for (Object bean : collection) { + if (c.remove(bean)) { + owner.modifyRemoval(bean); changed = true; } } return changed; } + @Override public boolean retainAll(Collection collection) { boolean changed = false; Iterator it = c.iterator(); while (it.hasNext()) { - Object o = (Object) it.next(); - if (!collection.contains(o)) { + Object bean = it.next(); + if (!collection.contains(bean)) { + // not retaining this bean so add to removals it.remove(); - owner.modifyRemoval(o); + owner.modifyRemoval(bean); changed = true; } } return changed; } + @Override public int size() { return c.size(); } + @Override public Object[] toArray() { return c.toArray(); } + @Override public T[] toArray(T[] a) { return c.toArray(a); } diff --git a/src/main/java/com/avaje/ebean/common/ModifyList.java b/src/main/java/com/avaje/ebean/common/ModifyList.java index 96f2e4bb2..1a60dbaba 100644 --- a/src/main/java/com/avaje/ebean/common/ModifyList.java +++ b/src/main/java/com/avaje/ebean/common/ModifyList.java @@ -39,12 +39,10 @@ class ModifyList extends ModifyCollection implements List { owner.modifyAddition(element); } - public boolean addAll(int index, Collection co) { - if (list.addAll(index, co)) { - Iterator it = co.iterator(); - while (it.hasNext()) { - E o = it.next(); - owner.modifyAddition(o); + public boolean addAll(int index, Collection addCollection) { + if (list.addAll(index, addCollection)) { + for (E bean : addCollection) { + owner.modifyAddition(bean); } return true; } diff --git a/src/test/java/com/avaje/ebean/common/BeanListTest.java b/src/test/java/com/avaje/ebean/common/BeanListTest.java new file mode 100644 index 000000000..a9c036322 --- /dev/null +++ b/src/test/java/com/avaje/ebean/common/BeanListTest.java @@ -0,0 +1,233 @@ +package com.avaje.ebean.common; + +import com.avaje.ebean.bean.BeanCollection; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.*; + + +public class BeanListTest { + + Object object1 = new Object(); + Object object2 = new Object(); + Object object3 = new Object(); + + @NotNull + private List all() { + List all = new ArrayList(); + all.add(object1); + all.add(object2); + all.add(object3); + return all; + } + + @NotNull + private List some() { + List some = new ArrayList(); + some.add(object2); + some.add(object3); + return some; + } + + @Test + public void testAdd() throws Exception { + + BeanList list = new BeanList(); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.add(object1); + + assertThat(list.getModifyAdditions()).containsExactly(object1); + assertThat(list.getModifyRemovals()).isEmpty(); + + list.add(object1); + assertThat(list.getModifyAdditions()).containsExactly(object1); + + list.add(object2); + assertThat(list.getModifyAdditions()).containsExactly(object1, object2); + + list.remove(object1); + assertThat(list.getModifyAdditions()).containsExactly(object2); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddAll_given_emptyStart() throws Exception { + + BeanList list = new BeanList(); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.addAll(all()); + + assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAdd_given_someAlreadyIn() throws Exception { + + BeanList list = new BeanList(some()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + assertThat(list.contains(object1)).isFalse(); + list.add(object1); + assertThat(list.contains(object2)).isTrue(); + list.add(object2); // object2 added as List allows duplicates + + assertThat(list.getModifyAdditions()).containsExactly(object1, object2); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddSome_given_someAlreadyIn() throws Exception { + + BeanList list = new BeanList(some()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.addAll(all()); + + assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansInAdditions() throws Exception { + + BeanList list = new BeanList(); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.addAll(all()); + assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + list.remove(object2); + list.remove(object3); + + assertThat(list.getModifyAdditions()).containsExactly(object1); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemoveAll_given_beansInAdditions() throws Exception { + + BeanList list = new BeanList(); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.addAll(all()); + assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + list.removeAll(some()); + + assertThat(list.getModifyAdditions()).containsExactly(object1); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansNotInAdditions() throws Exception { + + BeanList list = new BeanList(all()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.remove(object2); + list.remove(object3); + + // assert + assertThat(list.getModifyAdditions()).isEmpty(); + assertThat(list.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testRemoveAll_given_beansNotInAdditions() throws Exception { + + BeanList list = new BeanList(all()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.removeAll(some()); + + // assert + assertThat(list.getModifyAdditions()).isEmpty(); + assertThat(list.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testClear() throws Exception { + + BeanList list = new BeanList(all()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.clear(); + + //assert + assertThat(list.getModifyRemovals()).containsExactly(object1, object2, object3); + assertThat(list.getModifyAdditions()).isEmpty(); + } + + @Test + public void testClear_given_someBeansInAdditions() throws Exception { + + BeanList list = new BeanList(); + list.add(object1); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.add(object2); + list.add(object3); + + // act + list.clear(); + + //assert + assertThat(list.getModifyRemovals()).containsExactly(object1); + assertThat(list.getModifyAdditions()).isEmpty(); + } + + @Test + public void testRetainAll_given_beansInAdditions() throws Exception { + + BeanList list = new BeanList(); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.addAll(all()); + assertThat(list.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + list.retainAll(some()); + + assertThat(list.getModifyAdditions()).containsExactly(object2, object3); + assertThat(list.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRetainAll_given_someBeansInAdditions() throws Exception { + + BeanList list = new BeanList(); + list.add(object1); + list.add(object2); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + list.add(object3); + + // act + list.retainAll(some()); + + assertThat(list.getModifyAdditions()).containsExactly(object3); + assertThat(list.getModifyRemovals()).containsExactly(object1); + } + + @Test + public void testRetainAll_given_noBeansInAdditions() throws Exception { + + BeanList list = new BeanList(all()); + list.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + list.retainAll(some()); + + assertThat(list.getModifyRemovals()).containsExactly(object1); + } + +} \ No newline at end of file diff --git a/src/test/java/com/avaje/ebean/common/BeanMapTest.java b/src/test/java/com/avaje/ebean/common/BeanMapTest.java new file mode 100644 index 000000000..b611887e7 --- /dev/null +++ b/src/test/java/com/avaje/ebean/common/BeanMapTest.java @@ -0,0 +1,194 @@ +package com.avaje.ebean.common; + +import com.avaje.ebean.bean.BeanCollection; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class BeanMapTest { + + Object object1 = new Object(); + Object object2 = new Object(); + Object object3 = new Object(); + + @NotNull + private Map all() { + Map all = new LinkedHashMap(); + all.put("1", object1); + all.put("2", object2); + all.put("3", object3); + return all; + } + + @NotNull + private Map some() { + Map all = new LinkedHashMap(); + all.put("2", object2); + all.put("3", object3); + return all; + } + + @Test + public void testAdd() throws Exception { + + BeanMap map = new BeanMap(); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + map.put("1", object1); + map.put("4", null); + + assertThat(map.getModifyAdditions()).containsExactly(object1); + assertThat(map.getModifyRemovals()).isEmpty(); + + map.put("1", object1); + map.put("4", null); + assertThat(map.getModifyAdditions()).containsExactly(object1); + + map.put("2", object2); + assertThat(map.getModifyAdditions()).containsExactly(object1, object2); + + map.remove("1"); + assertThat(map.getModifyAdditions()).containsExactly(object2); + assertThat(map.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddAll_given_emptyStart() throws Exception { + + BeanMap set = new BeanMap(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.putAll(all()); + + assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAdd_given_someAlreadyIn() throws Exception { + + BeanMap map = new BeanMap(some()); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + assertThat(map.values().contains(object1)).isFalse(); + map.put("1", object1); + assertThat(map.values().contains(object2)).isTrue(); + map.put("2", object2); + + assertThat(map.getModifyAdditions()).containsExactly(object1); + assertThat(map.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddSome_given_someAlreadyIn() throws Exception { + + BeanMap map = new BeanMap(some()); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + map.putAll(all()); + + assertThat(map.getModifyAdditions()).containsExactly(object1); + assertThat(map.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansInAdditions() throws Exception { + + BeanMap map = new BeanMap(); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + map.putAll(all()); + assertThat(map.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + map.remove("2"); + map.remove("3"); + + assertThat(map.getModifyAdditions()).containsExactly(object1); + assertThat(map.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemoveAll_given_beansInAdditions() throws Exception { + + BeanMap map = new BeanMap(); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + map.putAll(all()); + assertThat(map.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + map.remove("2"); + map.remove("3"); + + assertThat(map.getModifyAdditions()).containsExactly(object1); + assertThat(map.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansNotInAdditions() throws Exception { + + BeanMap map = new BeanMap(all()); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + map.remove("2"); + map.remove("3"); + + // assert + assertThat(map.getModifyAdditions()).isEmpty(); + assertThat(map.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testRemoveAll_given_beansNotInAdditions() throws Exception { + + BeanMap map = new BeanMap(all()); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + map.remove("2"); + map.remove("3"); + + // assert + assertThat(map.getModifyAdditions()).isEmpty(); + assertThat(map.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testClear() throws Exception { + + BeanMap map = new BeanMap(all()); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + map.clear(); + + //assert + assertThat(map.getModifyRemovals()).containsExactly(object1, object2, object3); + assertThat(map.getModifyAdditions()).isEmpty(); + } + + @Test + public void testClear_given_someBeansInAdditions() throws Exception { + + BeanMap map = new BeanMap(); + map.put("1",object1); + map.setModifyListening(BeanCollection.ModifyListenMode.ALL); + map.put("2", object2); + map.put("3", object3); + + // act + map.clear(); + + //assert + assertThat(map.getModifyRemovals()).containsExactly(object1); + assertThat(map.getModifyAdditions()).isEmpty(); + } + +} \ No newline at end of file diff --git a/src/test/java/com/avaje/ebean/common/BeanSetTest.java b/src/test/java/com/avaje/ebean/common/BeanSetTest.java new file mode 100644 index 000000000..9eddbde9a --- /dev/null +++ b/src/test/java/com/avaje/ebean/common/BeanSetTest.java @@ -0,0 +1,233 @@ +package com.avaje.ebean.common; + +import com.avaje.ebean.bean.BeanCollection; +import org.jetbrains.annotations.NotNull; +import org.junit.Test; + +import java.util.LinkedHashSet; +import java.util.Set; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class BeanSetTest { + + Object object1 = new Object(); + Object object2 = new Object(); + Object object3 = new Object(); + + @NotNull + private Set all() { + Set all = new LinkedHashSet(); + all.add(object1); + all.add(object2); + all.add(object3); + return all; + } + + @NotNull + private Set some() { + Set some = new LinkedHashSet(); + some.add(object2); + some.add(object3); + return some; + } + + @Test + public void testAdd() throws Exception { + + BeanSet set = new BeanSet(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.add(object1); + + assertThat(set.getModifyAdditions()).containsExactly(object1); + assertThat(set.getModifyRemovals()).isEmpty(); + + set.add(object1); + assertThat(set.getModifyAdditions()).containsExactly(object1); + + set.add(object2); + assertThat(set.getModifyAdditions()).containsExactly(object1, object2); + + set.remove(object1); + assertThat(set.getModifyAdditions()).containsExactly(object2); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddAll_given_emptyStart() throws Exception { + + BeanSet set = new BeanSet(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.addAll(all()); + + assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAdd_given_someAlreadyIn() throws Exception { + + BeanSet set = new BeanSet(some()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + assertThat(set.contains(object1)).isFalse(); + set.add(object1); + assertThat(set.contains(object2)).isTrue(); + set.add(object2); + + assertThat(set.getModifyAdditions()).containsExactly(object1); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testAddSome_given_someAlreadyIn() throws Exception { + + BeanSet set = new BeanSet(some()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.addAll(all()); + + assertThat(set.getModifyAdditions()).containsExactly(object1); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansInAdditions() throws Exception { + + BeanSet set = new BeanSet(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.addAll(all()); + assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + set.remove(object2); + set.remove(object3); + + assertThat(set.getModifyAdditions()).containsExactly(object1); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemoveAll_given_beansInAdditions() throws Exception { + + BeanSet set = new BeanSet(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.addAll(all()); + assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + set.removeAll(some()); + + assertThat(set.getModifyAdditions()).containsExactly(object1); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRemove_given_beansNotInAdditions() throws Exception { + + BeanSet set = new BeanSet(all()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.remove(object2); + set.remove(object3); + + // assert + assertThat(set.getModifyAdditions()).isEmpty(); + assertThat(set.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testRemoveAll_given_beansNotInAdditions() throws Exception { + + BeanSet set = new BeanSet(all()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.removeAll(some()); + + // assert + assertThat(set.getModifyAdditions()).isEmpty(); + assertThat(set.getModifyRemovals()).containsExactly(object2, object3); + } + + @Test + public void testClear() throws Exception { + + BeanSet set = new BeanSet(all()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.clear(); + + //assert + assertThat(set.getModifyRemovals()).containsExactly(object1, object2, object3); + assertThat(set.getModifyAdditions()).isEmpty(); + } + + @Test + public void testClear_given_someBeansInAdditions() throws Exception { + + BeanSet set = new BeanSet(); + set.add(object1); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.add(object2); + set.add(object3); + + // act + set.clear(); + + //assert + assertThat(set.getModifyRemovals()).containsExactly(object1); + assertThat(set.getModifyAdditions()).isEmpty(); + } + + @Test + public void testRetainAll_given_beansInAdditions() throws Exception { + + BeanSet set = new BeanSet(); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.addAll(all()); + assertThat(set.getModifyAdditions()).containsExactly(object1, object2, object3); + + // act + set.retainAll(some()); + + assertThat(set.getModifyAdditions()).containsExactly(object2, object3); + assertThat(set.getModifyRemovals()).isEmpty(); + } + + @Test + public void testRetainAll_given_someBeansInAdditions() throws Exception { + + BeanSet set = new BeanSet(); + set.add(object1); + set.add(object2); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + set.add(object3); + + // act + set.retainAll(some()); + + assertThat(set.getModifyAdditions()).containsExactly(object3); + assertThat(set.getModifyRemovals()).containsExactly(object1); + } + + @Test + public void testRetainAll_given_noBeansInAdditions() throws Exception { + + BeanSet set = new BeanSet(all()); + set.setModifyListening(BeanCollection.ModifyListenMode.ALL); + + // act + set.retainAll(some()); + + assertThat(set.getModifyRemovals()).containsExactly(object1); + } + +} \ No newline at end of file