001: /*
002: * Copyright 2001-2004,2006 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.iterators;
017:
018: import java.util.ArrayList;
019: import java.util.Arrays;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.NoSuchElementException;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.commons.collections.Predicate;
029: import org.apache.commons.collections.functors.NotNullPredicate;
030: import org.apache.commons.collections.functors.TruePredicate;
031:
032: /**
033: * Test the filter iterator.
034: *
035: * @version $Revision: 366576 $ $Date: 2006-01-06 22:07:07 +0000 (Fri, 06 Jan 2006) $
036: *
037: * @author Jan Sorensen
038: * @author Ralph Wagner
039: * @author Huw Roberts
040: */
041: public class TestFilterIterator extends AbstractTestIterator {
042:
043: /** Creates new TestFilterIterator */
044: public TestFilterIterator(String name) {
045: super (name);
046: }
047:
048: private String[] array;
049: private List list;
050: private FilterIterator iterator;
051:
052: /**
053: * Set up instance variables required by this test case.
054: */
055: public void setUp() {
056: array = new String[] { "a", "b", "c" };
057: initIterator();
058: }
059:
060: /**
061: * Tear down instance variables required by this test case.
062: */
063: public void tearDown() throws Exception {
064: iterator = null;
065: }
066:
067: /**
068: * Return the tests included in this test suite.
069: */
070: public static Test suite() {
071: return (new TestSuite(TestFilterIterator.class));
072: }
073:
074: /**
075: * Returns an full iterator wrapped in a
076: * FilterIterator that blocks all the elements
077: *
078: * @return "empty" FilterIterator
079: */
080: public Iterator makeEmptyIterator() {
081: return makeBlockAllFilter(new ArrayIterator(array));
082: }
083:
084: /**
085: * Returns an array with elements wrapped in a pass-through
086: * FilterIterator
087: *
088: * @return
089: */
090: public Iterator makeFullIterator() {
091: array = new String[] { "a", "b", "c" };
092: list = new ArrayList(Arrays.asList(array));
093: return makePassThroughFilter(list.iterator());
094: }
095:
096: public void testRepeatedHasNext() {
097: for (int i = 0; i <= array.length; i++) {
098: assertTrue(iterator.hasNext());
099: }
100: }
101:
102: public void testRepeatedNext() {
103: for (int i = 0; i < array.length; i++)
104: iterator.next();
105: verifyNoMoreElements();
106: }
107:
108: public void testReturnValues() {
109: verifyElementsInPredicate(new String[0]);
110: verifyElementsInPredicate(new String[] { "a" });
111: verifyElementsInPredicate(new String[] { "b" });
112: verifyElementsInPredicate(new String[] { "c" });
113: verifyElementsInPredicate(new String[] { "a", "b" });
114: verifyElementsInPredicate(new String[] { "a", "c" });
115: verifyElementsInPredicate(new String[] { "b", "c" });
116: verifyElementsInPredicate(new String[] { "a", "b", "c" });
117: }
118:
119: /**
120: * Test that when the iterator is changed, the hasNext method returns the
121: * correct response for the new iterator.
122: */
123: public void testSetIterator() {
124: Iterator iter1 = Collections.singleton(new Object()).iterator();
125: Iterator iter2 = Collections.EMPTY_LIST.iterator();
126:
127: FilterIterator filterIterator = new FilterIterator(iter1);
128: filterIterator.setPredicate(TruePredicate.getInstance());
129: // this iterator has elements
130: assertEquals(true, filterIterator.hasNext());
131:
132: // this iterator has no elements
133: filterIterator.setIterator(iter2);
134: assertEquals(false, filterIterator.hasNext());
135: }
136:
137: /**
138: * Test that when the predicate is changed, the hasNext method returns the
139: * correct response for the new predicate.
140: */
141: public void testSetPredicate() {
142: Iterator iter = Collections.singleton(null).iterator();
143:
144: FilterIterator filterIterator = new FilterIterator(iter);
145: filterIterator.setPredicate(TruePredicate.getInstance());
146: // this predicate matches
147: assertEquals(true, filterIterator.hasNext());
148:
149: // this predicate doesn't match
150: filterIterator.setPredicate(NotNullPredicate.getInstance());
151: assertEquals(false, filterIterator.hasNext());
152: }
153:
154: private void verifyNoMoreElements() {
155: assertTrue(!iterator.hasNext());
156: try {
157: iterator.next();
158: fail("NoSuchElementException expected");
159: } catch (NoSuchElementException e) {
160: // success
161: }
162: }
163:
164: private void verifyElementsInPredicate(final String[] elements) {
165: Predicate pred = new Predicate() {
166: public boolean evaluate(Object x) {
167: for (int i = 0; i < elements.length; i++)
168: if (elements[i].equals(x))
169: return true;
170: return false;
171: }
172: };
173: initIterator();
174: iterator.setPredicate(pred);
175: for (int i = 0; i < elements.length; i++) {
176: String s = (String) iterator.next();
177: assertEquals(elements[i], s);
178: assertTrue(i == elements.length - 1 ? !iterator.hasNext()
179: : iterator.hasNext());
180: }
181: verifyNoMoreElements();
182:
183: // test removal
184: initIterator();
185: iterator.setPredicate(pred);
186: if (iterator.hasNext()) {
187: Object last = iterator.next();
188: iterator.remove();
189: assertTrue(
190: "Base of FilterIterator still contains removed element.",
191: !list.contains(last));
192: }
193: }
194:
195: private void initIterator() {
196: iterator = (FilterIterator) makeFullIterator();
197: }
198:
199: /**
200: * Returns a FilterIterator that does not filter
201: * any of its elements
202: *
203: * @param i the Iterator to "filter"
204: * @return "filtered" iterator
205: */
206: protected FilterIterator makePassThroughFilter(Iterator i) {
207: Predicate pred = new Predicate() {
208: public boolean evaluate(Object x) {
209: return true;
210: }
211: };
212: return new FilterIterator(i, pred);
213: }
214:
215: /**
216: * Returns a FilterIterator that blocks
217: * all of its elements
218: *
219: * @param i the Iterator to "filter"
220: * @return "filtered" iterator
221: */
222: protected FilterIterator makeBlockAllFilter(Iterator i) {
223: Predicate pred = new Predicate() {
224: public boolean evaluate(Object x) {
225: return false;
226: }
227: };
228: return new FilterIterator(i, pred);
229: }
230: }
|