http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/test/org/apache/commons/collections/keyvalue/TestMultiKey.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/keyvalue/TestMultiKey.java b/src/test/org/apache/commons/collections/keyvalue/TestMultiKey.java index 4ebef26..6202632 100644 --- a/src/test/org/apache/commons/collections/keyvalue/TestMultiKey.java +++ b/src/test/org/apache/commons/collections/keyvalue/TestMultiKey.java @@ -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. @@ -25,9 +25,9 @@ import junit.framework.TestSuite; /** * Unit tests for {@link org.apache.commons.collections.MultiKey}. - * + * * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ public class TestMultiKey extends TestCase { @@ -37,7 +37,7 @@ public class TestMultiKey extends TestCase { Integer THREE = new Integer(3); Integer FOUR = new Integer(4); Integer FIVE = new Integer(5); - + public TestMultiKey(String name) { super(name); } @@ -58,87 +58,87 @@ public class TestMultiKey extends TestCase { protected void tearDown() throws Exception { super.tearDown(); } - + //----------------------------------------------------------------------- public void testConstructors() throws Exception { - MultiKey mk = null; - mk = new MultiKey(ONE, TWO); - Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO}, mk.getKeys())); + MultiKey<Integer> mk = null; + mk = new MultiKey<Integer>(ONE, TWO); + Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO }, mk.getKeys())); - mk = new MultiKey(ONE, TWO, THREE); - Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE}, mk.getKeys())); + mk = new MultiKey<Integer>(ONE, TWO, THREE); + Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO, THREE }, mk.getKeys())); - mk = new MultiKey(ONE, TWO, THREE, FOUR); - Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR}, mk.getKeys())); + mk = new MultiKey<Integer>(ONE, TWO, THREE, FOUR); + Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO, THREE, FOUR }, mk.getKeys())); - mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE); - Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR, FIVE}, mk.getKeys())); + mk = new MultiKey<Integer>(ONE, TWO, THREE, FOUR, FIVE); + Assert.assertTrue(Arrays.equals(new Object[] { ONE, TWO, THREE, FOUR, FIVE }, mk.getKeys())); - mk = new MultiKey(new Object[] {THREE, FOUR, ONE, TWO}, false); - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + mk = new MultiKey<Integer>(new Integer[] { THREE, FOUR, ONE, TWO }, false); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); } - + public void testConstructorsByArray() throws Exception { - MultiKey mk = null; - Object[] keys = new Object[] {THREE, FOUR, ONE, TWO}; - mk = new MultiKey(keys); - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + MultiKey<Integer> mk = null; + Integer[] keys = new Integer[] { THREE, FOUR, ONE, TWO }; + mk = new MultiKey<Integer>(keys); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); keys[3] = FIVE; // no effect - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); - keys = new Object[] {}; - mk = new MultiKey(keys); + keys = new Integer[] {}; + mk = new MultiKey<Integer>(keys); Assert.assertTrue(Arrays.equals(new Object[] {}, mk.getKeys())); - keys = new Object[] {THREE, FOUR, ONE, TWO}; - mk = new MultiKey(keys, true); - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + keys = new Integer[] { THREE, FOUR, ONE, TWO }; + mk = new MultiKey<Integer>(keys, true); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); keys[3] = FIVE; // no effect - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); - keys = new Object[] {THREE, FOUR, ONE, TWO}; - mk = new MultiKey(keys, false); - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys())); + keys = new Integer[] { THREE, FOUR, ONE, TWO }; + mk = new MultiKey<Integer>(keys, false); + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, TWO }, mk.getKeys())); // change key - don't do this! // the hashcode of the MultiKey is now broken keys[3] = FIVE; - Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, FIVE}, mk.getKeys())); - } - + Assert.assertTrue(Arrays.equals(new Object[] { THREE, FOUR, ONE, FIVE }, mk.getKeys())); + } + public void testConstructorsByArrayNull() throws Exception { - Object[] keys = null; + Integer[] keys = null; try { - new MultiKey(keys); + new MultiKey<Integer>(keys); fail(); } catch (IllegalArgumentException ex) {} try { - new MultiKey(keys, true); + new MultiKey<Integer>(keys, true); fail(); } catch (IllegalArgumentException ex) {} try { - new MultiKey(keys, false); + new MultiKey<Integer>(keys, false); fail(); } catch (IllegalArgumentException ex) {} } - + public void testSize() { - Assert.assertEquals(2, new MultiKey(ONE, TWO).size()); - Assert.assertEquals(2, new MultiKey(null, null).size()); - Assert.assertEquals(3, new MultiKey(ONE, TWO, THREE).size()); - Assert.assertEquals(3, new MultiKey(null, null, null).size()); - Assert.assertEquals(4, new MultiKey(ONE, TWO, THREE, FOUR).size()); - Assert.assertEquals(4, new MultiKey(null, null, null, null).size()); - Assert.assertEquals(5, new MultiKey(ONE, TWO, THREE, FOUR, FIVE).size()); - Assert.assertEquals(5, new MultiKey(null, null, null, null, null).size()); - - Assert.assertEquals(0, new MultiKey(new Object[] {}).size()); - Assert.assertEquals(1, new MultiKey(new Object[] {ONE}).size()); - Assert.assertEquals(2, new MultiKey(new Object[] {ONE, TWO}).size()); - Assert.assertEquals(7, new MultiKey(new Object[] {ONE, TWO, ONE, TWO, ONE, TWO, ONE}).size()); + Assert.assertEquals(2, new MultiKey<Integer>(ONE, TWO).size()); + Assert.assertEquals(2, new MultiKey<Object>(null, null).size()); + Assert.assertEquals(3, new MultiKey<Integer>(ONE, TWO, THREE).size()); + Assert.assertEquals(3, new MultiKey<Object>(null, null, null).size()); + Assert.assertEquals(4, new MultiKey<Integer>(ONE, TWO, THREE, FOUR).size()); + Assert.assertEquals(4, new MultiKey<Object>(null, null, null, null).size()); + Assert.assertEquals(5, new MultiKey<Integer>(ONE, TWO, THREE, FOUR, FIVE).size()); + Assert.assertEquals(5, new MultiKey<Object>(null, null, null, null, null).size()); + + Assert.assertEquals(0, new MultiKey<Object>(new Object[] {}).size()); + Assert.assertEquals(1, new MultiKey<Integer>(new Integer[] { ONE }).size()); + Assert.assertEquals(2, new MultiKey<Integer>(new Integer[] { ONE, TWO }).size()); + Assert.assertEquals(7, new MultiKey<Integer>(new Integer[] { ONE, TWO, ONE, TWO, ONE, TWO, ONE }).size()); } - + public void testGetIndexed() { - MultiKey mk = new MultiKey(ONE, TWO); + MultiKey<Integer> mk = new MultiKey<Integer>(ONE, TWO); Assert.assertSame(ONE, mk.getKey(0)); Assert.assertSame(TWO, mk.getKey(1)); try { @@ -150,18 +150,18 @@ public class TestMultiKey extends TestCase { fail(); } catch (IndexOutOfBoundsException ex) {} } - + public void testGetKeysSimpleConstructor() { - MultiKey mk = new MultiKey(ONE, TWO); + MultiKey<Integer> mk = new MultiKey<Integer>(ONE, TWO); Object[] array = mk.getKeys(); Assert.assertSame(ONE, array[0]); Assert.assertSame(TWO, array[1]); Assert.assertEquals(2, array.length); } - + public void testGetKeysArrayConstructorCloned() { - Object[] keys = new Object[] {ONE, TWO}; - MultiKey mk = new MultiKey(keys, true); + Integer[] keys = new Integer[] { ONE, TWO }; + MultiKey<Integer> mk = new MultiKey<Integer>(keys, true); Object[] array = mk.getKeys(); Assert.assertTrue(array != keys); Assert.assertTrue(Arrays.equals(array, keys)); @@ -169,10 +169,10 @@ public class TestMultiKey extends TestCase { Assert.assertSame(TWO, array[1]); Assert.assertEquals(2, array.length); } - + public void testGetKeysArrayConstructorNonCloned() { - Object[] keys = new Object[] {ONE, TWO}; - MultiKey mk = new MultiKey(keys, false); + Integer[] keys = new Integer[] { ONE, TWO }; + MultiKey<Integer> mk = new MultiKey<Integer>(keys, false); Object[] array = mk.getKeys(); Assert.assertTrue(array != keys); // still not equal Assert.assertTrue(Arrays.equals(array, keys)); @@ -180,30 +180,30 @@ public class TestMultiKey extends TestCase { Assert.assertSame(TWO, array[1]); Assert.assertEquals(2, array.length); } - + public void testHashCode() { - MultiKey mk1 = new MultiKey(ONE, TWO); - MultiKey mk2 = new MultiKey(ONE, TWO); - MultiKey mk3 = new MultiKey(ONE, "TWO"); - + MultiKey<Integer> mk1 = new MultiKey<Integer>(ONE, TWO); + MultiKey<Integer> mk2 = new MultiKey<Integer>(ONE, TWO); + MultiKey<Object> mk3 = new MultiKey<Object>(ONE, "TWO"); + Assert.assertTrue(mk1.hashCode() == mk1.hashCode()); Assert.assertTrue(mk1.hashCode() == mk2.hashCode()); Assert.assertTrue(mk1.hashCode() != mk3.hashCode()); - + int total = (0 ^ ONE.hashCode()) ^ TWO.hashCode(); Assert.assertEquals(total, mk1.hashCode()); } - + public void testEquals() { - MultiKey mk1 = new MultiKey(ONE, TWO); - MultiKey mk2 = new MultiKey(ONE, TWO); - MultiKey mk3 = new MultiKey(ONE, "TWO"); - + MultiKey<Integer> mk1 = new MultiKey<Integer>(ONE, TWO); + MultiKey<Integer> mk2 = new MultiKey<Integer>(ONE, TWO); + MultiKey<Object> mk3 = new MultiKey<Object>(ONE, "TWO"); + Assert.assertEquals(mk1, mk1); Assert.assertEquals(mk1, mk2); Assert.assertTrue(mk1.equals(mk3) == false); Assert.assertTrue(mk1.equals("") == false); Assert.assertTrue(mk1.equals(null) == false); } - + }
http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/test/org/apache/commons/collections/keyvalue/TestTiedMapEntry.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/keyvalue/TestTiedMapEntry.java b/src/test/org/apache/commons/collections/keyvalue/TestTiedMapEntry.java index 9a2ec72..d3b812a 100644 --- a/src/test/org/apache/commons/collections/keyvalue/TestTiedMapEntry.java +++ b/src/test/org/apache/commons/collections/keyvalue/TestTiedMapEntry.java @@ -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. @@ -24,17 +24,16 @@ import junit.framework.TestSuite; /** * Test the TiedMapEntry class. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Stephen Colebourne */ -public class TestTiedMapEntry extends AbstractTestMapEntry { +public class TestTiedMapEntry<K, V> extends AbstractTestMapEntry<K, V> { public TestTiedMapEntry(String testName) { super(testName); - } public static void main(String[] args) { @@ -49,10 +48,10 @@ public class TestTiedMapEntry extends AbstractTestMapEntry { /** * Gets the instance to test */ - public Map.Entry makeMapEntry(Object key, Object value) { - Map map = new HashMap(); + public Map.Entry<K, V> makeMapEntry(K key, V value) { + Map<K, V> map = new HashMap<K, V>(); map.put(key, value); - return new TiedMapEntry(map, key); + return new TiedMapEntry<K, V>(map, key); } //----------------------------------------------------------------------- @@ -66,29 +65,30 @@ public class TestTiedMapEntry extends AbstractTestMapEntry { /** * Tests the constructors. */ + @SuppressWarnings("unchecked") public void testSetValue() { - Map map = new HashMap(); - map.put("A", "a"); - map.put("B", "b"); - map.put("C", "c"); - Map.Entry entry = new TiedMapEntry(map, "A"); + Map<K, V> map = new HashMap<K, V>(); + map.put((K) "A", (V) "a"); + map.put((K) "B", (V) "b"); + map.put((K) "C", (V) "c"); + Map.Entry<K, V> entry = new TiedMapEntry<K, V>(map, (K) "A"); assertSame("A", entry.getKey()); assertSame("a", entry.getValue()); - assertSame("a", entry.setValue("x")); + assertSame("a", entry.setValue((V) "x")); assertSame("A", entry.getKey()); assertSame("x", entry.getValue()); - - entry = new TiedMapEntry(map, "B"); + + entry = new TiedMapEntry<K, V>(map, (K) "B"); assertSame("B", entry.getKey()); assertSame("b", entry.getValue()); - assertSame("b", entry.setValue("y")); + assertSame("b", entry.setValue((V) "y")); assertSame("B", entry.getKey()); assertSame("y", entry.getValue()); - - entry = new TiedMapEntry(map, "C"); + + entry = new TiedMapEntry<K, V>(map, (K) "C"); assertSame("C", entry.getKey()); assertSame("c", entry.getValue()); - assertSame("c", entry.setValue("z")); + assertSame("c", entry.setValue((V) "z")); assertSame("C", entry.getKey()); assertSame("z", entry.getValue()); } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/test/org/apache/commons/collections/keyvalue/TestUnmodifiableMapEntry.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/keyvalue/TestUnmodifiableMapEntry.java b/src/test/org/apache/commons/collections/keyvalue/TestUnmodifiableMapEntry.java index 27711d9..9c3398f 100644 --- a/src/test/org/apache/commons/collections/keyvalue/TestUnmodifiableMapEntry.java +++ b/src/test/org/apache/commons/collections/keyvalue/TestUnmodifiableMapEntry.java @@ -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. @@ -26,17 +26,16 @@ import org.apache.commons.collections.Unmodifiable; /** * Test the UnmodifiableMapEntry class. - * + * * @since Commons Collections 3.0 * @version $Revision$ $Date$ - * + * * @author Neil O'Toole */ -public class TestUnmodifiableMapEntry extends AbstractTestMapEntry { +public class TestUnmodifiableMapEntry<K, V> extends AbstractTestMapEntry<K, V> { public TestUnmodifiableMapEntry(String testName) { super(testName); - } public static void main(String[] args) { @@ -53,8 +52,8 @@ public class TestUnmodifiableMapEntry extends AbstractTestMapEntry { * Subclasses should override this method to return a Map.Entry * of the type being tested. */ - public Map.Entry makeMapEntry() { - return new UnmodifiableMapEntry(null, null); + public Map.Entry<K, V> makeMapEntry() { + return new UnmodifiableMapEntry<K, V>(null, null); } /** @@ -62,8 +61,8 @@ public class TestUnmodifiableMapEntry extends AbstractTestMapEntry { * Subclasses should override this method to return a Map.Entry * of the type being tested. */ - public Map.Entry makeMapEntry(Object key, Object value) { - return new UnmodifiableMapEntry(key, value); + public Map.Entry<K, V> makeMapEntry(K key, V value) { + return new UnmodifiableMapEntry<K, V>(key, value); } //----------------------------------------------------------------------- @@ -71,28 +70,30 @@ public class TestUnmodifiableMapEntry extends AbstractTestMapEntry { * Subclasses should override this method. * */ + @SuppressWarnings("unchecked") public void testConstructors() { // 1. test key-value constructor - Map.Entry entry = new UnmodifiableMapEntry(key, value); + Map.Entry<K, V> entry = new UnmodifiableMapEntry<K, V>((K) key, (V) value); assertSame(key, entry.getKey()); assertSame(value, entry.getValue()); // 2. test pair constructor - KeyValue pair = new DefaultKeyValue(key, value); - entry = new UnmodifiableMapEntry(pair); + KeyValue<K, V> pair = new DefaultKeyValue<K, V>((K) key, (V) value); + entry = new UnmodifiableMapEntry<K, V>(pair); assertSame(key, entry.getKey()); assertSame(value, entry.getValue()); // 3. test copy constructor - Map.Entry entry2 = new UnmodifiableMapEntry(entry); + Map.Entry<K, V> entry2 = new UnmodifiableMapEntry<K, V>(entry); assertSame(key, entry2.getKey()); assertSame(value, entry2.getValue()); assertTrue(entry instanceof Unmodifiable); } + @SuppressWarnings("unchecked") public void testAccessorsAndMutators() { - Map.Entry entry = makeMapEntry(key, value); + Map.Entry<K, V> entry = makeMapEntry((K) key, (V) value); assertSame(key, entry.getKey()); assertSame(value, entry.getValue()); @@ -108,11 +109,10 @@ public class TestUnmodifiableMapEntry extends AbstractTestMapEntry { } public void testUnmodifiable() { - Map.Entry entry = makeMapEntry(); + Map.Entry<K, V> entry = makeMapEntry(); try { entry.setValue(null); fail(); - } catch (UnsupportedOperationException ex) {} } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/test/org/apache/commons/collections/list/AbstractTestList.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/list/AbstractTestList.java b/src/test/org/apache/commons/collections/list/AbstractTestList.java index ee1a912..c3f654a 100644 --- a/src/test/org/apache/commons/collections/list/AbstractTestList.java +++ b/src/test/org/apache/commons/collections/list/AbstractTestList.java @@ -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 @@ import org.apache.commons.collections.iterators.AbstractTestListIterator; * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { //----------------------------------------------------------------------- /** - * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { /** * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * * @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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * * @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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * * @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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } try { - list = makeEmptyList(); + list = makeObject(); list.add(-1, element); fail("List.add should throw IndexOutOfBoundsException [-1]"); } catch (IndexOutOfBoundsException e) { @@ -220,7 +202,7 @@ public abstract class AbstractTestList extends AbstractTestCollection { } try { - list = makeEmptyList(); + list = makeObject(); list.add(1, element); fail("List.add should throw IndexOutOfBoundsException [1]"); } catch (IndexOutOfBoundsException e) { @@ -228,7 +210,7 @@ public abstract class AbstractTestList extends AbstractTestCollection { } 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } try { - list = makeFullList(); + list = makeFullCollection(); list.add(-1, element); fail("List.add should throw IndexOutOfBoundsException [-1]"); } catch (IndexOutOfBoundsException e) { @@ -265,7 +247,7 @@ public abstract class AbstractTestList extends AbstractTestCollection { } 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { */ 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } 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 abstract class AbstractTestList extends AbstractTestCollection { */ 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 abstract class AbstractTestList extends AbstractTestCollection { */ 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * empty list. */ public void testListGetByIndexBoundsChecking() { - List list = makeEmptyList(); + List<E> list = makeObject(); try { list.get(Integer.MIN_VALUE); @@ -448,7 +430,7 @@ public abstract class AbstractTestList extends AbstractTestCollection { * full list. */ public void testListGetByIndexBoundsChecking2() { - List list = makeFullList(); + List<E> list = makeFullCollection(); try { list.get(Integer.MIN_VALUE); @@ -484,10 +466,10 @@ public abstract class AbstractTestList extends AbstractTestCollection { */ 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { */ 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { "[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 @@ public abstract class AbstractTestList extends AbstractTestCollection { "[Integer.MAX_VALUE]"); } catch(IndexOutOfBoundsException e) { // expected - } + } } @@ -625,29 +607,28 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { // thrown. verify(); } - /** * Tests bounds checking for {@link List#remove(int)} on an @@ -665,47 +645,44 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { public void testListRemoveByIndexBoundsChecking2() { if (!isRemoveSupported()) return; - List list = makeFullList(); + List<E> list = makeFullCollection(); try { list.remove(Integer.MIN_VALUE); @@ -721,21 +698,21 @@ public abstract class AbstractTestList extends AbstractTestCollection { "[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 @@ public abstract class AbstractTestList extends AbstractTestCollection { "[Integer.MAX_VALUE]"); } catch(IndexOutOfBoundsException e) { // expected - } + } } @@ -756,44 +733,42 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * @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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } } - /** * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } } - /** * Tests the {@link ListIterator#set(Object)} method of the list * iterator. @@ -1014,11 +986,11 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 abstract class AbstractTestList extends AbstractTestCollection { } } - - 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * 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 abstract class AbstractTestList extends AbstractTestCollection { */ 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { } } - /** * Tests that a sublist raises a {@link java.util.ConcurrentModificationException ConcurrentModificationException} * if elements are added to the original list. @@ -1210,29 +1175,27 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { * @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 abstract class AbstractTestList extends AbstractTestCollection { 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 @@ public abstract class AbstractTestList extends AbstractTestCollection { 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(); } } - + } http://git-wip-us.apache.org/repos/asf/commons-collections/blob/884baf0d/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java ---------------------------------------------------------------------- diff --git a/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java b/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java index 9943676..ee12658 100644 --- a/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java +++ b/src/test/org/apache/commons/collections/list/TestAbstractLinkedList.java @@ -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 @@ import java.util.Arrays; /** * 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"); + } + + 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( "value4" ); + 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 @@ public abstract class TestAbstractLinkedList extends AbstractTestList { 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(); + } }