1 33 34 package edu.rice.cs.drjava.model.definitions.reducedmodel; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 38 42 public final class ModelListTest extends DrJavaTestCase { 43 protected ModelList<Integer > fEmpty; 44 protected ModelList<Integer > fFull; 45 46 protected void setUp() throws Exception { 47 super.setUp(); 48 fFull = new ModelList<Integer >(); 49 fEmpty = new ModelList<Integer >(); 50 } 51 52 public void testInsert() { 53 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 54 ModelList<Integer >.Iterator itEmpty = fEmpty.getIterator(); 55 assertTrue("#0.0", fEmpty.isEmpty()); 56 assertTrue("#0.1", fFull.isEmpty()); 57 assertEquals("#0.2", 0, fEmpty.length()); 58 assertEquals("#0.3", 0, fFull.length()); 59 assertTrue("#0.4", itEmpty.atStart()); 60 assertTrue("#0.5", itFull.atStart()); 61 itFull.insert(new Integer (5)); 62 assertTrue("#1.0", !itFull.atStart()); 63 assertEquals("#1.1", 1, fFull.length()); 64 assertEquals("#1.2", new Integer (5), itFull.current()); 65 assertTrue("#2.0", fEmpty.isEmpty()); 66 assertTrue("#2.1", !fFull.isEmpty()); 67 itFull.insert(new Integer (4)); 68 assertEquals("#2.2", 2, fFull.length()); 69 assertEquals("#2.3", new Integer (4), itFull.current()); 70 assertTrue("#2.4", !fFull.isEmpty()); 71 } 72 73 public void testInsertFront() { 74 fFull.insertFront(new Integer (3)); 75 fFull.insertFront(new Integer (2)); 76 fFull.insertFront(new Integer (1)); 77 fFull.insertFront(new Integer (0)); 78 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 79 for (int i = 0; i < 4; i++) { 80 itFull.next(); 81 assertEquals(new Integer (i), itFull.current()); 82 } 83 } 84 85 public void testRemove() { 86 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 87 ModelList<Integer >.Iterator itEmpty = fEmpty.getIterator(); 88 assertTrue("#0.0", fEmpty.isEmpty()); 89 assertEquals("#0.1", 0, fEmpty.length()); 90 assertEquals("#0.2", 0, fFull.length()); 91 try { 92 itEmpty.remove(); 93 assertTrue("#1.0", false); 94 } 95 catch (Exception e) { 96 } 97 98 itFull.insert(new Integer (5)); 99 assertTrue("#2.0", !fFull.isEmpty()); 100 assertEquals("#2.1", 1, fFull.length()); 101 itFull.remove(); 102 assertTrue("#3.0", fFull.isEmpty()); 103 assertEquals("#3.1", 0, fFull.length()); 104 } 105 106 public void testNext() { 107 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 108 ModelList<Integer >.Iterator itEmpty = fEmpty.getIterator(); 109 110 try { 112 itEmpty.next(); 113 itEmpty.next(); 114 assertTrue("#0.0", false); 115 } 116 catch (Exception e) { 117 } 118 119 itFull.insert(new Integer (6)); 120 itFull.insert(new Integer (5)); 121 itFull.insert(new Integer (4)); 122 assertEquals("#1.0", new Integer (4), itFull.current()); 124 itFull.next(); 125 assertEquals("#1.1", new Integer (5), itFull.current()); 126 itFull.next(); 127 assertEquals("#1.2", new Integer (6), itFull.current()); 128 itFull.next(); 129 try { 130 itFull.next(); 131 assertTrue("#1.4", false); 132 } 133 catch (Exception f) { 134 } 135 } 136 137 public void testPrev() { 138 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 139 ModelList<Integer >.Iterator itEmpty = fEmpty.getIterator(); 140 141 try { 142 itEmpty.prev(); 143 assertTrue("#0.0", false); 144 } 145 catch (Exception e) { 146 } 147 148 itFull.insert(new Integer (6)); 149 itFull.insert(new Integer (5)); 150 itFull.insert(new Integer (4)); 151 itFull.next(); 152 itFull.next(); 153 itFull.next(); 154 155 itFull.prev(); 156 assertEquals("#1.1", new Integer (6), itFull.current()); 157 itFull.prev(); 158 assertEquals("#1.2", new Integer (5), itFull.current()); 159 itFull.prev(); 160 assertEquals("#1.3", new Integer (4), itFull.current()); 161 itFull.prev(); 162 163 try { 164 itFull.prev(); 165 assertTrue("#1.5", false); 166 } 167 catch (Exception f) { 168 } 169 } 170 171 public void testCurrent() { 172 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 173 try { 174 itFull.current(); 175 fail("Current call in initial position did not fail."); 176 } 177 catch (RuntimeException e) { 178 assertEquals("current() throws exception when at end", 180 e.getMessage(), 181 "Attempt to call current on an iterator in the initial position"); 182 } 183 itFull.next(); 184 try { 185 itFull.current(); 186 fail("Current call in final position did not fail."); 187 } 188 catch (RuntimeException e) { 189 assertEquals("current() throws exception when at end", 191 e.getMessage(), 192 "Attempt to call current on an iterator in the final position"); 193 } 194 } 195 196 public void testPrevItem() { 197 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 198 try { 199 itFull.prevItem(); 200 assertTrue(false); 201 } 202 catch (Exception e) { 203 } 204 205 itFull.insert(new Integer (0)); 206 try { 207 itFull.prevItem(); 208 assertTrue(false); 209 } 210 catch (Exception e) { 211 } 212 itFull.insert(new Integer (1)); 213 itFull.next(); 214 assertEquals("#0.0", new Integer (1), itFull.prevItem()); 215 } 216 217 public void testNextItem() { 218 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 219 try { 220 itFull.nextItem(); 221 assertTrue(false); 222 } 223 catch (Exception e) { 224 } 225 226 itFull.insert(new Integer (0)); 227 try { 228 itFull.nextItem(); 229 assertTrue(false); 230 } 231 catch (Exception e) { 232 } 233 itFull.insert(new Integer (1)); 234 assertEquals("#0.0", new Integer (0), itFull.nextItem()); 235 } 236 237 public void testCollapse() { 238 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 239 ModelList<Integer >.Iterator itEmpty = fEmpty.getIterator(); 240 ModelList<Integer >.Iterator itEmpty2 = itEmpty.copy(); 241 assertEquals("#0.0", 0, fEmpty.length()); 242 itEmpty.collapse(itEmpty2); 243 assertEquals("#0.0", 0, fEmpty.length()); 244 245 itFull.insert(new Integer (6)); 246 ModelList<Integer >.Iterator itFull2 = itFull.copy(); 247 assertEquals("#1.0", 1, fFull.length()); 248 itFull.collapse(itFull2); 249 assertEquals("#1.1", 1, fFull.length()); 250 251 itFull.insert(new Integer (5)); 252 assertEquals("#2.0", 2, fFull.length()); 253 itFull.collapse(itFull2); 254 assertEquals("#2.1", 2, fFull.length()); 255 256 itFull.insert(new Integer (4)); 258 assertEquals("#3.0", 3, fFull.length()); 259 assertEquals("#3.0b",new Integer (4),itFull.current()); 260 assertEquals("#3.0a", new Integer (6), itFull2.current()); 261 itFull.collapse(itFull2); 262 itFull.next(); 263 assertEquals("#3.0c",new Integer (6),itFull.current()); 264 assertEquals("#3.1", 2, fFull.length()); 265 itFull.prev(); 266 assertEquals("#4.0", new Integer (4), itFull.current()); 267 assertEquals("#4.1", new Integer (6), itFull2.current()); 268 269 itFull.insert(new Integer (7)); 271 assertEquals("#5.0a", 3, fFull.length()); 272 assertEquals("#5.0b", new Integer (7), itFull.current()); 273 assertEquals("#5.0c", new Integer (6), itFull2.current()); 274 itFull2.collapse(itFull); 275 assertEquals("#5.1a", 2, fFull.length()); 276 assertEquals("#5.1b", new Integer (7), itFull.current()); 277 assertEquals("#5.1c", new Integer (6), itFull2.current()); 278 assertEquals("#5.2a", new Integer (6), itFull.nextItem()); 279 assertEquals("#5.2b", new Integer (7), itFull2.prevItem()); 280 } 281 282 public void testNotifyInsert() { 283 ModelList<Integer >.Iterator itFull2 = fFull.getIterator(); 284 285 itFull2.insert(new Integer (0)); 286 ModelList<Integer >.Iterator itFull = itFull2.copy(); 287 itFull2.insert(new Integer (1)); 288 assertEquals(new Integer (0), itFull.current()); 289 } 290 291 public void testNotifyRemove() { 292 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 293 ModelList<Integer >.Iterator itFull2 = fFull.getIterator(); 294 295 itFull2.insert(new Integer (0)); 296 itFull2.insert(new Integer (1)); 297 itFull.next(); 298 itFull2.remove(); 299 assertEquals("#0.0", new Integer (0), itFull.current()); 300 } 301 302 public void testNotifyCollapse() { 303 ModelList<Integer >.Iterator itFull = fFull.getIterator(); 304 ModelList<Integer >.Iterator itFull2 = fFull.getIterator(); 305 ModelList<Integer >.Iterator itFull3 = fFull.getIterator(); 306 307 itFull2.insert(new Integer (0)); 308 itFull2.insert(new Integer (1)); 309 itFull2.insert(new Integer (2)); 310 itFull2.insert(new Integer (3)); 311 itFull2.insert(new Integer (4)); 312 313 assertTrue("#0.0.0",itFull.atStart()); 314 318 for (int i = 0; i < 3; i++) { 319 itFull.next(); 320 } 321 for (int j = 0; j < 5; j++) { 322 itFull3.next(); 323 } 324 assertEquals("#0.0", new Integer (2), itFull.current()); 325 assertEquals("#0.1", new Integer (0), itFull3.current()); 326 itFull2.collapse(itFull3); 327 328 assertEquals("#1.0", new Integer (4), itFull2.current()); 329 assertEquals("#1.1", new Integer (0), itFull3.current()); 330 assertEquals("#1.2", new Integer (0), itFull.current()); 331 } 332 333 public void testListenerCount() { 334 ModelList<Character > testList = new ModelList<Character >(); 335 336 assertEquals("No iterators", 0, testList.listenerCount()); 337 338 ModelList<Character >.Iterator iter1 = testList.getIterator(); 339 340 assertEquals("One iterator", 1, testList.listenerCount()); 341 342 ModelList<Character >.Iterator iter2 = testList.getIterator(); 343 344 assertEquals("Two iterators", 2, testList.listenerCount()); 345 346 iter1.dispose(); 347 iter1 = null; 348 349 assertEquals("Removed first iterator", 1, testList.listenerCount()); 350 351 iter2.dispose(); 352 iter2 = null; 353 354 assertEquals("Removed second iterator", 0, testList.listenerCount()); 355 } 356 } 357 358 359 360 361 362 363 364 365 | Popular Tags |