You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sk...@apache.org on 2007/11/08 13:21:56 UTC
svn commit: r593144 [2/2] - in
/commons/proper/collections/branches/collections_jdk5_branch: ./
src/java/org/apache/commons/collections/
src/test/org/apache/commons/collections/
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java?rev=593144&r1=593143&r2=593144&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/TestCollectionUtils.java Thu Nov 8 04:21:55 2007
@@ -16,8 +16,15 @@
*/
package org.apache.commons.collections;
+import static junit.framework.Assert.assertFalse;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
@@ -30,16 +37,14 @@
import java.util.TreeMap;
import java.util.Vector;
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
import org.apache.commons.collections.bag.HashBag;
import org.apache.commons.collections.buffer.BoundedFifoBuffer;
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.
@@ -51,83 +56,136 @@
* @author Steven Melzer
* @author Neil O'Toole
* @author Stephen Smith
- *
- * @version $Revision$ $Date$
+ * @author Stephen Kestle
+ * @version $Revision$ $Date: 2006-11-05 00:33:22 +1300 (Sun, 05 Nov
+ * 2006) $
*/
-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 Integers}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 Integers}s (cast as {@link Number}s) that are
+ * equivalent to the Longs in collectionB.
+ */
+ private Collection<Number> collectionC2 = null;
+
+ private Iterable<Integer> iterableA = 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<Long> iterableB = null;
- Set set = new HashSet();
+ private Iterable<Integer> iterableC = null;
+
+ 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, Integer>getCardinalityMap(iterableA);
+ assertEquals(1, freqA.get(1));
+ assertEquals(2, freqA.get(2));
+ assertEquals(3, freqA.get(3));
+ assertEquals(4, freqA.get(4));
+ assertNull(freqA.get(5));
+
+ Map<Long, Integer> freqB = CollectionUtils.getCardinalityMap(iterableB);
+ assertNull(freqB.get(1L));
+ assertEquals(4, freqB.get(2L));
+ assertEquals(3, freqB.get(3L));
+ assertEquals(2, freqB.get(4L));
+ assertEquals(1, 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");
@@ -138,7 +196,7 @@
assertEquals(0, CollectionUtils.cardinality("D", set));
assertEquals(1, CollectionUtils.cardinality("E", set));
- Bag bag = new HashBag();
+ Bag<String> bag = new HashBag();
bag.add("A", 3);
bag.add("C");
bag.add("E");
@@ -149,301 +207,302 @@
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);
assertNull(freq.get(null));
}
list.add("A");
- assertEquals(0,CollectionUtils.cardinality(null,list));
+ assertEquals(0, CollectionUtils.cardinality(null, list));
{
Map 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));
+ 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));
+ 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));
+ 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));
+ 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));
+ 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);
+ 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 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"));
+ 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 col2 = CollectionUtils.union(collectionB,collectionA);
+ Collection<Number> col2 = CollectionUtils.union(collectionC2, iterableA);
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"));
+ 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));
}
- public void testIntersection() {
- Collection col = CollectionUtils.intersection(collectionA,collectionB);
+ @Test
+ public void intersection() {
+ Collection<Integer> col = CollectionUtils.intersection(iterableA, iterableC);
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"));
+ assertNull(freq.get(1));
+ assertEquals(2, freq.get(2));
+ assertEquals(3, freq.get(3));
+ assertEquals(2, freq.get(4));
+ assertNull(freq.get(5));
- Collection col2 = CollectionUtils.intersection(collectionB,collectionA);
+ Collection<Number> col2 = CollectionUtils.intersection(collectionC2, 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"));
+ assertNull(freq2.get(1));
+ assertEquals(2, freq2.get(2));
+ assertEquals(3, freq2.get(3));
+ assertEquals(2, freq2.get(4));
+ assertNull(freq2.get(5));
}
- public void testDisjunction() {
- Collection col = CollectionUtils.disjunction(collectionA,collectionB);
+ @Test
+ public void disjunction() {
+ Collection<Integer> col = CollectionUtils.disjunction(iterableA, iterableC);
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"));
+ 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 col2 = CollectionUtils.disjunction(collectionB,collectionA);
+ Collection<Number> col2 = CollectionUtils.disjunction(collectionC2, 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"));
+ 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);
+ Collection<Integer> col = CollectionUtils.subtract(iterableA, iterableC);
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"));
+ assertEquals(1, freq.get(1));
+ assertNull(freq.get(2));
+ assertNull(freq.get(3));
+ assertEquals(2, freq.get(4));
+ assertNull(freq.get(5));
- Collection col2 = CollectionUtils.subtract(collectionB,collectionA);
+ Collection<Number> col2 = CollectionUtils.subtract(collectionC2, 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"));
+ 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 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)));
+ 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)));
}
-
- public void testFind() {
- Predicate testPredicate = PredicateUtils.equalPredicate("d");
- Object test = CollectionUtils.find(collectionA, testPredicate);
- assertTrue(test.equals("d"));
- testPredicate = PredicateUtils.equalPredicate("de");
+
+ @Test
+ public void find() {
+ Predicate<Number> testPredicate = PredicateUtils.equalPredicate(4);
+ Integer test = CollectionUtils.find(collectionA, testPredicate);
+ assertTrue(test.equals(4));
+ testPredicate = PredicateUtils.equalPredicate(45);
test = CollectionUtils.find(collectionA, testPredicate);
assertTrue(test == null);
- assertEquals(CollectionUtils.find(null,testPredicate), null);
+ assertEquals(CollectionUtils.find(null, testPredicate), null);
assertEquals(CollectionUtils.find(collectionA, null), null);
}
-
- public void testForAllDo() {
- Closure testClosure = ClosureUtils.invokerClosure("clear");
- Collection col = new ArrayList();
+
+ @Test
+ public void forAllDo() {
+ Closure<Collection> testClosure = ClosureUtils.invokerClosure("clear");
+ Collection<List> col = new ArrayList<List>();
col.add(collectionA);
col.add(collectionB);
CollectionUtils.forAllDo(col, testClosure);
@@ -454,197 +513,185 @@
col.add(null);
// null should be OK
CollectionUtils.forAllDo(col, testClosure);
- col.add("x");
- // This will lead to FunctorException
- try {
- CollectionUtils.forAllDo(col, testClosure);
- fail("Expecting FunctorException");
- } catch (FunctorException ex) {
- // expected from invoker -- method not found
- }
}
- 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
- }
- }
+ @Test(expected = FunctorException.class)
+ public void forAllDoFailure() {
+ Closure<String> testClosure = ClosureUtils.invokerClosure("clear");
+ Collection<String> col = new ArrayList<String>();
+ col.add("x");
+ CollectionUtils.forAllDo(col, testClosure);
+ }
- {
- // 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
- }
+ @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);
- // 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
- }
+ // Map index out of range
+ try {
+ CollectionUtils.get(expected, 2);
+ fail("Expecting 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
- }
+ try {
+ CollectionUtils.get(expected, -2);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // 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
- }
+
+ // 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, CollectionUtils.get(iterator, 0));
+ iterator = iterableA.iterator();
+ assertEquals(2, CollectionUtils.get(iterator, 1));
+
+ // Iterator, non-existent entry
+ try {
+ CollectionUtils.get(iterator, 10);
+ fail("Expecting IndexOutOfBoundsException.");
+ } catch (IndexOutOfBoundsException e) {
+ // 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
- }
+ assertTrue(!iterator.hasNext());
+ }
+
+ @Test
+ public void getFromEnumeration() throws Exception {
+ // 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());
+ }
+
+ @Test(expected = IndexOutOfBoundsException.class)
+ public void getFromIterable() throws Exception {
+ // Collection, entry exists
+ Bag bag = new HashBag();
+ bag.add("element", 1);
+ 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";
+ assertEquals("zero", CollectionUtils.get(objArray, 0));
+ assertEquals("one", CollectionUtils.get(objArray, 1));
+
+ // 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 = new ArrayList();
+ List<String> list = new ArrayList<String>();
assertEquals(0, CollectionUtils.size(list));
list.add("a");
assertEquals(1, CollectionUtils.size(list));
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";
@@ -652,10 +699,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;
@@ -663,6 +712,8 @@
doubleArray[2] = 2.5d;
assertEquals(3, CollectionUtils.size(doubleArray));
}
+
+ @Test
public void testSize_Enumeration() {
Vector list = new Vector();
assertEquals(0, CollectionUtils.size(list.elements()));
@@ -671,6 +722,8 @@
list.add("b");
assertEquals(2, CollectionUtils.size(list.elements()));
}
+
+ @Test
public void testSize_Iterator() {
List list = new ArrayList();
assertEquals(0, CollectionUtils.size(list.iterator()));
@@ -679,34 +732,43 @@
list.add("b");
assertEquals(2, CollectionUtils.size(list.iterator()));
}
+
+ @Test
public void testSize_Other() {
try {
CollectionUtils.size(null);
fail("Expecting IllegalArgumentException");
- } catch (IllegalArgumentException e) {}
+ } catch (IllegalArgumentException e) {
+ }
try {
CollectionUtils.size("not a list");
fail("Expecting IllegalArgumentException");
- } catch (IllegalArgumentException e) {}
+ } catch (IllegalArgumentException e) {
+ }
}
- //-----------------------------------------------------------------------
+ // -----------------------------------------------------------------------
+ @Test
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";
@@ -714,10 +776,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;
@@ -725,8 +789,10 @@
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()));
@@ -734,8 +800,10 @@
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()));
@@ -743,194 +811,215 @@
it.next();
assertEquals(true, CollectionUtils.sizeIsEmpty(it));
}
+
+ @Test
public void testSizeIsEmpty_Other() {
try {
CollectionUtils.sizeIsEmpty(null);
fail("Expecting IllegalArgumentException");
- } catch (IllegalArgumentException ex) {}
+ } catch (IllegalArgumentException ex) {
+ }
try {
CollectionUtils.sizeIsEmpty("not a list");
fail("Expecting IllegalArgumentException");
- } catch (IllegalArgumentException ex) {}
+ } catch (IllegalArgumentException ex) {
+ }
}
- //-----------------------------------------------------------------------
+ // -----------------------------------------------------------------------
+ @Test
public void testIsEmptyWithEmptyCollection() {
Collection coll = new ArrayList();
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;
assertEquals(true, CollectionUtils.isEmpty(coll));
}
+ @Test
public void testIsNotEmptyWithEmptyCollection() {
Collection coll = new ArrayList();
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;
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);
}
};
+
+//Up to here
+ @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, ints.size());
+ assertEquals(2, ints.get(0));
+ }
- 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());
+ @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, Integer>select(list, EQUALS_TWO);
+ Collection<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<Number> output2 = CollectionUtils.<Number, Long>selectRejected(list, EQUALS_TWO);
+ Collection<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"));
+ assertEquals(3, output1.size());
+ assertTrue(output1.contains(1L));
+ assertTrue(output1.contains(3L));
+ assertTrue(output1.contains(4L));
}
-
- public void testCollect() {
- Transformer transformer = TransformerUtils.constantTransformer("z");
- Collection collection = CollectionUtils.collect(collectionA, transformer);
+
+ @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"));
+ assertCollectResult(collection);
- collection = new ArrayList();
+ collection = new ArrayList<Number>();
CollectionUtils.collect(collectionA, transformer, collection);
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();
+ assertCollectResult(collection);
+
+ Iterator<Integer> iterator = null;
+ collection = new ArrayList<Number>();
CollectionUtils.collect(iterator, transformer, collection);
-
- iterator = collectionA.iterator();
+
+ iterator = iterableA.iterator();
CollectionUtils.collect(iterator, transformer, collection);
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"));
+ 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);
@@ -938,110 +1027,107 @@
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 {
+ collection = CollectionUtils.predicatedCollection(null, predicate);
+ fail("Expecting IllegalArgumentException for null collection.");
} catch (IllegalArgumentException ex) {
// expected
- }
+ }
}
-
-
- 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));
-
+ } catch (NullPointerException ex) {
+ }
+ assertFalse(CollectionUtils.isFull(set));
+
BoundedFifoBuffer buf = new BoundedFifoBuffer(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 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(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));
@@ -1049,61 +1135,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"));
@@ -1113,13 +1202,15 @@
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();
base.add("A");
@@ -1129,12 +1220,12 @@
sub.add("A");
sub.add("C");
sub.add("X");
-
+
Collection 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"));
@@ -1143,69 +1234,123 @@
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.");
+ 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.");
} 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.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
+ 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);
+ 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);
+ 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));
+ assertFalse(CollectionUtils.addAll(c, inputIterable));
+ verify();
+ }
+
+ /**
+ * Records the next object returned for a mock iterator
+ */
+ private <T> void next(Iterator<T> iterator, T t) {
+ expect(iterator.hasNext()).andReturn(true);
+ expect(iterator.next()).andReturn(t);
}
-
}