You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2009/01/29 19:48:47 UTC
svn commit: r738956 [27/34] - in
/commons/proper/collections/branches/collections_jdk5_branch/src:
java/org/apache/commons/collections/ java/org/apache/commons/collections/bag/
java/org/apache/commons/collections/bidimap/
java/org/apache/commons/collec...
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/AbstractTestList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/AbstractTestList.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/AbstractTestList.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/AbstractTestList.java Thu Jan 29 18:48:37 2009
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -47,17 +47,17 @@
* protected methods from AbstractTestCollection.
*
* @version $Revision$ $Date$
- *
+ *
* @author Rodney Waldhoff
* @author Paul Jack
* @author Stephen Colebourne
* @author Neil O'Toole
*/
-public abstract class AbstractTestList extends AbstractTestCollection {
+public abstract class AbstractTestList<E> extends AbstractTestCollection<E> {
/**
* JUnit constructor.
- *
+ *
* @param testName the test class name
*/
public AbstractTestList(String testName) {
@@ -66,7 +66,7 @@
//-----------------------------------------------------------------------
/**
- * Returns true if the collections produced by
+ * Returns true if the collections produced by
* {@link #makeCollection()} and {@link #makeFullCollection()}
* support the <code>set operation.<p>
* Default implementation returns true. Override if your collection
@@ -81,11 +81,12 @@
* Verifies that the test list implementation matches the confirmed list
* implementation.
*/
+ @SuppressWarnings("unchecked")
public void verify() {
super.verify();
- List list1 = getList();
- List list2 = getConfirmedList();
+ List<E> list1 = getCollection();
+ List<E> list2 = getConfirmed();
assertEquals("List should equal confirmed", list1, list2);
assertEquals("Confirmed should equal list", list2, list1);
@@ -93,9 +94,9 @@
assertEquals("Hash codes should be equal", list1.hashCode(), list2.hashCode());
int i = 0;
- Iterator iterator1 = list1.iterator();
- Iterator iterator2 = list2.iterator();
- Object[] array = list1.toArray();
+ Iterator<E> iterator1 = list1.iterator();
+ Iterator<E> iterator2 = list2.iterator();
+ E[] array = (E[]) list1.toArray();
while (iterator2.hasNext()) {
assertTrue("List iterator should have next", iterator1.hasNext());
Object o1 = iterator1.next();
@@ -120,35 +121,16 @@
/**
* Returns an empty {@link ArrayList}.
*/
- public Collection makeConfirmedCollection() {
- ArrayList list = new ArrayList();
+ public Collection<E> makeConfirmedCollection() {
+ ArrayList<E> list = new ArrayList<E>();
return list;
}
/**
* Returns a full {@link ArrayList}.
*/
- public Collection makeConfirmedFullCollection() {
- ArrayList list = new ArrayList();
- list.addAll(Arrays.asList(getFullElements()));
- return list;
- }
-
- /**
- * Return a new, empty {@link List} to be used for testing.
- *
- * @return an empty list for testing.
- */
- public abstract List makeEmptyList();
-
- /**
- * Return a new, full {@link List} to be used for testing.
- *
- * @return a full list for testing
- */
- public List makeFullList() {
- // only works if list supports optional "addAll(Collection)"
- List list = makeEmptyList();
+ public Collection<E> makeConfirmedFullCollection() {
+ ArrayList<E> list = new ArrayList<E>();
list.addAll(Arrays.asList(getFullElements()));
return list;
}
@@ -158,17 +140,16 @@
*
* @return an empty list to be used for testing
*/
- public final Collection makeCollection() {
- return makeEmptyList();
- }
+ public abstract List<E> makeObject();
/**
- * Returns {@link #makeFullList()}.
- *
- * @return a full list to be used for testing
+ * {@inheritDoc}
*/
- public final Collection makeFullCollection() {
- return makeFullList();
+ public List<E> makeFullCollection() {
+ // only works if list supports optional "addAll(Collection)"
+ List<E> list = makeObject();
+ list.addAll(Arrays.asList(getFullElements()));
+ return list;
}
//-----------------------------------------------------------------------
@@ -177,8 +158,9 @@
*
* @return the collection field as a List
*/
- public List getList() {
- return (List) collection;
+ @Override
+ public List<E> getCollection() {
+ return (List<E>) super.getCollection();
}
/**
@@ -186,8 +168,8 @@
*
* @return the confirmed field as a List
*/
- public List getConfirmedList() {
- return (List) confirmed;
+ public List<E> getConfirmed() {
+ return (List<E>) super.getConfirmed();
}
//-----------------------------------------------------------------------
@@ -200,11 +182,11 @@
return;
}
- List list;
- Object element = getOtherElements()[0];
+ List<E> list;
+ E element = getOtherElements()[0];
try {
- list = makeEmptyList();
+ list = makeObject();
list.add(Integer.MIN_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (IndexOutOfBoundsException e) {
@@ -212,7 +194,7 @@
}
try {
- list = makeEmptyList();
+ list = makeObject();
list.add(-1, element);
fail("List.add should throw IndexOutOfBoundsException [-1]");
} catch (IndexOutOfBoundsException e) {
@@ -220,7 +202,7 @@
}
try {
- list = makeEmptyList();
+ list = makeObject();
list.add(1, element);
fail("List.add should throw IndexOutOfBoundsException [1]");
} catch (IndexOutOfBoundsException e) {
@@ -228,7 +210,7 @@
}
try {
- list = makeEmptyList();
+ list = makeObject();
list.add(Integer.MAX_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (IndexOutOfBoundsException e) {
@@ -245,11 +227,11 @@
return;
}
- List list;
- Object element = getOtherElements()[0];
+ List<E> list;
+ E element = getOtherElements()[0];
try {
- list = makeFullList();
+ list = makeFullCollection();
list.add(Integer.MIN_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch (IndexOutOfBoundsException e) {
@@ -257,7 +239,7 @@
}
try {
- list = makeFullList();
+ list = makeFullCollection();
list.add(-1, element);
fail("List.add should throw IndexOutOfBoundsException [-1]");
} catch (IndexOutOfBoundsException e) {
@@ -265,7 +247,7 @@
}
try {
- list = makeFullList();
+ list = makeFullCollection();
list.add(list.size() + 1, element);
fail("List.add should throw IndexOutOfBoundsException [size + 1]");
} catch (IndexOutOfBoundsException e) {
@@ -273,7 +255,7 @@
}
try {
- list = makeFullList();
+ list = makeFullCollection();
list.add(Integer.MAX_VALUE, element);
fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch (IndexOutOfBoundsException e) {
@@ -289,13 +271,13 @@
return;
}
- Object element = getOtherElements()[0];
+ E element = getOtherElements()[0];
int max = getFullElements().length;
for (int i = 0; i <= max; i++) {
resetFull();
- ((List) collection).add(i, element);
- ((List) confirmed).add(i, element);
+ ((List<E>) getCollection()).add(i, element);
+ ((List<E>) getConfirmed()).add(i, element);
verify();
}
}
@@ -305,13 +287,13 @@
*/
public void testListEquals() {
resetEmpty();
- List list = getList();
- assertEquals("Empty lists should be equal", true, list.equals(confirmed));
+ List<E> list = getCollection();
+ assertEquals("Empty lists should be equal", true, list.equals(getConfirmed()));
verify();
assertEquals("Empty list should equal self", true, list.equals(list));
verify();
- List list2 = Arrays.asList(getFullElements());
+ List<E> list2 = Arrays.asList(getFullElements());
assertEquals("Empty list shouldn't equal full", false, list.equals(list2));
verify();
@@ -320,13 +302,13 @@
verify();
resetFull();
- list = getList();
- assertEquals("Full lists should be equal", true, list.equals(confirmed));
+ list = getCollection();
+ assertEquals("Full lists should be equal", true, list.equals(getConfirmed()));
verify();
assertEquals("Full list should equal self", true, list.equals(list));
verify();
- list2 = makeEmptyList();
+ list2 = makeObject();
assertEquals("Full list shouldn't equal empty", false, list.equals(list2));
verify();
@@ -338,8 +320,8 @@
if (list2.size() < 2 && isAddSupported()) {
// main list is only size 1, so lets add other elements to get a better list
list.addAll(Arrays.asList(getOtherElements()));
- confirmed.addAll(Arrays.asList(getOtherElements()));
- list2 = new ArrayList(list2);
+ getConfirmed().addAll(Arrays.asList(getOtherElements()));
+ list2 = new ArrayList<E>(list2);
list2.addAll(Arrays.asList(getOtherElements()));
}
if (list2.size() > 1) {
@@ -351,17 +333,17 @@
}
resetFull();
- list = getList();
+ list = getCollection();
assertEquals("List shouldn't equal String", false, list.equals(""));
verify();
- final List listForC = Arrays.asList(getFullElements());
- Collection c = new AbstractCollection() {
+ final List<E> listForC = Arrays.asList(getFullElements());
+ Collection<E> c = new AbstractCollection<E>() {
public int size() {
return listForC.size();
}
- public Iterator iterator() {
+ public Iterator<E> iterator() {
return listForC.iterator();
}
};
@@ -375,14 +357,14 @@
*/
public void testListHashCode() {
resetEmpty();
- int hash1 = collection.hashCode();
- int hash2 = confirmed.hashCode();
+ int hash1 = getCollection().hashCode();
+ int hash2 = getConfirmed().hashCode();
assertEquals("Empty lists should have equal hashCodes", hash1, hash2);
verify();
resetFull();
- hash1 = collection.hashCode();
- hash2 = confirmed.hashCode();
+ hash1 = getCollection().hashCode();
+ hash2 = getConfirmed().hashCode();
assertEquals("Full lists should have equal hashCodes", hash1, hash2);
verify();
}
@@ -392,8 +374,8 @@
*/
public void testListGetByIndex() {
resetFull();
- List list = getList();
- Object[] elements = getFullElements();
+ List<E> list = getCollection();
+ E[] elements = getFullElements();
for (int i = 0; i < elements.length; i++) {
assertEquals("List should contain correct elements", elements[i], list.get(i));
verify();
@@ -405,7 +387,7 @@
* empty list.
*/
public void testListGetByIndexBoundsChecking() {
- List list = makeEmptyList();
+ List<E> list = makeObject();
try {
list.get(Integer.MIN_VALUE);
@@ -448,7 +430,7 @@
* full list.
*/
public void testListGetByIndexBoundsChecking2() {
- List list = makeFullList();
+ List<E> list = makeFullCollection();
try {
list.get(Integer.MIN_VALUE);
@@ -484,10 +466,10 @@
*/
public void testListIndexOf() {
resetFull();
- List list1 = getList();
- List list2 = getConfirmedList();
+ List<E> list1 = getCollection();
+ List<E> list2 = getConfirmed();
- Iterator iterator = list2.iterator();
+ Iterator<E> iterator = list2.iterator();
while (iterator.hasNext()) {
Object element = iterator.next();
assertEquals("indexOf should return correct result",
@@ -495,7 +477,7 @@
verify();
}
- Object[] other = getOtherElements();
+ E[] other = getOtherElements();
for (int i = 0; i < other.length; i++) {
assertEquals("indexOf should return -1 for nonexistent element",
list1.indexOf(other[i]), -1);
@@ -508,18 +490,18 @@
*/
public void testListLastIndexOf() {
resetFull();
- List list1 = getList();
- List list2 = getConfirmedList();
+ List<E> list1 = getCollection();
+ List<E> list2 = getConfirmed();
- Iterator iterator = list2.iterator();
+ Iterator<E> iterator = list2.iterator();
while (iterator.hasNext()) {
- Object element = iterator.next();
+ E element = iterator.next();
assertEquals("lastIndexOf should return correct result",
list1.lastIndexOf(element), list2.lastIndexOf(element));
verify();
}
- Object[] other = getOtherElements();
+ E[] other = getOtherElements();
for (int i = 0; i < other.length; i++) {
assertEquals("lastIndexOf should return -1 for nonexistent " +
"element", list1.lastIndexOf(other[i]), -1);
@@ -536,8 +518,8 @@
return;
}
- List list = makeEmptyList();
- Object element = getOtherElements()[0];
+ List<E> list = makeObject();
+ E element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
@@ -583,8 +565,8 @@
public void testListSetByIndexBoundsChecking2() {
if (!isSetSupported()) return;
- List list = makeFullList();
- Object element = getOtherElements()[0];
+ List<E> list = makeFullCollection();
+ E element = getOtherElements()[0];
try {
list.set(Integer.MIN_VALUE, element);
@@ -592,21 +574,21 @@
"[Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.set(-1, element);
fail("List.set should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.set(getFullElements().length, element);
fail("List.set should throw IndexOutOfBoundsException [size]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.set(Integer.MAX_VALUE, element);
@@ -614,7 +596,7 @@
"[Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
}
@@ -625,29 +607,28 @@
if (!isSetSupported()) return;
resetFull();
- Object[] elements = getFullElements();
- Object[] other = getOtherElements();
+ E[] elements = getFullElements();
+ E[] other = getOtherElements();
for (int i = 0; i < elements.length; i++) {
- Object n = other[i % other.length];
- Object v = ((List)collection).set(i, n);
+ E n = other[i % other.length];
+ E v = ((List<E>) getCollection()).set(i, n);
assertEquals("Set should return correct element", elements[i], v);
- ((List)confirmed).set(i, n);
+ ((List<E>) getConfirmed()).set(i, n);
verify();
}
}
-
/**
* If {@link #isSetSupported()} returns false, tests that set operation
* raises <Code>UnsupportedOperationException.
*/
public void testUnsupportedSet() {
if (isSetSupported()) return;
-
+
resetFull();
try {
- ((List) collection).set(0, new Object());
+ ((List<E>) getCollection()).set(0, getFullElements()[0]);
fail("Emtpy collection should not support set.");
} catch (UnsupportedOperationException e) {
// expected
@@ -656,7 +637,6 @@
// thrown.
verify();
}
-
/**
* Tests bounds checking for {@link List#remove(int)} on an
@@ -665,47 +645,44 @@
public void testListRemoveByIndexBoundsChecking() {
if (!isRemoveSupported()) return;
- List list = makeEmptyList();
+ List<E> list = makeObject();
try {
list.remove(Integer.MIN_VALUE);
- fail("List.remove should throw IndexOutOfBoundsException " +
- "[Integer.MIN_VALUE]");
+ fail("List.remove should throw IndexOutOfBoundsException [Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(0);
fail("List.remove should throw IndexOutOfBoundsException [0]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(1);
fail("List.remove should throw IndexOutOfBoundsException [1]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(Integer.MAX_VALUE);
- fail("List.remove should throw IndexOutOfBoundsException " +
- "[Integer.MAX_VALUE]");
+ fail("List.remove should throw IndexOutOfBoundsException [Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
}
}
-
/**
* Tests bounds checking for {@link List#remove(int)} on a
* full list.
@@ -713,7 +690,7 @@
public void testListRemoveByIndexBoundsChecking2() {
if (!isRemoveSupported()) return;
- List list = makeFullList();
+ List<E> list = makeFullCollection();
try {
list.remove(Integer.MIN_VALUE);
@@ -721,21 +698,21 @@
"[Integer.MIN_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(-1);
fail("List.remove should throw IndexOutOfBoundsException [-1]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(getFullElements().length);
fail("List.remove should throw IndexOutOfBoundsException [size]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
try {
list.remove(Integer.MAX_VALUE);
@@ -743,7 +720,7 @@
"[Integer.MAX_VALUE]");
} catch(IndexOutOfBoundsException e) {
// expected
- }
+ }
}
@@ -756,44 +733,42 @@
int max = getFullElements().length;
for (int i = 0; i < max; i++) {
resetFull();
- Object o1 = ((List)collection).remove(i);
- Object o2 = ((List)confirmed).remove(i);
+ E o1 = ((List<E>) getCollection()).remove(i);
+ E o2 = ((List<E>) getConfirmed()).remove(i);
assertEquals("remove should return correct element", o1, o2);
verify();
}
}
-
/**
* Tests the read-only bits of {@link List#listIterator()}.
*/
public void testListListIterator() {
resetFull();
- forwardTest(getList().listIterator(), 0);
- backwardTest(getList().listIterator(), 0);
+ forwardTest(getCollection().listIterator(), 0);
+ backwardTest(getCollection().listIterator(), 0);
}
-
/**
* Tests the read-only bits of {@link List#listIterator(int)}.
*/
public void testListListIteratorByIndex() {
resetFull();
try {
- getList().listIterator(-1);
+ getCollection().listIterator(-1);
} catch (IndexOutOfBoundsException ex) {}
resetFull();
try {
- getList().listIterator(getList().size() + 1);
+ getCollection().listIterator(getCollection().size() + 1);
} catch (IndexOutOfBoundsException ex) {}
resetFull();
- for (int i = 0; i <= confirmed.size(); i++) {
- forwardTest(getList().listIterator(i), i);
- backwardTest(getList().listIterator(i), i);
+ for (int i = 0; i <= getConfirmed().size(); i++) {
+ forwardTest(getCollection().listIterator(i), i);
+ backwardTest(getCollection().listIterator(i), i);
}
resetFull();
- for (int i = 0; i <= confirmed.size(); i++) {
- backwardTest(getList().listIterator(i), i);
+ for (int i = 0; i <= getConfirmed().size(); i++) {
+ backwardTest(getCollection().listIterator(i), i);
}
}
@@ -804,25 +779,25 @@
public void testListListIteratorPreviousRemoveNext() {
if (isRemoveSupported() == false) return;
resetFull();
- if (collection.size() < 4) return;
- ListIterator it = getList().listIterator();
- Object zero = it.next();
- Object one = it.next();
- Object two = it.next();
- Object two2 = it.previous();
- Object one2 = it.previous();
+ if (getCollection().size() < 4) return;
+ ListIterator<E> it = getCollection().listIterator();
+ E zero = it.next();
+ E one = it.next();
+ E two = it.next();
+ E two2 = it.previous();
+ E one2 = it.previous();
assertEquals(one, one2);
assertEquals(two, two2);
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- assertEquals(two, getList().get(2));
-
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ assertEquals(two, getCollection().get(2));
+
it.remove(); // removed element at index 1 (one)
- assertEquals(zero, getList().get(0));
- assertEquals(two, getList().get(1));
- Object two3 = it.next(); // do next after remove
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(two, getCollection().get(1));
+ E two3 = it.next(); // do next after remove
assertEquals(two, two3);
- assertEquals(collection.size() > 2, it.hasNext());
+ assertEquals(getCollection().size() > 2, it.hasNext());
assertEquals(true, it.hasPrevious());
}
@@ -832,26 +807,26 @@
public void testListListIteratorPreviousRemovePrevious() {
if (isRemoveSupported() == false) return;
resetFull();
- if (collection.size() < 4) return;
- ListIterator it = getList().listIterator();
- Object zero = it.next();
- Object one = it.next();
- Object two = it.next();
- Object two2 = it.previous();
- Object one2 = it.previous();
+ if (getCollection().size() < 4) return;
+ ListIterator<E> it = getCollection().listIterator();
+ E zero = it.next();
+ E one = it.next();
+ E two = it.next();
+ E two2 = it.previous();
+ E one2 = it.previous();
assertEquals(one, one2);
assertEquals(two, two2);
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- assertEquals(two, getList().get(2));
-
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ assertEquals(two, getCollection().get(2));
+
it.remove(); // removed element at index 1 (one)
- assertEquals(zero, getList().get(0));
- assertEquals(two, getList().get(1));
- Object zero3 = it.previous(); // do previous after remove
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(two, getCollection().get(1));
+ E zero3 = it.previous(); // do previous after remove
assertEquals(zero, zero3);
assertEquals(false, it.hasPrevious());
- assertEquals(collection.size() > 2, it.hasNext());
+ assertEquals(getCollection().size() > 2, it.hasNext());
}
/**
@@ -860,22 +835,22 @@
public void testListListIteratorNextRemoveNext() {
if (isRemoveSupported() == false) return;
resetFull();
- if (collection.size() < 4) return;
- ListIterator it = getList().listIterator();
- Object zero = it.next();
- Object one = it.next();
- Object two = it.next();
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- assertEquals(two, getList().get(2));
- Object three = getList().get(3);
-
+ if (getCollection().size() < 4) return;
+ ListIterator<E> it = getCollection().listIterator();
+ E zero = it.next();
+ E one = it.next();
+ E two = it.next();
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ assertEquals(two, getCollection().get(2));
+ E three = getCollection().get(3);
+
it.remove(); // removed element at index 2 (two)
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- Object three2 = it.next(); // do next after remove
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ E three2 = it.next(); // do next after remove
assertEquals(three, three2);
- assertEquals(collection.size() > 3, it.hasNext());
+ assertEquals(getCollection().size() > 3, it.hasNext());
assertEquals(true, it.hasPrevious());
}
@@ -885,19 +860,19 @@
public void testListListIteratorNextRemovePrevious() {
if (isRemoveSupported() == false) return;
resetFull();
- if (collection.size() < 4) return;
- ListIterator it = getList().listIterator();
- Object zero = it.next();
- Object one = it.next();
- Object two = it.next();
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- assertEquals(two, getList().get(2));
-
+ if (getCollection().size() < 4) return;
+ ListIterator<E> it = getCollection().listIterator();
+ E zero = it.next();
+ E one = it.next();
+ E two = it.next();
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ assertEquals(two, getCollection().get(2));
+
it.remove(); // removed element at index 2 (two)
- assertEquals(zero, getList().get(0));
- assertEquals(one, getList().get(1));
- Object one2 = it.previous(); // do previous after remove
+ assertEquals(zero, getCollection().get(0));
+ assertEquals(one, getCollection().get(1));
+ E one2 = it.previous(); // do previous after remove
assertEquals(one, one2);
assertEquals(true, it.hasNext());
assertEquals(true, it.hasPrevious());
@@ -910,13 +885,13 @@
* @param iter the iterator to traverse
* @param i the starting index
*/
- private void forwardTest(ListIterator iter, int i) {
- List list = getList();
+ private void forwardTest(ListIterator<E> iter, int i) {
+ List<E> list = getCollection();
int max = getFullElements().length;
while (i < max) {
assertTrue("Iterator should have next", iter.hasNext());
- assertEquals("Iterator.nextIndex should work",
+ assertEquals("Iterator.nextIndex should work",
iter.nextIndex(), i);
assertEquals("Iterator.previousIndex should work",
iter.previousIndex(), i - 1);
@@ -927,8 +902,7 @@
assertTrue("Iterator shouldn't have next", !iter.hasNext());
assertEquals("nextIndex should be size", iter.nextIndex(), max);
- assertEquals("previousIndex should be size - 1",
- iter.previousIndex(), max - 1);
+ assertEquals("previousIndex should be size - 1", iter.previousIndex(), max - 1);
try {
iter.next();
@@ -938,21 +912,20 @@
}
}
-
/**
* Traverses to the beginning of the given iterator.
*
* @param iter the iterator to traverse
* @param i the starting index
*/
- private void backwardTest(ListIterator iter, int i) {
- List list = getList();
+ private void backwardTest(ListIterator<E> iter, int i) {
+ List<E> list = getCollection();
while (i > 0) {
assertTrue("Iterator should have previous, i:" + i, iter.hasPrevious());
assertEquals("Iterator.nextIndex should work, i:" + i, iter.nextIndex(), i);
assertEquals("Iterator.previousIndex should work, i:" + i, iter.previousIndex(), i - 1);
- Object o = iter.previous();
+ E o = iter.previous();
assertEquals("Iterator returned correct element", list.get(i - 1), o);
i--;
}
@@ -981,12 +954,12 @@
if (!isAddSupported()) return;
resetEmpty();
- List list1 = getList();
- List list2 = getConfirmedList();
+ List<E> list1 = getCollection();
+ List<E> list2 = getConfirmed();
- Object[] elements = getFullElements();
- ListIterator iter1 = list1.listIterator();
- ListIterator iter2 = list2.listIterator();
+ E[] elements = getFullElements();
+ ListIterator<E> iter1 = list1.listIterator();
+ ListIterator<E> iter2 = list2.listIterator();
for (int i = 0; i < elements.length; i++) {
iter1.add(elements[i]);
@@ -995,8 +968,8 @@
}
resetFull();
- iter1 = getList().listIterator();
- iter2 = getConfirmedList().listIterator();
+ iter1 = getCollection().listIterator();
+ iter2 = getConfirmed().listIterator();
for (int i = 0; i < elements.length; i++) {
iter1.next();
iter2.next();
@@ -1006,7 +979,6 @@
}
}
-
/**
* Tests the {@link ListIterator#set(Object)} method of the list
* iterator.
@@ -1014,11 +986,11 @@
public void testListIteratorSet() {
if (!isSetSupported()) return;
- Object[] elements = getFullElements();
+ E[] elements = getFullElements();
resetFull();
- ListIterator iter1 = getList().listIterator();
- ListIterator iter2 = getConfirmedList().listIterator();
+ ListIterator<E> iter1 = getCollection().listIterator();
+ ListIterator<E> iter2 = getConfirmed().listIterator();
for (int i = 0; i < elements.length; i++) {
iter1.next();
iter2.next();
@@ -1028,27 +1000,26 @@
}
}
-
- public void testEmptyListSerialization()
- throws IOException, ClassNotFoundException {
- List list = makeEmptyList();
+ @SuppressWarnings("unchecked")
+ public void testEmptyListSerialization() throws IOException, ClassNotFoundException {
+ List<E> list = makeObject();
if (!(list instanceof Serializable && isTestSerialization())) return;
-
+
byte[] objekt = writeExternalFormToBytes((Serializable) list);
- List list2 = (List) readExternalFormFromBytes(objekt);
+ List<E> list2 = (List<E>) readExternalFormFromBytes(objekt);
assertTrue("Both lists are empty",list.size() == 0);
assertTrue("Both lists are empty",list2.size() == 0);
}
- public void testFullListSerialization()
- throws IOException, ClassNotFoundException {
- List list = makeFullList();
+ @SuppressWarnings("unchecked")
+ public void testFullListSerialization() throws IOException, ClassNotFoundException {
+ List<E> list = makeFullCollection();
int size = getFullElements().length;
if (!(list instanceof Serializable && isTestSerialization())) return;
-
+
byte[] objekt = writeExternalFormToBytes((Serializable) list);
- List list2 = (List) readExternalFormFromBytes(objekt);
+ List<E> list2 = (List<E>) readExternalFormFromBytes(objekt);
assertEquals("Both lists are same size",list.size(), size);
assertEquals("Both lists are same size",list2.size(), size);
@@ -1069,19 +1040,21 @@
* Compare the current serialized form of the List
* against the canonical version in CVS.
*/
+ @SuppressWarnings("unchecked")
public void testEmptyListCompatibility() throws IOException, ClassNotFoundException {
/**
* Create canonical objects with this code
List list = makeEmptyList();
if (!(list instanceof Serializable)) return;
-
+
writeExternalFormToDisk((Serializable) list, getCanonicalEmptyCollectionName(list));
*/
// test to make sure the canonical form has been preserved
- List list = makeEmptyList();
- if(list instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
- List list2 = (List) readExternalFormFromDisk(getCanonicalEmptyCollectionName(list));
+ List<E> list = makeObject();
+ if (list instanceof Serializable && !skipSerializedCanonicalTests()
+ && isTestSerialization()) {
+ List<E> list2 = (List<E>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(list));
assertTrue("List is empty",list2.size() == 0);
assertEquals(list, list2);
}
@@ -1091,19 +1064,20 @@
* Compare the current serialized form of the List
* against the canonical version in CVS.
*/
+ @SuppressWarnings("unchecked")
public void testFullListCompatibility() throws IOException, ClassNotFoundException {
/**
* Create canonical objects with this code
List list = makeFullList();
if (!(list instanceof Serializable)) return;
-
+
writeExternalFormToDisk((Serializable) list, getCanonicalFullCollectionName(list));
*/
// test to make sure the canonical form has been preserved
- List list = makeFullList();
+ List<E> list = makeFullCollection();
if(list instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
- List list2 = (List) readExternalFormFromDisk(getCanonicalFullCollectionName(list));
+ List<E> list2 = (List<E>) readExternalFormFromDisk(getCanonicalFullCollectionName(list));
if (list2.size() == 4) {
// old serialized tests
return;
@@ -1126,32 +1100,28 @@
*/
public BulkTest bulkTestSubList() {
if (getFullElements().length - 6 < 10) return null;
- return new BulkTestSubList(this);
+ return new BulkTestSubList<E>(this);
}
+ public static class BulkTestSubList<E> extends AbstractTestList<E> {
- public static class BulkTestSubList extends AbstractTestList {
-
- private AbstractTestList outer;
+ private AbstractTestList<E> outer;
-
- public BulkTestSubList(AbstractTestList outer) {
+ public BulkTestSubList(AbstractTestList<E> outer) {
super("");
this.outer = outer;
}
-
- public Object[] getFullElements() {
- List l = Arrays.asList(outer.getFullElements());
- return l.subList(3, l.size() - 3).toArray();
+ @SuppressWarnings("unchecked")
+ public E[] getFullElements() {
+ List<E> l = Arrays.asList(outer.getFullElements());
+ return (E[]) l.subList(3, l.size() - 3).toArray();
}
-
- public Object[] getOtherElements() {
+ public E[] getOtherElements() {
return outer.getOtherElements();
}
-
public boolean isAddSupported() {
return outer.isAddSupported();
}
@@ -1164,32 +1134,28 @@
return outer.isRemoveSupported();
}
-
- public List makeEmptyList() {
- return outer.makeFullList().subList(4, 4);
+ public List<E> makeObject() {
+ return outer.makeFullCollection().subList(4, 4);
}
-
- public List makeFullList() {
+ public List<E> makeFullCollection() {
int size = getFullElements().length;
- return outer.makeFullList().subList(3, size - 3);
+ return outer.makeFullCollection().subList(3, size - 3);
}
-
public void resetEmpty() {
outer.resetFull();
- this.collection = outer.getList().subList(4, 4);
- this.confirmed = outer.getConfirmedList().subList(4, 4);
+ this.setCollection(outer.getCollection().subList(4, 4));
+ this.setConfirmed(outer.getConfirmed().subList(4, 4));
}
public void resetFull() {
outer.resetFull();
- int size = outer.confirmed.size();
- this.collection = outer.getList().subList(3, size - 3);
- this.confirmed = outer.getConfirmedList().subList(3, size - 3);
+ int size = outer.getConfirmed().size();
+ this.setCollection(outer.getCollection().subList(3, size - 3));
+ this.setConfirmed(outer.getConfirmed().subList(3, size - 3));
}
-
public void verify() {
super.verify();
outer.verify();
@@ -1200,7 +1166,6 @@
}
}
-
/**
* Tests that a sublist raises a {@link java.util.ConcurrentModificationException ConcurrentModificationException}
* if elements are added to the original list.
@@ -1210,29 +1175,27 @@
if (!isAddSupported()) return;
resetFull();
- int size = collection.size();
- List sub = getList().subList(1, size);
- getList().add(getOtherElements()[0]);
+ int size = getCollection().size();
+ List<E> sub = getCollection().subList(1, size);
+ getCollection().add(getOtherElements()[0]);
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().add(0, getOtherElements()[0]);
+ sub = getCollection().subList(1, size);
+ getCollection().add(0, getOtherElements()[0]);
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().addAll(Arrays.asList(getOtherElements()));
+ sub = getCollection().subList(1, size);
+ getCollection().addAll(Arrays.asList(getOtherElements()));
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().addAll(0, Arrays.asList(getOtherElements()));
+ sub = getCollection().subList(1, size);
+ getCollection().addAll(0, Arrays.asList(getOtherElements()));
failFastAll(sub);
-
}
-
/**
* Tests that a sublist raises a {@link java.util.ConcurrentModificationException ConcurrentModificationException}
* if elements are removed from the original list.
@@ -1242,45 +1205,43 @@
if (!isRemoveSupported()) return;
resetFull();
- int size = collection.size();
- List sub = getList().subList(1, size);
- getList().remove(0);
+ int size = getCollection().size();
+ List<E> sub = getCollection().subList(1, size);
+ getCollection().remove(0);
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().remove(getFullElements()[2]);
+ sub = getCollection().subList(1, size);
+ getCollection().remove(getFullElements()[2]);
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().removeAll(Arrays.asList(getFullElements()));
+ sub = getCollection().subList(1, size);
+ getCollection().removeAll(Arrays.asList(getFullElements()));
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().retainAll(Arrays.asList(getOtherElements()));
+ sub = getCollection().subList(1, size);
+ getCollection().retainAll(Arrays.asList(getOtherElements()));
failFastAll(sub);
resetFull();
- sub = getList().subList(1, size);
- getList().clear();
+ sub = getCollection().subList(1, size);
+ getCollection().clear();
failFastAll(sub);
}
-
/**
* Invokes all the methods on the given sublist to make sure they raise
* a {@link java.util.ConcurrentModificationException ConcurrentModificationException}.
*/
- protected void failFastAll(List list) {
+ protected void failFastAll(List<E> list) {
Method[] methods = List.class.getMethods();
for (int i = 0; i < methods.length; i++) {
failFastMethod(list, methods[i]);
}
}
-
/**
* Invokes the given method on the given sublist to make sure it raises
* a {@link java.util.ConcurrentModificationException ConcurrentModificationException}.
@@ -1293,13 +1254,13 @@
* @param list the sublist to test
* @param m the method to invoke
*/
- protected void failFastMethod(List list, Method m) {
+ protected void failFastMethod(List<E> list, Method m) {
if (m.getName().equals("equals")) return;
- Object element = getOtherElements()[0];
- Collection c = Collections.singleton(element);
+ E element = getOtherElements()[0];
+ Collection<E> c = Collections.singleton(element);
- Class[] types = m.getParameterTypes();
+ Class<?>[] types = m.getParameterTypes();
Object[] params = new Object[types.length];
for (int i = 0; i < params.length; i++) {
if (types[i] == Integer.TYPE) params[i] = new Integer(0);
@@ -1328,16 +1289,16 @@
public BulkTest bulkTestListIterator() {
return new TestListIterator();
}
-
- public class TestListIterator extends AbstractTestListIterator {
+
+ public class TestListIterator extends AbstractTestListIterator<E> {
public TestListIterator() {
super("TestListIterator");
}
-
- public Object addSetValue() {
+
+ public E addSetValue() {
return AbstractTestList.this.getOtherElements()[0];
}
-
+
public boolean supportsRemove() {
return AbstractTestList.this.isRemoveSupported();
}
@@ -1350,15 +1311,15 @@
return AbstractTestList.this.isSetSupported();
}
- public ListIterator makeEmptyListIterator() {
+ public ListIterator<E> makeEmptyIterator() {
resetEmpty();
- return ((List) AbstractTestList.this.collection).listIterator();
+ return AbstractTestList.this.getCollection().listIterator();
}
- public ListIterator makeFullListIterator() {
+ public ListIterator<E> makeObject() {
resetFull();
- return ((List) AbstractTestList.this.collection).listIterator();
+ return AbstractTestList.this.getCollection().listIterator();
}
}
-
+
}
Modified: commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java?rev=738956&r1=738955&r2=738956&view=diff
==============================================================================
--- commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java (original)
+++ commons/proper/collections/branches/collections_jdk5_branch/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java Thu Jan 29 18:48:37 2009
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,107 +20,111 @@
/**
* Test case for {@link AbstractLinkedList}.
- *
+ *
* @version $Revision$ $Date$
- *
+ *
* @author Rich Dougherty
* @author David Hay
* @author Phil Steitz
*/
-public abstract class TestAbstractLinkedList extends AbstractTestList {
-
+public abstract class TestAbstractLinkedList<E> extends AbstractTestList<E> {
+
public TestAbstractLinkedList(String testName) {
super(testName);
}
- //-----------------------------------------------------------------------
+ //-----------------------------------------------------------------------
+ @SuppressWarnings("unchecked")
public void testRemoveFirst() {
resetEmpty();
- AbstractLinkedList list = (AbstractLinkedList) collection;
+ AbstractLinkedList<E> list = getCollection();
if (isRemoveSupported() == false) {
try {
list.removeFirst();
} catch (UnsupportedOperationException ex) {}
- }
-
- list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
- assertEquals( "value1", list.removeFirst() );
+ }
+
+ list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
+ assertEquals("value1", list.removeFirst());
checkNodes();
- list.addLast( "value3");
+ list.addLast((E) "value3");
checkNodes();
- assertEquals( "value2", list.removeFirst() );
- assertEquals( "value3", list.removeFirst() );
+ assertEquals("value2", list.removeFirst());
+ assertEquals("value3", list.removeFirst());
checkNodes();
- list.addLast( "value4" );
+ list.addLast((E) "value4");
checkNodes();
- assertEquals( "value4", list.removeFirst() );
+ assertEquals("value4", list.removeFirst());
checkNodes();
}
-
+
+ @SuppressWarnings("unchecked")
public void testRemoveLast() {
resetEmpty();
- AbstractLinkedList list = (AbstractLinkedList) collection;
+ AbstractLinkedList<E> list = getCollection();
if (isRemoveSupported() == false) {
try {
list.removeLast();
} catch (UnsupportedOperationException ex) {}
- }
-
- list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
- assertEquals( "value2", list.removeLast() );
- list.addFirst( "value3");
- checkNodes();
- assertEquals( "value1", list.removeLast() );
- assertEquals( "value3", list.removeLast() );
- list.addFirst( "value4" );
+ }
+
+ list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
+ assertEquals("value2", list.removeLast());
+ list.addFirst((E) "value3");
+ checkNodes();
+ assertEquals("value1", list.removeLast());
+ assertEquals("value3", list.removeLast());
+ list.addFirst((E) "value4");
checkNodes();
- assertEquals( "value4", list.removeFirst() );
+ assertEquals("value4", list.removeFirst());
}
-
+
+ @SuppressWarnings("unchecked")
public void testAddNodeAfter() {
resetEmpty();
- AbstractLinkedList list = (AbstractLinkedList) collection;
+ AbstractLinkedList<E> list = getCollection();
if (isAddSupported() == false) {
try {
list.addFirst(null);
} catch (UnsupportedOperationException ex) {}
- }
-
- list.addFirst("value1");
- list.addNodeAfter(list.getNode(0,false),"value2");
+ }
+
+ list.addFirst((E) "value1");
+ list.addNodeAfter(list.getNode(0, false), (E) "value2");
assertEquals("value1", list.getFirst());
assertEquals("value2", list.getLast());
list.removeFirst();
checkNodes();
- list.addNodeAfter(list.getNode(0,false),"value3");
+ list.addNodeAfter(list.getNode(0, false), (E) "value3");
checkNodes();
assertEquals("value2", list.getFirst());
assertEquals("value3", list.getLast());
- list.addNodeAfter(list.getNode(0, false),"value4");
+ list.addNodeAfter(list.getNode(0, false), (E) "value4");
checkNodes();
assertEquals("value2", list.getFirst());
assertEquals("value3", list.getLast());
assertEquals("value4", list.get(1));
- list.addNodeAfter(list.getNode(2, false), "value5");
+ list.addNodeAfter(list.getNode(2, false), (E) "value5");
checkNodes();
assertEquals("value2", list.getFirst());
assertEquals("value4", list.get(1));
assertEquals("value3", list.get(2));
assertEquals("value5", list.getLast());
}
-
+
+ @SuppressWarnings("unchecked")
public void testRemoveNode() {
resetEmpty();
if (isAddSupported() == false || isRemoveSupported() == false) return;
- AbstractLinkedList list = (AbstractLinkedList) collection;
-
- list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
+ AbstractLinkedList<E> list = getCollection();
+
+ list.addAll(Arrays.asList((E[]) new String[] { "value1", "value2" }));
list.removeNode(list.getNode(0, false));
checkNodes();
assertEquals("value2", list.getFirst());
assertEquals("value2", list.getLast());
- list.addFirst("value1");
- list.addFirst("value0");
+ list.addFirst((E) "value1");
+ list.addFirst((E) "value0");
checkNodes();
list.removeNode(list.getNode(1, false));
assertEquals("value0", list.getFirst());
@@ -131,53 +135,61 @@
assertEquals("value0", list.getLast());
checkNodes();
}
-
+
+ @SuppressWarnings("unchecked")
public void testGetNode() {
resetEmpty();
- AbstractLinkedList list = (AbstractLinkedList) collection;
+ AbstractLinkedList<E> list = getCollection();
// get marker
assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
try {
- Object obj = list.getNode(0, false);
+ list.getNode(0, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
- list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
+ list.addAll( Arrays.asList((E[]) new String[]{"value1", "value2"}));
checkNodes();
- list.addFirst("value0");
+ list.addFirst((E) "value0");
checkNodes();
list.removeNode(list.getNode(1, false));
checkNodes();
try {
- Object obj = list.getNode(2, false);
+ list.getNode(2, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
- Object obj = list.getNode(-1, false);
+ list.getNode(-1, false);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
- Object obj = list.getNode(3, true);
+ list.getNode(3, true);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
- }
+ }
}
-
+
protected void checkNodes() {
- AbstractLinkedList list = (AbstractLinkedList) collection;
+ AbstractLinkedList<E> list = getCollection();
for (int i = 0; i < list.size; i++) {
assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true));
if (i < list.size - 1) {
- assertEquals(list.getNode(i + 1, false).previous,
- list.getNode(i, false));
+ assertEquals(list.getNode(i + 1, false).previous,
+ list.getNode(i, false));
}
}
}
-
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public AbstractLinkedList<E> getCollection() {
+ return (AbstractLinkedList<E>) super.getCollection();
+ }
}