Test reworked array iterators to include Iterator and ListIterator implementations.

Also test Object array versions.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130891 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2002-12-13 12:10:48 +00:00
parent 93b9f67629
commit 7fe55f624b
7 changed files with 748 additions and 100 deletions

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v 1.3 2002/11/21 23:09:26 scolebourne Exp $
* $Revision: 1.3 $
* $Date: 2002/11/21 23:09:26 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestAll.java,v 1.4 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.4 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
@ -68,7 +68,7 @@ import junit.framework.TestSuite;
/**
* Entry point for all Collections tests.
* @author Rodney Waldhoff
* @version $Id: TestAll.java,v 1.3 2002/11/21 23:09:26 scolebourne Exp $
* @version $Id: TestAll.java,v 1.4 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestAll extends TestCase {
public TestAll(String testName) {
@ -79,6 +79,10 @@ public class TestAll extends TestCase {
TestSuite suite = new TestSuite();
suite.addTest(TestArrayIterator.suite());
suite.addTest(TestArrayIterator2.suite());
suite.addTest(TestArrayListIterator.suite());
suite.addTest(TestArrayListIterator2.suite());
suite.addTest(TestObjectArrayIterator.suite());
suite.addTest(TestObjectArrayListIterator.suite());
suite.addTest(TestCollatingIterator.suite());
suite.addTest(TestFilterIterator.suite());
suite.addTest(TestFilterListIterator.suite());

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayIterator.java,v 1.1 2002/08/15 23:13:52 pjack Exp $
* $Revision: 1.1 $
* $Date: 2002/08/15 23:13:52 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayIterator.java,v 1.2 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.2 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
@ -58,13 +58,11 @@
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import junit.framework.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Tests the ArrayIterator to ensure that the next() method will actually
* perform the iteration rather than the hasNext() method.
@ -73,13 +71,12 @@ import java.util.NoSuchElementException;
* @author James Strachan
* @author Mauricio S. Moura
* @author Morgan Delagrange
* @version $Id: TestArrayIterator.java,v 1.1 2002/08/15 23:13:52 pjack Exp $
* @author Stephen Colebourne
* @version $Id: TestArrayIterator.java,v 1.2 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestArrayIterator extends TestIterator {
protected String[] testArray = {
"One", "Two", "Three"
};
protected String[] testArray = { "One", "Two", "Three" };
public static Test suite() {
return new TestSuite(TestArrayIterator.class);
@ -104,21 +101,27 @@ public class TestArrayIterator extends TestIterator {
return makeFullIterator();
}
public boolean supportsRemove() {
return false;
}
public void testIterator() {
Iterator iter = (Iterator) makeFullIterator();
for ( int i = 0; i < testArray.length; i++ ) {
for (int i = 0; i < testArray.length; i++) {
Object testValue = testArray[i];
Object iterValue = iter.next();
assertEquals( "Iteration value is correct", testValue, iterValue );
assertEquals("Iteration value is correct", testValue, iterValue);
}
assertTrue("Iterator should now be empty", ! iter.hasNext() );
assertTrue("Iterator should now be empty", !iter.hasNext());
try {
Object testValue = iter.next();
} catch (Exception e) {
assertTrue("NoSuchElementException must be thrown",
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
@ -127,8 +130,7 @@ public class TestArrayIterator extends TestIterator {
try {
Iterator iter = new ArrayIterator(null);
fail("Constructor should throw a NullPointerException when " +
"constructed with a null array");
fail("Constructor should throw a NullPointerException when constructed with a null array");
} catch (NullPointerException e) {
// expected
}
@ -142,5 +144,12 @@ public class TestArrayIterator extends TestIterator {
// expected
}
}
}
public void testReset() {
ArrayIterator it = (ArrayIterator) makeFullIterator();
it.next();
it.reset();
assertEquals("One", it.next());
}
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayIterator2.java,v 1.1 2002/08/15 23:13:52 pjack Exp $
* $Revision: 1.1 $
* $Date: 2002/08/15 23:13:52 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayIterator2.java,v 1.2 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.2 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
@ -58,25 +58,21 @@
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.iterators;
import junit.framework.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* Tests the ArrayIterator with primitive type arrays
*
* @author Morgan Delagrange
* @author James Strachan
* @version $Id: TestArrayIterator2.java,v 1.1 2002/08/15 23:13:52 pjack Exp $
* @version $Id: TestArrayIterator2.java,v 1.2 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestArrayIterator2 extends TestIterator {
protected int[] testArray = {
2, 4, 6, 8
};
protected int[] testArray = { 2, 4, 6, 8 };
public static Test suite() {
return new TestSuite(TestArrayIterator2.class);
@ -101,21 +97,47 @@ public class TestArrayIterator2 extends TestIterator {
return makeFullIterator();
}
public void testIterator() {
Iterator iter = (Iterator) makeFullIterator();
for ( int i = 0; i < testArray.length; i++ ) {
Integer testValue = new Integer( testArray[i] );
Number iterValue = (Number) iter.next();
assertEquals( "Iteration value is correct", testValue, iterValue );
/*
* We use these <code>makeArrayIterator</code> factory methods instead of
* directly calling the constructor so as to allow subclasses
* (e.g. TestArrayListIterator2) to use the existing test code.
*
* @return ArrayIterator
*/
public ArrayIterator makeArrayIterator() {
return (ArrayIterator) makeEmptyIterator();
}
public ArrayIterator makeArrayIterator(Object array) {
return new ArrayIterator(array);
}
public ArrayIterator makeArrayIterator(Object array, int index) {
return new ArrayIterator(array, index);
}
public ArrayIterator makeArrayIterator(Object array, int start, int end) {
return new ArrayIterator(array, start, end);
}
assertTrue("Iterator should now be empty", ! iter.hasNext() );
public boolean supportsRemove() {
return false;
}
public void testIterator() {
Iterator iter = (Iterator) makeFullIterator();
for (int i = 0; i < testArray.length; i++) {
Integer testValue = new Integer(testArray[i]);
Number iterValue = (Number) iter.next();
assertEquals("Iteration value is correct", testValue, iterValue);
}
assertTrue("Iterator should now be empty", !iter.hasNext());
try {
Object testValue = iter.next();
} catch (Exception e) {
assertTrue("NoSuchElementException must be thrown",
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
@ -123,17 +145,16 @@ public class TestArrayIterator2 extends TestIterator {
// proves that an ArrayIterator set with the constructor has the same number of elements
// as an ArrayIterator set with setArray(Object)
public void testSetArray() {
Iterator iter1 = new ArrayIterator(testArray);
Iterator iter1 = makeArrayIterator(testArray);
int count1 = 0;
while (iter1.hasNext()) {
++count1;
iter1.next();
}
assertEquals("the count should be right using the constructor",
count1,testArray.length);
assertEquals("the count should be right using the constructor", count1, testArray.length);
ArrayIterator iter2 = new ArrayIterator();
ArrayIterator iter2 = makeArrayIterator();
iter2.setArray(testArray);
int count2 = 0;
while (iter2.hasNext()) {
@ -141,78 +162,73 @@ public class TestArrayIterator2 extends TestIterator {
iter2.next();
}
assertEquals("the count should be right using setArray(Object)",
count2,testArray.length);
assertEquals("the count should be right using setArray(Object)", count2, testArray.length);
}
public void testIndexedArray() {
Iterator iter = new ArrayIterator(testArray,2);
Iterator iter = makeArrayIterator(testArray, 2);
int count = 0;
while (iter.hasNext()) {
++count;
iter.next();
}
assertEquals("the count should be right using ArrayIterator(Object,2) ",
count,testArray.length-2);
assertEquals("the count should be right using ArrayIterator(Object,2) ", count, testArray.length - 2);
iter = new ArrayIterator(testArray,1,testArray.length-1);
iter = makeArrayIterator(testArray, 1, testArray.length - 1);
count = 0;
while (iter.hasNext()) {
++count;
iter.next();
}
assertEquals("the count should be right using ArrayIterator(Object,1,"+
(testArray.length-1)+") ", count, testArray.length-2);
assertEquals(
"the count should be right using ArrayIterator(Object,1," + (testArray.length - 1) + ") ",
count,
testArray.length - 2);
try {
iter = new ArrayIterator(testArray,-1);
fail("new ArrayIterator(Object,-1) should throw an "+
"ArrayIndexOutOfBoundsException");
} catch(ArrayIndexOutOfBoundsException aioobe) {
iter = makeArrayIterator(testArray, -1);
fail("new ArrayIterator(Object,-1) should throw an ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aioobe) {
// expected
}
try {
iter = new ArrayIterator(testArray,testArray.length+1);
fail("new ArrayIterator(Object,length+1) should throw an "+
"ArrayIndexOutOfBoundsException");
} catch(ArrayIndexOutOfBoundsException aioobe) {
iter = makeArrayIterator(testArray, testArray.length + 1);
fail("new ArrayIterator(Object,length+1) should throw an ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aioobe) {
// expected
}
try {
iter = new ArrayIterator(testArray,0,-1);
fail("new ArrayIterator(Object,0,-1) should throw an "+
"ArrayIndexOutOfBoundsException");
} catch(ArrayIndexOutOfBoundsException aioobe) {
iter = makeArrayIterator(testArray, 0, -1);
fail("new ArrayIterator(Object,0,-1) should throw an ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aioobe) {
// expected
}
try {
iter = new ArrayIterator(testArray,0,testArray.length+1);
fail("new ArrayIterator(Object,0,length+1) should throw an "+
"ArrayIndexOutOfBoundsException");
} catch(ArrayIndexOutOfBoundsException aioobe) {
iter = makeArrayIterator(testArray, 0, testArray.length + 1);
fail("new ArrayIterator(Object,0,length+1) should throw an ArrayIndexOutOfBoundsException");
} catch (ArrayIndexOutOfBoundsException aioobe) {
// expected
}
try {
iter = new ArrayIterator(testArray,1,1);
fail("new ArrayIterator(Object,1,1) should throw an "+
"IllegalArgumentException");
} catch(IllegalArgumentException iae) {
// expected
iter = makeArrayIterator(testArray, 1, 1);
// expected not to fail
} catch (IllegalArgumentException iae) {
// MODIFIED: an iterator over a zero-length section of array
// should be perfectly legal behavior
fail("new ArrayIterator(Object,1,1) should NOT throw an IllegalArgumentException");
}
try {
iter = new ArrayIterator(testArray,testArray.length-1,testArray.length-2);
fail("new ArrayIterator(Object,length-2,length-1) should throw an "+
"IllegalArgumentException");
} catch(IllegalArgumentException iae) {
iter = makeArrayIterator(testArray, testArray.length - 1, testArray.length - 2);
fail("new ArrayIterator(Object,length-2,length-1) should throw an IllegalArgumentException");
} catch (IllegalArgumentException iae) {
// expected
}
}
}

View File

@ -0,0 +1,168 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayListIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.1 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
* 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 apache@apache.org.
*
* 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.iterators;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
*
* @author Neil O'Toole - <a href="mailto:neilotoole@users.sourceforge.net">neilotoole@users.sourceforge.net</a>
* @version $Id: TestArrayListIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestArrayListIterator extends TestArrayIterator {
public TestArrayListIterator(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestArrayListIterator.class);
}
public Iterator makeEmptyIterator() {
return new ArrayListIterator(new Object[0]);
}
public Iterator makeFullIterator() {
return new ArrayListIterator(testArray);
}
public ListIterator makeArrayListIterator(Object array) {
return new ArrayListIterator(array);
}
public boolean supportsRemove() {
return false;
}
/**
* Test the basic ListIterator functionality - going backwards using
* <code>previous()</code>.
*/
public void testListIterator() {
ListIterator iter = (ListIterator) makeFullIterator();
// TestArrayIterator#testIterator() has already tested the iterator forward,
// now we need to test it in reverse
// fast-forward the iterator to the end...
while (iter.hasNext()) {
iter.next();
}
for (int x = testArray.length - 1; x >= 0; x--) {
Object testValue = testArray[x];
Object iterValue = iter.previous();
assertEquals("Iteration value is correct", testValue, iterValue);
}
assertTrue("Iterator should now be empty", !iter.hasPrevious());
try {
Object testValue = iter.previous();
} catch (Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
/**
* Tests the {@link java.util.ListIterator#set} operation.
*/
public void testListIteratorSet() {
String[] testData = new String[] { "a", "b", "c" };
String[] result = new String[] { "0", "1", "2" };
ListIterator iter = (ListIterator) makeArrayListIterator(testData);
int x = 0;
while (iter.hasNext()) {
iter.next();
iter.set(Integer.toString(x));
x++;
}
assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result));
// a call to set() before a call to next() or previous() should throw an IllegalStateException
iter = makeArrayListIterator(testArray);
try {
iter.set("should fail");
fail("ListIterator#set should fail if next() or previous() have not yet been called.");
} catch (IllegalStateException e) {
// expected
} catch (Throwable t) { // should never happen
fail(t.toString());
}
}
}

View File

@ -0,0 +1,107 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestArrayListIterator2.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.1 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
* 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 apache@apache.org.
*
* 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.iterators;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* @author <a href="mailto:neilotoole@users.sourceforge.net">Neil O'Toole</a>
* @see org.apache.commons.collections.iterators.TestArrayIterator2
* @version $Id: TestArrayListIterator2.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestArrayListIterator2 extends TestArrayIterator2 {
public TestArrayListIterator2(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestArrayListIterator2.class);
}
public Iterator makeEmptyIterator() {
return new ArrayListIterator(new int[0]);
}
public Iterator makeFullIterator() {
return new ArrayListIterator(testArray);
}
public ArrayIterator makeArrayIterator() {
return (ArrayIterator) makeEmptyIterator();
}
public ArrayIterator makeArrayIterator(Object array) {
return new ArrayListIterator(array);
}
public ArrayIterator makeArrayIterator(Object array, int index) {
return new ArrayListIterator(array, index);
}
public ArrayIterator makeArrayIterator(Object array, int start, int end) {
return new ArrayListIterator(array, start, end);
}
}

View File

@ -0,0 +1,180 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestObjectArrayIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.1 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
* 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 apache@apache.org.
*
* 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.iterators;
import java.util.Iterator;
import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Tests the ObjectArrayIterator.
*
* @author James Strachan
* @author Mauricio S. Moura
* @author Morgan Delagrange
* @author Stephen Colebourne
* @version $Id: TestObjectArrayIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestObjectArrayIterator extends TestIterator {
protected String[] testArray = { "One", "Two", "Three" };
public static Test suite() {
return new TestSuite(TestObjectArrayIterator.class);
}
public TestObjectArrayIterator(String testName) {
super(testName);
}
public Iterator makeEmptyIterator() {
return new ObjectArrayIterator(new Object[0]);
}
public Iterator makeFullIterator() {
return new ObjectArrayIterator(testArray);
}
public ObjectArrayIterator makeArrayIterator() {
return new ObjectArrayIterator();
}
public ObjectArrayIterator makeArrayIterator(Object[] array) {
return new ObjectArrayIterator(array);
}
public ObjectArrayIterator makeArrayIterator(Object[] array, int index) {
return new ObjectArrayIterator(array, index);
}
public ObjectArrayIterator makeArrayIterator(Object[] array, int start, int end) {
return new ObjectArrayIterator(array, start, end);
}
public boolean supportsRemove() {
return false;
}
/**
* Return a new, empty {@link Object} to used for testing.
*/
public Object makeObject() {
return makeFullIterator();
}
public void testIterator() {
Iterator iter = (Iterator) makeFullIterator();
for (int i = 0; i < testArray.length; i++) {
Object testValue = testArray[i];
Object iterValue = iter.next();
assertEquals("Iteration value is correct", testValue, iterValue);
}
assertTrue("Iterator should now be empty", !iter.hasNext());
try {
Object testValue = iter.next();
} catch (Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
public void testNullArray() {
try {
Iterator iter = makeArrayIterator(null);
fail("Constructor should throw a NullPointerException when constructed with a null array");
} catch (NullPointerException e) {
// expected
}
ObjectArrayIterator iter = makeArrayIterator();
try {
iter.setArray(null);
fail("setArray(null) should throw a NullPointerException");
} catch (NullPointerException e) {
// expected
}
}
public void testDoubleSet() {
ObjectArrayIterator it = makeArrayIterator();
it.setArray(new String[0]);
try {
it.setArray(new String[0]);
fail();
} catch (IllegalStateException ex) {
}
}
public void testReset() {
ObjectArrayIterator it = makeArrayIterator(testArray);
it.next();
it.reset();
assertEquals("One", it.next());
}
}

View File

@ -0,0 +1,164 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/TestObjectArrayListIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
* $Revision: 1.1 $
* $Date: 2002/12/13 12:10:48 $
*
* ====================================================================
*
* 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 apache@apache.org.
*
* 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.iterators;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
*
* @author <a href="mailto:neilotoole@users.sourceforge.net">Neil O'Toole</a>
* @version $Id: TestObjectArrayListIterator.java,v 1.1 2002/12/13 12:10:48 scolebourne Exp $
*/
public class TestObjectArrayListIterator extends TestObjectArrayIterator {
public TestObjectArrayListIterator(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestObjectArrayListIterator.class);
}
public Iterator makeEmptyIterator() {
return new ObjectArrayListIterator(new Object[0]);
}
public Iterator makeFullIterator() {
return new ObjectArrayListIterator(testArray);
}
public ListIterator makeArrayListIterator(Object[] array) {
return new ObjectArrayListIterator(array);
}
/**
* Test the basic ListIterator functionality - going backwards using
* <code>previous()</code>.
*/
public void testListIterator() {
ListIterator iter = (ListIterator) makeFullIterator();
// TestArrayIterator#testIterator() has already tested the iterator forward,
// now we need to test it in reverse
// fast-forward the iterator to the end...
while (iter.hasNext()) {
iter.next();
}
for (int x = testArray.length - 1; x >= 0; x--) {
Object testValue = testArray[x];
Object iterValue = iter.previous();
assertEquals("Iteration value is correct", testValue, iterValue);
}
assertTrue("Iterator should now be empty", !iter.hasPrevious());
try {
Object testValue = iter.previous();
} catch (Exception e) {
assertTrue(
"NoSuchElementException must be thrown",
e.getClass().equals((new NoSuchElementException()).getClass()));
}
}
/**
* Tests the {@link java.util.ListIterator#set} operation.
*/
public void testListIteratorSet() {
String[] testData = new String[] { "a", "b", "c" };
String[] result = new String[] { "0", "1", "2" };
ListIterator iter = (ListIterator) makeArrayListIterator(testData);
int x = 0;
while (iter.hasNext()) {
iter.next();
iter.set(Integer.toString(x));
x++;
}
assertTrue("The two arrays should have the same value, i.e. {0,1,2}", Arrays.equals(testData, result));
// a call to set() before a call to next() or previous() should throw an IllegalStateException
iter = makeArrayListIterator(testArray);
try {
iter.set("should fail");
fail("ListIterator#set should fail if next() or previous() have not yet been called.");
} catch (IllegalStateException e) {
// expected
} catch (Throwable t) { // should never happen
fail(t.toString());
}
}
}