You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:58:00 UTC

svn commit: r815146 [2/2] - /commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java

Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java?rev=815146&r1=815145&r2=815146&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestCollectionUtils.java Tue Sep 15 05:57:59 2009
@@ -16,31 +16,24 @@
  */
 package org.apache.commons.collections;
 
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
-import java.util.TreeMap;
-import java.util.Vector;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
+import static junit.framework.Assert.assertFalse;
+import static org.apache.commons.collections.functors.EqualPredicate.equalPredicate;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+import java.util.*;
 
 import org.apache.commons.collections.bag.HashBag;
 import org.apache.commons.collections.buffer.BoundedFifoBuffer;
-import org.apache.commons.collections.collection.AbstractTestCollection;
 import org.apache.commons.collections.collection.PredicatedCollection;
 import org.apache.commons.collections.collection.SynchronizedCollection;
 import org.apache.commons.collections.collection.TransformedCollection;
 import org.apache.commons.collections.collection.UnmodifiableCollection;
+import org.junit.Before;
+import org.junit.Test;
 
 /**
  * Tests for CollectionUtils.
@@ -52,83 +45,136 @@
  * @author Steven Melzer
  * @author Neil O'Toole
  * @author Stephen Smith
+ * @author Stephen Kestle
  *
  * @version $Revision$ $Date$
  */
