You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/10/16 00:35:15 UTC

svn commit: r464310 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/AbstractCollection.java test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java

Author: ndbeyer
Date: Sun Oct 15 15:35:15 2006
New Revision: 464310

URL: http://svn.apache.org/viewvc?view=rev&rev=464310
Log:
AbstractCollection -
* Minor javadoc cleanup
* Rework unit test

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractCollection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractCollection.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractCollection.java?view=diff&rev=464310&r1=464309&r2=464310
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractCollection.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/AbstractCollection.java Sun Oct 15 15:35:15 2006
@@ -43,7 +43,7 @@
      * implementations may add specific limitations on this method to filter
      * permitted elements. For example, in some implementation, null element may
      * be denied, and NullPointerException will be thrown out. These limitations
-     * should be explicitly documented by specific collection implmentation.
+     * should be explicitly documented by specific collection implementation.
      * 
      * Add operation is not supported in this implementation, and
      * UnsupportedOperationException will always be thrown out.
@@ -52,15 +52,15 @@
      *            the element to be added.
      * @return true if the collection is changed successfully after invoking
      *         this method. Otherwise, false.
-     * @exception UnsupportedOperationException
+     * @throws UnsupportedOperationException
      *                if add operation is not supported by this class.
-     * @exception NullPointerException
+     * @throws NullPointerException
      *                if null is used to invoke this method, and null is not
      *                permitted by this collection.
-     * @exception ClassCastException
+     * @throws ClassCastException
      *                if the class type of the specified element is not
      *                compatible with the permitted class type.
-     * @exception IllegalArgumentException
+     * @throws IllegalArgumentException
      *                if limitations of this collection prevent the specified
      *                element from being added
      */
@@ -75,9 +75,9 @@
 	 *            the Collection of objects
 	 * @return true if this Collection is modified, false otherwise
 	 * 
-	 * @exception UnsupportedOperationException
+	 * @throws UnsupportedOperationException
 	 *                when adding to this Collection is not supported
