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