KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.TestCase;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.Collection JavaDoc;
53 import java.util.NoSuchElementException JavaDoc;
54
55 /**
56  * used for testing org.tigris.scarab.util.SubsetIterator
57  *
58  * @author <a HREF="mailto:sebastian.dietrich@anecon.com">Sebastian Dietrich</a>
59  */

60 public class SubsetIteratorTest extends TestCase
61 {
62  protected SubsetIterator subsetIterator;
63  protected SubsetIterator subsetIteratorUntilLast;
64  
65  protected static final String JavaDoc FIRST = "first element";
66  protected static final String JavaDoc SECOND = "second element";
67  protected static final String JavaDoc THIRD = "third element";
68  protected static final String JavaDoc FOURTH = "fourth element";
69  protected static final String JavaDoc LAST = "last element";
70  
71  public SubsetIteratorTest(String JavaDoc testName)
72  {
73      super(testName);
74  }
75
76  public void setUp()
77  {
78    Collection JavaDoc coll = new ArrayList JavaDoc();
79    
80    coll.add(FIRST);
81    coll.add(SECOND);
82    coll.add(THIRD);
83    coll.add(FOURTH);
84    coll.add(LAST);
85    
86    // a SubsetIterator from "third element" to "fourth element"
87
subsetIterator = new SubsetIterator(coll.iterator(), 2, 2);
88    
89    // a SubsetIterator from "third element" to the last element
90
subsetIteratorUntilLast = new SubsetIterator(coll.iterator(), 2);
91  }
92  
93  public void tearDown()
94  {
95    subsetIterator = null;
96  }
97
98  public void testConstructor()
99  {
100    assertEquals("Could not fetch third element", subsetIterator.next(), THIRD);
101    
102    assertTrue("Filled collection should have next element", subsetIterator.hasNext());
103    
104    subsetIterator.remove(); // remove "third element"
105
assertEquals("Could not fetch fourth element", subsetIterator.next(), FOURTH);
106    assertFalse("SubsetIterator on last position should not have next element", subsetIterator.hasNext());
107  }
108
109  public void testConstructorWithoutElements()
110  {
111    assertEquals("Could not fetch third element", subsetIteratorUntilLast.next(), THIRD);
112    
113    assertTrue("Filled collection should have next element", subsetIteratorUntilLast.hasNext());
114    
115    subsetIteratorUntilLast.remove(); // remove "third element"
116
assertEquals("Could not fetch fourth element", subsetIteratorUntilLast.next(), FOURTH);
117    assertEquals("Could not fetch last element", subsetIteratorUntilLast.next(), LAST);
118    assertFalse("SubsetIterator on last position should not have next element", subsetIteratorUntilLast.hasNext());
119  }
120  
121  public void testConstructorWithEmptyCollection()
122  {
123    Collection JavaDoc coll = new ArrayList JavaDoc();
124    SubsetIterator i = new SubsetIterator(coll.iterator(), 0, 0);
125    
126    assertFalse("Empty collection should not have next element", i.hasNext());
127    
128    try {
129      i.next();
130      fail("Empty collection should raise an exception on next()");
131    } catch (NoSuchElementException JavaDoc e) {
132      // that's what we expect
133
} catch (Exception JavaDoc e) {
134      fail("Empty collection should raise NoSuchElementException on next() and not " + e.getClass().getName());
135    }
136    
137    try {
138      i.remove();
139      fail("Empty collection should raise an exception on remove()");
140    } catch (IllegalStateException JavaDoc e) {
141      // that's what we expect
142
} catch (Exception JavaDoc e) {
143      fail("Empty collection should raise IllegalStateException on remove() and not " + e.getClass().getName());
144    }
145  }
146  
147  public void testConstructorWithBiggerSubsetThanTheOriginal()
148  {
149    Collection JavaDoc coll = new ArrayList JavaDoc();
150    
151    coll.add(FIRST);
152    coll.add(SECOND);
153    coll.add(THIRD);
154    coll.add(FOURTH);
155    coll.add(LAST);
156    
157    // a SubsetIterator from "third element" to the end
158
subsetIterator = new SubsetIterator(coll.iterator(), 2, 100);
159    
160    assertEquals("Could not fetch third element", subsetIterator.next(), THIRD);
161    
162    assertTrue("Filled collection should have next element", subsetIterator.hasNext());
163    assertEquals("Could not fetch fourth element", subsetIterator.next(), FOURTH);
164    assertEquals("Could not fetch last element", subsetIterator.next(), LAST);
165    assertFalse("SubsetIterator on last position should not have next element", subsetIterator.hasNext());
166  }
167  
168  public void testHasNext()
169  {
170    assertTrue("Before first element hasNext() should be true", subsetIterator.hasNext());
171    subsetIterator.next();
172    assertTrue("On first element hasNext() should be true", subsetIterator.hasNext());
173    subsetIterator.next();
174    assertFalse("On last element hasNext() should be false", subsetIterator.hasNext());
175  }
176
177  public void testNext()
178  {
179    assertEquals("Could not fetch third element", subsetIterator.next(), THIRD);
180    assertEquals("Could not fetch fourth element", subsetIterator.next(), FOURTH);
181    try
182    {
183      subsetIterator.next();
184      fail("next() on last element should raise an exception");
185    } catch (NoSuchElementException JavaDoc e) {
186      // that's what we expect
187
} catch (Exception JavaDoc e) {
188      fail("next() on last element should raise NoSuchElementException and not " +
189          e.getClass().getName());
190    }
191  }
192
193  public void testRemove()
194  {
195    try
196    {
197      subsetIterator.remove();
198      fail("remove() before first element should raise an exception");
199    } catch (IllegalStateException JavaDoc e) {
200      // that's what we expect
201
} catch (Exception JavaDoc e) {
202      fail("remove() before first element should raise IllegalStateException and not " +
203          e.getClass().getName());
204    }
205    
206    subsetIterator.next();
207    subsetIterator.remove();
208    assertTrue("Before first element hasNext() should be true", subsetIterator.hasNext());
209    subsetIterator.next();
210    assertFalse("On last element hasNext() should be false", subsetIterator.hasNext());
211    subsetIterator.remove();
212  }
213
214 }
215
Popular Tags