001: package org.tigris.scarab.util;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.util.ArrayList;
050: import java.util.Collection;
051: import java.util.Iterator;
052: import java.util.NoSuchElementException;
053:
054: /**
055: * used for testing org.tigris.scarab.util.SubsetIterator
056: *
057: * @author <a href="mailto:sebastian.dietrich@anecon.com">Sebastian Dietrich</a>
058: */
059: public class SubsetIteratorWithSizeTest extends SubsetIteratorTest {
060: protected static final String FIRST = "first element";
061: protected static final String SECOND = "second element";
062: protected static final String THIRD = "third element";
063: protected static final String FOURTH = "fourth element";
064: protected static final String LAST = "last element";
065:
066: protected static final int SIZE = 2;
067: protected static final int OFFSET = 2;
068:
069: public SubsetIteratorWithSizeTest(String testName) {
070: super (testName);
071: }
072:
073: /**
074: * A Mock-IteratorWithSize to test SubsetIteratorWithSize with
075: */
076: class MockIteratorWithSize implements IteratorWithSize {
077: private Collection coll;
078: private Iterator it;
079:
080: public MockIteratorWithSize(Collection coll) {
081: this .coll = coll;
082: this .it = coll.iterator();
083: }
084:
085: /* (non-Javadoc)
086: * @see org.tigris.scarab.util.IteratorWithSize#size()
087: */
088: public int size() {
089: return coll.size();
090: }
091:
092: /* (non-Javadoc)
093: * @see java.util.Iterator#remove()
094: */
095: public void remove() {
096: it.remove();
097: }
098:
099: /* (non-Javadoc)
100: * @see java.util.Iterator#hasNext()
101: */
102: public boolean hasNext() {
103: return it.hasNext();
104: }
105:
106: /* (non-Javadoc)
107: * @see java.util.Iterator#next()
108: */
109: public Object next() {
110: return it.next();
111: }
112:
113: }
114:
115: public void setUp() {
116: Collection coll = new ArrayList();
117:
118: coll.add(FIRST);
119: coll.add(SECOND);
120: coll.add(THIRD);
121: coll.add(FOURTH);
122: coll.add(LAST);
123:
124: // a SubsetIteratorWithSize from "third element" to "fourth element"
125: subsetIterator = new SubsetIteratorWithSize(
126: new MockIteratorWithSize(coll), OFFSET, SIZE);
127:
128: // a SubsetIterator from "third element" to the last element
129: subsetIteratorUntilLast = new SubsetIteratorWithSize(
130: new MockIteratorWithSize(coll), OFFSET);
131: }
132:
133: public void tearDown() {
134: subsetIterator = null;
135: }
136:
137: public void testConstructor() {
138: assertEquals("Could not fetch third element", subsetIterator
139: .next(), THIRD);
140: assertEquals("SubsetIterator has wrong size", SIZE,
141: ((SubsetIteratorWithSize) subsetIterator).size());
142:
143: assertTrue("Filled collection should have next element",
144: subsetIterator.hasNext());
145:
146: subsetIterator.remove(); // remove "third element"
147: assertEquals("Could not fetch fourth element", subsetIterator
148: .next(), FOURTH);
149: assertFalse(
150: "SubsetIterator on last position should not have next element",
151: subsetIterator.hasNext());
152: }
153:
154: public void testConstructorWithoutElements() {
155: assertEquals("Could not fetch third element",
156: subsetIteratorUntilLast.next(), THIRD);
157: assertEquals("SubsetIterator has wrong size", 3,
158: ((SubsetIteratorWithSize) subsetIteratorUntilLast)
159: .size());
160:
161: assertTrue("Filled collection should have next element",
162: subsetIteratorUntilLast.hasNext());
163:
164: subsetIteratorUntilLast.remove(); // remove "third element"
165: assertEquals("Could not fetch fourth element",
166: subsetIteratorUntilLast.next(), FOURTH);
167: assertEquals("Could not fetch last element",
168: subsetIteratorUntilLast.next(), LAST);
169: assertFalse(
170: "SubsetIterator on last position should not have next element",
171: subsetIteratorUntilLast.hasNext());
172: }
173:
174: public void testConstructorWithEmptyIteratorWithSize() {
175: SubsetIteratorWithSize i = new SubsetIteratorWithSize(
176: IteratorWithSize.EMPTY, 0, 0);
177:
178: assertFalse("Iterator on empty should not have next element", i
179: .hasNext());
180:
181: try {
182: i.next();
183: fail("Iterator on empty should raise an exception on next()");
184: } catch (NoSuchElementException e) {
185: // that's what we expect
186: } catch (Exception e) {
187: fail("Iterator on empty should raise NoSuchElementException on next() and not "
188: + e.getClass().getName());
189: }
190:
191: try {
192: i.remove();
193: fail("Iterator on empty should raise an exception on remove()");
194: } catch (IllegalStateException e) {
195: // that's what we expect
196: } catch (Exception e) {
197: fail("Iterator on empty should raise IllegalStateException on remove() and not "
198: + e.getClass().getName());
199: }
200: }
201:
202: public void testConstructorWithBiggerSubsetThanTheOriginal() {
203: Collection coll = new ArrayList();
204:
205: coll.add(FIRST);
206: coll.add(SECOND);
207: coll.add(THIRD);
208: coll.add(FOURTH);
209: coll.add(LAST);
210:
211: // a SubsetIteratorWithSize from "third element" to "fourth element"
212: subsetIterator = new SubsetIteratorWithSize(
213: new MockIteratorWithSize(coll), OFFSET, 100);
214: assertEquals("SubsetIterator has wrong size", 3,
215: ((SubsetIteratorWithSize) subsetIterator).size());
216:
217: assertEquals("Could not fetch third element", subsetIterator
218: .next(), THIRD);
219:
220: assertTrue("Filled collection should have next element",
221: subsetIterator.hasNext());
222: assertEquals("Could not fetch fourth element", subsetIterator
223: .next(), FOURTH);
224: assertEquals("Could not fetch last element", subsetIterator
225: .next(), LAST);
226: assertFalse(
227: "SubsetIterator on last position should not have next element",
228: subsetIterator.hasNext());
229: }
230:
231: public void testSize() {
232: assertEquals("size() didn't work", SIZE,
233: ((SubsetIteratorWithSize) subsetIterator).size());
234: }
235:
236: }
|