Author: bayard
Date: Tue Sep 15 05:58:09 2009
New Revision: 815151
URL: http://svn.apache.org/viewvc?rev=815151&view=rev
Log:
Merging from -r468106:814127 of collections_jdk5_branch - namely where this
code was generified; mostly in r738956.
Also see the following revisions:
------------------------------------------------------------------------
r555925 | skestle | 2007-07-13 03:39:24 -0700 (Fri, 13 Jul 2007) | 2 lines
Added Edwin Tellman's patch for COLLECTIONS-243.
It all seems pretty reasonable, and it should all be checked again as the
project is worked through
------------------------------------------------------------------------
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java?rev=815151&r1=815150&r2=815151&view=diff
==============================================================================
---
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
(original)
+++
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestListUtils.java
Tue Sep 15 05:58:09 2009
@@ -19,6 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import junit.framework.Test;
@@ -45,8 +46,8 @@
private static final String x = "x";
private String[] fullArray;
- private List fullList;
-
+ private List<String> fullList;
+
public TestListUtils(String name) {
super(name);
}
@@ -57,33 +58,74 @@
public void setUp() {
fullArray = new String[]{a, b, c, d, e};
- fullList = new ArrayList(Arrays.asList(fullArray));
+ fullList = new ArrayList<String>(Arrays.asList(fullArray));
}
-
-
+
public void testNothing() {
}
-
- public void testpredicatedList() {
- Predicate predicate = new Predicate() {
+
+ /**
+ * Tests intersecting a non-empty list with an empty list.
+ */
+ public void testIntersectNonEmptyWithEmptyList() {
+ final List<String> empty = Collections.<String>emptyList();
+ assertTrue("result not empty", ListUtils.intersection(empty,
fullList).isEmpty());
+ }
+
+ /**
+ * Tests intersecting a non-empty list with an empty list.
+ */
+ public void testIntersectEmptyWithEmptyList() {
+ final List<?> empty = Collections.EMPTY_LIST;
+ assertTrue("result not empty", ListUtils.intersection(empty,
empty).isEmpty());
+ }
+
+ /**
+ * Tests intersecting a non-empty list with an subset of iteself.
+ */
+ public void testIntersectNonEmptySubset() {
+ // create a copy
+ final List<String> other = new ArrayList<String>(fullList);
+
+ // remove a few items
+ assertNotNull(other.remove(0));
+ assertNotNull(other.remove(1));
+
+ // make sure the intersection is equal to the copy
+ assertEquals(other, ListUtils.intersection(fullList, other));
+ }
+
+ /**
+ * Tests intersecting a non-empty list with an subset of iteself.
+ */
+ public void testIntersectListWithNoOverlapAndDifferentTypes() {
+ final List<Integer> other = Arrays.asList(1, 23);
+ assertTrue(ListUtils.intersection(fullList, other).isEmpty());
+ }
+
+ /**
+ * Tests intersecting a non-empty list with iteself.
+ */
+ public void testIntersectListWithSelf() {
+ assertEquals(fullList, ListUtils.intersection(fullList, fullList));
+ }
+
+ public void testPredicatedList() {
+ Predicate<Object> predicate = new Predicate<Object>() {
public boolean evaluate(Object o) {
return o instanceof String;
}
};
- List list =
- ListUtils.predicatedList(new ArrayStack(), predicate);
- assertTrue("returned object should be a PredicatedList",
- list instanceof PredicatedList);
+ List<Object> list = ListUtils.predicatedList(new ArrayStack<Object>(),
predicate);
+ assertTrue("returned object should be a PredicatedList", list
instanceof PredicatedList);
try {
- list =
- ListUtils.predicatedList(new ArrayStack(), null);
+ list = ListUtils.predicatedList(new ArrayStack<Object>(), null);
fail("Expecting IllegalArgumentException for null predicate.");
} catch (IllegalArgumentException ex) {
// expected
}
try {
- list =
- ListUtils.predicatedList(null, predicate);
+ list = ListUtils.predicatedList(null, predicate);
fail("Expecting IllegalArgumentException for null list.");
} catch (IllegalArgumentException ex) {
// expected
@@ -91,29 +133,29 @@
}
public void testLazyList() {
- List list = ListUtils.lazyList(new ArrayList(), new Factory() {
+ List<Integer> list = ListUtils.lazyList(new ArrayList<Integer>(), new
Factory<Integer>() {
private int index;
- public Object create() {
+ public Integer create() {
index++;
return new Integer(index);
}
});
- assertNotNull((Integer)list.get(5));
+ assertNotNull(list.get(5));
assertEquals(6, list.size());
- assertNotNull((Integer)list.get(5));
+ assertNotNull(list.get(5));
assertEquals(6, list.size());
}
public void testEquals() {
- Collection data = Arrays.asList( new String[] { "a", "b", "c" });
-
- List a = new ArrayList( data );
- List b = new ArrayList( data );
-
+ Collection<String> data = Arrays.asList( new String[] { "a", "b", "c"
});
+
+ List<String> a = new ArrayList<String>( data );
+ List<String> b = new ArrayList<String>( data );
+
assertEquals(true, a.equals(b));
assertEquals(true, ListUtils.isEqualList(a, b));
a.clear();
@@ -122,13 +164,13 @@
assertEquals(false, ListUtils.isEqualList(null, b));
assertEquals(true, ListUtils.isEqualList(null, null));
}
-
+
public void testHashCode() {
- Collection data = Arrays.asList( new String[] { "a", "b", "c" });
-
- List a = new ArrayList( data );
- List b = new ArrayList( data );
-
+ Collection<String> data = Arrays.asList( new String[] { "a", "b", "c"
});
+
+ List<String> a = new ArrayList<String>(data);
+ List<String> b = new ArrayList<String>(data);
+
assertEquals(true, a.hashCode() == b.hashCode());
assertEquals(true, a.hashCode() == ListUtils.hashCodeForList(a));
assertEquals(true, b.hashCode() == ListUtils.hashCodeForList(b));
@@ -137,20 +179,20 @@
assertEquals(false, ListUtils.hashCodeForList(a) ==
ListUtils.hashCodeForList(b));
assertEquals(0, ListUtils.hashCodeForList(null));
}
-
+
public void testRetainAll() {
- List sub = new ArrayList();
+ List<String> sub = new ArrayList<String>();
sub.add(a);
sub.add(b);
sub.add(x);
- List retained = ListUtils.retainAll(fullList, sub);
+ List<String> retained = ListUtils.retainAll(fullList, sub);
assertTrue(retained.size() == 2);
sub.remove(x);
assertTrue(retained.equals(sub));
fullList.retainAll(sub);
assertTrue(retained.equals(fullList));
-
+
try {
ListUtils.retainAll(null, null);
fail("expecting NullPointerException");
@@ -158,16 +200,16 @@
}
public void testRemoveAll() {
- List sub = new ArrayList();
+ List<String> sub = new ArrayList<String>();
sub.add(a);
sub.add(b);
sub.add(x);
- List remainder = ListUtils.removeAll(fullList, sub);
+ List<String> remainder = ListUtils.removeAll(fullList, sub);
assertTrue(remainder.size() == 3);
fullList.removeAll(sub);
assertTrue(remainder.equals(fullList));
-
+
try {
ListUtils.removeAll(null, null);
fail("expecting NullPointerException");
@@ -177,6 +219,7 @@
/**
* Tests the <code>indexOf</code> method in <code>ListUtils</code> class..
*/
+ // TODO: Generics
public void testIndexOf() {
Predicate testPredicate = PredicateUtils.equalPredicate("d");
int index = ListUtils.indexOf(fullList, testPredicate);