1 16 package org.apache.commons.collections.set; 17 18 import junit.framework.Test; 19 import junit.framework.TestSuite; 20 21 import org.apache.commons.collections.collection.CompositeCollection; 22 23 import java.util.Set ; 24 import java.util.HashSet ; 25 import java.util.Collection ; 26 27 37 38 public class TestCompositeSet extends AbstractTestSet { 39 public TestCompositeSet(String name) { 40 super(name); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestCompositeSet.class); 45 } 46 47 public Set makeEmptySet() { 48 final HashSet contained = new HashSet (); 49 CompositeSet set = new CompositeSet(contained); 50 set.setMutator(new CompositeSet.SetMutator() { 51 public void resolveCollision(CompositeSet comp, Set existing, 52 Set added, Collection intersects) { 53 throw new IllegalArgumentException (); 54 } 55 56 public boolean add(CompositeCollection composite, 57 Collection[] collections, Object obj) { 58 return contained.add(obj); 59 } 60 61 public boolean addAll(CompositeCollection composite, 62 Collection[] collections, Collection coll) { 63 return contained.addAll(coll); 64 } 65 66 public boolean remove(CompositeCollection composite, 67 Collection[] collections, Object obj) { 68 return contained.remove(obj); 69 } 70 }); 71 return set; 72 } 73 74 public Set buildOne() { 75 HashSet set = new HashSet (); 76 set.add("1"); 77 set.add("2"); 78 return set; 79 } 80 81 public Set buildTwo() { 82 HashSet set = new HashSet (); 83 set.add("3"); 84 set.add("4"); 85 return set; 86 } 87 88 public void testContains() { 89 CompositeSet set = new CompositeSet(new Set []{buildOne(), buildTwo()}); 90 assertTrue(set.contains("1")); 91 } 92 93 public void testRemoveUnderlying() { 94 Set one = buildOne(); 95 Set two = buildTwo(); 96 CompositeSet set = new CompositeSet(new Set []{one, two}); 97 one.remove("1"); 98 assertFalse(set.contains("1")); 99 100 two.remove("3"); 101 assertFalse(set.contains("3")); 102 } 103 104 public void testRemoveComposited() { 105 Set one = buildOne(); 106 Set two = buildTwo(); 107 CompositeSet set = new CompositeSet(new Set []{one, two}); 108 set.remove("1"); 109 assertFalse(one.contains("1")); 110 111 set.remove("3"); 112 assertFalse(one.contains("3")); 113 } 114 115 public void testFailedCollisionResolution() { 116 Set one = buildOne(); 117 Set two = buildTwo(); 118 CompositeSet set = new CompositeSet(new Set []{one, two}); 119 set.setMutator(new CompositeSet.SetMutator() { 120 public void resolveCollision(CompositeSet comp, Set existing, 121 Set added, Collection intersects) { 122 } 123 124 public boolean add(CompositeCollection composite, 125 Collection[] collections, Object obj) { 126 throw new UnsupportedOperationException (); 127 } 128 129 public boolean addAll(CompositeCollection composite, 130 Collection[] collections, Collection coll) { 131 throw new UnsupportedOperationException (); 132 } 133 134 public boolean remove(CompositeCollection composite, 135 Collection[] collections, Object obj) { 136 throw new UnsupportedOperationException (); 137 } 138 }); 139 140 HashSet three = new HashSet (); 141 three.add("1"); 142 try { 143 set.addComposited(three); 144 fail("IllegalArgumentException should have been thrown"); 145 } 146 catch (IllegalArgumentException e) { 147 } 149 } 150 151 public void testAddComposited() { 152 Set one = buildOne(); 153 Set two = buildTwo(); 154 CompositeSet set = new CompositeSet(); 155 set.addComposited(one, two); 156 CompositeSet set2 = new CompositeSet(buildOne()); 157 set2.addComposited(buildTwo()); 158 assertTrue(set.equals(set2)); 159 HashSet set3 = new HashSet (); 160 set3.add("1"); 161 set3.add("2"); 162 set3.add("3"); 163 HashSet set4 = new HashSet (); 164 set4.add("4"); 165 CompositeSet set5 = new CompositeSet(set3); 166 set5.addComposited(set4); 167 assertTrue(set.equals(set5)); 168 try { 169 set.addComposited(set3); 170 fail("Expecting UnsupportedOperationException."); 171 } catch (UnsupportedOperationException ex) { 172 } 174 } 175 } 176 | Popular Tags |