rwaldhoff 01/04/20 09:54:15 Modified: collections/src/test/org/apache/commons/collections TestList.java TestFastTreeMap.java TestFastHashMap.java TestFastArrayList.java TestCursorableLinkedList.java TestCollection.java TestArrayStack.java TestAll.java Added: collections/src/test/org/apache/commons/collections TestObject.java TestMap.java Log: * refactored test framework slightly to support more interesting tests and to make the contract more enforceable * added some new tests and test cases Revision Changes Path 1.2 +127 -9 jakarta-commons/collections/src/test/org/apache/commons/collections/TestList.java Index: TestList.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestList.java 2001/04/14 15:39:53 1.1 +++ TestList.java 2001/04/20 16:54:04 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestList.java,v 1.1 2001/04/14 15:39:53 rwaldhoff Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/14 15:39:53 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestList.java,v 1.2 2001/04/20 16:54:04 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:04 $ * * ==================================================================== * @@ -63,22 +63,140 @@ import junit.framework.*; import java.util.List; +import java.util.Collection; /** + * Tests base {@link java.util.List} methods and contracts. + * <p> + * To use, simply extend this class, and implement + * the {@link #makeList} method. + * <p> + * If your {@link List} fails one of these tests by design, + * you may still use this base set of cases. Simply override the + * test case (method) your {@link List} fails. + * * @author Rodney Waldhoff - * @version $Id: TestList.java,v 1.1 2001/04/14 15:39:53 rwaldhoff Exp $ + * @version $Id: TestList.java,v 1.2 2001/04/20 16:54:04 rwaldhoff Exp $ */ public abstract class TestList extends TestCollection { public TestList(String testName) { super(testName); } + + /** + * Return a new, empty {@link List} to used for testing. + */ + public abstract List makeList(); + + public Collection makeCollection() { + return makeList(); + } + + /* + + // optional operation + public void testListAddByIndex() { + // XXX finish me + } + + // optional operation + public void testListAdd() { + // XXX finish me + } + + // optional operation + public void testListAddAll() { + // XXX finish me + } + + // optional operation + public void testListClear() { + // XXX finish me + } + + public void testListContains() { + // XXX finish me + // is this any different from Collection.contains? + } + + public void testListContainsAll() { + // XXX finish me + // is this any different from Collection.containsAll? + } + + public void testListEquals() { + // XXX finish me + } + + public void testListGetByIndex() { + // XXX finish me + } + + public void testListHashCode() { + // XXX finish me + } - private List _list = null; + public void testListIndexOf() { + // XXX finish me + } + + public void testListIsEmpty() { + // XXX finish me + // is this any different from Collection.isEmpty? + } + + public void testListIterator() { + // XXX finish me + // is this any different from Collection.iterator? + } + + public void testListLastIndexOf() { + // XXX finish me + } + + public void testListListIterator() { + // XXX finish me + } - protected void setList(List l) { - _list = l; - setCollection(_list); + public void testListListIteratorByIndex() { + // XXX finish me } + + // optional operation + public void testListRemoveByIndex() { + // XXX finish me + } + + // optional operation + public void testListRemoveByValue() { + // XXX finish me + } + + // optional operation + public void testListRemoveAll() { + // XXX finish me + // is this any different from Collection.removeAll? + } + + // optional operation + public void testListRetainAll() { + // XXX finish me + // is this any different from Collection.retainAll? + } + + // optional operation + public void testListSet() { + // XXX finish me + } + + // size() same as Collection.size() ? + + public void testListSubList() { + // XXX finish me + } + + // toArray() same as Collection.toArray() ? + // toArray(Object[]) same as Collection.toArray(Object[]) ? - // placeholder. add list contract tests here + */ } 1.2 +16 -11 jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java Index: TestFastTreeMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestFastTreeMap.java 2001/04/16 22:42:04 1.1 +++ TestFastTreeMap.java 2001/04/20 16:54:04 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/16 22:42:04 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v 1.2 2001/04/20 16:54:04 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:04 $ * * ==================================================================== * @@ -64,43 +64,48 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import java.util.Map; /** * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a> - * @version $Id: TestFastTreeMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ + * @version $Id: TestFastTreeMap.java,v 1.2 2001/04/20 16:54:04 rwaldhoff Exp $ */ -public class TestFastTreeMap extends TestCase +public class TestFastTreeMap extends TestMap { - public TestFastTreeMap(String testName) + public TestFastTreeMap(String testName) { super(testName); } - public static Test suite() + public static Test suite() { return new TestSuite(TestFastTreeMap.class); } - public static void main(String args[]) + public static void main(String args[]) { String[] testCaseName = { TestFastTreeMap.class.getName() }; junit.textui.TestRunner.main(testCaseName); } + public Map makeMap() { + return new FastTreeMap(); + } + private FastTreeMap map = null; - public void setUp() + public void setUp() { map = new FastTreeMap(); } - public void testNewMap() + public void testNewMap() { assert("New map is empty", map.isEmpty()); assertEquals("New map has size zero", map.size(), 0); } - public void testSearch() + public void testSearch() { map.put("first", "First Item"); map.put("second", "Second Item"); 1.2 +16 -11 jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap.java Index: TestFastHashMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestFastHashMap.java 2001/04/16 22:42:04 1.1 +++ TestFastHashMap.java 2001/04/20 16:54:05 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/16 22:42:04 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastHashMap.java,v 1.2 2001/04/20 16:54:05 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:05 $ * * ==================================================================== * @@ -64,43 +64,48 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import java.util.Map; /** * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a> - * @version $Id: TestFastHashMap.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ + * @version $Id: TestFastHashMap.java,v 1.2 2001/04/20 16:54:05 rwaldhoff Exp $ */ -public class TestFastHashMap extends TestCase +public class TestFastHashMap extends TestMap { - public TestFastHashMap(String testName) + public TestFastHashMap(String testName) { super(testName); } - public static Test suite() + public static Test suite() { return new TestSuite(TestFastHashMap.class); } - public static void main(String args[]) + public static void main(String args[]) { String[] testCaseName = { TestFastHashMap.class.getName() }; junit.textui.TestRunner.main(testCaseName); } + public Map makeMap() { + return new FastHashMap(); + } + private FastHashMap map = null; - public void setUp() + public void setUp() { map = new FastHashMap(); } - public void testNewMap() + public void testNewMap() { assert("New map is empty", map.isEmpty()); assertEquals("New map has size zero", map.size(), 0); } - public void testSearch() + public void testSearch() { map.put("first", "First Item"); map.put("second", "Second Item"); 1.2 +23 -13 jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList.java Index: TestFastArrayList.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestFastArrayList.java 2001/04/16 22:42:04 1.1 +++ TestFastArrayList.java 2001/04/20 16:54:06 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/16 22:42:04 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFastArrayList.java,v 1.2 2001/04/20 16:54:06 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:06 $ * * ==================================================================== * @@ -64,24 +64,25 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import java.util.List; /** * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a> - * @version $Id: TestFastArrayList.java,v 1.1 2001/04/16 22:42:04 jvanzyl Exp $ + * @version $Id: TestFastArrayList.java,v 1.2 2001/04/20 16:54:06 rwaldhoff Exp $ */ -public class TestFastArrayList extends TestCase +public class TestFastArrayList extends TestList { - public TestFastArrayList(String testName) + public TestFastArrayList(String testName) { super(testName); } - public static Test suite() + public static Test suite() { return new TestSuite(TestFastArrayList.class); } - public static void main(String args[]) + public static void main(String args[]) { String[] testCaseName = { TestFastArrayList.class.getName() }; junit.textui.TestRunner.main(testCaseName); @@ -89,32 +90,41 @@ private FastArrayList list = null; - public void setUp() + public void setUp() { list = new FastArrayList(); } - public void testNewFastArrayList() + public List makeList() { + return new FastArrayList(); + } + + public void testNewFastArrayList() + { assert("New list is empty", list.isEmpty()); assertEquals("New list has size zero", list.size(), 0); - try + try { list.get(1); fail("get(int i) should have thrown IndexOutOfBoundsException"); - } + } catch (IndexOutOfBoundsException e) { ; // Expected result } } - public void testSearch() + public void testSearch() { list.add("First Item"); list.add("Last Item"); assertEquals("First item is 'First Item'", list.get(0), "First Item"); assertEquals("Last Item is 'Last Item'", list.get(1), "Last Item"); + } + + public void testCollectionEquals() { + // FastArrayList currently doesn't support the List equals contract } } 1.2 +8 -5 jakarta-commons/collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java Index: TestCursorableLinkedList.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestCursorableLinkedList.java 2001/04/14 15:39:44 1.1 +++ TestCursorableLinkedList.java 2001/04/20 16:54:07 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v 1.1 2001/04/14 15:39:44 rwaldhoff Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/14 15:39:44 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCursorableLinkedList.java,v 1.2 2001/04/20 16:54:07 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:07 $ * * ==================================================================== * @@ -66,7 +66,7 @@ /** * @author Rodney Waldhoff - * @version $Id: TestCursorableLinkedList.java,v 1.1 2001/04/14 15:39:44 rwaldhoff Exp $ + * @version $Id: TestCursorableLinkedList.java,v 1.2 2001/04/20 16:54:07 rwaldhoff Exp $ */ public class TestCursorableLinkedList extends TestList { public TestCursorableLinkedList(String testName) { @@ -86,7 +86,10 @@ public void setUp() { list = new CursorableLinkedList(); - setList(list); + } + + public List makeList() { + return new CursorableLinkedList(); } public void testAdd() { 1.2 +199 -172 jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java Index: TestCollection.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- TestCollection.java 2001/04/14 15:39:51 1.1 +++ TestCollection.java 2001/04/20 16:54:08 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java,v 1.1 2001/04/14 15:39:51 rwaldhoff Exp $ - * $Revision: 1.1 $ - * $Date: 2001/04/14 15:39:51 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java,v 1.2 2001/04/20 16:54:08 rwaldhoff Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/20 16:54:08 $ * * ==================================================================== * @@ -68,60 +68,49 @@ import java.util.NoSuchElementException; /** + * Tests base {@link java.util.Collection} methods and contracts. + * <p> + * To use, simply extend this class, and implement + * the {@link #makeCollection} method. + * <p> + * If your {@link Collection} fails one of these tests by design, + * you may still use this base set of cases. Simply override the + * test case (method) your {@link Collection} fails. + * * @author Rodney Waldhoff - * @version $Id: TestCollection.java,v 1.1 2001/04/14 15:39:51 rwaldhoff Exp $ + * @version $Id: TestCollection.java,v 1.2 2001/04/20 16:54:08 rwaldhoff Exp $ */ -public abstract class TestCollection extends TestCase { +public abstract class TestCollection extends TestObject { public TestCollection(String testName) { super(testName); } - private Collection _collection = null; + /** + * Return a new, empty {@link Collection} to used for testing. + */ + public abstract Collection makeCollection(); - protected void setCollection(Collection c) { - _collection = c; + public Object makeObject() { + return makeCollection(); } // optional operation public void testCollectionAdd() { - boolean added1 = false; - try { - added1 = _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } - - boolean added2 = false; - try { - added2 = _collection.add("element2"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } + Collection c = makeCollection(); + boolean added1 = tryToAdd(c,"element1"); + boolean added2 = tryToAdd(c,"element2"); } // optional operation public void testCollectionAddAll() { + Collection c = makeCollection(); Collection col = new ArrayList(); col.add("element1"); col.add("element2"); col.add("element3"); boolean added = false; try { - added = _collection.addAll(col); + added = c.addAll(col); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -136,9 +125,10 @@ // optional operation public void testCollectionClear() { + Collection c = makeCollection(); boolean cleared = false; try { - _collection.clear(); + c.clear(); cleared = true; } catch(UnsupportedOperationException e) { // ignored, must not be supported @@ -148,28 +138,16 @@ } if(cleared) { - assert("After Collection.clear(), Collection.isEmpty() should be true.",_collection.isEmpty()); + assert("After Collection.clear(), Collection.isEmpty() should be true.",c.isEmpty()); } - boolean added = false; - try { - added = _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } + boolean added = tryToAdd(c,"element1"); if(added) { - assert("After element is added, Collection.isEmpty() should be false.",!_collection.isEmpty()); + assert("After element is added, Collection.isEmpty() should be false.",!c.isEmpty()); boolean cleared2 = false; try { - _collection.clear(); + c.clear(); cleared2 = true; } catch(UnsupportedOperationException e) { // ignored, must not be supported @@ -178,127 +156,104 @@ fail("Collection.clear should only throw UnsupportedOperationException. Found " + t.toString()); } if(cleared2) { - assert("After Collection.clear(), Collection.isEmpty() should be true.",_collection.isEmpty()); + assert("After Collection.clear(), Collection.isEmpty() should be true.",c.isEmpty()); } } } public void testCollectionContains() { - boolean added1 = false; - try { - added1 = _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } - assert("If an element was added, it should be contained.",added1 == _collection.contains("element1")); - - boolean added2 = false; - try { - added2 = _collection.add("element2"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } - assert("If an element was added, it should be contained.",added1 == _collection.contains("element1")); - assert("If an element was added, it should be contained.",added2 == _collection.contains("element2")); + Collection c = makeCollection(); + assert("Empty Collection shouldn't contain element.",!c.contains("element1")); + boolean added1 = tryToAdd(c,"element1"); + assert("[1] If an element was added, it should be contained, if it wasn't, it shouldn't.",added1 == c.contains("element1")); + + assert("Shouldn't be contained.",!c.contains("element2")); + boolean added2 = tryToAdd(c,"element2"); + assert("[2] If an element was added, it should be contained, if it wasn't, it shouldn't.",added1 == c.contains("element1")); + assert("[3] If an element was added, it should be contained, if it wasn't, it shouldn't.",added2 == c.contains("element2")); } public void testCollectionContainsAll() { + Collection c = makeCollection(); Collection col = new ArrayList(); - assert("Every Collection should contain all elements of an empty Collection.",_collection.containsAll(col)); + assert("Every Collection should contain all elements of an empty Collection.",c.containsAll(col)); col.add("element1"); - assert("Empty Collection shouldn't contain all elements of a non-empty Collection.",!_collection.containsAll(col)); + assert("Empty Collection shouldn't contain all elements of a non-empty Collection.",!c.containsAll(col)); - boolean added1 = false; - try { - added1 = _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } + boolean added1 = tryToAdd(c,"element1"); if(added1) { - assert("Should contain all.",_collection.containsAll(col)); + assert("[1] Should contain all.",c.containsAll(col)); } col.add("element2"); - assert("Shouldn't contain all.",!_collection.containsAll(col)); + assert("Shouldn't contain all.",!c.containsAll(col)); - boolean added2 = false; - try { - added2 = _collection.add("element2"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); - } + boolean added2 = tryToAdd(c,"element2"); if(added1 && added2) { - assert("Should contain all.",_collection.containsAll(col)); + assert("[2] Should contain all.",c.containsAll(col)); } } + public void testCollectionEqualsSelf() { + Collection c = makeCollection(); + assertEquals("A Collection should equal itself",c,c); + tryToAdd(c,"element1"); + assertEquals("A Collection should equal itself",c,c); + tryToAdd(c,"element1"); + tryToAdd(c,"element2"); + assertEquals("A Collection should equal itself",c,c); + } + public void testCollectionEquals() { - assertEquals("A Collection should equal itself",_collection,_collection); - try { - _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); + Collection c1 = makeCollection(); + Collection c2 = makeCollection(); + assertEquals("Empty Collections are equal.",c1,c2); + + boolean added1_1 = tryToAdd(c1,"element1"); + if(added1_1) { + assert("Empty Collection not equal to non-empty Collection.",!c2.equals(c1)); + assert("Non-empty Collection not equal to empty Collection.",!c1.equals(c2)); } - assertEquals("A Collection should equal itself",_collection,_collection); - try { - _collection.add("element1"); - } catch(UnsupportedOperationException e) { - // ignored, must not be supported - } catch(ClassCastException e) { - // ignored, type must not be supported - } catch(IllegalArgumentException e) { - // ignored, element must not be supported - } catch(Throwable t) { - t.printStackTrace(); - fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); + + boolean added1_2 = tryToAdd(c2,"element1"); + assertEquals("After duplicate adds, Collections should be equal.",c1,c2); + + boolean added2_1 = tryToAdd(c1,"element2"); + boolean added3_2 = tryToAdd(c2,"element3"); + if(added2_1 || added3_2) { + assert("Should not be equal.",!c1.equals(c2)); } - assertEquals("A Collection should equal itself",_collection,_collection); } - public void testCollectionHashCode() { - assertEquals("A Collection's hashCode should equal itself",_collection.hashCode(),_collection.hashCode()); + public void testCollectionHashCodeEqualsSelfHashCode() { + Collection c = makeCollection(); + assertEquals("hashCode should be repeatable",c.hashCode(),c.hashCode()); + tryToAdd(c,"element1"); + assertEquals("after add, hashCode should be repeatable",c.hashCode(),c.hashCode()); + } + + public void testCollectionHashCodeEqualsContract() { + Collection c1 = makeCollection(); + if(c1.equals(c1)) { + assertEquals("[1] When two objects are equal, their hashCodes should be also.",c1.hashCode(),c1.hashCode()); + } + Collection c2 = makeCollection(); + if(c1.equals(c2)) { + assertEquals("[2] When two objects are equal, their hashCodes should be also.",c1.hashCode(),c2.hashCode()); + } + tryToAdd(c1,"element1"); + tryToAdd(c2,"element1"); + if(c1.equals(c2)) { + assertEquals("[3] When two objects are equal, their hashCodes should be also.",c1.hashCode(),c2.hashCode()); + } } public void testCollectionIsEmpty() { - assert("New Collection should be empty.",_collection.isEmpty()); + Collection c = makeCollection(); + assert("New Collection should be empty.",c.isEmpty()); boolean added = false; try { - added = _collection.add("element1"); + added = c.add("element1"); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -310,12 +265,13 @@ fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); } if(added) { - assert("If an element was added, the Collection.isEmpty() should return false.",!_collection.isEmpty()); + assert("If an element was added, the Collection.isEmpty() should return false.",!c.isEmpty()); } } public void testCollectionIterator() { - Iterator it1 = _collection.iterator(); + Collection c = makeCollection(); + Iterator it1 = c.iterator(); assert("Iterator for empty Collection shouldn't have next.",!it1.hasNext()); try { it1.next(); @@ -329,7 +285,7 @@ boolean added = false; try { - added = _collection.add("element1"); + added = c.add("element1"); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -341,7 +297,7 @@ fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); } if(added) { - Iterator it2 = _collection.iterator(); + Iterator it2 = c.iterator(); assert("Iterator for non-empty Collection should have next.",it2.hasNext()); assertEquals("element1",it2.next()); assert("Iterator at end of Collection shouldn't have next.",!it2.hasNext()); @@ -359,9 +315,10 @@ // optional operation public void testCollectionRemove() { + Collection c = makeCollection(); boolean added = false; try { - added = _collection.add("element1"); + added = c.add("element1"); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -374,7 +331,7 @@ } try { - assert("Shouldn't be able to remove an element that wasn't added.",!_collection.remove("element2")); + assert("Shouldn't be able to remove an element that wasn't added.",!c.remove("element2")); } catch(UnsupportedOperationException e) { } catch(Throwable t) { t.printStackTrace(); @@ -382,8 +339,8 @@ } try { - assert("If added, should be removed by call to remove.",added == _collection.remove("element1")); - assert("If removed, shouldn't be contained.",!_collection.contains("element1")); + assert("If added, should be removed by call to remove.",added == c.remove("element1")); + assert("If removed, shouldn't be contained.",!c.contains("element1")); } catch(UnsupportedOperationException e) { } catch(Throwable t) { t.printStackTrace(); @@ -393,20 +350,21 @@ // optional operation public void testCollectionRemoveAll() { - assert("Initial Collection is empty.",_collection.isEmpty()); + Collection c = makeCollection(); + assert("Initial Collection is empty.",c.isEmpty()); try { - _collection.removeAll(_collection); + c.removeAll(c); } catch(UnsupportedOperationException e) { // expected } catch(Throwable t) { t.printStackTrace(); fail("Collection.removeAll should only throw UnsupportedOperationException. Found " + t.toString()); } - assert("Collection is still empty.",_collection.isEmpty()); + assert("Collection is still empty.",c.isEmpty()); boolean added = false; try { - added = _collection.add("element1"); + added = c.add("element1"); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -418,10 +376,10 @@ fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); } if(added) { - assert("Collection is not empty.",!_collection.isEmpty()); + assert("Collection is not empty.",!c.isEmpty()); try { - _collection.removeAll(_collection); - assert("Collection is empty.",_collection.isEmpty()); + c.removeAll(c); + assert("Collection is empty.",c.isEmpty()); } catch(UnsupportedOperationException e) { // expected } catch(Throwable t) { @@ -433,15 +391,16 @@ // optional operation public void testCollectionRemoveAll2() { + Collection c = makeCollection(); Collection col = new ArrayList(); col.add("element1"); col.add("element2"); col.add("element3"); boolean added = false; try { - added = _collection.addAll(col); + added = c.addAll(col); if(added) { - added = _collection.add("element0"); + added = c.add("element0"); } } catch(UnsupportedOperationException e) { // ignored, must not be supported @@ -455,15 +414,15 @@ } col.add("element4"); if(added) { - assert("Collection is not empty.",!_collection.isEmpty()); + assert("Collection is not empty.",!c.isEmpty()); try { - assert("Should be changed",_collection.removeAll(col)); - assert("Collection is not empty.",!_collection.isEmpty()); - assert("Collection should contain element",_collection.contains("element0")); - assert("Collection shouldn't contain removed element",!_collection.contains("element1")); - assert("Collection shouldn't contain removed element",!_collection.contains("element2")); - assert("Collection shouldn't contain removed element",!_collection.contains("element3")); - assert("Collection shouldn't contain removed element",!_collection.contains("element4")); + assert("Should be changed",c.removeAll(col)); + assert("Collection is not empty.",!c.isEmpty()); + assert("Collection should contain element",c.contains("element0")); + assert("Collection shouldn't contain removed element",!c.contains("element1")); + assert("Collection shouldn't contain removed element",!c.contains("element2")); + assert("Collection shouldn't contain removed element",!c.contains("element3")); + assert("Collection shouldn't contain removed element",!c.contains("element4")); } catch(UnsupportedOperationException e) { // expected } catch(Throwable t) { @@ -475,13 +434,15 @@ // optional operation public void testCollectionRetainAll() { + // XXX finish me } public void testCollectionSize() { - assertEquals("Size of new Collection is 0.",0,_collection.size()); + Collection c = makeCollection(); + assertEquals("Size of new Collection is 0.",0,c.size()); boolean added = false; try { - added = _collection.add("element1"); + added = c.add("element1"); } catch(UnsupportedOperationException e) { // ignored, must not be supported } catch(ClassCastException e) { @@ -493,13 +454,79 @@ fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); } if(added) { - assertEquals("If one element was added, the Collection.size() should be 1.",1,_collection.size()); + assertEquals("If one element was added, the Collection.size() should be 1.",1,c.size()); } } public void testCollectionToArray() { + Collection c = makeCollection(); + assertEquals("Empty Collection should return empty array for toArray",0,c.toArray().length); + boolean added = false; + try { + added = c.add("element1"); + } catch(UnsupportedOperationException e) { + // ignored, must not be supported + } catch(ClassCastException e) { + // ignored, type must not be supported + } catch(IllegalArgumentException e) { + // ignored, element must not be supported + } catch(Throwable t) { + t.printStackTrace(); + fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); + } + if(added) { + assertEquals("If an element was added, the Collection.toArray().length should be 1.",1,c.toArray().length); + } else { + assertEquals("Empty Collection should return empty array for toArray",0,c.toArray().length); + } + + boolean added2 = false; + try { + added2 = c.add("element1"); + } catch(UnsupportedOperationException e) { + // ignored, must not be supported + } catch(ClassCastException e) { + // ignored, type must not be supported + } catch(IllegalArgumentException e) { + // ignored, element must not be supported + } catch(Throwable t) { + t.printStackTrace(); + fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); + } + if(added && added2) { + assertEquals("If another element was added, the Collection.toArray().length should be 2.",2,c.toArray().length); + } else if(added2) { + assertEquals("If an element was added, the Collection.toArray().length should be 1.",1,c.toArray().length); + } else { + assertEquals("Empty Collection should return empty array for toArray",0,c.toArray().length); + } } public void testCollectionToArray2() { + // XXX finish me + } + + /** + * Try to add the given object to the given Collection. + * Returns <tt>true</tt> if the element was added, + * <tt>false</tt> otherwise. + * + * Fails any Throwable except UnsupportedOperationException, + * ClassCastException, or IllegalArgumentException is thrown. + */ + protected boolean tryToAdd(Collection c,Object obj) { + try { + return c.add(obj); + } catch(UnsupportedOperationException e) { + return false; + } catch(ClassCastException e) { + return false; + } catch(IllegalArgumentException e) { + return false; + } catch(Throwable t) { + t.printStackTrace(); + fail("Collection.add should only throw UnsupportedOperationException, ClassCastException or IllegalArgumentException. Found " + t.toString()); + return false; // never get here, since fail throws exception + } } } 1.3 +8 -6 jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayStack.java Index: TestArrayStack.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayStack.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestArrayStack.java 2001/04/14 19:50:31 1.2 +++ TestArrayStack.java 2001/04/20 16:54:08 1.3 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayStack.java,v 1.2 2001/04/14 19:50:31 craigmcc Exp $ - * $Revision: 1.2 $ - * $Date: 2001/04/14 19:50:31 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestArrayStack.java,v 1.3 2001/04/20 16:54:08 rwaldhoff Exp $ + * $Revision: 1.3 $ + * $Date: 2001/04/20 16:54:08 $ * * ==================================================================== * @@ -66,7 +66,7 @@ /** * @author Craig McClanahan - * @version $Id: TestArrayStack.java,v 1.2 2001/04/14 19:50:31 craigmcc Exp $ + * @version $Id: TestArrayStack.java,v 1.3 2001/04/20 16:54:08 rwaldhoff Exp $ */ public class TestArrayStack extends TestList { @@ -84,13 +84,15 @@ junit.textui.TestRunner.main(testCaseName); } + public List makeList() { + return new ArrayStack(); + } + private ArrayStack stack = null; public void setUp() { stack = new ArrayStack(); - setList(stack); } - public void testNewStack() { 1.4 +5 -4 jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java Index: TestAll.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- TestAll.java 2001/04/16 22:42:04 1.3 +++ TestAll.java 2001/04/20 16:54:10 1.4 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v 1.3 2001/04/16 22:42:04 jvanzyl Exp $ - * $Revision: 1.3 $ - * $Date: 2001/04/16 22:42:04 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestAll.java,v 1.4 2001/04/20 16:54:10 rwaldhoff Exp $ + * $Revision: 1.4 $ + * $Date: 2001/04/20 16:54:10 $ * * ==================================================================== * @@ -64,8 +64,9 @@ import junit.framework.*; /** + * Entry point for all Collections tests. * @author Rodney Waldhoff - * @version $Id: TestAll.java,v 1.3 2001/04/16 22:42:04 jvanzyl Exp $ + * @version $Id: TestAll.java,v 1.4 2001/04/20 16:54:10 rwaldhoff Exp $ */ public class TestAll extends TestCase { public TestAll(String testName) { 1.1 jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java Index: TestObject.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestObject.java,v 1.1 2001/04/20 16:54:03 rwaldhoff Exp $ * $Revision: 1.1 $ * $Date: 2001/04/20 16:54:03 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.collections; import junit.framework.*; import java.util.Collection; import java.util.ArrayList; import java.util.Iterator; import java.util.NoSuchElementException; /** * Tests base {@link java.util.Object} methods and contracts. * <p> * To use, simply extend this class, and implement * the {@link #makeObject} method. * <p> * If your {@link Object} fails one of these tests by design, * you may still use this base set of cases. Simply override the * test case (method) your {@link Object} fails. * * @author Rodney Waldhoff * @version $Id: TestObject.java,v 1.1 2001/04/20 16:54:03 rwaldhoff Exp $ */ public abstract class TestObject extends TestCase { public TestObject(String testName) { super(testName); } /** * Return a new, empty {@link Object} to used for testing. */ public abstract Object makeObject(); public void testObjectEqualsSelf() { Object obj = makeObject(); assertEquals("A Object should equal itself",obj,obj); } public void testObjectHashCodeEqualsSelfHashCode() { Object obj = makeObject(); assertEquals("hashCode should be repeatable",obj.hashCode(),obj.hashCode()); } public void testObjectHashCodeEqualsContract() { Object obj1 = makeObject(); if(obj1.equals(obj1)) { assertEquals("[1] When two objects are equal, their hashCodes should be also.",obj1.hashCode(),obj1.hashCode()); } Object obj2 = makeObject(); if(obj1.equals(obj2)) { assertEquals("[2] When two objects are equal, their hashCodes should be also.",obj1.hashCode(),obj2.hashCode()); } } } 1.1 jakarta-commons/collections/src/test/org/apache/commons/collections/TestMap.java Index: TestMap.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestMap.java,v 1.1 2001/04/20 16:54:04 rwaldhoff Exp $ * $Revision: 1.1 $ * $Date: 2001/04/20 16:54:04 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Commons", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.collections; import junit.framework.*; import java.util.Map; import java.util.Collection; /** * Tests base {@link java.util.Map} methods and contracts. * <p> * To use, simply extend this class, and implement * the {@link #makeMap} method. * <p> * If your {@link Map} fails one of these tests by design, * you may still use this base set of cases. Simply override the * test case (method) your {@link Map} fails. * * @author Rodney Waldhoff * @version $Id: TestMap.java,v 1.1 2001/04/20 16:54:04 rwaldhoff Exp $ */ public abstract class TestMap extends TestObject { public TestMap(String testName) { super(testName); } /** * Return a new, empty {@link Map} to used for testing. */ public abstract Map makeMap(); public Object makeObject() { return makeMap(); } /* // optional operation public void testMapClear() { // XXX finish me } public void testMapContainsKey() { // XXX finish me } public void testMapContainsValue() { // XXX finish me } public void testMapEntrySet() { // XXX finish me } public void testMapEquals() { // XXX finish me } public void testMapGet() { // XXX finish me } public void testMapHashCode() { // XXX finish me } public void testMapIsEmpty() { // XXX finish me } public void testMapKeySet() { // XXX finish me } // optional operation public void testMapPut() { // XXX finish me } // optional operation public void testMapPutAll() { // XXX finish me } // optional operation public void testMapRemove() { // XXX finish me } public void testMapSize() { // XXX finish me } public void testMapValues() { // XXX finish me } */ /** * Try to put the given pair into the given Collection. * * Fails any Throwable except UnsupportedOperationException, * ClassCastException, or IllegalArgumentException * or NullPointerException is thrown. */ protected Object tryToPut(Map map, Object key, Object val) { try { return map.put(key,val); } catch(UnsupportedOperationException e) { return null; } catch(ClassCastException e) { return null; } catch(IllegalArgumentException e) { return null; } catch(NullPointerException e) { return null; } catch(Throwable t) { t.printStackTrace(); fail("Map.put should only throw UnsupportedOperationException, ClassCastException, IllegalArgumentException or NullPointerException. Found " + t.toString()); return null; // never get here, since fail throws exception } } }