1 package org.tigris.scarab.util; 2 3 48 49 import junit.framework.TestCase; 50 51 import java.util.ArrayList ; 52 import java.util.Collection ; 53 import java.util.NoSuchElementException ; 54 55 60 public class SubsetIteratorTest extends TestCase 61 { 62 protected SubsetIterator subsetIterator; 63 protected SubsetIterator subsetIteratorUntilLast; 64 65 protected static final String FIRST = "first element"; 66 protected static final String SECOND = "second element"; 67 protected static final String THIRD = "third element"; 68 protected static final String FOURTH = "fourth element"; 69 protected static final String LAST = "last element"; 70 71 public SubsetIteratorTest(String testName) 72 { 73 super(testName); 74 } 75 76 public void setUp() 77 { 78 Collection coll = new ArrayList (); 79 80 coll.add(FIRST); 81 coll.add(SECOND); 82 coll.add(THIRD); 83 coll.add(FOURTH); 84 coll.add(LAST); 85 86 subsetIterator = new SubsetIterator(coll.iterator(), 2, 2); 88 89 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(); 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(); 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 coll = new ArrayList (); 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 e) { 132 } catch (Exception 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 e) { 141 } catch (Exception 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 coll = new ArrayList (); 150 151 coll.add(FIRST); 152 coll.add(SECOND); 153 coll.add(THIRD); 154 coll.add(FOURTH); 155 coll.add(LAST); 156 157 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 e) { 186 } catch (Exception 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 e) { 200 } catch (Exception 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 |