-public class TestCollectionUtils extends TestCase {
-    
-    public TestCollectionUtils(String testName) {
-        super(testName);
-    }
-
-    public static Test suite() {
-        return new TestSuite(TestCollectionUtils.class);
-    }
-
+public class TestCollectionUtils extends MockTestCase {
     public static void main(String args[]) {
         String[] testCaseName = { TestCollectionUtils.class.getName() };
         junit.textui.TestRunner.main(testCaseName);
     }
 
-    private Collection collectionA = null;
-    private Collection collectionB = null;
+    /**
+     * Collection of {@link Integer}s
+     */
+    private List<Integer> collectionA = null;
 
-    public void setUp() {
-        collectionA = new ArrayList();
-        collectionA.add("a");
-        collectionA.add("b");
-        collectionA.add("b");
-        collectionA.add("c");
-        collectionA.add("c");
-        collectionA.add("c");
-        collectionA.add("d");
-        collectionA.add("d");
-        collectionA.add("d");
-        collectionA.add("d");
-        collectionB = new LinkedList();
-        collectionB.add("e");
-        collectionB.add("d");
-        collectionB.add("d");
-        collectionB.add("c");
-        collectionB.add("c");
-        collectionB.add("c");
-        collectionB.add("b");
-        collectionB.add("b");
-        collectionB.add("b");
-        collectionB.add("b");
+    /**
+     * Collection of {@link Long}s
+     */
+    private List<Long> collectionB = null;
 
-    }
+    /**
+     * Collection of {@link Integer}s that are equivalent to the Longs in
+     * collectionB.
+     */
+    private Collection<Integer> collectionC = null;
+
+    /**
+     * Collection of {@link Integer}s, bound as {@link Number}s
+     */
+    private Collection<Number> collectionA2 = null;
+
+    /**
+     * Collection of {@link Long}s, bound as {@link Number}s
+     */
+    private Collection<Number> collectionB2 = null;
+
+    /**
+     * Collection of {@link Integer}s (cast as {@link Number}s) that are
+     * equivalent to the Longs in collectionB.
+     */
+    private Collection<Number> collectionC2 = null;
+
+    private Iterable<Integer> iterableA = null;
+
+    private Iterable<Long> iterableB = null;
 
-    public void testGetCardinalityMap() {
-        Map freq = CollectionUtils.getCardinalityMap(collectionA);
-        assertEquals(new Integer(1),freq.get("a"));
-        assertEquals(new Integer(2),freq.get("b"));
-        assertEquals(new Integer(3),freq.get("c"));
-        assertEquals(new Integer(4),freq.get("d"));
-        assertNull(freq.get("e"));
-
-        freq = CollectionUtils.getCardinalityMap(collectionB);
-        assertNull(freq.get("a"));
-        assertEquals(new Integer(4),freq.get("b"));
-        assertEquals(new Integer(3),freq.get("c"));
-        assertEquals(new Integer(2),freq.get("d"));
-        assertEquals(new Integer(1),freq.get("e"));
-    }
-
-    public void testCardinality() {
-        assertEquals(1, CollectionUtils.cardinality("a", collectionA));
-        assertEquals(2, CollectionUtils.cardinality("b", collectionA));
-        assertEquals(3, CollectionUtils.cardinality("c", collectionA));
-        assertEquals(4, CollectionUtils.cardinality("d", collectionA));
-        assertEquals(0, CollectionUtils.cardinality("e", collectionA));
-
-        assertEquals(0, CollectionUtils.cardinality("a", collectionB));
-        assertEquals(4, CollectionUtils.cardinality("b", collectionB));
-        assertEquals(3, CollectionUtils.cardinality("c", collectionB));
-        assertEquals(2, CollectionUtils.cardinality("d", collectionB));
-        assertEquals(1, CollectionUtils.cardinality("e", collectionB));
+    private Iterable<Integer> iterableC = null;
 
-        Set set = new HashSet();
+    private Iterable<Number> iterableA2 = null;
+
+    private Iterable<Number> iterableB2 = null;
+
+    @Before
+    public void setUp() {
+        collectionA = new ArrayList<Integer>();
+        collectionA.add(1);
+        collectionA.add(2);
+        collectionA.add(2);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(3);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionA.add(4);
+        collectionB = new LinkedList<Long>();
+        collectionB.add(5L);
+        collectionB.add(4L);
+        collectionB.add(4L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(3L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+        collectionB.add(2L);
+
+        collectionC = new ArrayList<Integer>();
+        for (Long l : collectionB) {
+            collectionC.add(l.intValue());
+        }
+
+        iterableA = collectionA;
+        iterableB = collectionB;
+        iterableC = collectionC;
+        collectionA2 = new ArrayList<Number>(collectionA);
+        collectionB2 = new LinkedList<Number>(collectionB);
+        collectionC2 = new LinkedList<Number>(collectionC);
+        iterableA2 = collectionA2;
+        iterableB2 = collectionB2;
+    }
+
+    @Test
+    public void getCardinalityMap() {
+        Map<Number, Integer> freqA = CollectionUtils.<Number>getCardinalityMap(iterableA);
+        assertEquals(1, (int) freqA.get(1));
+        assertEquals(2, (int) freqA.get(2));
+        assertEquals(3, (int) freqA.get(3));
+        assertEquals(4, (int) freqA.get(4));
+        assertNull(freqA.get(5));
+
+        Map<Long, Integer> freqB = CollectionUtils.getCardinalityMap(iterableB);
+        assertNull(freqB.get(1L));
+        assertEquals(4, (int) freqB.get(2L));
+        assertEquals(3, (int) freqB.get(3L));
+        assertEquals(2, (int) freqB.get(4L));
+        assertEquals(1, (int) freqB.get(5L));
+    }
+
+    @Test
+    public void cardinality() {
+        assertEquals(1, CollectionUtils.cardinality(1, iterableA));
+        assertEquals(2, CollectionUtils.cardinality(2, iterableA));
+        assertEquals(3, CollectionUtils.cardinality(3, iterableA));
+        assertEquals(4, CollectionUtils.cardinality(4, iterableA));
+        assertEquals(0, CollectionUtils.cardinality(5, iterableA));
+
+        assertEquals(0, CollectionUtils.cardinality(1L, iterableB));
+        assertEquals(4, CollectionUtils.cardinality(2L, iterableB));
+        assertEquals(3, CollectionUtils.cardinality(3L, iterableB));
+        assertEquals(2, CollectionUtils.cardinality(4L, iterableB));
+        assertEquals(1, CollectionUtils.cardinality(5L, iterableB));
+
+        // Ensure that generic bounds accept valid parameters, but return
+        // expected results
+        // e.g. no longs in the "int" Iterable<Number>, and vice versa.
+        assertEquals(0, CollectionUtils.cardinality(2L, iterableA2));
+        assertEquals(0, CollectionUtils.cardinality(2, iterableB2));
+
+        Set<String> set = new HashSet<String>();
         set.add("A");
         set.add("C");
         set.add("E");
@@ -139,7 +185,7 @@
         assertEquals(0, CollectionUtils.cardinality("D", set));
         assertEquals(1, CollectionUtils.cardinality("E", set));
 
-        Bag bag = new HashBag();
+        Bag<String> bag = new HashBag<String>();
         bag.add("A", 3);
         bag.add("C");
         bag.add("E");
@@ -150,594 +196,479 @@
         assertEquals(0, CollectionUtils.cardinality("D", bag));
         assertEquals(2, CollectionUtils.cardinality("E", bag));
     }
-    
-    public void testCardinalityOfNull() {
-        List list = new ArrayList();
-        assertEquals(0,CollectionUtils.cardinality(null,list));
+
+    @Test
+    public void cardinalityOfNull() {
+        List<String> list = new ArrayList<String>();
+        assertEquals(0, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
             assertNull(freq.get(null));
         }
         list.add("A");
-        assertEquals(0,CollectionUtils.cardinality(null,list));
+        assertEquals(0, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
             assertNull(freq.get(null));
         }
         list.add(null);
-        assertEquals(1,CollectionUtils.cardinality(null,list));
+        assertEquals(1, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
-            assertEquals(new Integer(1),freq.get(null));
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
+            assertEquals(1, freq.get(null));
         }
         list.add("B");
-        assertEquals(1,CollectionUtils.cardinality(null,list));
+        assertEquals(1, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
-            assertEquals(new Integer(1),freq.get(null));
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
+            assertEquals(1, freq.get(null));
         }
         list.add(null);
-        assertEquals(2,CollectionUtils.cardinality(null,list));
+        assertEquals(2, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
-            assertEquals(new Integer(2),freq.get(null));
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
+            assertEquals(2, freq.get(null));
         }
         list.add("B");
-        assertEquals(2,CollectionUtils.cardinality(null,list));
+        assertEquals(2, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
-            assertEquals(new Integer(2),freq.get(null));
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
+            assertEquals(2, freq.get(null));
         }
         list.add(null);
-        assertEquals(3,CollectionUtils.cardinality(null,list));
+        assertEquals(3, CollectionUtils.cardinality(null, list));
         {
-            Map freq = CollectionUtils.getCardinalityMap(list);
-            assertEquals(new Integer(3),freq.get(null));
+            Map<String, Integer> freq = CollectionUtils.getCardinalityMap(list);
+            assertEquals(3, freq.get(null));
         }
     }
 
-    public void testContainsAny() {
-        Collection empty = new ArrayList(0);
-        Collection one = new ArrayList(1);
+    @Test
+    public void containsAny() {
+        Collection<String> empty = new ArrayList<String>(0);
+        Collection<String> one = new ArrayList<String>(1);
         one.add("1");
-        Collection two = new ArrayList(1);
+        Collection<String> two = new ArrayList<String>(1);
         two.add("2");
-        Collection three = new ArrayList(1);
+        Collection<String> three = new ArrayList<String>(1);
         three.add("3");
-        Collection odds = new ArrayList(2);
+        Collection<String> odds = new ArrayList<String>(2);
         odds.add("1");
         odds.add("3");
-        
-        assertTrue("containsAny({1},{1,3}) should return true.",
-            CollectionUtils.containsAny(one,odds));
-        assertTrue("containsAny({1,3},{1}) should return true.",
-            CollectionUtils.containsAny(odds,one));
-        assertTrue("containsAny({3},{1,3}) should return true.",
-            CollectionUtils.containsAny(three,odds));
-        assertTrue("containsAny({1,3},{3}) should return true.",
-            CollectionUtils.containsAny(odds,three));
-        assertTrue("containsAny({2},{2}) should return true.",
-            CollectionUtils.containsAny(two,two));
-        assertTrue("containsAny({1,3},{1,3}) should return true.",
-            CollectionUtils.containsAny(odds,odds));
-        
-        assertTrue("containsAny({2},{1,3}) should return false.",
-            !CollectionUtils.containsAny(two,odds));
-        assertTrue("containsAny({1,3},{2}) should return false.",
-            !CollectionUtils.containsAny(odds,two));
-        assertTrue("containsAny({1},{3}) should return false.",
-            !CollectionUtils.containsAny(one,three));
-        assertTrue("containsAny({3},{1}) should return false.",
-            !CollectionUtils.containsAny(three,one));
-        assertTrue("containsAny({1,3},{}) should return false.",
-            !CollectionUtils.containsAny(odds,empty));
-        assertTrue("containsAny({},{1,3}) should return false.",
-            !CollectionUtils.containsAny(empty,odds));
-        assertTrue("containsAny({},{}) should return false.",
-            !CollectionUtils.containsAny(empty,empty));
-    }
-
-    public void testUnion() {
-        Collection col = CollectionUtils.union(collectionA,collectionB);
-        Map freq = CollectionUtils.getCardinalityMap(col);
-        assertEquals(new Integer(1),freq.get("a"));
-        assertEquals(new Integer(4),freq.get("b"));
-        assertEquals(new Integer(3),freq.get("c"));
-        assertEquals(new Integer(4),freq.get("d"));
-        assertEquals(new Integer(1),freq.get("e"));
-
-        Collection col2 = CollectionUtils.union(collectionB,collectionA);
-        Map freq2 = CollectionUtils.getCardinalityMap(col2);
-        assertEquals(new Integer(1),freq2.get("a"));
-        assertEquals(new Integer(4),freq2.get("b"));
-        assertEquals(new Integer(3),freq2.get("c"));
-        assertEquals(new Integer(4),freq2.get("d"));
-        assertEquals(new Integer(1),freq2.get("e"));        
-    }
-
-    public void testIntersection() {
-        Collection col = CollectionUtils.intersection(collectionA,collectionB);
-        Map freq = CollectionUtils.getCardinalityMap(col);
-        assertNull(freq.get("a"));
-        assertEquals(new Integer(2),freq.get("b"));
-        assertEquals(new Integer(3),freq.get("c"));
-        assertEquals(new Integer(2),freq.get("d"));
-        assertNull(freq.get("e"));
-
-        Collection col2 = CollectionUtils.intersection(collectionB,collectionA);
-        Map freq2 = CollectionUtils.getCardinalityMap(col2);
-        assertNull(freq2.get("a"));
-        assertEquals(new Integer(2),freq2.get("b"));
-        assertEquals(new Integer(3),freq2.get("c"));
-        assertEquals(new Integer(2),freq2.get("d"));
-        assertNull(freq2.get("e"));      
-    }
-
-    public void testDisjunction() {
-        Collection col = CollectionUtils.disjunction(collectionA,collectionB);
-        Map freq = CollectionUtils.getCardinalityMap(col);
-        assertEquals(new Integer(1),freq.get("a"));
-        assertEquals(new Integer(2),freq.get("b"));
-        assertNull(freq.get("c"));
-        assertEquals(new Integer(2),freq.get("d"));
-        assertEquals(new Integer(1),freq.get("e"));
-
-        Collection col2 = CollectionUtils.disjunction(collectionB,collectionA);
-        Map freq2 = CollectionUtils.getCardinalityMap(col2);
-        assertEquals(new Integer(1),freq2.get("a"));
-        assertEquals(new Integer(2),freq2.get("b"));
-        assertNull(freq2.get("c"));
-        assertEquals(new Integer(2),freq2.get("d"));
-        assertEquals(new Integer(1),freq2.get("e"));
+
+        assertTrue("containsAny({1},{1,3}) should return true.", CollectionUtils.containsAny(one, odds));
+        assertTrue("containsAny({1,3},{1}) should return true.", CollectionUtils.containsAny(odds, one));
+        assertTrue("containsAny({3},{1,3}) should return true.", CollectionUtils.containsAny(three, odds));
+        assertTrue("containsAny({1,3},{3}) should return true.", CollectionUtils.containsAny(odds, three));
+        assertTrue("containsAny({2},{2}) should return true.", CollectionUtils.containsAny(two, two));
+        assertTrue("containsAny({1,3},{1,3}) should return true.", CollectionUtils.containsAny(odds, odds));
+
+        assertTrue("containsAny({2},{1,3}) should return false.", !CollectionUtils.containsAny(two, odds));
+        assertTrue("containsAny({1,3},{2}) should return false.", !CollectionUtils.containsAny(odds, two));
+        assertTrue("containsAny({1},{3}) should return false.", !CollectionUtils.containsAny(one, three));
+        assertTrue("containsAny({3},{1}) should return false.", !CollectionUtils.containsAny(three, one));
+        assertTrue("containsAny({1,3},{}) should return false.", !CollectionUtils.containsAny(odds, empty));
+        assertTrue("containsAny({},{1,3}) should return false.", !CollectionUtils.containsAny(empty, odds));
+        assertTrue("containsAny({},{}) should return false.", !CollectionUtils.containsAny(empty, empty));
+    }
+
+    @Test
+    public void union() {
+        Collection<Integer> col = CollectionUtils.union(iterableA, iterableC);
+        Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col);
+        assertEquals(1, freq.get(1));
+        assertEquals(4, freq.get(2));
+        assertEquals(3, freq.get(3));
+        assertEquals(4, freq.get(4));
+        assertEquals(1, freq.get(5));
+
+        Collection<Number> col2 = CollectionUtils.union(collectionC2, iterableA);
+        Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2);
+        assertEquals(1, freq2.get(1));
+        assertEquals(4, freq2.get(2));
+        assertEquals(3, freq2.get(3));
+        assertEquals(4, freq2.get(4));
+        assertEquals(1, freq2.get(5));
+    }
+
+    @Test
+    public void intersection() {
+        Collection<Integer> col = CollectionUtils.intersection(iterableA, iterableC);
+        Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col);
+        assertNull(freq.get(1));
+        assertEquals(2, freq.get(2));
+        assertEquals(3, freq.get(3));
+        assertEquals(2, freq.get(4));
+        assertNull(freq.get(5));
+
+        Collection<Number> col2 = CollectionUtils.intersection(collectionC2, collectionA);
+        Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2);
+        assertNull(freq2.get(1));
+        assertEquals(2, freq2.get(2));
+        assertEquals(3, freq2.get(3));
+        assertEquals(2, freq2.get(4));
+        assertNull(freq2.get(5));
+    }
+
+    @Test
+    public void disjunction() {
+        Collection<Integer> col = CollectionUtils.disjunction(iterableA, iterableC);
+        Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col);
+        assertEquals(1, freq.get(1));
+        assertEquals(2, freq.get(2));
+        assertNull(freq.get(3));
+        assertEquals(2, freq.get(4));
+        assertEquals(1, freq.get(5));
+
+        Collection<Number> col2 = CollectionUtils.disjunction(collectionC2, collectionA);
+        Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2);
+        assertEquals(1, freq2.get(1));
+        assertEquals(2, freq2.get(2));
+        assertNull(freq2.get(3));
+        assertEquals(2, freq2.get(4));
+        assertEquals(1, freq2.get(5));
     }
 
+    @Test
     public void testDisjunctionAsUnionMinusIntersection() {
-        Collection dis = CollectionUtils.disjunction(collectionA,collectionB);
-        Collection un = CollectionUtils.union(collectionA,collectionB);
-        Collection inter = CollectionUtils.intersection(collectionA,collectionB);
-        assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.subtract(un,inter)));
+        Collection<Number> dis = CollectionUtils.<Number>disjunction(collectionA, collectionC);
+        Collection<Number> un = CollectionUtils.<Number>union(collectionA, collectionC);
+        Collection<Number> inter = CollectionUtils.<Number>intersection(collectionA, collectionC);
+        assertTrue(CollectionUtils.isEqualCollection(dis, CollectionUtils.subtract(un, inter)));
     }
 