-	 * @exception NullPointerException
+	 * @throws NullPointerException
 	 *                if null is used to invoke this method
 	 */
 	public boolean addAll(Collection<? extends E> collection) {
@@ -100,7 +100,7 @@
      * by this collection does not implement the remove method and the collection
      * is not zero length.
      * 
-     * @exception UnsupportedOperationException 
+     * @throws UnsupportedOperationException 
      *                  if this operation is not implemented.
      */
 	public void clear() {
@@ -144,7 +144,7 @@
 	 *            the Collection of objects
 	 * @return true if all objects in the specified Collection are elements of
 	 *         this Collection, false otherwise
-     * @exception NullPointerException
+     * @throws NullPointerException
      *                if null is used to invoke this method
 	 */
 	public boolean containsAll(Collection<?> collection) {
@@ -190,7 +190,7 @@
 	 *            the object to remove
 	 * @return true if this Collection is modified, false otherwise
 	 * 
-	 * @exception UnsupportedOperationException
+	 * @throws UnsupportedOperationException
 	 *                when removing from this Collection is not supported
 	 */
 	public boolean remove(Object object) {
@@ -229,9 +229,9 @@
 	 *            the Collection of objects to remove
 	 * @return true if this Collection is modified, false otherwise
 	 * 
-	 * @exception UnsupportedOperationException
+	 * @throws UnsupportedOperationException
 	 *                when removing from this Collection is not supported
-     * @exception NullPointerException
+     * @throws NullPointerException
      *                if null is used to invoke this method
 	 */
 	public boolean removeAll(Collection<?> collection) {
@@ -262,9 +262,9 @@
 	 *            the Collection of objects to retain
 	 * @return true if this Collection is modified, false otherwise
 	 * 
-	 * @exception UnsupportedOperationException
+	 * @throws UnsupportedOperationException
 	 *                when removing from this Collection is not supported
-     * @exception NullPointerException
+     * @throws NullPointerException
      *                if null is used to invoke this method
 	 */
 	public boolean retainAll(Collection<?> collection) {
@@ -316,10 +316,10 @@
 	 *            the array
 	 * @return an array of the elements from this Collection
 	 * 
-	 * @exception ArrayStoreException
+	 * @throws ArrayStoreException
 	 *                when the type of an element in this Collection cannot be
 	 *                stored in the type of the specified array
-     * @exception NullPointerException
+     * @throws NullPointerException
      *                if null is used to invoke this method
 	 */
 	@SuppressWarnings("unchecked")

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java?view=diff&rev=464310&r1=464309&r2=464310
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/luni/tests/java/util/AbstractCollectionTest.java Sun Oct 15 15:35:15 2006
@@ -18,100 +18,234 @@
 package org.apache.harmony.luni.tests.java.util;
 
 import java.util.AbstractCollection;
-import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.Iterator;
+import junit.framework.TestCase;
 
-public class AbstractCollectionTest extends junit.framework.TestCase {
-
-    Collection org;
+public class AbstractCollectionTest extends TestCase {
 
     /**
      * @tests java.util.AbstractCollection#add(java.lang.Object)
      */
     public void test_addLjava_lang_Object() {
-        assertTrue("Nothing to test--unsupported operation", true);
+        AbstractCollection<Object> ac = new AbstractCollection<Object>() {
+
+            @Override
+            public Iterator<Object> iterator() {
+                fail("iterator should not get called");
+                return null;
+            }
+
+            @Override
+            public int size() {
+                fail("size should not get called");
+                return 0;
+            }
+            
+        };
+        try {
+            ac.add(null);
+        } catch (UnsupportedOperationException e) {
+        }
     }
 
     /**
      * @tests java.util.AbstractCollection#addAll(java.util.Collection)
      */
     public void test_addAllLjava_util_Collection() {
-        Collection col = new HashSet();
-        col.addAll(org);
-        assertEquals("Size", 100, col.size());
+        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
+
+            @Override
+            public boolean add(String object) {
+                assertTrue(fixtures.contains(object));
+                return true;
+            }
+
+            @Override
+            public Iterator<String> iterator() {
+                fail("iterator should not get called");
+                return null;
+            }
+
+            @Override
+            public int size() {
+                fail("size should not get called");
+                return 0;
+            }
+            
+        };
+        assertTrue(ac.addAll(fixtures));
     }
 
     /**
      * @tests java.util.AbstractCollection#containsAll(java.util.Collection)
      */
     public void test_containsAllLjava_util_Collection() {
-        ArrayList col = new ArrayList(org);
-        assertTrue("Should contain all of the elements it started with", col
-                .containsAll(org));
-        col.remove(5);
-        assertTrue(
-                "Should no longer contain all of the elements it started with",
-                !col.containsAll(org));
+        final Collection<String> fixtures = Arrays.asList("0", "1", "2");
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
+
+            @Override
+            public boolean contains(Object object) {
+                assertTrue(fixtures.contains(object));
+                return true;
+            }
+
+            @Override
+            public Iterator<String> iterator() {
+                fail("iterator should not get called");
+                return null;
+            }
+
+            @Override
+            public int size() {
+                fail("size should not get called");
+                return 0;
+            }
+            
+        };
+        assertTrue(ac.containsAll(fixtures));
     }
 
     /**
      * @tests java.util.AbstractCollection#isEmpty()
      */
     public void test_isEmpty() {
-        Collection col = new ArrayList();
-        assertTrue("Should be empty when created", col.isEmpty());
-        col.addAll(org);
-        assertTrue("Should not be empty", !col.isEmpty());
-        col.clear();
-        assertTrue("Should be empty after clear", col.isEmpty());
+        final boolean[] sizeCalled = new boolean[1];
+        AbstractCollection<Object> ac = new AbstractCollection<Object>(){
+            @Override
+            public Iterator<Object> iterator() {
+                fail("iterator should not get called");
+                return null;
+            }
+            @Override
+            public int size() {
+                sizeCalled[0] = true;
+                return 0;
+            }
+        };
+        assertTrue(ac.isEmpty());
+        assertTrue(sizeCalled[0]);
     }
 
     /**
      * @tests java.util.AbstractCollection#removeAll(java.util.Collection)
      */
     public void test_removeAllLjava_util_Collection() {
-        Collection someCol = new HashSet(org);
-        Collection anotherCol = new HashSet(org);
+        final String[] removed = new String[3];
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
 
-        anotherCol.remove(new Integer(5));
-        someCol.removeAll(anotherCol);
-        assertEquals("Size--wanted 1", 1, someCol.size());
+            @Override
+            public Iterator<String> iterator() {
+                return new Iterator<String>() {
+                    String[] values = new String[] {"0", "1", "2"};
+                    int index;
+                    public boolean hasNext() {
+                        return index < values.length;
+                    }
+
+                    public String next() {
+                        return values[index++];
+                    }
+
+                    public void remove() {
+                        removed[index - 1] = values[index - 1];
+                    }
+                    
+                };
+            }
 
-        someCol.remove(new Integer(5));
-        assertEquals("Size--wanted 0", 0, someCol.size());
+            @Override
+            public int size() {
+                fail("size should not get called");
+                return 0;
+            }
+            
+        };
+        assertTrue(ac.removeAll(Arrays.asList("0", "1", "2")));
+        for (String r : removed) {
+            if (!"0".equals(r) && !"1".equals(r) && !"2".equals(r)) {
+                fail("an unexpected element was removed");
+            }
+        }
     }
 
     /**
      * @tests java.util.AbstractCollection#retainAll(java.util.Collection)
      */
     public void test_retainAllLjava_util_Collection() {
-        Collection someCol = new HashSet(org);
-        Collection anotherCol = new HashSet(org);
+        final String[] removed = new String[1];
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
 
-        anotherCol.remove(new Integer(5));
-        someCol.retainAll(anotherCol);
-        assertEquals("Size--wanted 99", 99, someCol.size());
+            @Override
+            public Iterator<String> iterator() {
+                return new Iterator<String>() {
+                    String[] values = new String[] {"0", "1", "2"};
+                    int index;
+                    public boolean hasNext() {
+                        return index < values.length;
+                    }
+
+                    public String next() {
+                        return values[index++];
+                    }
+
+                    public void remove() {
+                        removed[index - 1] = values[index - 1];
+                    }
+                    
+                };
+            }
 
-        someCol.add(new Integer(5));
-        assertEquals("Size--wanted 100", 100, someCol.size());
+            @Override
+            public int size() {
+                fail("size should not get called");
+                return 0;
+            }
+            
+        };
+        assertTrue(ac.retainAll(Arrays.asList("1", "2")));
+        assertEquals("0", removed[0]);
     }
 
     /**
      * @tests java.util.AbstractCollection#toArray()
      */
     public void test_toArray() {
-        Object[] objArray = org.toArray();
-        assertEquals("Length", 100, objArray.length);
-        HashSet duplicates = new HashSet();
-        for (int i = objArray.length - 1; i >= 0; i--) {
-            assertTrue("The returned array has an incorrect value. At i = " + i
-                    + " got: " + ((Integer) objArray[i]).intValue(), org
-                    .contains(objArray[i]));
-            assertTrue("Duplicate found at i = " + i, !duplicates
-                    .contains(objArray[i]));
-            duplicates.add(objArray[i]);
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
+            @Override
+            public Iterator<String> iterator() {
+                return new Iterator<String>() {
+                    String[] values = new String[] {"0", "1", "2"};
+                    int index;
+                    public boolean hasNext() {
+                        return index < values.length;
+                    }
+
+                    public String next() {
+                        return values[index++];
+                    }
+
+                    public void remove() {
+                        fail("remove should not get called");
+                    }
+                    
+                };
+            }
+            
+            @Override
+            public int size() {
+                return 3;
+            }
+        };
+        
+        Object[] array = ac.toArray();
+        assertEquals(3, array.length);
+        for (Object o : array) {
+            if (!"0".equals(o) && !"1".equals(o) && !"2".equals(o)) {
+                fail("an unexpected element was removed");
+            }
         }
     }
 
@@ -119,59 +253,59 @@
      * @tests java.util.AbstractCollection#toArray(java.lang.Object[])
      */
     public void test_toArray$Ljava_lang_Object() {
-
+        AbstractCollection<String> ac = new AbstractCollection<String>() {
+            @Override
+            public Iterator<String> iterator() {
+                return new Iterator<String>() {
+                    String[] values = new String[] {"0", "1", "2"};
+                    int index;
+                    public boolean hasNext() {
+                        return index < values.length;
+                    }
+
+                    public String next() {
+                        return values[index++];
+                    }
+
+                    public void remove() {
+                        fail("remove should not get called");
+                    }
+                    
+                };
+            }
+            @Override
+            public int size() {
+                return 3;
+            }
+        };
         try {
-            org.toArray(null);
+            ac.toArray(null);
             fail("No expected NullPointerException");
         } catch (NullPointerException e) {
             // expected
         }
 
         try {
-            org.toArray(new String[org.size()]);
+            ac.toArray(new StringBuffer[ac.size()]);
             fail("No expected ArrayStoreException");
         } catch (ArrayStoreException e) {
             // expected
         }
-
-        Object[] objArray = new Object[100];
-        org.toArray(objArray);
-        assertEquals("a) Length", 100, objArray.length);
-        HashSet duplicates = new HashSet();
-        for (int i = objArray.length - 1; i >= 0; i--) {
-            assertTrue("a) The returned array has an incorrect value at i = "
-                    + i, org.contains(objArray[i]));
-            assertTrue("Duplicate found at i = " + i, !duplicates
-                    .contains(objArray[i]));
-            duplicates.add(objArray[i]);
-        }
-
-        Integer[] intArray = new Integer[105];
-        intArray[100] = new Integer(1203);
-        org.toArray(intArray);
-        assertEquals("b) Length", 105, intArray.length);
-        duplicates = new HashSet();
-        for (int i = 99; i >= 0; i--) {
-            assertTrue("b) The returned array has an incorrect value at i = "
-                    + i, org.contains(intArray[i]));
-            assertTrue("Duplicate found at i = " + i, !duplicates
-                    .contains(intArray[i]));
-            duplicates.add(intArray[i]);
-        }
-        assertNull("End of list should be null", intArray[100]);
-
-        intArray = new Integer[1];
-        intArray = (Integer[]) org.toArray(intArray);
-        assertEquals("c) Length", 100, intArray.length);
-        duplicates = new HashSet();
-        for (int i = intArray.length - 1; i >= 0; i--) {
-            assertTrue("c) The returned array has an incorrect value. At i = "
-                    + i + " got: " + intArray[i].intValue(), org
-                    .contains(intArray[i]));
-            assertTrue("Duplicate found at i = " + i, !duplicates
-                    .contains(intArray[i]));
-            duplicates.add(intArray[i]);
-        }
+        
+        String[] a = new String[3];
+        assertSame(a, ac.toArray(a));
+        
+        a = new String[0];
+        assertNotSame(a, ac.toArray(a));
+        a = ac.toArray(a);
+        assertEquals(3, a.length);
+        
+        CharSequence[] csa = new CharSequence[3];
+        ac.toArray(csa);
+        assertEquals(3, csa.length);
+        assertEquals("0", csa[0]);
+        assertEquals("1", csa[1]);
+        assertEquals("2", csa[2]);
     }
 
     /**
@@ -180,13 +314,15 @@
     public void test_toString() {
         // see HARMONY-1522
         // collection that returns null iterator(this is against the spec.)
-        AbstractCollection c = new AbstractCollection() {
+        AbstractCollection<?> c = new AbstractCollection<Object>() {
+            @Override
             public int size() {
-                // return not zero to pass 'is empty' check
+                // return non-zero value to pass 'isEmpty' check
                 return 1;
             }
 
-            public Iterator iterator() {
+            @Override
+            public Iterator<Object> iterator() {
                 // this violates the spec.
                 return null;
             }
@@ -199,15 +335,5 @@
             fail("No expected NullPointerException");
         } catch (NullPointerException e) {
         }
-    }
-
-    protected void setUp() {
-        org = new HashSet();
-        for (int i = 0; i < 100; i++)
-            org.add(new Integer(i));
-
-    }
-
-    protected void tearDown() {
     }
 }