001: /*
002: * Copyright 2001-2005 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.Iterator;
020: import java.util.List;
021: import java.util.NoSuchElementException;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.collections.IteratorUtils;
027: import org.apache.commons.collections.Predicate;
028:
029: /**
030: * Tests the IteratorChain class.
031: *
032: * @version $Revision: 171347 $ $Date: 2005-05-22 18:27:34 +0100 (Sun, 22 May 2005) $
033: *
034: * @author James Strachan
035: * @author Mauricio S. Moura
036: * @author Morgan Delagrange
037: */
038: public class TestIteratorChain extends AbstractTestIterator {
039:
040: protected String[] testArray = { "One", "Two", "Three", "Four",
041: "Five", "Six" };
042:
043: protected List list1 = null;
044: protected List list2 = null;
045: protected List list3 = null;
046:
047: public static Test suite() {
048: return new TestSuite(TestIteratorChain.class);
049: }
050:
051: public TestIteratorChain(String testName) {
052: super (testName);
053: }
054:
055: public void setUp() {
056: list1 = new ArrayList();
057: list1.add("One");
058: list1.add("Two");
059: list1.add("Three");
060: list2 = new ArrayList();
061: list2.add("Four");
062: list3 = new ArrayList();
063: list3.add("Five");
064: list3.add("Six");
065: }
066:
067: public Iterator makeEmptyIterator() {
068: ArrayList list = new ArrayList();
069: return new IteratorChain(list.iterator());
070: }
071:
072: public Iterator makeFullIterator() {
073: IteratorChain chain = new IteratorChain();
074:
075: chain.addIterator(list1.iterator());
076: chain.addIterator(list2.iterator());
077: chain.addIterator(list3.iterator());
078: return chain;
079: }
080:
081: public void testIterator() {
082: Iterator iter = (Iterator) makeFullIterator();
083: for (int i = 0; i < testArray.length; i++) {
084: Object testValue = testArray[i];
085: Object iterValue = iter.next();
086:
087: assertEquals("Iteration value is correct", testValue,
088: iterValue);
089: }
090:
091: assertTrue("Iterator should now be empty", !iter.hasNext());
092:
093: try {
094: Object testValue = iter.next();
095: } catch (Exception e) {
096: assertTrue("NoSuchElementException must be thrown", e
097: .getClass().equals(
098: (new NoSuchElementException()).getClass()));
099: }
100: }
101:
102: public void testRemoveFromFilteredIterator() {
103:
104: final Predicate myPredicate = new Predicate() {
105: public boolean evaluate(Object object) {
106: Integer i = (Integer) object;
107: if (i.compareTo(new Integer(4)) < 0)
108: return true;
109: return false;
110: }
111: };
112:
113: List list1 = new ArrayList();
114: List list2 = new ArrayList();
115:
116: list1.add(new Integer(1));
117: list1.add(new Integer(2));
118: list2.add(new Integer(3));
119: list2.add(new Integer(4)); // will be ignored by the predicate
120:
121: Iterator it1 = IteratorUtils.filteredIterator(list1.iterator(),
122: myPredicate);
123: Iterator it2 = IteratorUtils.filteredIterator(list2.iterator(),
124: myPredicate);
125:
126: Iterator it = IteratorUtils.chainedIterator(it1, it2);
127: while (it.hasNext()) {
128: it.next();
129: it.remove();
130: }
131: assertEquals(0, list1.size());
132: assertEquals(1, list2.size());
133:
134: }
135:
136: public void testRemove() {
137: Iterator iter = (Iterator) makeFullIterator();
138:
139: try {
140: iter.remove();
141: fail("Calling remove before the first call to next() should throw an exception");
142: } catch (IllegalStateException e) {
143:
144: }
145:
146: for (int i = 0; i < testArray.length; i++) {
147: Object testValue = testArray[i];
148: Object iterValue = iter.next();
149:
150: assertEquals("Iteration value is correct", testValue,
151: iterValue);
152:
153: if (!iterValue.equals("Four")) {
154: iter.remove();
155: }
156: }
157:
158: assertTrue("List is empty", list1.size() == 0);
159: assertTrue("List is empty", list2.size() == 1);
160: assertTrue("List is empty", list3.size() == 0);
161: }
162:
163: public void testFirstIteratorIsEmptyBug() {
164: List empty = new ArrayList();
165: List notEmpty = new ArrayList();
166: notEmpty.add("A");
167: notEmpty.add("B");
168: notEmpty.add("C");
169: IteratorChain chain = new IteratorChain();
170: chain.addIterator(empty.iterator());
171: chain.addIterator(notEmpty.iterator());
172: assertTrue("should have next", chain.hasNext());
173: assertEquals("A", chain.next());
174: assertTrue("should have next", chain.hasNext());
175: assertEquals("B", chain.next());
176: assertTrue("should have next", chain.hasNext());
177: assertEquals("C", chain.next());
178: assertTrue("should not have next", !chain.hasNext());
179: }
180:
181: public void testEmptyChain() {
182: IteratorChain chain = new IteratorChain();
183: assertEquals(false, chain.hasNext());
184: try {
185: chain.next();
186: fail();
187: } catch (NoSuchElementException ex) {
188: }
189: try {
190: chain.remove();
191: fail();
192: } catch (IllegalStateException ex) {
193: }
194: }
195:
196: }
|