001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.collection;
017:
018: import java.util.Arrays;
019: import java.util.Collection;
020: import java.util.HashSet;
021: import java.util.Iterator;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025:
026: /**
027: * Extension of {@link AbstractTestCollection} for exercising the
028: * {@link CompositeCollection} implementation.
029: *
030: * @since Commons Collections 3.0
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Brian McCallister
034: * @author Phil Steitz
035: */
036: public class TestCompositeCollection extends AbstractTestCollection {
037:
038: public TestCompositeCollection(String name) {
039: super (name);
040: }
041:
042: public static Test suite() {
043: return new TestSuite(TestCompositeCollection.class);
044: }
045:
046: public static void main(String args[]) {
047: String[] testCaseName = { TestCompositeCollection.class
048: .getName() };
049: junit.textui.TestRunner.main(testCaseName);
050: }
051:
052: //-----------------------------------------------------------------------------
053: /**
054: * Run stock collection tests without Mutator, so turn off add, remove
055: */
056: public boolean isAddSupported() {
057: return false;
058: }
059:
060: public boolean isRemoveSupported() {
061: return false;
062: }
063:
064: /**
065: * Empty collection is empty composite
066: */
067: public Collection makeCollection() {
068: return new CompositeCollection();
069: }
070:
071: public Collection makeConfirmedCollection() {
072: return new HashSet();
073: }
074:
075: public Object[] getFullElements() {
076: return new Object[] { "1", "2", "3", "4" };
077: }
078:
079: /**
080: * Full collection consists of 4 collections, each with one element
081: */
082: public Collection makeFullCollection() {
083: CompositeCollection compositeCollection = new CompositeCollection();
084: Object[] elements = getFullElements();
085: for (int i = 0; i < elements.length; i++) {
086: Collection summand = new HashSet();
087: summand.add(elements[i]);
088: compositeCollection.addComposited(summand);
089: }
090: return compositeCollection;
091: }
092:
093: /**
094: * Full collection should look like a collection with 4 elements
095: */
096: public Collection makeConfirmedFullCollection() {
097: Collection collection = new HashSet();
098: collection.addAll(Arrays.asList(getFullElements()));
099: return collection;
100: }
101:
102: /**
103: * Override testUnsupportedRemove, since the default impl expects removeAll,
104: * retainAll and iterator().remove to throw
105: */
106: public void testUnsupportedRemove() {
107: resetFull();
108: try {
109: collection.remove(null);
110: fail("remove should raise UnsupportedOperationException");
111: } catch (UnsupportedOperationException e) {
112: // expected
113: }
114: verify();
115: }
116:
117: //--------------------------------------------------------------------------
118:
119: protected CompositeCollection c;
120: protected Collection one;
121: protected Collection two;
122:
123: protected void setUpTest() {
124: c = new CompositeCollection();
125: one = new HashSet();
126: two = new HashSet();
127: }
128:
129: protected void setUpMutatorTest() {
130: setUpTest();
131: c.setMutator(new CompositeCollection.CollectionMutator() {
132: public boolean add(CompositeCollection composite,
133: Collection[] collections, Object obj) {
134: for (int i = 0; i < collections.length; i++) {
135: collections[i].add(obj);
136: }
137: return true;
138: }
139:
140: public boolean addAll(CompositeCollection composite,
141: Collection[] collections, Collection coll) {
142: for (int i = 0; i < collections.length; i++) {
143: collections[i].addAll(coll);
144: }
145: return true;
146: }
147:
148: public boolean remove(CompositeCollection composite,
149: Collection[] collections, Object obj) {
150: for (int i = 0; i < collections.length; i++) {
151: collections[i].remove(obj);
152: }
153: return true;
154: }
155: });
156: }
157:
158: public void testSize() {
159: setUpTest();
160: HashSet set = new HashSet();
161: set.add("a");
162: set.add("b");
163: c.addComposited(set);
164: assertEquals(set.size(), c.size());
165: }
166:
167: public void testMultipleCollectionsSize() {
168: setUpTest();
169: HashSet set = new HashSet();
170: set.add("a");
171: set.add("b");
172: c.addComposited(set);
173: HashSet other = new HashSet();
174: other.add("c");
175: c.addComposited(other);
176: assertEquals(set.size() + other.size(), c.size());
177: }
178:
179: public void testIsEmpty() {
180: setUpTest();
181: assertTrue(c.isEmpty());
182: HashSet empty = new HashSet();
183: c.addComposited(empty);
184: assertTrue(c.isEmpty());
185: empty.add("a");
186: assertTrue(!c.isEmpty());
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: }
|