1 16 package org.apache.commons.collections.collection; 17 18 import java.util.Arrays ; 19 import java.util.Collection ; 20 import java.util.HashSet ; 21 import java.util.Iterator ; 22 23 import junit.framework.Test; 24 import junit.framework.TestSuite; 25 26 36 public class TestCompositeCollection extends AbstractTestCollection { 37 38 public TestCompositeCollection(String name) { 39 super(name); 40 } 41 42 public static Test suite() { 43 return new TestSuite(TestCompositeCollection.class); 44 } 45 46 public static void main(String args[]) { 47 String [] testCaseName = { TestCompositeCollection.class.getName()}; 48 junit.textui.TestRunner.main(testCaseName); 49 } 50 51 55 public boolean isAddSupported() { 56 return false; 57 } 58 59 public boolean isRemoveSupported() { 60 return false; 61 } 62 63 66 public Collection makeCollection() { 67 return new CompositeCollection(); 68 } 69 70 public Collection makeConfirmedCollection() { 71 return new HashSet (); 72 } 73 74 public Object [] getFullElements() { 75 return new Object [] {"1", "2", "3", "4"}; 76 } 77 78 81 public Collection makeFullCollection() { 82 CompositeCollection compositeCollection = new CompositeCollection(); 83 Object [] elements = getFullElements(); 84 for (int i = 0; i < elements.length; i++) { 85 Collection summand = new HashSet (); 86 summand.add(elements[i]); 87 compositeCollection.addComposited(summand); 88 } 89 return compositeCollection; 90 } 91 92 95 public Collection makeConfirmedFullCollection() { 96 Collection collection = new HashSet (); 97 collection.addAll(Arrays.asList(getFullElements())); 98 return collection; 99 } 100 101 105 public void testUnsupportedRemove() { 106 resetFull(); 107 try { 108 collection.remove(null); 109 fail("remove should raise UnsupportedOperationException"); 110 } catch (UnsupportedOperationException e) { 111 } 113 verify(); 114 } 115 116 118 protected CompositeCollection c; 119 protected Collection one; 120 protected Collection two; 121 122 protected void setUpTest() { 123 c = new CompositeCollection(); 124 one = new HashSet (); 125 two = new HashSet (); 126 } 127 128 protected void setUpMutatorTest() { 129 setUpTest(); 130 c.setMutator(new CompositeCollection.CollectionMutator() { 131 public boolean add(CompositeCollection composite, 132 Collection [] collections, Object obj) { 133 for (int i = 0; i < collections.length; i++) { 134 collections[i].add(obj); 135 } 136 return true; 137 } 138 139 public boolean addAll(CompositeCollection composite, 140 Collection [] collections, Collection coll) { 141 for (int i = 0; i < collections.length; i++) { 142 collections[i].addAll(coll); 143 } 144 return true; 145 } 146 147 public boolean remove(CompositeCollection composite, 148 Collection [] collections, Object obj) { 149 for (int i = 0; i < collections.length; i++) { 150 collections[i].remove(obj); 151 } 152 return true; 153 } 154 }); 155 } 156 157 public void testSize() { 158 setUpTest(); 159 HashSet set = new HashSet (); 160 set.add("a"); 161 set.add("b"); 162 c.addComposited(set); 163 assertEquals(set.size(), c.size()); 164 } 165 166 public void testMultipleCollectionsSize() { 167 setUpTest(); 168 HashSet set = new HashSet (); 169 set.add("a"); 170 set.add("b"); 171 c.addComposited(set); 172 HashSet other = new HashSet (); 173 other.add("c"); 174 c.addComposited(other); 175 assertEquals(set.size() + other.size(), c.size()); 176 } 177 178 public void testIsEmpty() { 179 setUpTest(); 180 assertTrue(c.isEmpty()); 181 HashSet empty = new HashSet (); 182 c.addComposited(empty); 183 assertTrue(c.isEmpty()); 184 empty.add("a"); 185 assertTrue(!c.isEmpty()); 186 } 187 188 189 public void testIterator() { 190 setUpTest(); 191 one.add("1"); 192 two.add("2"); 193 c.addComposited(one); 194 c.addComposited(two); 195 Iterator i = c.iterator(); 196 Object next = i.next(); 197 assertTrue(c.contains(next)); 198 assertTrue(one.contains(next)); 199 next = i.next(); 200 i.remove(); 201 assertTrue(!c.contains(next)); 202 assertTrue(!two.contains(next)); 203 } 204 205 public void testClear() { 206 setUpTest(); 207 one.add("1"); 208 two.add("2"); 209 c.addComposited(one, two); 210 c.clear(); 211 assertTrue(one.isEmpty()); 212 assertTrue(two.isEmpty()); 213 assertTrue(c.isEmpty()); 214 } 215 216 public void testContainsAll() { 217 setUpTest(); 218 one.add("1"); 219 two.add("1"); 220 c.addComposited(one); 221 assertTrue(c.containsAll(two)); 222 } 223 224 public void testRetainAll() { 225 setUpTest(); 226 one.add("1"); 227 one.add("2"); 228 two.add("1"); 229 c.addComposited(one); 230 c.retainAll(two); 231 assertTrue(!c.contains("2")); 232 assertTrue(!one.contains("2")); 233 assertTrue(c.contains("1")); 234 assertTrue(one.contains("1")); 235 } 236 237 public void testAddAllMutator() { 238 setUpTest(); 239 c.setMutator(new CompositeCollection.CollectionMutator() { 240 public boolean add(CompositeCollection composite, 241 Collection [] collections, Object obj) { 242 for (int i = 0; i < collections.length; i++) { 243 collections[i].add(obj); 244 } 245 return true; 246 } 247 248 public boolean addAll(CompositeCollection composite, 249 Collection [] collections, Collection coll) { 250 for (int i = 0; i < collections.length; i++) { 251 collections[i].addAll(coll); 252 } 253 return true; 254 } 255 256 public boolean remove(CompositeCollection composite, 257 Collection [] collections, Object obj) { 258 return false; 259 } 260 }); 261 262 c.addComposited(one); 263 two.add("foo"); 264 c.addAll(two); 265 assertTrue(c.contains("foo")); 266 assertTrue(one.contains("foo")); 267 } 268 269 public void testAddMutator() { 270 setUpTest(); 271 c.setMutator(new CompositeCollection.CollectionMutator() { 272 public boolean add(CompositeCollection composite, 273 Collection [] collections, Object obj) { 274 for (int i = 0; i < collections.length; i++) { 275 collections[i].add(obj); 276 } 277 return true; 278 } 279 280 public boolean addAll(CompositeCollection composite, 281 Collection [] collections, Collection coll) { 282 for (int i = 0; i < collections.length; i++) { 283 collections[i].addAll(coll); 284 } 285 return true; 286 } 287 288 public boolean remove(CompositeCollection composite, 289 Collection [] collections, Object obj) { 290 return false; 291 } 292 }); 293 294 c.addComposited(one); 295 c.add("foo"); 296 assertTrue(c.contains("foo")); 297 assertTrue(one.contains("foo")); 298 } 299 300 public void testToCollection() { 301 setUpTest(); 302 one.add("1"); 303 two.add("2"); 304 c.addComposited(one, two); 305 Collection foo = c.toCollection(); 306 assertTrue(foo.containsAll(c)); 307 assertEquals(c.size(), foo.size()); 308 one.add("3"); 309 assertTrue(!foo.containsAll(c)); 310 } 311 312 public void testAddAllToCollection() { 313 setUpTest(); 314 one.add("1"); 315 two.add("2"); 316 c.addComposited(one, two); 317 Collection toCollection = new HashSet (); 318 toCollection.addAll(c); 319 assertTrue(toCollection.containsAll(c)); 320 assertEquals(c.size(), toCollection.size()); 321 } 322 323 public void testRemove() { 324 setUpMutatorTest(); 325 one.add("1"); 326 two.add("2"); 327 two.add("1"); 328 c.addComposited(one, two); 329 c.remove("1"); 330 assertTrue(!c.contains("1")); 331 assertTrue(!one.contains("1")); 332 assertTrue(!two.contains("1")); 333 } 334 335 public void testRemoveAll() { 336 setUpMutatorTest(); 337 one.add("1"); 338 two.add("2"); 339 two.add("1"); 340 c.addComposited(one, two); 341 c.removeAll(one); 342 assertTrue(!c.contains("1")); 343 assertTrue(!one.contains("1")); 344 assertTrue(!two.contains("1")); 345 } 346 347 public void testRemoveComposited() { 348 setUpMutatorTest(); 349 one.add("1"); 350 two.add("2"); 351 two.add("1"); 352 c.addComposited(one, two); 353 c.removeComposited(one); 354 assertTrue(c.contains("1")); 355 assertEquals(c.size(), 2); 356 } 357 } 358 | Popular Tags |