KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > SubsetIteratorWithSizeTest


1 package org.tigris.scarab.util;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.util.ArrayList JavaDoc;
50 import java.util.Collection JavaDoc;
51 import java.util.Iterator JavaDoc;
52 import java.util.NoSuchElementException JavaDoc;
53
54 /**
55  * used for testing org.tigris.scarab.util.SubsetIterator
56  *
57  * @author <a HREF="mailto:sebastian.dietrich@anecon.com">Sebastian Dietrich</a>
58  */

59 public class SubsetIteratorWithSizeTest extends SubsetIteratorTest
60 {
61     protected static final String JavaDoc FIRST = "first element";
62     protected static final String JavaDoc SECOND = "second element";
63     protected static final String JavaDoc THIRD = "third element";
64     protected static final String JavaDoc FOURTH = "fourth element";
65     protected static final String JavaDoc LAST = "last element";
66
67     protected static final int SIZE = 2;
68     protected static final int OFFSET = 2;
69
70
71     public SubsetIteratorWithSizeTest(String JavaDoc testName)
72     {
73         super(testName);
74     }
75     /**
76      * A Mock-IteratorWithSize to test SubsetIteratorWithSize with
77      */

78     class MockIteratorWithSize implements IteratorWithSize
79     {
80         private Collection JavaDoc coll;
81         private Iterator JavaDoc it;
82
83         public MockIteratorWithSize(Collection JavaDoc coll)
84         {
85             this.coll = coll;
86             this.it = coll.iterator();
87         }
88
89         /* (non-Javadoc)
90          * @see org.tigris.scarab.util.IteratorWithSize#size()
91          */

92         public int size()
93         {
94             return coll.size();
95         }
96
97         /* (non-Javadoc)
98         * @see java.util.Iterator#remove()
99         */

100         public void remove()
101         {
102             it.remove();
103         }
104
105         /* (non-Javadoc)
106          * @see java.util.Iterator#hasNext()
107          */

108         public boolean hasNext()
109         {
110             return it.hasNext();
111         }
112
113         /* (non-Javadoc)
114          * @see java.util.Iterator#next()
115          */

116         public Object JavaDoc next()
117         {
118             return it.next();
119         }
120
121     }
122
123     public void setUp()
124     {
125         Collection JavaDoc coll = new ArrayList JavaDoc();
126
127         coll.add(FIRST);
128         coll.add(SECOND);
129         coll.add(THIRD);
130         coll.add(FOURTH);
131         coll.add(LAST);
132
133         // a SubsetIteratorWithSize from "third element" to "fourth element"
134
subsetIterator =
135             new SubsetIteratorWithSize(
136                 new MockIteratorWithSize(coll),
137                 OFFSET,
138                 SIZE);
139
140         // a SubsetIterator from "third element" to the last element
141
subsetIteratorUntilLast =
142             new SubsetIteratorWithSize(new MockIteratorWithSize(coll), OFFSET);
143     }
144
145     public void tearDown()
146     {
147         subsetIterator = null;
148     }
149
150     public void testConstructor()
151     {
152         assertEquals(
153             "Could not fetch third element",
154             subsetIterator.next(),
155             THIRD);
156         assertEquals(
157             "SubsetIterator has wrong size",
158             SIZE,
159             ((SubsetIteratorWithSize) subsetIterator).size());
160
161         assertTrue(
162             "Filled collection should have next element",
163             subsetIterator.hasNext());
164
165         subsetIterator.remove(); // remove "third element"
166
assertEquals(
167             "Could not fetch fourth element",
168             subsetIterator.next(),
169             FOURTH);
170         assertFalse(
171             "SubsetIterator on last position should not have next element",
172             subsetIterator.hasNext());
173     }
174
175     public void testConstructorWithoutElements()
176     {
177         assertEquals(
178             "Could not fetch third element",
179             subsetIteratorUntilLast.next(),
180             THIRD);
181         assertEquals(
182             "SubsetIterator has wrong size",
183             3,
184             ((SubsetIteratorWithSize) subsetIteratorUntilLast).size());
185
186         assertTrue(
187             "Filled collection should have next element",
188             subsetIteratorUntilLast.hasNext());
189
190         subsetIteratorUntilLast.remove(); // remove "third element"
191
assertEquals(
192             "Could not fetch fourth element",
193             subsetIteratorUntilLast.next(),
194             FOURTH);
195         assertEquals(
196             "Could not fetch last element",
197             subsetIteratorUntilLast.next(),
198             LAST);
199         assertFalse(
200             "SubsetIterator on last position should not have next element",
201             subsetIteratorUntilLast.hasNext());
202     }
203
204     public void testConstructorWithEmptyIteratorWithSize()
205     {
206         SubsetIteratorWithSize i =
207             new SubsetIteratorWithSize(IteratorWithSize.EMPTY, 0, 0);
208
209         assertFalse(
210             "Iterator on empty should not have next element",
211             i.hasNext());
212
213         try
214         {
215             i.next();
216             fail("Iterator on empty should raise an exception on next()");
217         }
218         catch (NoSuchElementException JavaDoc e)
219         {
220             // that's what we expect
221
}
222         catch (Exception JavaDoc e)
223         {
224             fail(
225                 "Iterator on empty should raise NoSuchElementException on next() and not "
226                     + e.getClass().getName());
227         }
228
229         try
230         {
231             i.remove();
232             fail("Iterator on empty should raise an exception on remove()");
233         }
234         catch (IllegalStateException JavaDoc e)
235         {
236             // that's what we expect
237
}
238         catch (Exception JavaDoc e)
239         {
240             fail(
241                 "Iterator on empty should raise IllegalStateException on remove() and not "
242                     + e.getClass().getName());
243         }
244     }
245
246     public void testConstructorWithBiggerSubsetThanTheOriginal()
247     {
248         Collection JavaDoc coll = new ArrayList JavaDoc();
249
250         coll.add(FIRST);
251         coll.add(SECOND);
252         coll.add(THIRD);
253         coll.add(FOURTH);
254         coll.add(LAST);
255
256         // a SubsetIteratorWithSize from "third element" to "fourth element"
257
subsetIterator =
258             new SubsetIteratorWithSize(
259                 new MockIteratorWithSize(coll),
260                 OFFSET,
261                 100);
262         assertEquals(
263             "SubsetIterator has wrong size",
264             3,
265             ((SubsetIteratorWithSize) subsetIterator).size());
266
267         assertEquals(
268             "Could not fetch third element",
269             subsetIterator.next(),
270             THIRD);
271
272         assertTrue(
273             "Filled collection should have next element",
274             subsetIterator.hasNext());
275         assertEquals(
276             "Could not fetch fourth element",
277             subsetIterator.next(),
278             FOURTH);
279         assertEquals(
280             "Could not fetch last element",
281             subsetIterator.next(),
282             LAST);
283         assertFalse(
284             "SubsetIterator on last position should not have next element",
285             subsetIterator.hasNext());
286     }
287
288     public void testSize()
289     {
290         assertEquals(
291             "size() didn't work",
292             SIZE,
293             ((SubsetIteratorWithSize) subsetIterator).size());
294     }
295
296 }
297
Popular Tags