01: /*
02: * Copyright 2001-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.collections.iterators;
17:
18: import java.util.ArrayList;
19: import java.util.Iterator;
20: import java.util.List;
21: import java.util.NoSuchElementException;
22:
23: import junit.framework.Test;
24: import junit.framework.TestSuite;
25:
26: /**
27: * Tests the UniqueFilterIterator class.
28: *
29: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
30: *
31: * @author James Strachan
32: * @author Mauricio S. Moura
33: * @author Morgan Delagrange
34: * @author Stephen Colebourne
35: */
36: public class TestUniqueFilterIterator extends AbstractTestIterator {
37:
38: protected String[] testArray = { "One", "Two", "Three", "Four",
39: "Five", "Six" };
40:
41: protected List list1 = null;
42:
43: public static Test suite() {
44: return new TestSuite(TestUniqueFilterIterator.class);
45: }
46:
47: public TestUniqueFilterIterator(String testName) {
48: super (testName);
49: }
50:
51: public void setUp() {
52: list1 = new ArrayList();
53: list1.add("One");
54: list1.add("Two");
55: list1.add("Three");
56: list1.add("Two");
57: list1.add("One");
58: list1.add("Four");
59: list1.add("Five");
60: list1.add("Five");
61: list1.add("Six");
62: list1.add("Five");
63: }
64:
65: public Iterator makeEmptyIterator() {
66: ArrayList list = new ArrayList();
67: return new UniqueFilterIterator(list.iterator());
68: }
69:
70: public Iterator makeFullIterator() {
71: Iterator i = list1.iterator();
72:
73: return new UniqueFilterIterator(i);
74: }
75:
76: public void testIterator() {
77: Iterator iter = (Iterator) makeFullIterator();
78: for (int i = 0; i < testArray.length; i++) {
79: Object testValue = testArray[i];
80: Object iterValue = iter.next();
81:
82: assertEquals("Iteration value is correct", testValue,
83: iterValue);
84: }
85:
86: assertTrue("Iterator should now be empty", !iter.hasNext());
87:
88: try {
89: Object testValue = iter.next();
90: } catch (Exception e) {
91: assertTrue("NoSuchElementException must be thrown", e
92: .getClass().equals(
93: (new NoSuchElementException()).getClass()));
94: }
95: }
96:
97: }
|