001: /*
002: * Copyright 2001-2004 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.List;
021: import java.util.NoSuchElementException;
022:
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026:
027: /**
028: * Tests the LoopingIterator class.
029: *
030: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
031: *
032: * @author Jonathan Carlson
033: * @author Stephen Colebourne
034: */
035: public class TestLoopingIterator extends TestCase {
036:
037: public TestLoopingIterator(String testName) {
038: super (testName);
039: }
040:
041: public static Test suite() {
042: return new TestSuite(TestLoopingIterator.class);
043: }
044:
045: /**
046: * Tests constructor exception.
047: */
048: public void testConstructorEx() throws Exception {
049: try {
050: new LoopingIterator(null);
051: fail();
052: } catch (NullPointerException ex) {
053: }
054: }
055:
056: /**
057: * Tests whether an empty looping iterator works as designed.
058: * @throws Exception If something unexpected occurs.
059: */
060: public void testLooping0() throws Exception {
061: List list = new ArrayList();
062: LoopingIterator loop = new LoopingIterator(list);
063: assertTrue("hasNext should return false",
064: loop.hasNext() == false);
065:
066: try {
067: loop.next();
068: fail("NoSuchElementException was not thrown during next() call.");
069: } catch (NoSuchElementException ex) {
070: }
071: }
072:
073: /**
074: * Tests whether a populated looping iterator works as designed.
075: * @throws Exception If something unexpected occurs.
076: */
077: public void testLooping1() throws Exception {
078: List list = new ArrayList(Arrays.asList(new String[] { "a" }));
079: LoopingIterator loop = new LoopingIterator(list);
080:
081: assertTrue("1st hasNext should return true", loop.hasNext());
082: assertEquals("a", loop.next());
083:
084: assertTrue("2nd hasNext should return true", loop.hasNext());
085: assertEquals("a", loop.next());
086:
087: assertTrue("3rd hasNext should return true", loop.hasNext());
088: assertEquals("a", loop.next());
089:
090: }
091:
092: /**
093: * Tests whether a populated looping iterator works as designed.
094: * @throws Exception If something unexpected occurs.
095: */
096: public void testLooping2() throws Exception {
097: List list = new ArrayList(Arrays
098: .asList(new String[] { "a", "b" }));
099: LoopingIterator loop = new LoopingIterator(list);
100:
101: assertTrue("1st hasNext should return true", loop.hasNext());
102: assertEquals("a", loop.next());
103:
104: assertTrue("2nd hasNext should return true", loop.hasNext());
105: assertEquals("b", loop.next());
106:
107: assertTrue("3rd hasNext should return true", loop.hasNext());
108: assertEquals("a", loop.next());
109:
110: }
111:
112: /**
113: * Tests whether a populated looping iterator works as designed.
114: * @throws Exception If something unexpected occurs.
115: */
116: public void testLooping3() throws Exception {
117: List list = new ArrayList(Arrays.asList(new String[] { "a",
118: "b", "c" }));
119: LoopingIterator loop = new LoopingIterator(list);
120:
121: assertTrue("1st hasNext should return true", loop.hasNext());
122: assertEquals("a", loop.next());
123:
124: assertTrue("2nd hasNext should return true", loop.hasNext());
125: assertEquals("b", loop.next());
126:
127: assertTrue("3rd hasNext should return true", loop.hasNext());
128: assertEquals("c", loop.next());
129:
130: assertTrue("4th hasNext should return true", loop.hasNext());
131: assertEquals("a", loop.next());
132:
133: }
134:
135: /**
136: * Tests the remove() method on a LoopingIterator wrapped ArrayList.
137: * @throws Exception If something unexpected occurs.
138: */
139: public void testRemoving1() throws Exception {
140: List list = new ArrayList(Arrays.asList(new String[] { "a",
141: "b", "c" }));
142: LoopingIterator loop = new LoopingIterator(list);
143: assertEquals("list should have 3 elements.", 3, list.size());
144:
145: assertTrue("1st hasNext should return true", loop.hasNext());
146: assertEquals("a", loop.next());
147: loop.remove(); // removes a
148: assertEquals("list should have 2 elements.", 2, list.size());
149:
150: assertTrue("2nd hasNext should return true", loop.hasNext());
151: assertEquals("b", loop.next());
152: loop.remove(); // removes b
153: assertEquals("list should have 1 elements.", 1, list.size());
154:
155: assertTrue("3rd hasNext should return true", loop.hasNext());
156: assertEquals("c", loop.next());
157: loop.remove(); // removes c
158: assertEquals("list should have 0 elements.", 0, list.size());
159:
160: assertTrue("4th hasNext should return false",
161: loop.hasNext() == false);
162: try {
163: loop.next();
164: fail("Expected NoSuchElementException to be thrown.");
165: } catch (NoSuchElementException ex) {
166: }
167: }
168:
169: /**
170: * Tests the reset() method on a LoopingIterator wrapped ArrayList.
171: * @throws Exception If something unexpected occurs.
172: */
173: public void testReset() throws Exception {
174: List list = new ArrayList(Arrays.asList(new String[] { "a",
175: "b", "c" }));
176: LoopingIterator loop = new LoopingIterator(list);
177:
178: assertEquals("a", loop.next());
179: assertEquals("b", loop.next());
180: loop.reset();
181: assertEquals("a", loop.next());
182: loop.reset();
183: assertEquals("a", loop.next());
184: assertEquals("b", loop.next());
185: assertEquals("c", loop.next());
186: loop.reset();
187: assertEquals("a", loop.next());
188: assertEquals("b", loop.next());
189: assertEquals("c", loop.next());
190: }
191:
192: /**
193: * Tests the size() method on a LoopingIterator wrapped ArrayList.
194: * @throws Exception If something unexpected occurs.
195: */
196: public void testSize() throws Exception {
197: List list = new ArrayList(Arrays.asList(new String[] { "a",
198: "b", "c" }));
199: LoopingIterator loop = new LoopingIterator(list);
200:
201: assertEquals(3, loop.size());
202: loop.next();
203: loop.next();
204: assertEquals(3, loop.size());
205: loop.reset();
206: assertEquals(3, loop.size());
207: loop.next();
208: loop.remove();
209: assertEquals(2, loop.size());
210: }
211:
212: }
|