1 16 package org.apache.commons.collections.bag; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.ConcurrentModificationException ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.NoSuchElementException ; 25 26 import org.apache.commons.collections.AbstractTestObject; 27 import org.apache.commons.collections.Bag; 28 29 44 public abstract class AbstractTestBag extends AbstractTestObject { 45 50 55 public AbstractTestBag(String testName) { 56 super(testName); 57 } 58 59 65 public abstract Bag makeBag(); 66 67 72 public Object makeObject() { 73 return makeBag(); 74 } 75 76 public void testBagAdd() { 78 Bag bag = makeBag(); 79 bag.add("A"); 80 assertTrue("Should contain 'A'", bag.contains("A")); 81 assertEquals("Should have count of 1", 1, bag.getCount("A")); 82 bag.add("A"); 83 assertTrue("Should contain 'A'", bag.contains("A")); 84 assertEquals("Should have count of 2", 2, bag.getCount("A")); 85 bag.add("B"); 86 assertTrue(bag.contains("A")); 87 assertTrue(bag.contains("B")); 88 } 89 90 public void testBagEqualsSelf() { 91 Bag bag = makeBag(); 92 assertTrue(bag.equals(bag)); 93 bag.add("elt"); 94 assertTrue(bag.equals(bag)); 95 bag.add("elt"); assertTrue(bag.equals(bag)); 97 bag.add("elt2"); 98 assertTrue(bag.equals(bag)); 99 } 100 101 public void testRemove() { 102 Bag bag = makeBag(); 103 bag.add("A"); 104 assertEquals("Should have count of 1", 1, bag.getCount("A")); 105 bag.remove("A"); 106 assertEquals("Should have count of 0", 0, bag.getCount("A")); 107 bag.add("A"); 108 bag.add("A"); 109 bag.add("A"); 110 bag.add("A"); 111 assertEquals("Should have count of 4", 4, bag.getCount("A")); 112 bag.remove("A", 0); 113 assertEquals("Should have count of 4", 4, bag.getCount("A")); 114 bag.remove("A", 2); 115 assertEquals("Should have count of 2", 2, bag.getCount("A")); 116 bag.remove("A"); 117 assertEquals("Should have count of 0", 0, bag.getCount("A")); 118 } 119 120 public void testRemoveAll() { 121 Bag bag = makeBag(); 122 bag.add("A", 2); 123 assertEquals("Should have count of 2", 2, bag.getCount("A")); 124 bag.add("B"); 125 bag.add("C"); 126 assertEquals("Should have count of 4", 4, bag.size()); 127 List delete = new ArrayList (); 128 delete.add("A"); 129 delete.add("B"); 130 bag.removeAll(delete); 131 assertEquals("Should have count of 1", 1, bag.getCount("A")); 132 assertEquals("Should have count of 0", 0, bag.getCount("B")); 133 assertEquals("Should have count of 1", 1, bag.getCount("C")); 134 assertEquals("Should have count of 2", 2, bag.size()); 135 } 136 137 public void testContains() { 138 Bag bag = makeBag(); 139 140 assertEquals("Bag does not have at least 1 'A'", false, bag.contains("A")); 141 assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B")); 142 143 bag.add("A"); assertEquals("Bag has at least 1 'A'", true, bag.contains("A")); 145 assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B")); 146 147 bag.add("A"); assertEquals("Bag has at least 1 'A'", true, bag.contains("A")); 149 assertEquals("Bag does not have at least 1 'B'", false, bag.contains("B")); 150 151 bag.add("B"); assertEquals("Bag has at least 1 'A'", true, bag.contains("A")); 153 assertEquals("Bag has at least 1 'B'", true, bag.contains("B")); 154 } 155 156 public void testContainsAll() { 157 Bag bag = makeBag(); 158 List known = new ArrayList (); 159 List known1A = new ArrayList (); 160 known1A.add("A"); 161 List known2A = new ArrayList (); 162 known2A.add("A"); 163 known2A.add("A"); 164 List known1B = new ArrayList (); 165 known1B.add("B"); 166 List known1A1B = new ArrayList (); 167 known1A1B.add("A"); 168 known1A1B.add("B"); 169 170 assertEquals("Bag containsAll of empty", true, bag.containsAll(known)); 171 assertEquals("Bag does not containsAll of 1 'A'", false, bag.containsAll(known1A)); 172 assertEquals("Bag does not containsAll of 2 'A'", false, bag.containsAll(known2A)); 173 assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B)); 174 assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B)); 175 176 bag.add("A"); assertEquals("Bag containsAll of empty", true, bag.containsAll(known)); 178 assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A)); 179 assertEquals("Bag does not containsAll of 2 'A'", false, bag.containsAll(known2A)); 180 assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B)); 181 assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B)); 182 183 bag.add("A"); assertEquals("Bag containsAll of empty", true, bag.containsAll(known)); 185 assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A)); 186 assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A)); 187 assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B)); 188 assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B)); 189 190 bag.add("A"); assertEquals("Bag containsAll of empty", true, bag.containsAll(known)); 192 assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A)); 193 assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A)); 194 assertEquals("Bag does not containsAll of 1 'B'", false, bag.containsAll(known1B)); 195 assertEquals("Bag does not containsAll of 1 'A' 1 'B'", false, bag.containsAll(known1A1B)); 196 197 bag.add("B"); assertEquals("Bag containsAll of empty", true, bag.containsAll(known)); 199 assertEquals("Bag containsAll of 1 'A'", true, bag.containsAll(known1A)); 200 assertEquals("Bag containsAll of 2 'A'", true, bag.containsAll(known2A)); 201 assertEquals("Bag containsAll of 1 'B'", true, bag.containsAll(known1B)); 202 assertEquals("Bag containsAll of 1 'A' 1 'B'", true, bag.containsAll(known1A1B)); 203 } 204 205 public void testSize() { 206 Bag bag = makeBag(); 207 assertEquals("Should have 0 total items", 0, bag.size()); 208 bag.add("A"); 209 assertEquals("Should have 1 total items", 1, bag.size()); 210 bag.add("A"); 211 assertEquals("Should have 2 total items", 2, bag.size()); 212 bag.add("A"); 213 assertEquals("Should have 3 total items", 3, bag.size()); 214 bag.add("B"); 215 assertEquals("Should have 4 total items", 4, bag.size()); 216 bag.add("B"); 217 assertEquals("Should have 5 total items", 5, bag.size()); 218 bag.remove("A", 2); 219 assertEquals("Should have 1 'A'", 1, bag.getCount("A")); 220 assertEquals("Should have 3 total items", 3, bag.size()); 221 bag.remove("B"); 222 assertEquals("Should have 1 total item", 1, bag.size()); 223 } 224 225 public void testRetainAll() { 226 Bag bag = makeBag(); 227 bag.add("A"); 228 bag.add("A"); 229 bag.add("A"); 230 bag.add("B"); 231 bag.add("B"); 232 bag.add("C"); 233 List retains = new ArrayList (); 234 retains.add("B"); 235 retains.add("C"); 236 bag.retainAll(retains); 237 assertEquals("Should have 2 total items", 2, bag.size()); 238 } 239 240 public void testIterator() { 241 Bag bag = makeBag(); 242 bag.add("A"); 243 bag.add("A"); 244 bag.add("B"); 245 assertEquals("Bag should have 3 items", 3, bag.size()); 246 Iterator i = bag.iterator(); 247 248 boolean foundA = false; 249 while (i.hasNext()) { 250 String element = (String ) i.next(); 251 if (element.equals("A")) { 253 if (foundA == false) { 254 foundA = true; 255 } else { 256 i.remove(); 257 } 258 } 259 } 260 261 assertTrue("Bag should still contain 'A'", bag.contains("A")); 262 assertEquals("Bag should have 2 items", 2, bag.size()); 263 assertEquals("Bag should have 1 'A'", 1, bag.getCount("A")); 264 } 265 266 public void testIteratorFail() { 267 Bag bag = makeBag(); 268 bag.add("A"); 269 bag.add("A"); 270 bag.add("B"); 271 Iterator it = bag.iterator(); 272 it.next(); 273 bag.remove("A"); 274 try { 275 it.next(); 276 fail("Should throw ConcurrentModificationException"); 277 } catch (ConcurrentModificationException e) { 278 } 280 } 281 282 public void testIteratorFailNoMore() { 283 Bag bag = makeBag(); 284 bag.add("A"); 285 bag.add("A"); 286 bag.add("B"); 287 Iterator it = bag.iterator(); 288 it.next(); 289 it.next(); 290 it.next(); 291 try { 292 it.next(); 293 fail("Should throw NoSuchElementException"); 294 } catch (NoSuchElementException ex) { 295 } 297 } 298 299 public void testIteratorFailDoubleRemove() { 300 Bag bag = makeBag(); 301 bag.add("A"); 302 bag.add("A"); 303 bag.add("B"); 304 Iterator it = bag.iterator(); 305 it.next(); 306 it.next(); 307 assertEquals(3, bag.size()); 308 it.remove(); 309 assertEquals(2, bag.size()); 310 try { 311 it.remove(); 312 fail("Should throw IllegalStateException"); 313 } catch (IllegalStateException ex) { 314 } 316 assertEquals(2, bag.size()); 317 it.next(); 318 it.remove(); 319 assertEquals(1, bag.size()); 320 } 321 322 public void testToArray() { 323 Bag bag = makeBag(); 324 bag.add("A"); 325 bag.add("A"); 326 bag.add("B"); 327 bag.add("B"); 328 bag.add("C"); 329 Object [] array = bag.toArray(); 330 int a = 0, b = 0, c = 0; 331 for (int i = 0; i < array.length; i++) { 332 a += (array[i].equals("A") ? 1 : 0); 333 b += (array[i].equals("B") ? 1 : 0); 334 c += (array[i].equals("C") ? 1 : 0); 335 } 336 assertEquals(2, a); 337 assertEquals(2, b); 338 assertEquals(1, c); 339 } 340 341 public void testToArrayPopulate() { 342 Bag bag = makeBag(); 343 bag.add("A"); 344 bag.add("A"); 345 bag.add("B"); 346 bag.add("B"); 347 bag.add("C"); 348 String [] array = (String []) bag.toArray(new String [0]); 349 int a = 0, b = 0, c = 0; 350 for (int i = 0; i < array.length; i++) { 351 a += (array[i].equals("A") ? 1 : 0); 352 b += (array[i].equals("B") ? 1 : 0); 353 c += (array[i].equals("C") ? 1 : 0); 354 } 355 assertEquals(2, a); 356 assertEquals(2, b); 357 assertEquals(1, c); 358 } 359 360 public void testEquals() { 362 Bag bag = makeBag(); 363 Bag bag2 = makeBag(); 364 assertEquals(true, bag.equals(bag2)); 365 bag.add("A"); 366 assertEquals(false, bag.equals(bag2)); 367 bag2.add("A"); 368 assertEquals(true, bag.equals(bag2)); 369 bag.add("A"); 370 bag.add("B"); 371 bag.add("B"); 372 bag.add("C"); 373 bag2.add("A"); 374 bag2.add("B"); 375 bag2.add("B"); 376 bag2.add("C"); 377 assertEquals(true, bag.equals(bag2)); 378 } 379 380 public void testEqualsHashBag() { 381 Bag bag = makeBag(); 382 Bag bag2 = new HashBag(); 383 assertEquals(true, bag.equals(bag2)); 384 bag.add("A"); 385 assertEquals(false, bag.equals(bag2)); 386 bag2.add("A"); 387 assertEquals(true, bag.equals(bag2)); 388 bag.add("A"); 389 bag.add("B"); 390 bag.add("B"); 391 bag.add("C"); 392 bag2.add("A"); 393 bag2.add("B"); 394 bag2.add("B"); 395 bag2.add("C"); 396 assertEquals(true, bag.equals(bag2)); 397 } 398 399 public void testHashCode() { 400 Bag bag = makeBag(); 401 Bag bag2 = makeBag(); 402 assertEquals(0, bag.hashCode()); 403 assertEquals(0, bag2.hashCode()); 404 assertEquals(bag.hashCode(), bag2.hashCode()); 405 bag.add("A"); 406 bag.add("A"); 407 bag.add("B"); 408 bag.add("B"); 409 bag.add("C"); 410 bag2.add("A"); 411 bag2.add("A"); 412 bag2.add("B"); 413 bag2.add("B"); 414 bag2.add("C"); 415 assertEquals(bag.hashCode(), bag2.hashCode()); 416 417 int total = 0; 418 total += ("A".hashCode() ^ 2); 419 total += ("B".hashCode() ^ 2); 420 total += ("C".hashCode() ^ 1); 421 assertEquals(total, bag.hashCode()); 422 assertEquals(total, bag2.hashCode()); 423 } 424 425 public void testEmptyBagSerialization() throws IOException , ClassNotFoundException { 427 Bag bag = makeBag(); 428 if (!(bag instanceof Serializable && isTestSerialization())) return; 429 430 byte[] objekt = writeExternalFormToBytes((Serializable ) bag); 431 Bag bag2 = (Bag) readExternalFormFromBytes(objekt); 432 433 assertEquals("Bag should be empty",0, bag.size()); 434 assertEquals("Bag should be empty",0, bag2.size()); 435 } 436 437 public void testFullBagSerialization() throws IOException , ClassNotFoundException { 438 Bag bag = makeBag(); 439 bag.add("A"); 440 bag.add("A"); 441 bag.add("B"); 442 bag.add("B"); 443 bag.add("C"); 444 int size = bag.size(); 445 if (!(bag instanceof Serializable && isTestSerialization())) return; 446 447 byte[] objekt = writeExternalFormToBytes((Serializable ) bag); 448 Bag bag2 = (Bag) readExternalFormFromBytes(objekt); 449 450 assertEquals("Bag should be same size", size, bag.size()); 451 assertEquals("Bag should be same size", size, bag2.size()); 452 } 453 454 458 public void testEmptyBagCompatibility() throws IOException , ClassNotFoundException { 459 Bag bag = makeBag(); 461 if(bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 462 Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalEmptyCollectionName(bag)); 463 assertTrue("Bag is empty",bag2.size() == 0); 464 assertEquals(bag, bag2); 465 } 466 } 467 468 472 public void testFullBagCompatibility() throws IOException , ClassNotFoundException { 473 Bag bag = makeBag(); 475 bag.add("A"); 476 bag.add("A"); 477 bag.add("B"); 478 bag.add("B"); 479 bag.add("C"); 480 if(bag instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 481 Bag bag2 = (Bag) readExternalFormFromDisk(getCanonicalFullCollectionName(bag)); 482 assertEquals("Bag is the right size",bag.size(), bag2.size()); 483 assertEquals(bag, bag2); 484 } 485 } 486 } 487 | Popular Tags |