+    @Test
     public void testDisjunctionAsSymmetricDifference() {
-        Collection dis = CollectionUtils.disjunction(collectionA,collectionB);
-        Collection amb = CollectionUtils.subtract(collectionA,collectionB);
-        Collection bma = CollectionUtils.subtract(collectionB,collectionA);
-        assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.union(amb,bma)));
+        Collection<Number> dis = CollectionUtils.<Number>disjunction(collectionA, collectionC);
+        Collection<Number> amb = CollectionUtils.<Number>subtract(collectionA, collectionC);
+        Collection<Number> bma = CollectionUtils.<Number>subtract(collectionC, collectionA);
+        assertTrue(CollectionUtils.isEqualCollection(dis, CollectionUtils.union(amb, bma)));
     }
 
+    @Test
     public void testSubtract() {
-        Collection col = CollectionUtils.subtract(collectionA,collectionB);
-        Map freq = CollectionUtils.getCardinalityMap(col);
-        assertEquals(new Integer(1),freq.get("a"));
-        assertNull(freq.get("b"));
-        assertNull(freq.get("c"));
-        assertEquals(new Integer(2),freq.get("d"));
-        assertNull(freq.get("e"));
-
-        Collection col2 = CollectionUtils.subtract(collectionB,collectionA);
-        Map freq2 = CollectionUtils.getCardinalityMap(col2);
-        assertEquals(new Integer(1),freq2.get("e"));
-        assertNull(freq2.get("d"));
-        assertNull(freq2.get("c"));
-        assertEquals(new Integer(2),freq2.get("b"));
-        assertNull(freq2.get("a"));
+        Collection<Integer> col = CollectionUtils.subtract(iterableA, iterableC);
+        Map<Integer, Integer> freq = CollectionUtils.getCardinalityMap(col);
+        assertEquals(1, freq.get(1));
+        assertNull(freq.get(2));
+        assertNull(freq.get(3));
+        assertEquals(2, freq.get(4));
+        assertNull(freq.get(5));
+
+        Collection<Number> col2 = CollectionUtils.subtract(collectionC2, collectionA);
+        Map<Number, Integer> freq2 = CollectionUtils.getCardinalityMap(col2);
+        assertEquals(1, freq2.get(5));
+        assertNull(freq2.get(4));
+        assertNull(freq2.get(3));
+        assertEquals(2, freq2.get(2));
+        assertNull(freq2.get(1));
     }
 
+    @Test
     public void testIsSubCollectionOfSelf() {
-        assertTrue(CollectionUtils.isSubCollection(collectionA,collectionA));
-        assertTrue(CollectionUtils.isSubCollection(collectionB,collectionB));
+        assertTrue(CollectionUtils.isSubCollection(collectionA, collectionA));
+        assertTrue(CollectionUtils.isSubCollection(collectionB, collectionB));
     }
 
+    @Test
     public void testIsSubCollection() {
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,collectionB));
-        assertTrue(!CollectionUtils.isSubCollection(collectionB,collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, collectionC));
+        assertTrue(!CollectionUtils.isSubCollection(collectionC, collectionA));
     }
 
