1 20 package org.enhydra.barracuda.plankton.data; 21 22 import java.io.*; 23 import java.util.*; 24 25 import junit.framework.*; 26 27 import org.enhydra.barracuda.plankton.data.*; 28 import org.apache.log4j.*; 29 import org.enhydra.barracuda.testbed.*; 30 31 32 37 public abstract class PListTestCases extends PDataTestCases { 38 39 43 public PListTestCases(String name) { 44 super(name); 45 } 46 47 53 56 public abstract void testClone(); 57 58 61 public void testEquals() { 62 if (logger.isInfoEnabled()) logger.info("testing equals()"); 63 PList plist1 = null; 64 PList plist2 = null; 65 66 plist1 = this.getPListInstance(); 68 plist2 = this.getPListInstance(); 69 assertEquals("Empty lists equality check failed", plist1, plist2); 70 71 plist1 = this.getPListInstance(); 73 plist2 = this.getPListInstance(); 74 String foo1 = "foo1"; 75 String foo2 = "foo2"; 76 String foo3 = "foo3"; 77 plist1 = this.getPListInstance(); 78 plist1.add(foo1); 79 plist1.add(foo2); 80 plist1.add(foo3); 81 plist2 = this.getPListInstance(); 82 plist2.add(foo1); 83 plist2.add(foo2); 84 plist2.add(foo3); 85 assertEquals("Non-empty lists equality check 1a failed", plist1, plist2); 86 plist2.add("blah"); 87 assertTrue("Non-empty lists inequality check 1b failed", !(plist1.equals(plist2))); 88 assertTrue("Non-empty lists inequality check 1c failed", !(plist2.equals(plist1))); 89 90 plist1 = this.getPListInstance(); 92 plist2 = this.getPListInstance(); 93 plist1.add("foo1"); 94 plist1.add("foo2"); 95 plist1.add("foo3"); 96 plist2 = this.getPListInstance(); 97 plist2.add("foo1"); 98 plist2.add("foo2"); 99 plist2.add("foo3"); 100 assertEquals("Non-empty lists equality check 2a failed", plist1, plist2); 101 plist2.add("blah"); 102 assertTrue("Non-empty lists inequality check 2b failed", !(plist1.equals(plist2))); 103 assertTrue("Non-empty lists inequality check 2c failed", !(plist2.equals(plist1))); 104 105 } 106 107 110 public void testParentalInheritance() { 111 if (logger.isInfoEnabled()) logger.info("testing parental inheritance methods"); 112 PList proot = null; 113 PList pchild1 = null; 114 PList pchild2 = null; 115 PList pchild3 = null; 116 List tlist = null; 117 118 119 proot = getPListInstance(); 122 pchild1 = getPListInstance(); 123 pchild2 = getPListInstance(); 124 proot.add(pchild1); 125 pchild1.add(pchild2); 126 assertTrue("Parental Inheritance check 1 failed...parent inheritance failed (should've but didn't)", proot.getParent()==null); 127 assertTrue("Parental Inheritance check 1a failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot); 128 assertTrue("Parental Inheritance check 1b failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==pchild1); 129 pchild3 = getPListInstance(); 131 pchild1.add(0, pchild3); 132 assertTrue("Parental Inheritance check 1c failed...parent inheritance failed (should've but didn't)", pchild3.getParent()==pchild1); 133 proot = getPListInstance(); 135 pchild1 = getPListInstance(); 136 pchild2 = getPListInstance(); 137 tlist = new ArrayList(); 138 tlist.add(pchild1); 139 tlist.add(pchild2); 140 proot.addAll(tlist); 141 assertTrue("Parental Inheritance check 1d failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot); 142 assertTrue("Parental Inheritance check 1e failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==proot); 143 proot = getPListInstance(); 145 pchild1 = getPListInstance(); 146 pchild2 = getPListInstance(); 147 tlist = new ArrayList(); 148 tlist.add(pchild1); 149 tlist.add(pchild2); 150 proot.addAll(0, tlist); 151 assertTrue("Parental Inheritance check 1f failed...parent inheritance failed (should've but didn't)", pchild1.getParent()==proot); 152 assertTrue("Parental Inheritance check 1g failed...parent inheritance failed (should've but didn't)", pchild2.getParent()==proot); 153 proot = getPListInstance(); 155 pchild1 = getPListInstance(); 156 pchild2 = getPListInstance(); 157 proot.add(pchild1); 158 proot.set(0, pchild2); 159 assertTrue("Parental Inheritance check 1h failed...child1 retained parental setting", pchild1.getParent()==null); 160 assertTrue("Parental Inheritance check 1i failed...child2 didn't inherit parent", pchild2.getParent()==proot); 161 proot = getPListInstance(); 163 pchild1 = getPListInstance(); 164 pchild2 = getPListInstance(); 165 proot.add(pchild1); 166 proot.add(pchild2); 167 proot.clear(); 168 assertTrue("Parental Inheritance check 1j failed...child1 retained parental setting", pchild1.getParent()==null); 169 assertTrue("Parental Inheritance check 1k failed...child2 retained parental setting", pchild2.getParent()==null); 170 proot = getPListInstance(); 172 pchild1 = getPListInstance(); 173 proot.add(pchild1); 174 proot.remove(0); 175 assertTrue("Parental Inheritance check 1m failed...child retained parental setting", pchild1.getParent()==null); 176 proot = getPListInstance(); 178 pchild1 = getPListInstance(); 179 proot.add(pchild1); 180 proot.remove(pchild1); 181 assertTrue("Parental Inheritance check 1n failed...child retained parental setting", pchild1.getParent()==null); 182 proot = getPListInstance(); 184 pchild1 = getPListInstance(); 185 pchild2 = getPListInstance(); 186 pchild3 = getPListInstance(); 187 proot.add(pchild1); 188 proot.add(pchild2); 189 proot.add(pchild3); 190 tlist = new ArrayList(); 191 tlist.add(pchild2); 192 tlist.add(pchild3); 193 proot.removeAll(tlist); 194 assertTrue("Parental Inheritance check 1o failed...child1 did not retain parental setting", pchild1.getParent()==proot); 195 assertTrue("Parental Inheritance check 1p failed...child2 retained parental setting", pchild2.getParent()==null); 196 assertTrue("Parental Inheritance check 1q failed...child3 retained parental setting", pchild3.getParent()==null); 197 proot = getPListInstance(); 199 pchild1 = getPListInstance(); 200 pchild2 = getPListInstance(); 201 pchild3 = getPListInstance(); 202 pchild1.add("pchild 1"); 203 pchild2.add("pchild 2"); 204 pchild3.add("pchild 3"); 205 proot.add(pchild1); 206 proot.add(pchild2); 207 proot.add(pchild3); 208 tlist = new ArrayList(); 209 tlist.add(pchild2); 210 tlist.add(pchild3); 211 proot.retainAll(tlist); 212 assertTrue("Parental Inheritance check 1r failed...child1 retained parental setting", pchild1.getParent()==null); 213 assertTrue("Parental Inheritance check 1s failed...child2 did not retain parental setting", pchild2.getParent()==proot); 214 assertTrue("Parental Inheritance check 1t failed...child3 did not retain parental setting", pchild3.getParent()==proot); 215 216 217 proot = getPListInstance(); 220 pchild1 = getPListInstance(); 221 pchild1.setInheritParents(false); 222 pchild2 = getPListInstance(); 223 pchild2.setInheritParents(false); 224 proot.add(pchild1); 225 pchild1.add(pchild2); 226 assertTrue("Parental Inheritance check 2 failed...parent inheritance failed (shouldn't have but did)", proot.getParent()==null); 227 assertTrue("Parental Inheritance check 2a failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot); 228 assertTrue("Parental Inheritance check 2b failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=pchild1); 229 pchild3 = getPListInstance(); 231 pchild3.setInheritParents(false); 232 pchild1.add(0, pchild3); 233 assertTrue("Parental Inheritance check 2c failed...parent inheritance failed (shouldn't have but did)", pchild3.getParent()!=pchild1); 234 proot = getPListInstance(); 236 pchild1 = getPListInstance(); 237 pchild1.setInheritParents(false); 238 pchild2 = getPListInstance(); 239 pchild2.setInheritParents(false); 240 tlist = new ArrayList(); 241 tlist.add(pchild1); 242 tlist.add(pchild2); 243 proot.addAll(tlist); 244 assertTrue("Parental Inheritance check 2d failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot); 245 assertTrue("Parental Inheritance check 2e failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=proot); 246 proot = getPListInstance(); 248 pchild1 = getPListInstance(); 249 pchild1.setInheritParents(false); 250 pchild2 = getPListInstance(); 251 pchild2.setInheritParents(false); 252 tlist = new ArrayList(); 253 tlist.add(pchild1); 254 tlist.add(pchild2); 255 proot.addAll(0, tlist); 256 assertTrue("Parental Inheritance check 2f failed...parent inheritance failed (shouldn't have but did)", pchild1.getParent()!=proot); 257 assertTrue("Parental Inheritance check 2g failed...parent inheritance failed (shouldn't have but did)", pchild2.getParent()!=proot); 258 proot = getPListInstance(); 260 pchild1 = getPListInstance(); 261 pchild2 = getPListInstance(); 262 proot.add(pchild1); 263 pchild1.setInheritParents(false); pchild2.setInheritParents(false); 265 proot.set(0, pchild2); 266 assertTrue("Parental Inheritance check 2h failed...child1's parental value was improperly cleared", pchild1.getParent()==proot); 267 assertTrue("Parental Inheritance check 2i failed...child2's parental value was improperly set", pchild2.getParent()==null); 268 proot = getPListInstance(); 270 pchild1 = getPListInstance(); 271 pchild2 = getPListInstance(); 272 proot.add(pchild1); 273 proot.add(pchild2); 274 pchild1.setInheritParents(false); pchild2.setInheritParents(false); proot.clear(); 277 assertTrue("Parental Inheritance check 2j failed...child1's parental value was improperly cleared", pchild1.getParent()==proot); 278 assertTrue("Parental Inheritance check 2k failed...child2's parental value was improperly cleared", pchild2.getParent()==proot); 279 proot = getPListInstance(); 281 pchild1 = getPListInstance(); 282 proot.add(pchild1); 283 pchild1.setInheritParents(false); proot.remove(0); 285 assertTrue("Parental Inheritance check 2m failed...child's parental value was improperly cleared", pchild1.getParent()==proot); 286 proot = getPListInstance(); 288 pchild1 = getPListInstance(); 289 proot.add(pchild1); 290 pchild1.setInheritParents(false); proot.remove(pchild1); 292 assertTrue("Parental Inheritance check 2n failed...child's parental value was improperly cleared", pchild1.getParent()==proot); 293 proot = getPListInstance(); 295 pchild1 = getPListInstance(); 296 pchild2 = getPListInstance(); 297 pchild3 = getPListInstance(); 298 proot.add(pchild1); 299 proot.add(pchild2); 300 proot.add(pchild3); 301 pchild1.setInheritParents(false); 302 pchild2.setInheritParents(false); 303 pchild3.setInheritParents(false); 304 tlist = new ArrayList(); 305 tlist.add(pchild2); 306 tlist.add(pchild3); 307 proot.removeAll(tlist); 308 assertTrue("Parental Inheritance check 2p failed...child2 did not retain parental setting", pchild2.getParent()==proot); 309 assertTrue("Parental Inheritance check 2q failed...child3 did not retain parental setting", pchild3.getParent()==proot); 310 proot = getPListInstance(); 312 pchild1 = getPListInstance(); 313 pchild2 = getPListInstance(); 314 pchild3 = getPListInstance(); 315 proot.add(pchild1); 316 proot.add(pchild2); 317 proot.add(pchild3); 318 tlist = new ArrayList(); 319 tlist.add(pchild2); 320 tlist.add(pchild3); 321 proot.retainAll(tlist); 322 assertTrue("Parental Inheritance check 2r failed...child1 did not retain parental setting", pchild1.getParent()==proot); 323 } 324 325 328 public void testListManipulation() { 329 if (logger.isInfoEnabled()) logger.info("testing basic data manipulation"); 330 PList proot = null; 331 PList plist1 = null; 332 PList plist2 = null; 333 PList plist3 = null; 334 List tlist = null; 335 Object [] els = new Object [] {"Foo", new Integer (99), null}; 336 337 proot = getPListInstance(); 340 for (int i=0; i<els.length; i++) { 341 proot.add(els[i]); 342 assertTrue("Add data check 1a, idx["+i+"] - data not added", proot.contains(els[i])); 343 assertTrue("Add data check 1b, idx["+i+"] - incorrect size", proot.size()==i+1); 344 assertTrue("Add data check 1c, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i); 345 assertTrue("Add data check 1d, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i); 346 assertTrue("Add data check 1e, idx["+i+"] - data not retrieved", proot.get(i)==els[i]); 347 assertTrue("Add data check 1f, idx["+i+"] - list claims to be empty", !proot.isEmpty()); 348 } 349 for (int i=els.length-1; i>=0; i--) { 351 proot.remove(els[i]); 352 assertTrue("Remove data check 1a, idx["+i+"] - data not removed", !proot.contains(els[i])); 353 assertTrue("Remove data check 1b, idx["+i+"] - incorrect size", proot.size()==i); 354 assertTrue("Remove data check 1c, idx["+i+"] - incorrect index", proot.indexOf(els[i])==-1); 355 assertTrue("Remove data check 1d, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==-1); 356 try { 357 Object o = proot.get(i); 358 fail("Remove data check 1e, idx["+i+"] - exception not generated"); 359 } catch (IndexOutOfBoundsException e) {} 360 } 361 assertTrue("Remove data check 1f - list not empty", proot.isEmpty()); 362 363 tlist = new ArrayList(); 365 for (int i=0; i<els.length; i++) {tlist.add(els[i]);} 366 proot = getPListInstance(); 368 proot.addAll(tlist); 369 assertTrue("Add data check 2a - incorrect size", proot.size()==els.length); 370 assertTrue("Add data check 2b - doesn't contain all items", proot.containsAll(tlist)); 371 for (int i=0; i<els.length; i++) { 372 assertTrue("Add data check 2c, idx["+i+"] - data not added", proot.contains(els[i])); 373 assertTrue("Add data check 2d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i); 374 assertTrue("Add data check 2e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i); 375 assertTrue("Add data check 2f, idx["+i+"] - data not retrieved", proot.get(i)==els[i]); 376 assertTrue("Add data check 2f, idx["+i+"] - list claims to be empty", !proot.isEmpty()); 377 } 378 proot.removeAll(tlist); 380 assertTrue("Remove data check 2a - incorrect size", proot.size()==0); 381 assertTrue("Remove data check 2b - still contain all items", !proot.containsAll(tlist)); 382 for (int i=els.length-1; i>=0; i--) { 383 proot.remove(els[i]); 384 assertTrue("Remove data check 2c, idx["+i+"] - data not removed", !proot.contains(els[i])); 385 assertTrue("Remove data check 2d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==-1); 386 assertTrue("Remove data check 2e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==-1); 387 try { 388 Object o = proot.get(i); 389 fail("Remove data check 2f, idx["+i+"] - exception not generated"); 390 } catch (IndexOutOfBoundsException e) {} 391 } 392 assertTrue("Remove data check 2f - list not empty", proot.isEmpty()); 393 proot = getPListInstance(); 395 proot.add("some other item"); 396 proot.addAll(0, tlist); 397 assertTrue("Add data check 3a - incorrect size", proot.size()==els.length+1); 398 assertTrue("Add data check 3b - doesn't contain all items", proot.containsAll(tlist)); 399 for (int i=0; i<els.length; i++) { 400 assertTrue("Add data check 3c, idx["+i+"] - data not added", proot.contains(els[i])); 401 assertTrue("Add data check 3d, idx["+i+"] - incorrect index", proot.indexOf(els[i])==i); 402 assertTrue("Add data check 3e, idx["+i+"] - incorrect last index", proot.lastIndexOf(els[i])==i); 403 assertTrue("Add data check 3f, idx["+i+"] - data not retrieved", proot.get(i)==els[i]); 404 } 405 proot.retainAll(tlist); 407 assertTrue("Remove data check 3a - incorrect size", proot.size()==els.length); 408 409 proot = getPListInstance(); 412 proot.add("Foo"); 413 proot.set(0, "Blah"); 414 assertTrue("Set method failed", proot.get(0).equals("Blah")); 415 proot = getPListInstance(); 417 proot.add("Foo"); 418 tlist = new ArrayList(); 419 for (int i=0; i<els.length; i++) {tlist.add(els[i]);} 420 proot.addAll(tlist); 421 proot.add("Blah"); 422 List sublist = proot.subList(1,els.length+1); 423 assertTrue("subList method failed", sublist.size()==els.length); 424 proot = getPListInstance(); 426 for (int i=0; i<els.length; i++) { 427 proot.add(els[i]); 428 } 429 assertTrue("Clear check 4a failed - data not added", proot.size()==els.length); 430 proot.clear(); 431 assertTrue("Clear check 4b failed - data not removed", proot.size()==0); 432 435 } 436 437 438 } 439 | Popular Tags |