rwaldhoff 01/04/24 16:35:13 Modified: collections/src/test/org/apache/commons/collections TestCollection.java Log: using tryToAdd() where appropriate Revision Changes Path 1.3 +11 -95 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.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- TestCollection.java 2001/04/20 16:54:08 1.2 +++ TestCollection.java 2001/04/24 23:35:13 1.3 @@ -1,7 +1,7 @@ /* - * $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 $ + * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestCollection.java,v 1.3 2001/04/24 23:35:13 rwaldhoff Exp $ + * $Revision: 1.3 $ + * $Date: 2001/04/24 23:35:13 $ * * ==================================================================== * @@ -78,7 +78,7 @@ * test case (method) your {@link Collection} fails. * * @author Rodney Waldhoff - * @version $Id: TestCollection.java,v 1.2 2001/04/20 16:54:08 rwaldhoff Exp $ + * @version $Id: TestCollection.java,v 1.3 2001/04/24 23:35:13 rwaldhoff Exp $ */ public abstract class TestCollection extends TestObject { public TestCollection(String testName) { @@ -251,19 +251,7 @@ public void testCollectionIsEmpty() { Collection c = makeCollection(); assert("New Collection should be empty.",c.isEmpty()); - 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()); - } + boolean added = tryToAdd(c,"element1"); if(added) { assert("If an element was added, the Collection.isEmpty() should return false.",!c.isEmpty()); } @@ -283,19 +271,7 @@ fail("Collection.iterator.next() should only throw NoSuchElementException. Found " + t.toString()); } - 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()); - } + boolean added = tryToAdd(c,"element1"); if(added) { Iterator it2 = c.iterator(); assert("Iterator for non-empty Collection should have next.",it2.hasNext()); @@ -316,19 +292,7 @@ // optional operation public void testCollectionRemove() { Collection c = makeCollection(); - 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()); - } + boolean added = tryToAdd(c,"element1"); try { assert("Shouldn't be able to remove an element that wasn't added.",!c.remove("element2")); @@ -362,19 +326,7 @@ } assert("Collection is still empty.",c.isEmpty()); - 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()); - } + boolean added = tryToAdd(c,"element1"); if(added) { assert("Collection is not empty.",!c.isEmpty()); try { @@ -440,19 +392,7 @@ public void testCollectionSize() { Collection c = makeCollection(); assertEquals("Size of new Collection is 0.",0,c.size()); - 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()); - } + boolean added = tryToAdd(c,"element1"); if(added) { assertEquals("If one element was added, the Collection.size() should be 1.",1,c.size()); } @@ -461,38 +401,14 @@ 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()); - } + boolean added = tryToAdd(c,"element1"); 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()); - } + boolean added2 = tryToAdd(c,"element2"); if(added && added2) { assertEquals("If another element was added, the Collection.toArray().length should be 2.",2,c.toArray().length); } else if(added2) {