+    @Test
     public void testIsSubCollection2() {
-        Collection c = new ArrayList();
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("a");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("b");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("b");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("c");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("c");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("c");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("d");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("d");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("d");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
-        c.add("d");
-        assertTrue(CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(CollectionUtils.isSubCollection(collectionA,c));
-        c.add("e");
-        assertTrue(!CollectionUtils.isSubCollection(c,collectionA));
-        assertTrue(CollectionUtils.isSubCollection(collectionA,c));
+        Collection<Integer> c = new ArrayList<Integer>();
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(1);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(2);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(2);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(3);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(3);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(3);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(4);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(4);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(4);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
+        c.add(4);
+        assertTrue(CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(CollectionUtils.isSubCollection(collectionA, c));
+        c.add(5);
+        assertTrue(!CollectionUtils.isSubCollection(c, collectionA));
+        assertTrue(CollectionUtils.isSubCollection(collectionA, c));
     }
 
+    @Test
     public void testIsEqualCollectionToSelf() {
-        assertTrue(CollectionUtils.isEqualCollection(collectionA,collectionA));
-        assertTrue(CollectionUtils.isEqualCollection(collectionB,collectionB));
+        assertTrue(CollectionUtils.isEqualCollection(collectionA, collectionA));
+        assertTrue(CollectionUtils.isEqualCollection(collectionB, collectionB));
     }
 
+    @Test
     public void testIsEqualCollection() {
-        assertTrue(!CollectionUtils.isEqualCollection(collectionA,collectionB));
-        assertTrue(!CollectionUtils.isEqualCollection(collectionB,collectionA));
+        assertTrue(!CollectionUtils.isEqualCollection(collectionA, collectionC));
+        assertTrue(!CollectionUtils.isEqualCollection(collectionC, collectionA));
     }
 
+    @Test
+    public void testIsEqualCollectionReturnsFalse() {
+        List<Integer> b = new ArrayList<Integer>(collectionA);
+        // remove an extra '2', and add a 5.  This will increase the size of the cardinality
+        b.remove(1);
+        b.add(5);
+        assertFalse(CollectionUtils.isEqualCollection(collectionA, b));
+        assertFalse(CollectionUtils.isEqualCollection(b, collectionA));
+    }
+
+    @Test
     public void testIsEqualCollection2() {
-        Collection a = new ArrayList();
-        Collection b = new ArrayList();
-        assertTrue(CollectionUtils.isEqualCollection(a,b));
-        assertTrue(CollectionUtils.isEqualCollection(b,a));
+        Collection<String> a = new ArrayList<String>();
+        Collection<String> b = new ArrayList<String>();
+        assertTrue(CollectionUtils.isEqualCollection(a, b));
+        assertTrue(CollectionUtils.isEqualCollection(b, a));
         a.add("1");
-        assertTrue(!CollectionUtils.isEqualCollection(a,b));
-        assertTrue(!CollectionUtils.isEqualCollection(b,a));
+        assertTrue(!CollectionUtils.isEqualCollection(a, b));
+        assertTrue(!CollectionUtils.isEqualCollection(b, a));
         b.add("1");
-        assertTrue(CollectionUtils.isEqualCollection(a,b));
-        assertTrue(CollectionUtils.isEqualCollection(b,a));
+        assertTrue(CollectionUtils.isEqualCollection(a, b));
+        assertTrue(CollectionUtils.isEqualCollection(b, a));
         a.add("2");
-        assertTrue(!CollectionUtils.isEqualCollection(a,b));
-        assertTrue(!CollectionUtils.isEqualCollection(b,a));
+        assertTrue(!CollectionUtils.isEqualCollection(a, b));
+        assertTrue(!CollectionUtils.isEqualCollection(b, a));
         b.add("2");
-        assertTrue(CollectionUtils.isEqualCollection(a,b));
-        assertTrue(CollectionUtils.isEqualCollection(b,a));
+        assertTrue(CollectionUtils.isEqualCollection(a, b));
+        assertTrue(CollectionUtils.isEqualCollection(b, a));
         a.add("1");
-        assertTrue(!CollectionUtils.isEqualCollection(a,b));
-        assertTrue(!CollectionUtils.isEqualCollection(b,a));
+        assertTrue(!CollectionUtils.isEqualCollection(a, b));
+        assertTrue(!CollectionUtils.isEqualCollection(b, a));
         b.add("1");
-        assertTrue(CollectionUtils.isEqualCollection(a,b));
-        assertTrue(CollectionUtils.isEqualCollection(b,a));
+        assertTrue(CollectionUtils.isEqualCollection(a, b));
+        assertTrue(CollectionUtils.isEqualCollection(b, a));
     }
-    
+
+    @Test
     public void testIsProperSubCollection() {
-        Collection a = new ArrayList();
-        Collection b = new ArrayList();
-        assertTrue(!CollectionUtils.isProperSubCollection(a,b));
+        Collection<String> a = new ArrayList<String>();
+        Collection<String> b = new ArrayList<String>();
+        assertTrue(!CollectionUtils.isProperSubCollection(a, b));
         b.add("1");
-        assertTrue(CollectionUtils.isProperSubCollection(a,b));
-        assertTrue(!CollectionUtils.isProperSubCollection(b,a));
-        assertTrue(!CollectionUtils.isProperSubCollection(b,b));
-        assertTrue(!CollectionUtils.isProperSubCollection(a,a));
+        assertTrue(CollectionUtils.isProperSubCollection(a, b));
+        assertTrue(!CollectionUtils.isProperSubCollection(b, a));
+        assertTrue(!CollectionUtils.isProperSubCollection(b, b));
+        assertTrue(!CollectionUtils.isProperSubCollection(a, a));
         a.add("1");
         a.add("2");
         b.add("2");
-        assertTrue(!CollectionUtils.isProperSubCollection(b,a));
-        assertTrue(!CollectionUtils.isProperSubCollection(a,b));
+        assertTrue(!CollectionUtils.isProperSubCollection(b, a));
+        assertTrue(!CollectionUtils.isProperSubCollection(a, b));
         a.add("1");
-        assertTrue(CollectionUtils.isProperSubCollection(b,a));
-        assertTrue(CollectionUtils.isProperSubCollection(
-            CollectionUtils.intersection(collectionA, collectionB), collectionA));
-        assertTrue(CollectionUtils.isProperSubCollection(
-            CollectionUtils.subtract(a, b), a));
-        assertTrue(!CollectionUtils.isProperSubCollection(
-            a, CollectionUtils.subtract(a, b)));
-    }
-    
-    public void testFind() {
-        Predicate testPredicate = PredicateUtils.equalPredicate("d");
-        Object test = CollectionUtils.find(collectionA, testPredicate);
-        assertTrue(test.equals("d"));
-        testPredicate = PredicateUtils.equalPredicate("de");
+        assertTrue(CollectionUtils.isProperSubCollection(b, a));
+        assertTrue(CollectionUtils.isProperSubCollection(CollectionUtils.intersection(collectionA, collectionC), collectionA));
+        assertTrue(CollectionUtils.isProperSubCollection(CollectionUtils.subtract(a, b), a));
+        assertTrue(!CollectionUtils.isProperSubCollection(a, CollectionUtils.subtract(a, b)));
+    }
+
+    @Test
+    public void find() {
+        Predicate<Number> testPredicate = equalPredicate((Number) 4);
+        Integer test = CollectionUtils.find(collectionA, testPredicate);
+        assertTrue(test.equals(4));
+        testPredicate = equalPredicate((Number) 45);
         test = CollectionUtils.find(collectionA, testPredicate);
         assertTrue(test == null);
         assertNull(CollectionUtils.find(null,testPredicate));
         assertNull(CollectionUtils.find(collectionA, null));
     }
-    
-    public void testForAllDo() {
-        Closure testClosure = ClosureUtils.invokerClosure("clear");
-        Collection col = new ArrayList();
+
+    @Test
+    public void forAllDo() {
+        Closure<List<? extends Number>> testClosure = ClosureUtils.invokerClosure("clear");
+        Collection<List<? extends Number>> col = new ArrayList<List<? extends Number>>();
         col.add(collectionA);
         col.add(collectionB);
-        CollectionUtils.forAllDo(col, testClosure);
+        Closure<List<? extends Number>> resultClosure = CollectionUtils.forAllDo(col, testClosure);
+        assertSame(testClosure, resultClosure);
         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
-        CollectionUtils.forAllDo(col, null);
+        resultClosure = CollectionUtils.forAllDo(col, null);
+        assertNull(resultClosure);
         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
-        CollectionUtils.forAllDo(null, testClosure);
+        resultClosure = CollectionUtils.forAllDo(null, testClosure);
         col.add(null);
         // null should be OK
         CollectionUtils.forAllDo(col, testClosure);
+    }
+
+    @Test(expected = FunctorException.class)
+    public void forAllDoFailure() {
+        Closure<String> testClosure = ClosureUtils.invokerClosure("clear");
+        Collection<String> col = new ArrayList<String>();
         col.add("x");
-        // This will lead to FunctorException
+        CollectionUtils.forAllDo(col, testClosure);
+    }
+
+    @Test
+    public void getFromMap() {
+        // Unordered map, entries exist
+        Map<String, String> expected = new HashMap<String, String>();
+        expected.put("zeroKey", "zero");
+        expected.put("oneKey", "one");
+
+        Map<String, String> found = new HashMap<String, String>();
+        Map.Entry<String, String> entry = CollectionUtils.get(expected, 0);
+        found.put(entry.getKey(), entry.getValue());
+        entry = CollectionUtils.get(expected, 1);
+        found.put(entry.getKey(), entry.getValue());
+        assertEquals(expected, found);
+
+        // Map index out of range
         try {
-            CollectionUtils.forAllDo(col, testClosure);
-            fail("Expecting FunctorException");
-        } catch (FunctorException ex) {
-            // expected from invoker -- method not found
+            CollectionUtils.get(expected, 2);
+            fail("Expecting IndexOutOfBoundsException.");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        try {
+            CollectionUtils.get(expected, -2);
+            fail("Expecting IndexOutOfBoundsException.");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
         }
-    }
 
-    public void testIndex() {     
-        // normal map behavior when index is an Integer and a key
-        Map map = new HashMap();
-        map.put(new Integer(0), "zero");
-        map.put(new Integer(-1), "minusOne");
-        Object test = CollectionUtils.index(map, 0);
-        assertTrue(test.equals("zero"));
-        test = CollectionUtils.index(map, new Integer(-1));
-        assertTrue(test.equals("minusOne"));
-        
-        // map, non-integer key that does not exist -- map returned
-        test = CollectionUtils.index(map, "missing");
-        assertTrue(test.equals(map));
-        
-        // map, integer not a key, valid index -- "some" element of keyset returned
-        test = CollectionUtils.index(map, new Integer(1));   
-        assertTrue(map.keySet().contains(test)); 
-        
-        // map, integer not a key, not valid index -- "dead" keyset iterator returned
-        test = CollectionUtils.index(map, new Integer(4));         
-        assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext());  
-
-        // sorted map, integer not a key, valid index -- ith key returned
-        SortedMap map2 = new TreeMap();
-        map2.put(new Integer(23), "u");
-        map2.put(new Integer(21), "x");
-        map2.put(new Integer(17), "v");
-        map2.put(new Integer(42), "w");
-        Integer val = (Integer) CollectionUtils.index(map2, 0);
-        assertTrue(val.intValue() == 17);
-        val = (Integer) CollectionUtils.index(map2, 1);
-        assertTrue(val.intValue() == 21);
-        val = (Integer) CollectionUtils.index(map2, 2);
-        assertTrue(val.intValue() == 23);
-        val = (Integer) CollectionUtils.index(map2, 3);
-        assertTrue(val.intValue() == 42);   
-                
-        // list, entry exists
-        List list = new ArrayList();
-        list.add("zero");
-        list.add("one");
-        test = CollectionUtils.index(list, 0);
-        assertTrue(test.equals("zero"));
-        test = CollectionUtils.index(list, 1);
-        assertTrue(test.equals("one"));
-        
+        // Sorted map, entries exist, should respect order
+        SortedMap<String, String> map = new TreeMap<String, String>();
+        map.put("zeroKey", "zero");
+        map.put("oneKey", "one");
+        Map.Entry<String, String> test = CollectionUtils.get(map, 1);
+        assertEquals("zeroKey", test.getKey());
+        assertEquals("zero", test.getValue());
+        test = CollectionUtils.get(map, 0);
+        assertEquals("oneKey", test.getKey());
+        assertEquals("one", test.getValue());
+    }
+
+    /**
+     * Tests that {@link List}s are handled correctly - e.g. using
+     * {@link List#get(int)}.
+     */
+    @Test(expected=IndexOutOfBoundsException.class)
+    public void getFromList() throws Exception {
+        // List, entry exists
+        List<String> list = createMock(List.class);
+        expect(list.get(0)).andReturn("zero");
+        expect(list.get(1)).andReturn("one");
+        replay();
+        String string = CollectionUtils.get(list, 0);
+        assertEquals("zero", string);
+        assertEquals("one", CollectionUtils.get(list, 1));
         // list, non-existent entry -- IndexOutOfBoundsException
+        CollectionUtils.get(new ArrayList<Object>(), 2);
+    }
+
+    @Test
+    public void getFromIterator() throws Exception {
+        // Iterator, entry exists
+        Iterator<Integer> iterator = iterableA.iterator();
+        assertEquals(1, (int) CollectionUtils.get(iterator, 0));
+        iterator = iterableA.iterator();
+        assertEquals(2, (int) CollectionUtils.get(iterator, 1));
+
+        // Iterator, non-existent entry
         try {
-            test = CollectionUtils.index(list, 2);
-            fail("Expecting IndexOutOfBoundsException");
+            CollectionUtils.get(iterator, 10);
+            fail("Expecting IndexOutOfBoundsException.");
         } catch (IndexOutOfBoundsException e) {
             // expected
         }
-        
-        // iterator, entry exists
-        Iterator iterator = list.iterator();
-        test = CollectionUtils.index(iterator,0);
-        assertTrue(test.equals("zero"));
-        iterator = list.iterator();
-        test = CollectionUtils.index(iterator,1);
-        assertTrue(test.equals("one"));
-        
-        // iterator, non-existent entry -- "dead" iterator returned
-        test = CollectionUtils.index(iterator,3);
-        assertTrue(test.equals(iterator) && !iterator.hasNext());
-        
+        assertTrue(!iterator.hasNext());
+    }
+
+    @Test
+    public void getFromEnumeration() throws Exception {
         // Enumeration, entry exists
-        Vector vector = new Vector(list);
-        Enumeration en = vector.elements();
-        test = CollectionUtils.index(en,0);
-        assertTrue(test.equals("zero"));
+        Vector<String> vector = new Vector<String>();
+        vector.addElement("zero");
+        vector.addElement("one");
+        Enumeration<String> en = vector.elements();
+        assertEquals("zero", CollectionUtils.get(en, 0));
         en = vector.elements();
-        test = CollectionUtils.index(en,1);
-        assertTrue(test.equals("one"));
-        
-        // Enumeration, non-existent entry -- "dead" enumerator returned
-        test = CollectionUtils.index(en,3);
-        assertTrue(test.equals(en) && !en.hasMoreElements());
-        
+        assertEquals("one", CollectionUtils.get(en, 1));
+
+        // Enumerator, non-existent entry
+        try {
+            CollectionUtils.get(en, 3);
+            fail("Expecting IndexOutOfBoundsException.");
+        } catch (IndexOutOfBoundsException e) {
+            // expected
+        }
+        assertTrue(!en.hasMoreElements());
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void getFromIterable() throws Exception {
         // Collection, entry exists
-        Bag bag = new HashBag();
+        Bag<String> bag = new HashBag<String>();
         bag.add("element", 1);
-        test = CollectionUtils.index(bag, 0);
-        assertTrue(test.equals("element"));
-        
-        // Collection, non-existent entry -- "dead" iterator returned
-        test = CollectionUtils.index(bag, 2);
-        assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext()); 
-        
+        assertEquals("element", CollectionUtils.get(bag, 0));
+
+        // Collection, non-existent entry
+        CollectionUtils.get(bag, 1);
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void getFromObjectArray() throws Exception {
         // Object array, entry exists
         Object[] objArray = new Object[2];
         objArray[0] = "zero";
         objArray[1] = "one";
-        test = CollectionUtils.index(objArray,0);
-        assertTrue(test.equals("zero"));
-        test = CollectionUtils.index(objArray,1);
-        assertTrue(test.equals("one"));
-        
-        // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
-        try {
-            test = CollectionUtils.index(objArray,2);
-            fail("Expecting ArrayIndexOutOfBoundsException.");
-        } catch (ArrayIndexOutOfBoundsException ex) {
-            // expected
-        }
-        
-        // Non-collection object -- returned unchanged
-        Object obj = new Object();
-        test = CollectionUtils.index(obj, obj);
-        assertTrue(test.equals(obj));
-    }
-    
-    public void testGet() {     
-        {
-            // Unordered map, entries exist
-            Map expected = new HashMap();
-            expected.put("zeroKey", "zero");
-            expected.put("oneKey", "one");
-        
-            Map found = new HashMap();
-            Map.Entry entry = (Map.Entry)(CollectionUtils.get(expected, 0));
-            found.put(entry.getKey(),entry.getValue());
-            entry = (Map.Entry)(CollectionUtils.get(expected, 1));
-            found.put(entry.getKey(),entry.getValue());
-            assertEquals(expected,found);
-        
-            // Map index out of range
-            try {
-                CollectionUtils.get(expected,  2);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
-            try {
-                CollectionUtils.get(expected,  -2);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
-        }
-
-        {
-            // Sorted map, entries exist, should respect order
-            SortedMap map = new TreeMap();
-            map.put("zeroKey", "zero");
-            map.put("oneKey", "one");
-            Object test = CollectionUtils.get(map, 1);
-            assertEquals("zeroKey",((Map.Entry) test).getKey());
-            assertEquals("zero",((Map.Entry) test).getValue());
-            test = CollectionUtils.get(map, 0);
-            assertEquals("oneKey",((Map.Entry) test).getKey());
-            assertEquals("one",((Map.Entry) test).getValue());
-        }
-                
-        {
-            // List, entry exists
-            List list = new ArrayList();
-            list.add("zero");
-            list.add("one");
-            assertEquals("zero",CollectionUtils.get(list, 0));
-            assertEquals("one",CollectionUtils.get(list, 1));
-            // list, non-existent entry -- IndexOutOfBoundsException
-            try {
-                CollectionUtils.get(list, 2);
-                fail("Expecting IndexOutOfBoundsException");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
+        assertEquals("zero", CollectionUtils.get(objArray, 0));
+        assertEquals("one", CollectionUtils.get(objArray, 1));
 
-            // Iterator, entry exists
-            Iterator iterator = list.iterator();
-            assertEquals("zero",CollectionUtils.get(iterator,0));
-            iterator = list.iterator();
-            assertEquals("one",CollectionUtils.get(iterator,1));
-        
-            // Iterator, non-existent entry 
-            try {
-                CollectionUtils.get(iterator,3);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
-            assertTrue(!iterator.hasNext());
-        }
-        
-        {
-            // Enumeration, entry exists
-            Vector vector = new Vector();
-            vector.addElement("zero");
-            vector.addElement("one");
-            Enumeration en = vector.elements();
-            assertEquals("zero",CollectionUtils.get(en,0));
-            en = vector.elements();
-            assertEquals("one",CollectionUtils.get(en,1));
-        
-            // Enumerator, non-existent entry 
-            try {
-                CollectionUtils.get(en,3);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
-            assertTrue(!en.hasMoreElements());
-        }
-        
-        {
-            // Collection, entry exists
-            Bag bag = new HashBag();
-            bag.add("element", 1);
-            assertEquals("element",CollectionUtils.get(bag, 0));
-        
-            // Collection, non-existent entry
-            try {
-                CollectionUtils.get(bag, 1);
-                fail("Expceting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException e) {
-                // expected
-            }
-        }
-        
-        {
-            // Object array, entry exists
-            Object[] objArray = new Object[2];
-            objArray[0] = "zero";
-            objArray[1] = "one";
-            assertEquals("zero",CollectionUtils.get(objArray,0));
-            assertEquals("one",CollectionUtils.get(objArray,1));
-        
-            // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
-            try {
-                CollectionUtils.get(objArray,2);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException ex) {
-                // expected
-            }
-        }
-        
-        {
-            // Primitive array, entry exists
-            int[] array = new int[2];
-            array[0] = 10;
-            array[1] = 20;
-            assertEquals(new Integer(10), CollectionUtils.get(array,0));
-            assertEquals(new Integer(20), CollectionUtils.get(array,1));
-        
-            // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
-            try {
-                CollectionUtils.get(array,2);
-                fail("Expecting IndexOutOfBoundsException.");
-            } catch (IndexOutOfBoundsException ex) {
-                // expected
-            }
-        }
-        
-        {
-            // Invalid object
-            Object obj = new Object();
-            try {
-                CollectionUtils.get(obj, 0);
-                fail("Expecting IllegalArgumentException.");
-            } catch (IllegalArgumentException e) {
-                // expected
-            }
-            try {
-                CollectionUtils.get(null, 0);
-                fail("Expecting IllegalArgumentException.");
-            } catch (IllegalArgumentException e) {
-                // expected
-            }
-        }
+        // Object array, non-existent entry --
+        // ArrayIndexOutOfBoundsException
+        CollectionUtils.get(objArray, 2);
+    }
+
+    @Test(expected = IndexOutOfBoundsException.class)
+    public void getFromPrimativeArray() throws Exception {
+        // Primitive array, entry exists
+        int[] array = new int[2];
+        array[0] = 10;
+        array[1] = 20;
+        assertEquals(10, CollectionUtils.get(array, 0));
+        assertEquals(20, CollectionUtils.get(array, 1));
+
+        // Object array, non-existent entry --
+        // ArrayIndexOutOfBoundsException
+        CollectionUtils.get(array, 2);
+    }
+
+    @Test(expected=IllegalArgumentException.class)
+    public void getFromObject() throws Exception {
+        // Invalid object
+        Object obj = new Object();
+        CollectionUtils.get(obj, 0);
     }
 
-    //-----------------------------------------------------------------------
+    // -----------------------------------------------------------------------
+    @Test
     public void testSize_List() {
         List list = null;
         assertEquals(0, CollectionUtils.size(list));
@@ -748,18 +679,22 @@
         list.add("b");
         assertEquals(2, CollectionUtils.size(list));
     }
+
+    @Test
     public void testSize_Map() {
-        Map map = new HashMap();
+        Map<String, String> map = new HashMap<String, String>();
         assertEquals(0, CollectionUtils.size(map));
         map.put("1", "a");
         assertEquals(1, CollectionUtils.size(map));
         map.put("2", "b");
         assertEquals(2, CollectionUtils.size(map));
     }
+
+    @Test
     public void testSize_Array() {
         Object[] objectArray = new Object[0];
         assertEquals(0, CollectionUtils.size(objectArray));
-        
+
         String[] stringArray = new String[3];
         assertEquals(3, CollectionUtils.size(stringArray));
         stringArray[0] = "a";
@@ -767,10 +702,12 @@
         stringArray[2] = "c";
         assertEquals(3, CollectionUtils.size(stringArray));
     }
+
+    @Test
     public void testSize_PrimitiveArray() {
         int[] intArray = new int[0];
         assertEquals(0, CollectionUtils.size(intArray));
-        
+
         double[] doubleArray = new double[3];
         assertEquals(3, CollectionUtils.size(doubleArray));
         doubleArray[0] = 0.0d;
@@ -778,49 +715,61 @@
         doubleArray[2] = 2.5d;
         assertEquals(3, CollectionUtils.size(doubleArray));
     }
+
+    @Test
     public void testSize_Enumeration() {
-        Vector list = new Vector();
+        Vector<String> list = new Vector<String>();
         assertEquals(0, CollectionUtils.size(list.elements()));
         list.add("a");
         assertEquals(1, CollectionUtils.size(list.elements()));
         list.add("b");
         assertEquals(2, CollectionUtils.size(list.elements()));
     }
+
+    @Test
     public void testSize_Iterator() {
-        List list = new ArrayList();
+        List<String> list = new ArrayList<String>();
         assertEquals(0, CollectionUtils.size(list.iterator()));
         list.add("a");
         assertEquals(1, CollectionUtils.size(list.iterator()));
         list.add("b");
         assertEquals(2, CollectionUtils.size(list.iterator()));
     }
+
+    @Test
     public void testSize_Other() {
         try {
             CollectionUtils.size("not a list");
             fail("Expecting IllegalArgumentException");
-        } catch (IllegalArgumentException e) {}
+        } catch (IllegalArgumentException e) {
+        }
     }
 
-    //-----------------------------------------------------------------------
+    // -----------------------------------------------------------------------
+    @Test
     public void testSizeIsEmpty_Null() {
         assertEquals(true, CollectionUtils.sizeIsEmpty(null));
     }
     public void testSizeIsEmpty_List() {
-        List list = new ArrayList();
+        List<String> list = new ArrayList<String>();
         assertEquals(true, CollectionUtils.sizeIsEmpty(list));
         list.add("a");
         assertEquals(false, CollectionUtils.sizeIsEmpty(list));
     }
+
+    @Test
     public void testSizeIsEmpty_Map() {
-        Map map = new HashMap();
+        Map<String, String> map = new HashMap<String, String>();
         assertEquals(true, CollectionUtils.sizeIsEmpty(map));
         map.put("1", "a");
         assertEquals(false, CollectionUtils.sizeIsEmpty(map));
     }
+
+    @Test
     public void testSizeIsEmpty_Array() {
         Object[] objectArray = new Object[0];
         assertEquals(true, CollectionUtils.sizeIsEmpty(objectArray));
-        
+
         String[] stringArray = new String[3];
         assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
         stringArray[0] = "a";
@@ -828,10 +777,12 @@
         stringArray[2] = "c";
         assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
     }
+
+    @Test
     public void testSizeIsEmpty_PrimitiveArray() {
         int[] intArray = new int[0];
         assertEquals(true, CollectionUtils.sizeIsEmpty(intArray));
-        
+
         double[] doubleArray = new double[3];
         assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
         doubleArray[0] = 0.0d;
@@ -839,208 +790,232 @@
         doubleArray[2] = 2.5d;
         assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
     }
+
+    @Test
     public void testSizeIsEmpty_Enumeration() {
-        Vector list = new Vector();
+        Vector<String> list = new Vector<String>();
         assertEquals(true, CollectionUtils.sizeIsEmpty(list.elements()));
         list.add("a");
         assertEquals(false, CollectionUtils.sizeIsEmpty(list.elements()));
-        Enumeration en = list.elements();
+        Enumeration<String> en = list.elements();
         en.nextElement();
         assertEquals(true, CollectionUtils.sizeIsEmpty(en));
     }
+
+    @Test
     public void testSizeIsEmpty_Iterator() {
-        List list = new ArrayList();
+        List<String> list = new ArrayList<String>();
         assertEquals(true, CollectionUtils.sizeIsEmpty(list.iterator()));
         list.add("a");
         assertEquals(false, CollectionUtils.sizeIsEmpty(list.iterator()));
-        Iterator it = list.iterator();
+        Iterator<String> it = list.iterator();
         it.next();
         assertEquals(true, CollectionUtils.sizeIsEmpty(it));
     }
+
+    @Test
     public void testSizeIsEmpty_Other() {
         try {
             CollectionUtils.sizeIsEmpty("not a list");
             fail("Expecting IllegalArgumentException");
-        } catch (IllegalArgumentException ex) {}
+        } catch (IllegalArgumentException ex) {
+        }
     }
 
-    //-----------------------------------------------------------------------
+    // -----------------------------------------------------------------------
+    @Test
     public void testIsEmptyWithEmptyCollection() {
-        Collection coll = new ArrayList();
+        Collection<Object> coll = new ArrayList<Object>();
         assertEquals(true, CollectionUtils.isEmpty(coll));
     }
 
+    @Test
     public void testIsEmptyWithNonEmptyCollection() {
-        Collection coll = new ArrayList();
+        Collection<String> coll = new ArrayList<String>();
         coll.add("item");
         assertEquals(false, CollectionUtils.isEmpty(coll));
     }
 
+    @Test
     public void testIsEmptyWithNull() {
-        Collection coll = null;
+        Collection<?> coll = null;
         assertEquals(true, CollectionUtils.isEmpty(coll));
     }
 
+    @Test
     public void testIsNotEmptyWithEmptyCollection() {
-        Collection coll = new ArrayList();
+        Collection<Object> coll = new ArrayList<Object>();
         assertEquals(false, CollectionUtils.isNotEmpty(coll));
     }
 
+    @Test
     public void testIsNotEmptyWithNonEmptyCollection() {
-        Collection coll = new ArrayList();
+        Collection<String> coll = new ArrayList<String>();
         coll.add("item");
         assertEquals(true, CollectionUtils.isNotEmpty(coll));
     }
 
+    @Test
     public void testIsNotEmptyWithNull() {
-        Collection coll = null;
+        Collection<?> coll = null;
         assertEquals(false, CollectionUtils.isNotEmpty(coll));
     }
 
-    //-----------------------------------------------------------------------
-    private static Predicate EQUALS_TWO = new Predicate() {
-        public boolean evaluate(Object input) {
-            return (input.equals("Two"));
+    // -----------------------------------------------------------------------
+    private static Predicate<Number> EQUALS_TWO = new Predicate<Number>() {
+        public boolean evaluate(Number input) {
+            return (input.intValue() == 2);
         }
     };
-    
-    public void testFilter() {
-        List list = new ArrayList();
-        list.add("One");
-        list.add("Two");
-        list.add("Three");
-        list.add("Four");
-        CollectionUtils.filter(list, EQUALS_TWO);
-        assertEquals(1, list.size());
-        assertEquals("Two", list.get(0));
-        
-        list = new ArrayList();
-        list.add("One");
-        list.add("Two");
-        list.add("Three");
-        list.add("Four");
-        CollectionUtils.filter(list, null);
-        assertEquals(4, list.size());
+
+//Up to here
+    @SuppressWarnings("cast")
+    @Test
+    public void filter() {
+        List<Integer> ints = new ArrayList<Integer>();
+        ints.add(1);
+        ints.add(2);
+        ints.add(3);
+        ints.add(3);
+        Iterable<Integer> iterable = ints;
+        CollectionUtils.filter(iterable, EQUALS_TWO);
+        assertEquals(1, (int) ints.size());
+        assertEquals(2, (int) ints.get(0));
+    }
+
+    @Test
+    public void filterNullParameters() throws Exception {
+        List<Long> longs = Collections.nCopies(4, 10L);
+        CollectionUtils.filter(longs, null);
+        assertEquals(4, longs.size());
         CollectionUtils.filter(null, EQUALS_TWO);
-        assertEquals(4, list.size());
+        assertEquals(4, longs.size());
         CollectionUtils.filter(null, null);
-        assertEquals(4, list.size());
+        assertEquals(4, longs.size());
     }
 
-    public void testCountMatches() {
-        List list = new ArrayList();
-        list.add("One");
-        list.add("Two");
-        list.add("Three");
-        list.add("Four");
-        int count = CollectionUtils.countMatches(list, EQUALS_TWO);
-        assertEquals(4, list.size());
-        assertEquals(1, count);
-        assertEquals(0, CollectionUtils.countMatches(list, null));
+    @Test
+    public void countMatches() {
+        assertEquals(4, CollectionUtils.countMatches(iterableB, EQUALS_TWO));
+        assertEquals(0, CollectionUtils.countMatches(iterableA, null));
         assertEquals(0, CollectionUtils.countMatches(null, EQUALS_TWO));
         assertEquals(0, CollectionUtils.countMatches(null, null));
     }
 
-    public void testExists() {
-        List list = new ArrayList();
-        assertEquals(false, CollectionUtils.exists(null, null));
-        assertEquals(false, CollectionUtils.exists(list, null));
-        assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO));
-        assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
-        list.add("One");
-        list.add("Three");
-        list.add("Four");
-        assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
+    @Test
+    public void exists() {
+        List<Integer> list = new ArrayList<Integer>();
+        assertFalse(CollectionUtils.exists(null, null));
+        assertFalse(CollectionUtils.exists(list, null));
+        assertFalse(CollectionUtils.exists(null, EQUALS_TWO));
+        assertFalse(CollectionUtils.exists(list, EQUALS_TWO));
+        list.add(1);
+        list.add(3);
+        list.add(4);
+        assertFalse(CollectionUtils.exists(list, EQUALS_TWO));
 
-        list.add("Two");
+        list.add(2);
         assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO));
     }
-    
-    public void testSelect() {
-        List list = new ArrayList();
-        list.add("One");
-        list.add("Two");
-        list.add("Three");
-        list.add("Four");
-        Collection output = CollectionUtils.select(list, EQUALS_TWO);
+
+    @Test
+    public void select() {
+        List<Integer> list = new ArrayList<Integer>();
+        list.add(1);
+        list.add(2);
+        list.add(3);
+        list.add(4);
+        // Ensure that the collection is the input type or a super type
+        Collection<Integer> output1 = CollectionUtils.select(list, EQUALS_TWO);
+        Collection<Number> output2 = CollectionUtils.<Number>select(list, EQUALS_TWO);
+        HashSet<Number> output3 = CollectionUtils.select(list, EQUALS_TWO, new HashSet<Number>());
+        assertTrue(CollectionUtils.isEqualCollection(output1, output3));
         assertEquals(4, list.size());
-        assertEquals(1, output.size());
-        assertEquals("Two", output.iterator().next());
+        assertEquals(1, output1.size());
+        assertEquals(2, output2.iterator().next());
     }
 
-    public void testSelectRejected() {
-        List list = new ArrayList();
-        list.add("One");
-        list.add("Two");
-        list.add("Three");
-        list.add("Four");
-        Collection output = CollectionUtils.selectRejected(list, EQUALS_TWO);
+    @Test
+    public void selectRejected() {
+        List<Long> list = new ArrayList<Long>();
+        list.add(1L);
+        list.add(2L);
+        list.add(3L);
+        list.add(4L);
+        Collection<Long> output1 = CollectionUtils.selectRejected(list, EQUALS_TWO);
+        Collection<? extends Number> output2 = CollectionUtils.selectRejected(list, EQUALS_TWO);
+        HashSet<Number> output3 = CollectionUtils.selectRejected(list, EQUALS_TWO, new HashSet<Number>());
+        assertTrue(CollectionUtils.isEqualCollection(output1, output2));
+        assertTrue(CollectionUtils.isEqualCollection(output1, output3));
         assertEquals(4, list.size());
-        assertEquals(3, output.size());
-        assertTrue(output.contains("One"));
-        assertTrue(output.contains("Three"));
-        assertTrue(output.contains("Four"));
-    }
-    
-    public void testCollect() {
-        Transformer transformer = TransformerUtils.constantTransformer("z");
-        Collection collection = CollectionUtils.collect(collectionA, transformer);
-        assertTrue(collection.size() == collectionA.size());
-        assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
-        assertTrue(collection.contains("z") && !collection.contains("a"));
-        
-        collection = new ArrayList();
-        CollectionUtils.collect(collectionA, transformer, collection);
+        assertEquals(3, output1.size());
+        assertTrue(output1.contains(1L));
+        assertTrue(output1.contains(3L));
+        assertTrue(output1.contains(4L));
+    }
+
+    @Test
+    public void collect() {
+        Transformer<Number, Long> transformer = TransformerUtils.constantTransformer(2L);
+        Collection<Number> collection = CollectionUtils.<Integer, Number>collect(iterableA, transformer);
         assertTrue(collection.size() == collectionA.size());
-        assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
-        assertTrue(collection.contains("z") && !collection.contains("a"));
-        
-        Iterator iterator = null;
-        collection = new ArrayList();
-        CollectionUtils.collect(iterator, transformer, collection);
-        
-        iterator = collectionA.iterator();
-        CollectionUtils.collect(iterator, transformer, collection);
+        assertCollectResult(collection);
+
+        ArrayList<Number> list;
+        list = CollectionUtils.collect(collectionA, transformer, new ArrayList<Number>());
+        assertTrue(list.size() == collectionA.size());
+        assertCollectResult(list);
+
+        Iterator<Integer> iterator = null;
+        list = CollectionUtils.collect(iterator, transformer, new ArrayList<Number>());
+
+        iterator = iterableA.iterator();
+        list = CollectionUtils.collect(iterator, transformer, list);
         assertTrue(collection.size() == collectionA.size());
-        assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
-        assertTrue(collection.contains("z") && !collection.contains("a")); 
-        
+        assertCollectResult(collection);
+
         iterator = collectionA.iterator();
-        collection = CollectionUtils.collect(iterator, transformer);
+        collection = CollectionUtils.<Integer, Number>collect(iterator, transformer);
         assertTrue(collection.size() == collectionA.size());
-        assertTrue(collection.contains("z") && !collection.contains("a")); 
-        collection = CollectionUtils.collect((Iterator) null, (Transformer) null);
+        assertTrue(collection.contains(2L) && !collection.contains(1));
+        collection = CollectionUtils.collect((Iterator<Integer>) null, (Transformer<Integer, Number>) null);
         assertTrue(collection.size() == 0);
-           
+
         int size = collectionA.size();
-        CollectionUtils.collect((Collection) null, transformer, collectionA);
-        assertTrue(collectionA.size() == size && collectionA.contains("a"));
+        collectionB = CollectionUtils.collect((Collection<Integer>) null, transformer, collectionB);
+        assertTrue(collectionA.size() == size && collectionA.contains(1));
         CollectionUtils.collect(collectionB, null, collectionA);
-        assertTrue(collectionA.size() == size && collectionA.contains("a"));
-        
+        assertTrue(collectionA.size() == size && collectionA.contains(1));
+
+    }
+
+    private void assertCollectResult(Collection<Number> collection) {
+        assertTrue(collectionA.contains(1) && !collectionA.contains(2L));
+        assertTrue(collection.contains(2L) && !collection.contains(1));
     }
 
-    Transformer TRANSFORM_TO_INTEGER = new Transformer() {
-        public Object transform(Object input) {
-            return new Integer((String) input);
+    Transformer<Object, Integer> TRANSFORM_TO_INTEGER = new Transformer<Object, Integer>() {
+        public Integer transform(Object input) {
+            return new Integer(((Long)input).intValue());
         }
     };
-    
-    public void testTransform1() {
-        List list = new ArrayList();
-        list.add("1");
-        list.add("2");
-        list.add("3");
+
+    @Test
+    public void transform1() {
+        List<Number> list = new ArrayList<Number>();
+        list.add(1L);
+        list.add(2L);
+        list.add(3L);
         CollectionUtils.transform(list, TRANSFORM_TO_INTEGER);
         assertEquals(3, list.size());
-        assertEquals(new Integer(1), list.get(0));
-        assertEquals(new Integer(2), list.get(1));
-        assertEquals(new Integer(3), list.get(2));
-        
-        list = new ArrayList();
-        list.add("1");
-        list.add("2");
-        list.add("3");
+        assertEquals(1, list.get(0));
+        assertEquals(2, list.get(1));
+        assertEquals(3, list.get(2));
+
+        list = new ArrayList<Number>();
+        list.add(1L);
+        list.add(2L);
+        list.add(3L);
         CollectionUtils.transform(null, TRANSFORM_TO_INTEGER);
         assertEquals(3, list.size());
         CollectionUtils.transform(list, null);
@@ -1048,147 +1023,113 @@
         CollectionUtils.transform(null, null);
         assertEquals(3, list.size());
     }
-    
-    public void testTransform2() {
-        Set set = new HashSet();
-        set.add("1");
-        set.add("2");
-        set.add("3");
-        CollectionUtils.transform(set, new Transformer() {
-            public Object transform(Object input) {
-                return new Integer(4);
+
+    @Test
+    public void transform2() {
+        Set<Number> set = new HashSet<Number>();
+        set.add(1L);
+        set.add(2L);
+        set.add(3L);
+        CollectionUtils.transform(set, new Transformer<Object, Integer>() {
+            public Integer transform(Object input) {
+                return 4;
             }
         });
         assertEquals(1, set.size());
-        assertEquals(new Integer(4), set.iterator().next());
+        assertEquals(4, set.iterator().next());
     }
 
-    //-----------------------------------------------------------------------
-    public void testAddIgnoreNull() {
-        Set set = new HashSet();
+    // -----------------------------------------------------------------------
+    @Test
+    public void addIgnoreNull() {
+        Set<String> set = new HashSet<String>();
         set.add("1");
         set.add("2");
         set.add("3");
-        assertEquals(false, CollectionUtils.addIgnoreNull(set, null));
+        assertFalse(CollectionUtils.addIgnoreNull(set, null));
         assertEquals(3, set.size());
-        assertEquals(false, CollectionUtils.addIgnoreNull(set, "1"));
+        assertFalse(CollectionUtils.addIgnoreNull(set, "1"));
         assertEquals(3, set.size());
         assertEquals(true, CollectionUtils.addIgnoreNull(set, "4"));
         assertEquals(4, set.size());
         assertEquals(true, set.contains("4"));
     }
 
-    //-----------------------------------------------------------------------
-    public void testPredicatedCollection() {
-        Predicate predicate = new Predicate() {
-            public boolean evaluate(Object o) {
-                return o instanceof String;
-            }
-        };
-        Collection collection = 
-            CollectionUtils.predicatedCollection(new ArrayList(), predicate);
-        assertTrue("returned object should be a PredicatedCollection",
-            collection instanceof PredicatedCollection);
-        try { 
-           collection = 
-                CollectionUtils.predicatedCollection(new ArrayList(), null); 
-           fail("Expecting IllegalArgumentException for null predicate.");
+    // -----------------------------------------------------------------------
+    @Test
+    public void predicatedCollection() {
+        Predicate<Object> predicate = PredicateUtils.instanceofPredicate(Integer.class);
+        Collection<Number> collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), predicate);
+        assertTrue("returned object should be a PredicatedCollection", collection instanceof PredicatedCollection);
+        try {
+            collection = CollectionUtils.predicatedCollection(new ArrayList<Number>(), null);
+            fail("Expecting IllegalArgumentException for null predicate.");
         } catch (IllegalArgumentException ex) {
             // expected
         }
-        try { 
-           collection = 
-                CollectionUtils.predicatedCollection(null, predicate); 
-           fail("Expecting IllegalArgumentException for null collection.");
+        try {
+            CollectionUtils.predicatedCollection(null, predicate);
+            fail("Expecting IllegalArgumentException for null collection.");
         } catch (IllegalArgumentException ex) {
             // expected
-        }             
+        }
     }
-        
-   
 
-    public BulkTest bulkTestTypedCollection() {
-        return new TestTypedCollection("") {
-            public Collection typedCollection() {
-                return CollectionUtils.typedCollection(
-                    new ArrayList(),
-                    super.getType());
-            }
- 
-            public BulkTest bulkTestAll() {
-                return new AbstractTestCollection("") {
-                    public Collection makeCollection() {
-                        return typedCollection();
-                    }
- 
-                    public Collection makeConfirmedCollection() {
-                        return new ArrayList();
-                    }
- 
-                    public Collection makeConfirmedFullCollection() {
-                        ArrayList list = new ArrayList();
-                        list.addAll(java.util.Arrays.asList(getFullElements()));
-                        return list;
-                    }
- 
-                    public Object[] getFullElements() {
-                        return getFullNonNullStringElements();
-                    }
- 
-                    public Object[] getOtherElements() {
-                        return getOtherNonNullStringElements();
-                    }
- 
-                };
-            }
-        };
-    }
-    
-    public void testIsFull() {
-        Set set = new HashSet();
+    @Test
+    public void isFull() {
+        Set<String> set = new HashSet<String>();
         set.add("1");
         set.add("2");
         set.add("3");
         try {
             CollectionUtils.isFull(null);
             fail();
-        } catch (NullPointerException ex) {}
-        assertEquals(false, CollectionUtils.isFull(set));
-        
-        BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
+        } catch (NullPointerException ex) {
+        }
+        assertFalse(CollectionUtils.isFull(set));
+
+        BoundedFifoBuffer<String> buf = new BoundedFifoBuffer<String>(set);
         assertEquals(true, CollectionUtils.isFull(buf));
         buf.remove("2");
-        assertEquals(false, CollectionUtils.isFull(buf));
+        assertFalse(CollectionUtils.isFull(buf));
         buf.add("2");
         assertEquals(true, CollectionUtils.isFull(buf));
-        
-        Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
+
+        Buffer<String> buf2 = BufferUtils.synchronizedBuffer(buf);
         assertEquals(true, CollectionUtils.isFull(buf2));
         buf2.remove("2");
-        assertEquals(false, CollectionUtils.isFull(buf2));
+        assertFalse(CollectionUtils.isFull(buf2));
         buf2.add("2");
         assertEquals(true, CollectionUtils.isFull(buf2));
     }
 
-    public void testMaxSize() {
-        Set set = new HashSet();
+    @Test
+    public void isEmpty() {
+        assertFalse(CollectionUtils.isNotEmpty(null));
+        assertTrue(CollectionUtils.isNotEmpty(collectionA));
+    }
+
+    @Test
+    public void maxSize() {
+        Set<String> set = new HashSet<String>();
         set.add("1");
         set.add("2");
         set.add("3");
         try {
             CollectionUtils.maxSize(null);
             fail();
-        } catch (NullPointerException ex) {}
+        } catch (NullPointerException ex) {
+        }
         assertEquals(-1, CollectionUtils.maxSize(set));
-        
-        BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
+
+        Buffer<String> buf = new BoundedFifoBuffer<String>(set);
         assertEquals(3, CollectionUtils.maxSize(buf));
         buf.remove("2");
         assertEquals(3, CollectionUtils.maxSize(buf));
         buf.add("2");
         assertEquals(3, CollectionUtils.maxSize(buf));
-        
-        Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
+
+        Buffer<String> buf2 = BufferUtils.synchronizedBuffer(buf);
         assertEquals(3, CollectionUtils.maxSize(buf2));
         buf2.remove("2");
         assertEquals(3, CollectionUtils.maxSize(buf2));
@@ -1196,61 +1137,64 @@
         assertEquals(3, CollectionUtils.maxSize(buf2));
     }
 
-    public void testIntersectionUsesMethodEquals() {
+    @Test
+    public void intersectionUsesMethodEquals() {
         // Let elta and eltb be objects...
-        Object elta = new Integer(17);
-        Object eltb = new Integer(17);
-        
+        Integer elta = new Integer(17);
+        Integer eltb = new Integer(17);
+
         // ...which are equal...
-        assertEquals(elta,eltb);
-        assertEquals(eltb,elta);
-        
+        assertEquals(elta, eltb);
+        assertEquals(eltb, elta);
+
         // ...but not the same (==).
         assertTrue(elta != eltb);
-        
+
         // Let cola and colb be collections...
-        Collection cola = new ArrayList();
-        Collection colb = new ArrayList();
-        
-        // ...which contain elta and eltb, 
+        Collection<Number> cola = new ArrayList<Number>();
+        Collection<Integer> colb = new ArrayList<Integer>();
+
+        // ...which contain elta and eltb,
         // respectively.
         cola.add(elta);
         colb.add(eltb);
-        
+
         // Then the intersection of the two
         // should contain one element.
-        Collection intersection = CollectionUtils.intersection(cola,colb);
-        assertEquals(1,intersection.size());
-        
+        Collection<Number> intersection = CollectionUtils.intersection(cola, colb);
+        assertEquals(1, intersection.size());
+
         // In practice, this element will be the same (==) as elta
         // or eltb, although this isn't strictly part of the
         // contract.
         Object eltc = intersection.iterator().next();
-        assertTrue((eltc == elta  && eltc != eltb) || (eltc != elta  && eltc == eltb));
-        
+        assertTrue((eltc == elta && eltc != eltb) || (eltc != elta && eltc == eltb));
+
         // In any event, this element remains equal,
         // to both elta and eltb.
-        assertEquals(elta,eltc);
-        assertEquals(eltc,elta);
-        assertEquals(eltb,eltc);
-        assertEquals(eltc,eltb);
+        assertEquals(elta, eltc);
+        assertEquals(eltc, elta);
+        assertEquals(eltb, eltc);
+        assertEquals(eltc, eltb);
     }
 
-    //-----------------------------------------------------------------------
+    // -----------------------------------------------------------------------
+    //Up to here
+    @Test
     public void testRetainAll() {
-        List base = new ArrayList();
+        List<String> base = new ArrayList<String>();
         base.add("A");
         base.add("B");
         base.add("C");
-        List sub = new ArrayList();
+        List<Object> sub = new ArrayList<Object>();
         sub.add("A");
         sub.add("C");
         sub.add("X");
-        
-        Collection result = CollectionUtils.retainAll(base, sub);
+
+        Collection<String> result = CollectionUtils.retainAll(base, sub);
         assertEquals(2, result.size());
         assertEquals(true, result.contains("A"));
-        assertEquals(false, result.contains("B"));
+        assertFalse(result.contains("B"));
         assertEquals(true, result.contains("C"));
         assertEquals(3, base.size());
         assertEquals(true, base.contains("A"));
@@ -1260,28 +1204,30 @@
         assertEquals(true, sub.contains("A"));
         assertEquals(true, sub.contains("C"));
         assertEquals(true, sub.contains("X"));
-        
+
         try {
             CollectionUtils.retainAll(null, null);
             fail("expecting NullPointerException");
-        } catch(NullPointerException npe){} // this is what we want
+        } catch (NullPointerException npe) {
+        } // this is what we want
     }
 
+    @Test
     public void testRemoveAll() {
-        List base = new ArrayList();
+        List<String> base = new ArrayList<String>();
         base.add("A");
         base.add("B");
         base.add("C");
-        List sub = new ArrayList();
+        List<String> sub = new ArrayList<String>();
         sub.add("A");
         sub.add("C");
         sub.add("X");
-        
-        Collection result = CollectionUtils.removeAll(base, sub);
+
+        Collection<String> result = CollectionUtils.removeAll(base, sub);
         assertEquals(1, result.size());
-        assertEquals(false, result.contains("A"));
+        assertFalse(result.contains("A"));
         assertEquals(true, result.contains("B"));
-        assertEquals(false, result.contains("C"));
+        assertFalse(result.contains("C"));
         assertEquals(3, base.size());
         assertEquals(true, base.contains("A"));
         assertEquals(true, base.contains("B"));
@@ -1290,69 +1236,208 @@
         assertEquals(true, sub.contains("A"));
         assertEquals(true, sub.contains("C"));
         assertEquals(true, sub.contains("X"));
-        
+
         try {
             CollectionUtils.removeAll(null, null);
             fail("expecting NullPointerException");
-        } catch(NullPointerException npe){} // this is what we want
+        } catch (NullPointerException npe) {
+        } // this is what we want
     }
 
-    //-----------------------------------------------------------------------
+    // -----------------------------------------------------------------------
+    @Test
     public void testTransformedCollection() {
-        Transformer transformer = TransformerUtils.nopTransformer();
-        Collection collection = 
-            CollectionUtils.transformedCollection(new ArrayList(), transformer);
-        assertTrue("returned object should be a TransformedCollection",
-            collection instanceof TransformedCollection);
-        try { 
-           collection = 
-                CollectionUtils.transformedCollection(new ArrayList(), null); 
-           fail("Expecting IllegalArgumentException for null transformer.");
+        Transformer<Object, Object> transformer = TransformerUtils.nopTransformer();
+        Collection<Object> collection = CollectionUtils.transformedCollection(new ArrayList<Object>(), transformer);
+        assertTrue("returned object should be a TransformedCollection", collection instanceof TransformedCollection);
+        try {
+            collection = CollectionUtils.transformedCollection(new ArrayList<Object>(), null);
+            fail("Expecting IllegalArgumentException for null transformer.");
         } catch (IllegalArgumentException ex) {
             // expected
         }
-        try { 
-           collection = 
-                CollectionUtils.transformedCollection(null, transformer); 
-           fail("Expecting IllegalArgumentException for null collection.");
+        try {
+            collection = CollectionUtils.transformedCollection(null, transformer);
+            fail("Expecting IllegalArgumentException for null collection.");
         } catch (IllegalArgumentException ex) {
             // expected
-        }             
+        }
     }
 
+    @Test
     public void testTransformedCollection_2() {
-        List list = new ArrayList();
+        List<Object> list = new ArrayList<Object>();
         list.add("1");
         list.add("2");
         list.add("3");
-        Collection result = CollectionUtils.transformedCollection(list, TRANSFORM_TO_INTEGER);
-        assertEquals(true, result.contains("1"));  // untransformed
-        assertEquals(true, result.contains("2"));  // untransformed
-        assertEquals(true, result.contains("3"));  // untransformed
+        Collection<Object> result = CollectionUtils.transformedCollection(list, TRANSFORM_TO_INTEGER);
+        assertEquals(true, result.contains("1")); // untransformed
+        assertEquals(true, result.contains("2")); // untransformed
+        assertEquals(true, result.contains("3")); // untransformed
     }
 
+    @Test
     public void testSynchronizedCollection() {
-        Collection col = CollectionUtils.synchronizedCollection(new ArrayList());
-        assertTrue("Returned object should be a SynchronizedCollection.",
-            col instanceof SynchronizedCollection);
+        Collection<Object> col = CollectionUtils.synchronizedCollection(new ArrayList<Object>());
+        assertTrue("Returned object should be a SynchronizedCollection.", col instanceof SynchronizedCollection);
         try {
             col = CollectionUtils.synchronizedCollection(null);
             fail("Expecting IllegalArgumentException for null collection.");
         } catch (IllegalArgumentException ex) {
             // expected
-        }  
+        }
     }
-    
+
+    @Test
     public void testUnmodifiableCollection() {
-        Collection col = CollectionUtils.unmodifiableCollection(new ArrayList());
-        assertTrue("Returned object should be a UnmodifiableCollection.",
-            col instanceof UnmodifiableCollection);
+        Collection<Object> col = CollectionUtils.unmodifiableCollection(new ArrayList<Object>());
+        assertTrue("Returned object should be a UnmodifiableCollection.", col instanceof UnmodifiableCollection);
         try {
             col = CollectionUtils.unmodifiableCollection(null);
             fail("Expecting IllegalArgumentException for null collection.");
         } catch (IllegalArgumentException ex) {
             // expected
-        }  
+        }
+    }
+
+    @Test
+    public void emptyCollection() throws Exception {
+        Collection<Number> coll = CollectionUtils.emptyCollection();
+        assertEquals(CollectionUtils.EMPTY_COLLECTION, coll);
+    }
+
+    /**
+     * This test ensures that {@link Iterable}s are supported by {@link CollectionUtils}.
+     * Specifically, it uses mocks to ensure that if the passed in
+     * {@link Iterable} is a {@link Collection} then
+     * {@link Collection#addAll(Collection)} is called instead of iterating.
+     */
+    @Test
+    public void addAllForIterable() {
+        Collection<Integer> inputCollection = createMock(Collection.class);
+        Iterable<Integer> inputIterable = inputCollection;
+        Iterable<Long> iterable = createMock(Iterable.class);
+        Iterator<Long> iterator = createMock(Iterator.class);
+        Collection<Number> c = createMock(Collection.class);
+
+        expect(iterable.iterator()).andReturn(iterator);
+        next(iterator, 1L);
+        next(iterator, 2L);
+        next(iterator, 3L);
+        expect(iterator.hasNext()).andReturn(false);
+        expect(c.add(1L)).andReturn(true);
+        expect(c.add(2L)).andReturn(true);
+        expect(c.add(3L)).andReturn(true);
+        // Check that the collection is added using
+        // Collection.addAll(Collection)
+        expect(c.addAll(inputCollection)).andReturn(true);
+
+        // Ensure the method returns false if nothing is added
+        expect(iterable.iterator()).andReturn(iterator);
+        next(iterator, 1L);
+        expect(iterator.hasNext()).andReturn(false);
+        expect(c.add(1L)).andReturn(false);
+        expect(c.addAll(inputCollection)).andReturn(false);
+
+        replay();
+        assertTrue(CollectionUtils.addAll(c, iterable));
+        assertTrue(CollectionUtils.addAll(c, inputIterable));
+
+        assertFalse(CollectionUtils.addAll(c, iterable));

[... 99 lines stripped ...]