From d3a61e7648609d365ac04814829515741f025ced Mon Sep 17 00:00:00 2001 From: Morgan James Delagrange Date: Mon, 25 Feb 2002 23:26:43 +0000 Subject: [PATCH] added base class for testing iterators git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130578 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/TestFilterIterator.java | 57 +++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/src/test/org/apache/commons/collections/TestFilterIterator.java b/src/test/org/apache/commons/collections/TestFilterIterator.java index 12aadf1eb..86331d938 100644 --- a/src/test/org/apache/commons/collections/TestFilterIterator.java +++ b/src/test/org/apache/commons/collections/TestFilterIterator.java @@ -4,13 +4,14 @@ package org.apache.commons.collections; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Test; +import java.util.Iterator; import java.util.NoSuchElementException; /** * * @author Jan Sorensen */ -public class TestFilterIterator extends TestCase { +public class TestFilterIterator extends TestIterator { /** Creates new TestFilterIterator */ public TestFilterIterator(String name) { @@ -41,6 +42,30 @@ public class TestFilterIterator extends TestCase { return (new TestSuite(TestFilterIterator.class)); } + /** + * Returns an full iterator wrapped in a + * FilterIterator that blocks all the elements + * + * @return "empty" FilterIterator + */ + public Iterator makeEmptyIterator() { + return makeBlockAllFilter(new ArrayIterator(array)); + } + + /** + * Returns an array with elements wrapped in a pass-through + * FilterIterator + * + * @return + */ + public Iterator makeFullIterator() { + return makePassThroughFilter(new ArrayIterator(array)); + } + + public Object makeObject() { + return makeFullIterator(); + } + public void testRepeatedHasNext() { for (int i = 0; i <= array.length; i++) { assertTrue(iterator.hasNext()); @@ -95,11 +120,35 @@ public class TestFilterIterator extends TestCase { } private void initIterator() { - iterator = new FilterIterator(new ArrayIterator(array)); + iterator = makePassThroughFilter(new ArrayIterator(array)); + } + + /** + * Returns a FilterIterator that does not filter + * any of its elements + * + * @param i the Iterator to "filter" + * @return "filtered" iterator + */ + protected FilterIterator makePassThroughFilter(Iterator i) { Predicate pred = new Predicate() { - public boolean evaluate(Object x) { return true; } + public boolean evaluate(Object x) { return true; } }; - iterator.setPredicate(pred); + return new FilterIterator(i,pred); + } + + /** + * Returns a FilterIterator that blocks + * all of its elements + * + * @param i the Iterator to "filter" + * @return "filtered" iterator + */ + protected FilterIterator makeBlockAllFilter(Iterator i) { + Predicate pred = new Predicate() { + public boolean evaluate(Object x) { return false; } + }; + return new FilterIterator(i,pred); } }