1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.collections;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.SortedMap;
30  import java.util.TreeMap;
31  import java.util.Vector;
32  
33  import junit.framework.Test;
34  import junit.framework.TestCase;
35  import junit.framework.TestSuite;
36  
37  import org.apache.commons.collections.bag.HashBag;
38  import org.apache.commons.collections.buffer.BoundedFifoBuffer;
39  import org.apache.commons.collections.collection.AbstractTestCollection;
40  import org.apache.commons.collections.collection.PredicatedCollection;
41  import org.apache.commons.collections.collection.SynchronizedCollection;
42  import org.apache.commons.collections.collection.TransformedCollection;
43  import org.apache.commons.collections.collection.UnmodifiableCollection;
44  
45  /**
46   * Tests for CollectionUtils.
47   * 
48   * @author Rodney Waldhoff
49   * @author Matthew Hawthorne
50   * @author Stephen Colebourne
51   * @author Phil Steitz
52   * @author Steven Melzer
53   * @author Neil O'Toole
54   * @author Stephen Smith
55   * 
56   * @version $Revision: 646780 $ $Date: 2008-04-10 13:48:07 +0100 (Thu, 10 Apr 2008) $
57   */
58  public class TestCollectionUtils extends TestCase {
59      
60      public TestCollectionUtils(String testName) {
61          super(testName);
62      }
63  
64      public static Test suite() {
65          return new TestSuite(TestCollectionUtils.class);
66      }
67  
68      public static void main(String args[]) {
69          String[] testCaseName = { TestCollectionUtils.class.getName() };
70          junit.textui.TestRunner.main(testCaseName);
71      }
72  
73      private Collection collectionA = null;
74      private Collection collectionB = null;
75  
76      public void setUp() {
77          collectionA = new ArrayList();
78          collectionA.add("a");
79          collectionA.add("b");
80          collectionA.add("b");
81          collectionA.add("c");
82          collectionA.add("c");
83          collectionA.add("c");
84          collectionA.add("d");
85          collectionA.add("d");
86          collectionA.add("d");
87          collectionA.add("d");
88          collectionB = new LinkedList();
89          collectionB.add("e");
90          collectionB.add("d");
91          collectionB.add("d");
92          collectionB.add("c");
93          collectionB.add("c");
94          collectionB.add("c");
95          collectionB.add("b");
96          collectionB.add("b");
97          collectionB.add("b");
98          collectionB.add("b");
99  
100     }
101 
102     public void testGetCardinalityMap() {
103         Map freq = CollectionUtils.getCardinalityMap(collectionA);
104         assertEquals(new Integer(1),freq.get("a"));
105         assertEquals(new Integer(2),freq.get("b"));
106         assertEquals(new Integer(3),freq.get("c"));
107         assertEquals(new Integer(4),freq.get("d"));
108         assertNull(freq.get("e"));
109 
110         freq = CollectionUtils.getCardinalityMap(collectionB);
111         assertNull(freq.get("a"));
112         assertEquals(new Integer(4),freq.get("b"));
113         assertEquals(new Integer(3),freq.get("c"));
114         assertEquals(new Integer(2),freq.get("d"));
115         assertEquals(new Integer(1),freq.get("e"));
116     }
117 
118     public void testCardinality() {
119         assertEquals(1, CollectionUtils.cardinality("a", collectionA));
120         assertEquals(2, CollectionUtils.cardinality("b", collectionA));
121         assertEquals(3, CollectionUtils.cardinality("c", collectionA));
122         assertEquals(4, CollectionUtils.cardinality("d", collectionA));
123         assertEquals(0, CollectionUtils.cardinality("e", collectionA));
124 
125         assertEquals(0, CollectionUtils.cardinality("a", collectionB));
126         assertEquals(4, CollectionUtils.cardinality("b", collectionB));
127         assertEquals(3, CollectionUtils.cardinality("c", collectionB));
128         assertEquals(2, CollectionUtils.cardinality("d", collectionB));
129         assertEquals(1, CollectionUtils.cardinality("e", collectionB));
130 
131         Set set = new HashSet();
132         set.add("A");
133         set.add("C");
134         set.add("E");
135         set.add("E");
136         assertEquals(1, CollectionUtils.cardinality("A", set));
137         assertEquals(0, CollectionUtils.cardinality("B", set));
138         assertEquals(1, CollectionUtils.cardinality("C", set));
139         assertEquals(0, CollectionUtils.cardinality("D", set));
140         assertEquals(1, CollectionUtils.cardinality("E", set));
141 
142         Bag bag = new HashBag();
143         bag.add("A", 3);
144         bag.add("C");
145         bag.add("E");
146         bag.add("E");
147         assertEquals(3, CollectionUtils.cardinality("A", bag));
148         assertEquals(0, CollectionUtils.cardinality("B", bag));
149         assertEquals(1, CollectionUtils.cardinality("C", bag));
150         assertEquals(0, CollectionUtils.cardinality("D", bag));
151         assertEquals(2, CollectionUtils.cardinality("E", bag));
152     }
153     
154     public void testCardinalityOfNull() {
155         List list = new ArrayList();
156         assertEquals(0,CollectionUtils.cardinality(null,list));
157         {
158             Map freq = CollectionUtils.getCardinalityMap(list);
159             assertNull(freq.get(null));
160         }
161         list.add("A");
162         assertEquals(0,CollectionUtils.cardinality(null,list));
163         {
164             Map freq = CollectionUtils.getCardinalityMap(list);
165             assertNull(freq.get(null));
166         }
167         list.add(null);
168         assertEquals(1,CollectionUtils.cardinality(null,list));
169         {
170             Map freq = CollectionUtils.getCardinalityMap(list);
171             assertEquals(new Integer(1),freq.get(null));
172         }
173         list.add("B");
174         assertEquals(1,CollectionUtils.cardinality(null,list));
175         {
176             Map freq = CollectionUtils.getCardinalityMap(list);
177             assertEquals(new Integer(1),freq.get(null));
178         }
179         list.add(null);
180         assertEquals(2,CollectionUtils.cardinality(null,list));
181         {
182             Map freq = CollectionUtils.getCardinalityMap(list);
183             assertEquals(new Integer(2),freq.get(null));
184         }
185         list.add("B");
186         assertEquals(2,CollectionUtils.cardinality(null,list));
187         {
188             Map freq = CollectionUtils.getCardinalityMap(list);
189             assertEquals(new Integer(2),freq.get(null));
190         }
191         list.add(null);
192         assertEquals(3,CollectionUtils.cardinality(null,list));
193         {
194             Map freq = CollectionUtils.getCardinalityMap(list);
195             assertEquals(new Integer(3),freq.get(null));
196         }
197     }
198 
199     public void testContainsAny() {
200         Collection empty = new ArrayList(0);
201         Collection one = new ArrayList(1);
202         one.add("1");
203         Collection two = new ArrayList(1);
204         two.add("2");
205         Collection three = new ArrayList(1);
206         three.add("3");
207         Collection odds = new ArrayList(2);
208         odds.add("1");
209         odds.add("3");
210         
211         assertTrue("containsAny({1},{1,3}) should return true.",
212             CollectionUtils.containsAny(one,odds));
213         assertTrue("containsAny({1,3},{1}) should return true.",
214             CollectionUtils.containsAny(odds,one));
215         assertTrue("containsAny({3},{1,3}) should return true.",
216             CollectionUtils.containsAny(three,odds));
217         assertTrue("containsAny({1,3},{3}) should return true.",
218             CollectionUtils.containsAny(odds,three));
219         assertTrue("containsAny({2},{2}) should return true.",
220             CollectionUtils.containsAny(two,two));
221         assertTrue("containsAny({1,3},{1,3}) should return true.",
222             CollectionUtils.containsAny(odds,odds));
223         
224         assertTrue("containsAny({2},{1,3}) should return false.",
225             !CollectionUtils.containsAny(two,odds));
226         assertTrue("containsAny({1,3},{2}) should return false.",
227             !CollectionUtils.containsAny(odds,two));
228         assertTrue("containsAny({1},{3}) should return false.",
229             !CollectionUtils.containsAny(one,three));
230         assertTrue("containsAny({3},{1}) should return false.",
231             !CollectionUtils.containsAny(three,one));
232         assertTrue("containsAny({1,3},{}) should return false.",
233             !CollectionUtils.containsAny(odds,empty));
234         assertTrue("containsAny({},{1,3}) should return false.",
235             !CollectionUtils.containsAny(empty,odds));
236         assertTrue("containsAny({},{}) should return false.",
237             !CollectionUtils.containsAny(empty,empty));
238     }
239 
240     public void testUnion() {
241         Collection col = CollectionUtils.union(collectionA,collectionB);
242         Map freq = CollectionUtils.getCardinalityMap(col);
243         assertEquals(new Integer(1),freq.get("a"));
244         assertEquals(new Integer(4),freq.get("b"));
245         assertEquals(new Integer(3),freq.get("c"));
246         assertEquals(new Integer(4),freq.get("d"));
247         assertEquals(new Integer(1),freq.get("e"));
248 
249         Collection col2 = CollectionUtils.union(collectionB,collectionA);
250         Map freq2 = CollectionUtils.getCardinalityMap(col2);
251         assertEquals(new Integer(1),freq2.get("a"));
252         assertEquals(new Integer(4),freq2.get("b"));
253         assertEquals(new Integer(3),freq2.get("c"));
254         assertEquals(new Integer(4),freq2.get("d"));
255         assertEquals(new Integer(1),freq2.get("e"));        
256     }
257 
258     public void testIntersection() {
259         Collection col = CollectionUtils.intersection(collectionA,collectionB);
260         Map freq = CollectionUtils.getCardinalityMap(col);
261         assertNull(freq.get("a"));
262         assertEquals(new Integer(2),freq.get("b"));
263         assertEquals(new Integer(3),freq.get("c"));
264         assertEquals(new Integer(2),freq.get("d"));
265         assertNull(freq.get("e"));
266 
267         Collection col2 = CollectionUtils.intersection(collectionB,collectionA);
268         Map freq2 = CollectionUtils.getCardinalityMap(col2);
269         assertNull(freq2.get("a"));
270         assertEquals(new Integer(2),freq2.get("b"));
271         assertEquals(new Integer(3),freq2.get("c"));
272         assertEquals(new Integer(2),freq2.get("d"));
273         assertNull(freq2.get("e"));      
274     }
275 
276     public void testDisjunction() {
277         Collection col = CollectionUtils.disjunction(collectionA,collectionB);
278         Map freq = CollectionUtils.getCardinalityMap(col);
279         assertEquals(new Integer(1),freq.get("a"));
280         assertEquals(new Integer(2),freq.get("b"));
281         assertNull(freq.get("c"));
282         assertEquals(new Integer(2),freq.get("d"));
283         assertEquals(new Integer(1),freq.get("e"));
284 
285         Collection col2 = CollectionUtils.disjunction(collectionB,collectionA);
286         Map freq2 = CollectionUtils.getCardinalityMap(col2);
287         assertEquals(new Integer(1),freq2.get("a"));
288         assertEquals(new Integer(2),freq2.get("b"));
289         assertNull(freq2.get("c"));
290         assertEquals(new Integer(2),freq2.get("d"));
291         assertEquals(new Integer(1),freq2.get("e"));
292     }
293 
294     public void testDisjunctionAsUnionMinusIntersection() {
295         Collection dis = CollectionUtils.disjunction(collectionA,collectionB);
296         Collection un = CollectionUtils.union(collectionA,collectionB);
297         Collection inter = CollectionUtils.intersection(collectionA,collectionB);
298         assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.subtract(un,inter)));
299     }
300 
301     public void testDisjunctionAsSymmetricDifference() {
302         Collection dis = CollectionUtils.disjunction(collectionA,collectionB);
303         Collection amb = CollectionUtils.subtract(collectionA,collectionB);
304         Collection bma = CollectionUtils.subtract(collectionB,collectionA);
305         assertTrue(CollectionUtils.isEqualCollection(dis,CollectionUtils.union(amb,bma)));
306     }
307 
308     public void testSubtract() {
309         Collection col = CollectionUtils.subtract(collectionA,collectionB);
310         Map freq = CollectionUtils.getCardinalityMap(col);
311         assertEquals(new Integer(1),freq.get("a"));
312         assertNull(freq.get("b"));
313         assertNull(freq.get("c"));
314         assertEquals(new Integer(2),freq.get("d"));
315         assertNull(freq.get("e"));
316 
317         Collection col2 = CollectionUtils.subtract(collectionB,collectionA);
318         Map freq2 = CollectionUtils.getCardinalityMap(col2);
319         assertEquals(new Integer(1),freq2.get("e"));
320         assertNull(freq2.get("d"));
321         assertNull(freq2.get("c"));
322         assertEquals(new Integer(2),freq2.get("b"));
323         assertNull(freq2.get("a"));
324     }
325 
326     public void testIsSubCollectionOfSelf() {
327         assertTrue(CollectionUtils.isSubCollection(collectionA,collectionA));
328         assertTrue(CollectionUtils.isSubCollection(collectionB,collectionB));
329     }
330 
331     public void testIsSubCollection() {
332         assertTrue(!CollectionUtils.isSubCollection(collectionA,collectionB));
333         assertTrue(!CollectionUtils.isSubCollection(collectionB,collectionA));
334     }
335 
336     public void testIsSubCollection2() {
337         Collection c = new ArrayList();
338         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
339         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
340         c.add("a");
341         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
342         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
343         c.add("b");
344         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
345         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
346         c.add("b");
347         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
348         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
349         c.add("c");
350         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
351         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
352         c.add("c");
353         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
354         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
355         c.add("c");
356         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
357         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
358         c.add("d");
359         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
360         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
361         c.add("d");
362         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
363         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
364         c.add("d");
365         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
366         assertTrue(!CollectionUtils.isSubCollection(collectionA,c));
367         c.add("d");
368         assertTrue(CollectionUtils.isSubCollection(c,collectionA));
369         assertTrue(CollectionUtils.isSubCollection(collectionA,c));
370         c.add("e");
371         assertTrue(!CollectionUtils.isSubCollection(c,collectionA));
372         assertTrue(CollectionUtils.isSubCollection(collectionA,c));
373     }
374 
375     public void testIsEqualCollectionToSelf() {
376         assertTrue(CollectionUtils.isEqualCollection(collectionA,collectionA));
377         assertTrue(CollectionUtils.isEqualCollection(collectionB,collectionB));
378     }
379 
380     public void testIsEqualCollection() {
381         assertTrue(!CollectionUtils.isEqualCollection(collectionA,collectionB));
382         assertTrue(!CollectionUtils.isEqualCollection(collectionB,collectionA));
383     }
384 
385     public void testIsEqualCollection2() {
386         Collection a = new ArrayList();
387         Collection b = new ArrayList();
388         assertTrue(CollectionUtils.isEqualCollection(a,b));
389         assertTrue(CollectionUtils.isEqualCollection(b,a));
390         a.add("1");
391         assertTrue(!CollectionUtils.isEqualCollection(a,b));
392         assertTrue(!CollectionUtils.isEqualCollection(b,a));
393         b.add("1");
394         assertTrue(CollectionUtils.isEqualCollection(a,b));
395         assertTrue(CollectionUtils.isEqualCollection(b,a));
396         a.add("2");
397         assertTrue(!CollectionUtils.isEqualCollection(a,b));
398         assertTrue(!CollectionUtils.isEqualCollection(b,a));
399         b.add("2");
400         assertTrue(CollectionUtils.isEqualCollection(a,b));
401         assertTrue(CollectionUtils.isEqualCollection(b,a));
402         a.add("1");
403         assertTrue(!CollectionUtils.isEqualCollection(a,b));
404         assertTrue(!CollectionUtils.isEqualCollection(b,a));
405         b.add("1");
406         assertTrue(CollectionUtils.isEqualCollection(a,b));
407         assertTrue(CollectionUtils.isEqualCollection(b,a));
408     }
409     
410     public void testIsProperSubCollection() {
411         Collection a = new ArrayList();
412         Collection b = new ArrayList();
413         assertTrue(!CollectionUtils.isProperSubCollection(a,b));
414         b.add("1");
415         assertTrue(CollectionUtils.isProperSubCollection(a,b));
416         assertTrue(!CollectionUtils.isProperSubCollection(b,a));
417         assertTrue(!CollectionUtils.isProperSubCollection(b,b));
418         assertTrue(!CollectionUtils.isProperSubCollection(a,a));
419         a.add("1");
420         a.add("2");
421         b.add("2");
422         assertTrue(!CollectionUtils.isProperSubCollection(b,a));
423         assertTrue(!CollectionUtils.isProperSubCollection(a,b));
424         a.add("1");
425         assertTrue(CollectionUtils.isProperSubCollection(b,a));
426         assertTrue(CollectionUtils.isProperSubCollection(
427             CollectionUtils.intersection(collectionA, collectionB), collectionA));
428         assertTrue(CollectionUtils.isProperSubCollection(
429             CollectionUtils.subtract(a, b), a));
430         assertTrue(!CollectionUtils.isProperSubCollection(
431             a, CollectionUtils.subtract(a, b)));
432     }
433     
434     public void testFind() {
435         Predicate testPredicate = PredicateUtils.equalPredicate("d");
436         Object test = CollectionUtils.find(collectionA, testPredicate);
437         assertTrue(test.equals("d"));
438         testPredicate = PredicateUtils.equalPredicate("de");
439         test = CollectionUtils.find(collectionA, testPredicate);
440         assertTrue(test == null);
441         assertEquals(CollectionUtils.find(null,testPredicate), null);
442         assertEquals(CollectionUtils.find(collectionA, null), null);
443     }
444     
445     public void testForAllDo() {
446         Closure testClosure = ClosureUtils.invokerClosure("clear");
447         Collection col = new ArrayList();
448         col.add(collectionA);
449         col.add(collectionB);
450         CollectionUtils.forAllDo(col, testClosure);
451         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
452         CollectionUtils.forAllDo(col, null);
453         assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
454         CollectionUtils.forAllDo(null, testClosure);
455         col.add(null);
456         // null should be OK
457         CollectionUtils.forAllDo(col, testClosure);
458         col.add("x");
459         // This will lead to FunctorException
460         try {
461             CollectionUtils.forAllDo(col, testClosure);
462             fail("Expecting FunctorException");
463         } catch (FunctorException ex) {
464             // expected from invoker -- method not found
465         }
466     }
467 
468     public void testIndex() {     
469         // normal map behavior when index is an Integer and a key
470         Map map = new HashMap();
471         map.put(new Integer(0), "zero");
472         map.put(new Integer(-1), "minusOne");
473         Object test = CollectionUtils.index(map, 0);
474         assertTrue(test.equals("zero"));
475         test = CollectionUtils.index(map, new Integer(-1));
476         assertTrue(test.equals("minusOne"));
477         
478         // map, non-integer key that does not exist -- map returned
479         test = CollectionUtils.index(map, "missing");
480         assertTrue(test.equals(map));
481         
482         // map, integer not a key, valid index -- "some" element of keyset returned
483         test = CollectionUtils.index(map, new Integer(1));   
484         assertTrue(map.keySet().contains(test)); 
485         
486         // map, integer not a key, not valid index -- "dead" keyset iterator returned
487         test = CollectionUtils.index(map, new Integer(4));         
488         assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext());  
489 
490         // sorted map, integer not a key, valid index -- ith key returned
491         SortedMap map2 = new TreeMap();
492         map2.put(new Integer(23), "u");
493         map2.put(new Integer(21), "x");
494         map2.put(new Integer(17), "v");
495         map2.put(new Integer(42), "w");
496         Integer val = (Integer) CollectionUtils.index(map2, 0);
497         assertTrue(val.intValue() == 17);
498         val = (Integer) CollectionUtils.index(map2, 1);
499         assertTrue(val.intValue() == 21);
500         val = (Integer) CollectionUtils.index(map2, 2);
501         assertTrue(val.intValue() == 23);
502         val = (Integer) CollectionUtils.index(map2, 3);
503         assertTrue(val.intValue() == 42);   
504                 
505         // list, entry exists
506         List list = new ArrayList();
507         list.add("zero");
508         list.add("one");
509         test = CollectionUtils.index(list, 0);
510         assertTrue(test.equals("zero"));
511         test = CollectionUtils.index(list, 1);
512         assertTrue(test.equals("one"));
513         
514         // list, non-existent entry -- IndexOutOfBoundsException
515         try {
516             test = CollectionUtils.index(list, 2);
517             fail("Expecting IndexOutOfBoundsException");
518         } catch (IndexOutOfBoundsException e) {
519             // expected
520         }
521         
522         // iterator, entry exists
523         Iterator iterator = list.iterator();
524         test = CollectionUtils.index(iterator,0);
525         assertTrue(test.equals("zero"));
526         iterator = list.iterator();
527         test = CollectionUtils.index(iterator,1);
528         assertTrue(test.equals("one"));
529         
530         // iterator, non-existent entry -- "dead" iterator returned
531         test = CollectionUtils.index(iterator,3);
532         assertTrue(test.equals(iterator) && !iterator.hasNext());
533         
534         // Enumeration, entry exists
535         Vector vector = new Vector(list);
536         Enumeration en = vector.elements();
537         test = CollectionUtils.index(en,0);
538         assertTrue(test.equals("zero"));
539         en = vector.elements();
540         test = CollectionUtils.index(en,1);
541         assertTrue(test.equals("one"));
542         
543         // Enumeration, non-existent entry -- "dead" enumerator returned
544         test = CollectionUtils.index(en,3);
545         assertTrue(test.equals(en) && !en.hasMoreElements());
546         
547         // Collection, entry exists
548         Bag bag = new HashBag();
549         bag.add("element", 1);
550         test = CollectionUtils.index(bag, 0);
551         assertTrue(test.equals("element"));
552         
553         // Collection, non-existent entry -- "dead" iterator returned
554         test = CollectionUtils.index(bag, 2);
555         assertTrue((test instanceof Iterator) && !((Iterator) test).hasNext()); 
556         
557         // Object array, entry exists
558         Object[] objArray = new Object[2];
559         objArray[0] = "zero";
560         objArray[1] = "one";
561         test = CollectionUtils.index(objArray,0);
562         assertTrue(test.equals("zero"));
563         test = CollectionUtils.index(objArray,1);
564         assertTrue(test.equals("one"));
565         
566         // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
567         try {
568             test = CollectionUtils.index(objArray,2);
569             fail("Expecting ArrayIndexOutOfBoundsException.");
570         } catch (ArrayIndexOutOfBoundsException ex) {
571             // expected
572         }
573         
574         // Non-collection object -- returned unchanged
575         Object obj = new Object();
576         test = CollectionUtils.index(obj, obj);
577         assertTrue(test.equals(obj));
578     }
579     
580     public void testGet() {     
581         {
582             // Unordered map, entries exist
583             Map expected = new HashMap();
584             expected.put("zeroKey", "zero");
585             expected.put("oneKey", "one");
586         
587             Map found = new HashMap();
588             Map.Entry entry = (Map.Entry)(CollectionUtils.get(expected, 0));
589             found.put(entry.getKey(),entry.getValue());
590             entry = (Map.Entry)(CollectionUtils.get(expected, 1));
591             found.put(entry.getKey(),entry.getValue());
592             assertEquals(expected,found);
593         
594             // Map index out of range
595             try {
596                 CollectionUtils.get(expected,  2);
597                 fail("Expecting IndexOutOfBoundsException.");
598             } catch (IndexOutOfBoundsException e) {
599                 // expected
600             }
601             try {
602                 CollectionUtils.get(expected,  -2);
603                 fail("Expecting IndexOutOfBoundsException.");
604             } catch (IndexOutOfBoundsException e) {
605                 // expected
606             }
607         }
608 
609         {
610             // Sorted map, entries exist, should respect order
611             SortedMap map = new TreeMap();
612             map.put("zeroKey", "zero");
613             map.put("oneKey", "one");
614             Object test = CollectionUtils.get(map, 1);
615             assertEquals("zeroKey",((Map.Entry) test).getKey());
616             assertEquals("zero",((Map.Entry) test).getValue());
617             test = CollectionUtils.get(map, 0);
618             assertEquals("oneKey",((Map.Entry) test).getKey());
619             assertEquals("one",((Map.Entry) test).getValue());
620         }
621                 
622         {
623             // List, entry exists
624             List list = new ArrayList();
625             list.add("zero");
626             list.add("one");
627             assertEquals("zero",CollectionUtils.get(list, 0));
628             assertEquals("one",CollectionUtils.get(list, 1));
629             // list, non-existent entry -- IndexOutOfBoundsException
630             try {
631                 CollectionUtils.get(list, 2);
632                 fail("Expecting IndexOutOfBoundsException");
633             } catch (IndexOutOfBoundsException e) {
634                 // expected
635             }
636 
637             // Iterator, entry exists
638             Iterator iterator = list.iterator();
639             assertEquals("zero",CollectionUtils.get(iterator,0));
640             iterator = list.iterator();
641             assertEquals("one",CollectionUtils.get(iterator,1));
642         
643             // Iterator, non-existent entry 
644             try {
645                 CollectionUtils.get(iterator,3);
646                 fail("Expecting IndexOutOfBoundsException.");
647             } catch (IndexOutOfBoundsException e) {
648                 // expected
649             }
650             assertTrue(!iterator.hasNext());
651         }
652         
653         {
654             // Enumeration, entry exists
655             Vector vector = new Vector();
656             vector.addElement("zero");
657             vector.addElement("one");
658             Enumeration en = vector.elements();
659             assertEquals("zero",CollectionUtils.get(en,0));
660             en = vector.elements();
661             assertEquals("one",CollectionUtils.get(en,1));
662         
663             // Enumerator, non-existent entry 
664             try {
665                 CollectionUtils.get(en,3);
666                 fail("Expecting IndexOutOfBoundsException.");
667             } catch (IndexOutOfBoundsException e) {
668                 // expected
669             }
670             assertTrue(!en.hasMoreElements());
671         }
672         
673         {
674             // Collection, entry exists
675             Bag bag = new HashBag();
676             bag.add("element", 1);
677             assertEquals("element",CollectionUtils.get(bag, 0));
678         
679             // Collection, non-existent entry
680             try {
681                 CollectionUtils.get(bag, 1);
682                 fail("Expceting IndexOutOfBoundsException.");
683             } catch (IndexOutOfBoundsException e) {
684                 // expected
685             }
686         }
687         
688         {
689             // Object array, entry exists
690             Object[] objArray = new Object[2];
691             objArray[0] = "zero";
692             objArray[1] = "one";
693             assertEquals("zero",CollectionUtils.get(objArray,0));
694             assertEquals("one",CollectionUtils.get(objArray,1));
695         
696             // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
697             try {
698                 CollectionUtils.get(objArray,2);
699                 fail("Expecting IndexOutOfBoundsException.");
700             } catch (IndexOutOfBoundsException ex) {
701                 // expected
702             }
703         }
704         
705         {
706             // Primitive array, entry exists
707             int[] array = new int[2];
708             array[0] = 10;
709             array[1] = 20;
710             assertEquals(new Integer(10), CollectionUtils.get(array,0));
711             assertEquals(new Integer(20), CollectionUtils.get(array,1));
712         
713             // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
714             try {
715                 CollectionUtils.get(array,2);
716                 fail("Expecting IndexOutOfBoundsException.");
717             } catch (IndexOutOfBoundsException ex) {
718                 // expected
719             }
720         }
721         
722         {
723             // Invalid object
724             Object obj = new Object();
725             try {
726                 CollectionUtils.get(obj, 0);
727                 fail("Expecting IllegalArgumentException.");
728             } catch (IllegalArgumentException e) {
729                 // expected
730             }
731             try {
732                 CollectionUtils.get(null, 0);
733                 fail("Expecting IllegalArgumentException.");
734             } catch (IllegalArgumentException e) {
735                 // expected
736             }
737         }
738     }
739 
740     //-----------------------------------------------------------------------
741     public void testSize_List() {
742         List list = new ArrayList();
743         assertEquals(0, CollectionUtils.size(list));
744         list.add("a");
745         assertEquals(1, CollectionUtils.size(list));
746         list.add("b");
747         assertEquals(2, CollectionUtils.size(list));
748     }
749     public void testSize_Map() {
750         Map map = new HashMap();
751         assertEquals(0, CollectionUtils.size(map));
752         map.put("1", "a");
753         assertEquals(1, CollectionUtils.size(map));
754         map.put("2", "b");
755         assertEquals(2, CollectionUtils.size(map));
756     }
757     public void testSize_Array() {
758         Object[] objectArray = new Object[0];
759         assertEquals(0, CollectionUtils.size(objectArray));
760         
761         String[] stringArray = new String[3];
762         assertEquals(3, CollectionUtils.size(stringArray));
763         stringArray[0] = "a";
764         stringArray[1] = "b";
765         stringArray[2] = "c";
766         assertEquals(3, CollectionUtils.size(stringArray));
767     }
768     public void testSize_PrimitiveArray() {
769         int[] intArray = new int[0];
770         assertEquals(0, CollectionUtils.size(intArray));
771         
772         double[] doubleArray = new double[3];
773         assertEquals(3, CollectionUtils.size(doubleArray));
774         doubleArray[0] = 0.0d;
775         doubleArray[1] = 1.0d;
776         doubleArray[2] = 2.5d;
777         assertEquals(3, CollectionUtils.size(doubleArray));
778     }
779     public void testSize_Enumeration() {
780         Vector list = new Vector();
781         assertEquals(0, CollectionUtils.size(list.elements()));
782         list.add("a");
783         assertEquals(1, CollectionUtils.size(list.elements()));
784         list.add("b");
785         assertEquals(2, CollectionUtils.size(list.elements()));
786     }
787     public void testSize_Iterator() {
788         List list = new ArrayList();
789         assertEquals(0, CollectionUtils.size(list.iterator()));
790         list.add("a");
791         assertEquals(1, CollectionUtils.size(list.iterator()));
792         list.add("b");
793         assertEquals(2, CollectionUtils.size(list.iterator()));
794     }
795     public void testSize_Other() {
796         try {
797             CollectionUtils.size(null);
798             fail("Expecting IllegalArgumentException");
799         } catch (IllegalArgumentException e) {}
800         try {
801             CollectionUtils.size("not a list");
802             fail("Expecting IllegalArgumentException");
803         } catch (IllegalArgumentException e) {}
804     }
805 
806     //-----------------------------------------------------------------------
807     public void testSizeIsEmpty_List() {
808         List list = new ArrayList();
809         assertEquals(true, CollectionUtils.sizeIsEmpty(list));
810         list.add("a");
811         assertEquals(false, CollectionUtils.sizeIsEmpty(list));
812     }
813     public void testSizeIsEmpty_Map() {
814         Map map = new HashMap();
815         assertEquals(true, CollectionUtils.sizeIsEmpty(map));
816         map.put("1", "a");
817         assertEquals(false, CollectionUtils.sizeIsEmpty(map));
818     }
819     public void testSizeIsEmpty_Array() {
820         Object[] objectArray = new Object[0];
821         assertEquals(true, CollectionUtils.sizeIsEmpty(objectArray));
822         
823         String[] stringArray = new String[3];
824         assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
825         stringArray[0] = "a";
826         stringArray[1] = "b";
827         stringArray[2] = "c";
828         assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
829     }
830     public void testSizeIsEmpty_PrimitiveArray() {
831         int[] intArray = new int[0];
832         assertEquals(true, CollectionUtils.sizeIsEmpty(intArray));
833         
834         double[] doubleArray = new double[3];
835         assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
836         doubleArray[0] = 0.0d;
837         doubleArray[1] = 1.0d;
838         doubleArray[2] = 2.5d;
839         assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
840     }
841     public void testSizeIsEmpty_Enumeration() {
842         Vector list = new Vector();
843         assertEquals(true, CollectionUtils.sizeIsEmpty(list.elements()));
844         list.add("a");
845         assertEquals(false, CollectionUtils.sizeIsEmpty(list.elements()));
846         Enumeration en = list.elements();
847         en.nextElement();
848         assertEquals(true, CollectionUtils.sizeIsEmpty(en));
849     }
850     public void testSizeIsEmpty_Iterator() {
851         List list = new ArrayList();
852         assertEquals(true, CollectionUtils.sizeIsEmpty(list.iterator()));
853         list.add("a");
854         assertEquals(false, CollectionUtils.sizeIsEmpty(list.iterator()));
855         Iterator it = list.iterator();
856         it.next();
857         assertEquals(true, CollectionUtils.sizeIsEmpty(it));
858     }
859     public void testSizeIsEmpty_Other() {
860         try {
861             CollectionUtils.sizeIsEmpty(null);
862             fail("Expecting IllegalArgumentException");
863         } catch (IllegalArgumentException ex) {}
864         try {
865             CollectionUtils.sizeIsEmpty("not a list");
866             fail("Expecting IllegalArgumentException");
867         } catch (IllegalArgumentException ex) {}
868     }
869 
870     //-----------------------------------------------------------------------
871     public void testIsEmptyWithEmptyCollection() {
872         Collection coll = new ArrayList();
873         assertEquals(true, CollectionUtils.isEmpty(coll));
874     }
875 
876     public void testIsEmptyWithNonEmptyCollection() {
877         Collection coll = new ArrayList();
878         coll.add("item");
879         assertEquals(false, CollectionUtils.isEmpty(coll));
880     }
881 
882     public void testIsEmptyWithNull() {
883         Collection coll = null;
884         assertEquals(true, CollectionUtils.isEmpty(coll));
885     }
886 
887     public void testIsNotEmptyWithEmptyCollection() {
888         Collection coll = new ArrayList();
889         assertEquals(false, CollectionUtils.isNotEmpty(coll));
890     }
891 
892     public void testIsNotEmptyWithNonEmptyCollection() {
893         Collection coll = new ArrayList();
894         coll.add("item");
895         assertEquals(true, CollectionUtils.isNotEmpty(coll));
896     }
897 
898     public void testIsNotEmptyWithNull() {
899         Collection coll = null;
900         assertEquals(false, CollectionUtils.isNotEmpty(coll));
901     }
902 
903     //-----------------------------------------------------------------------
904     private static Predicate EQUALS_TWO = new Predicate() {
905         public boolean evaluate(Object input) {
906             return (input.equals("Two"));
907         }
908     };
909     
910     public void testFilter() {
911         List list = new ArrayList();
912         list.add("One");
913         list.add("Two");
914         list.add("Three");
915         list.add("Four");
916         CollectionUtils.filter(list, EQUALS_TWO);
917         assertEquals(1, list.size());
918         assertEquals("Two", list.get(0));
919         
920         list = new ArrayList();
921         list.add("One");
922         list.add("Two");
923         list.add("Three");
924         list.add("Four");
925         CollectionUtils.filter(list, null);
926         assertEquals(4, list.size());
927         CollectionUtils.filter(null, EQUALS_TWO);
928         assertEquals(4, list.size());
929         CollectionUtils.filter(null, null);
930         assertEquals(4, list.size());
931     }
932 
933     public void testCountMatches() {
934         List list = new ArrayList();
935         list.add("One");
936         list.add("Two");
937         list.add("Three");
938         list.add("Four");
939         int count = CollectionUtils.countMatches(list, EQUALS_TWO);
940         assertEquals(4, list.size());
941         assertEquals(1, count);
942         assertEquals(0, CollectionUtils.countMatches(list, null));
943         assertEquals(0, CollectionUtils.countMatches(null, EQUALS_TWO));
944         assertEquals(0, CollectionUtils.countMatches(null, null));
945     }
946 
947     public void testExists() {
948         List list = new ArrayList();
949         assertEquals(false, CollectionUtils.exists(null, null));
950         assertEquals(false, CollectionUtils.exists(list, null));
951         assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO));
952         assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
953         list.add("One");
954         list.add("Three");
955         list.add("Four");
956         assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
957 
958         list.add("Two");
959         assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO));
960     }
961     
962     public void testSelect() {
963         List list = new ArrayList();
964         list.add("One");
965         list.add("Two");
966         list.add("Three");
967         list.add("Four");
968         Collection output = CollectionUtils.select(list, EQUALS_TWO);
969         assertEquals(4, list.size());
970         assertEquals(1, output.size());
971         assertEquals("Two", output.iterator().next());
972     }
973 
974     public void testSelectRejected() {
975         List list = new ArrayList();
976         list.add("One");
977         list.add("Two");
978         list.add("Three");
979         list.add("Four");
980         Collection output = CollectionUtils.selectRejected(list, EQUALS_TWO);
981         assertEquals(4, list.size());
982         assertEquals(3, output.size());
983         assertTrue(output.contains("One"));
984         assertTrue(output.contains("Three"));
985         assertTrue(output.contains("Four"));
986     }
987     
988     public void testCollect() {
989         Transformer transformer = TransformerUtils.constantTransformer("z");
990         Collection collection = CollectionUtils.collect(collectionA, transformer);
991         assertTrue(collection.size() == collectionA.size());
992         assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
993         assertTrue(collection.contains("z") && !collection.contains("a"));
994         
995         collection = new ArrayList();
996         CollectionUtils.collect(collectionA, transformer, collection);
997         assertTrue(collection.size() == collectionA.size());
998         assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
999         assertTrue(collection.contains("z") && !collection.contains("a"));
1000         
1001         Iterator iterator = null;
1002         collection = new ArrayList();
1003         CollectionUtils.collect(iterator, transformer, collection);
1004         
1005         iterator = collectionA.iterator();
1006         CollectionUtils.collect(iterator, transformer, collection);
1007         assertTrue(collection.size() == collectionA.size());
1008         assertTrue(collectionA.contains("a") && ! collectionA.contains("z"));
1009         assertTrue(collection.contains("z") && !collection.contains("a")); 
1010         
1011         iterator = collectionA.iterator();
1012         collection = CollectionUtils.collect(iterator, transformer);
1013         assertTrue(collection.size() == collectionA.size());
1014         assertTrue(collection.contains("z") && !collection.contains("a")); 
1015         collection = CollectionUtils.collect((Iterator) null, (Transformer) null);
1016         assertTrue(collection.size() == 0);
1017            
1018         int size = collectionA.size();
1019         CollectionUtils.collect((Collection) null, transformer, collectionA);
1020         assertTrue(collectionA.size() == size && collectionA.contains("a"));
1021         CollectionUtils.collect(collectionB, null, collectionA);
1022         assertTrue(collectionA.size() == size && collectionA.contains("a"));
1023         
1024     }
1025 
1026     Transformer TRANSFORM_TO_INTEGER = new Transformer() {
1027         public Object transform(Object input) {
1028             return new Integer((String) input);
1029         }
1030     };
1031     
1032     public void testTransform1() {
1033         List list = new ArrayList();
1034         list.add("1");
1035         list.add("2");
1036         list.add("3");
1037         CollectionUtils.transform(list, TRANSFORM_TO_INTEGER);
1038         assertEquals(3, list.size());
1039         assertEquals(new Integer(1), list.get(0));
1040         assertEquals(new Integer(2), list.get(1));
1041         assertEquals(new Integer(3), list.get(2));
1042         
1043         list = new ArrayList();
1044         list.add("1");
1045         list.add("2");
1046         list.add("3");
1047         CollectionUtils.transform(null, TRANSFORM_TO_INTEGER);
1048         assertEquals(3, list.size());
1049         CollectionUtils.transform(list, null);
1050         assertEquals(3, list.size());
1051         CollectionUtils.transform(null, null);
1052         assertEquals(3, list.size());
1053     }
1054     
1055     public void testTransform2() {
1056         Set set = new HashSet();
1057         set.add("1");
1058         set.add("2");
1059         set.add("3");
1060         CollectionUtils.transform(set, new Transformer() {
1061             public Object transform(Object input) {
1062                 return new Integer(4);
1063             }
1064         });
1065         assertEquals(1, set.size());
1066         assertEquals(new Integer(4), set.iterator().next());
1067     }
1068 
1069     //-----------------------------------------------------------------------
1070     public void testAddIgnoreNull() {
1071         Set set = new HashSet();
1072         set.add("1");
1073         set.add("2");
1074         set.add("3");
1075         assertEquals(false, CollectionUtils.addIgnoreNull(set, null));
1076         assertEquals(3, set.size());
1077         assertEquals(false, CollectionUtils.addIgnoreNull(set, "1"));
1078         assertEquals(3, set.size());
1079         assertEquals(true, CollectionUtils.addIgnoreNull(set, "4"));
1080         assertEquals(4, set.size());
1081         assertEquals(true, set.contains("4"));
1082     }
1083 
1084     //-----------------------------------------------------------------------
1085     public void testPredicatedCollection() {
1086         Predicate predicate = new Predicate() {
1087             public boolean evaluate(Object o) {
1088                 return o instanceof String;
1089             }
1090         };
1091         Collection collection = 
1092             CollectionUtils.predicatedCollection(new ArrayList(), predicate);
1093         assertTrue("returned object should be a PredicatedCollection",
1094             collection instanceof PredicatedCollection);
1095         try { 
1096            collection = 
1097                 CollectionUtils.predicatedCollection(new ArrayList(), null); 
1098            fail("Expecting IllegalArgumentException for null predicate.");
1099         } catch (IllegalArgumentException ex) {
1100             // expected
1101         }
1102         try { 
1103            collection = 
1104                 CollectionUtils.predicatedCollection(null, predicate); 
1105            fail("Expecting IllegalArgumentException for null collection.");
1106         } catch (IllegalArgumentException ex) {
1107             // expected
1108         }             
1109     }
1110         
1111    
1112 
1113     public BulkTest bulkTestTypedCollection() {
1114         return new TestTypedCollection("") {
1115             public Collection typedCollection() {
1116                 return CollectionUtils.typedCollection(
1117                     new ArrayList(),
1118                     super.getType());
1119             }
1120  
1121             public BulkTest bulkTestAll() {
1122                 return new AbstractTestCollection("") {
1123                     public Collection makeCollection() {
1124                         return typedCollection();
1125                     }
1126  
1127                     public Collection makeConfirmedCollection() {
1128                         return new ArrayList();
1129                     }
1130  
1131                     public Collection makeConfirmedFullCollection() {
1132                         ArrayList list = new ArrayList();
1133                         list.addAll(java.util.Arrays.asList(getFullElements()));
1134                         return list;
1135                     }
1136  
1137                     public Object[] getFullElements() {
1138                         return getFullNonNullStringElements();
1139                     }
1140  
1141                     public Object[] getOtherElements() {
1142                         return getOtherNonNullStringElements();
1143                     }
1144  
1145                 };
1146             }
1147         };
1148     }
1149     
1150     public void testIsFull() {
1151         Set set = new HashSet();
1152         set.add("1");
1153         set.add("2");
1154         set.add("3");
1155         try {
1156             CollectionUtils.isFull(null);
1157             fail();
1158         } catch (NullPointerException ex) {}
1159         assertEquals(false, CollectionUtils.isFull(set));
1160         
1161         BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
1162         assertEquals(true, CollectionUtils.isFull(buf));
1163         buf.remove("2");
1164         assertEquals(false, CollectionUtils.isFull(buf));
1165         buf.add("2");
1166         assertEquals(true, CollectionUtils.isFull(buf));
1167         
1168         Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
1169         assertEquals(true, CollectionUtils.isFull(buf2));
1170         buf2.remove("2");
1171         assertEquals(false, CollectionUtils.isFull(buf2));
1172         buf2.add("2");
1173         assertEquals(true, CollectionUtils.isFull(buf2));
1174     }
1175 
1176     public void testMaxSize() {
1177         Set set = new HashSet();
1178         set.add("1");
1179         set.add("2");
1180         set.add("3");
1181         try {
1182             CollectionUtils.maxSize(null);
1183             fail();
1184         } catch (NullPointerException ex) {}
1185         assertEquals(-1, CollectionUtils.maxSize(set));
1186         
1187         BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
1188         assertEquals(3, CollectionUtils.maxSize(buf));
1189         buf.remove("2");
1190         assertEquals(3, CollectionUtils.maxSize(buf));
1191         buf.add("2");
1192         assertEquals(3, CollectionUtils.maxSize(buf));
1193         
1194         Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
1195         assertEquals(3, CollectionUtils.maxSize(buf2));
1196         buf2.remove("2");
1197         assertEquals(3, CollectionUtils.maxSize(buf2));
1198         buf2.add("2");
1199         assertEquals(3, CollectionUtils.maxSize(buf2));
1200     }
1201 
1202     public void testIntersectionUsesMethodEquals() {
1203         // Let elta and eltb be objects...
1204         Object elta = new Integer(17);
1205         Object eltb = new Integer(17);
1206         
1207         // ...which are equal...
1208         assertEquals(elta,eltb);
1209         assertEquals(eltb,elta);
1210         
1211         // ...but not the same (==).
1212         assertTrue(elta != eltb);
1213         
1214         // Let cola and colb be collections...
1215         Collection cola = new ArrayList();
1216         Collection colb = new ArrayList();
1217         
1218         // ...which contain elta and eltb, 
1219         // respectively.
1220         cola.add(elta);
1221         colb.add(eltb);
1222         
1223         // Then the intersection of the two
1224         // should contain one element.
1225         Collection intersection = CollectionUtils.intersection(cola,colb);
1226         assertEquals(1,intersection.size());
1227         
1228         // In practice, this element will be the same (==) as elta
1229         // or eltb, although this isn't strictly part of the
1230         // contract.
1231         Object eltc = intersection.iterator().next();
1232         assertTrue((eltc == elta  && eltc != eltb) || (eltc != elta  && eltc == eltb));
1233         
1234         // In any event, this element remains equal,
1235         // to both elta and eltb.
1236         assertEquals(elta,eltc);
1237         assertEquals(eltc,elta);
1238         assertEquals(eltb,eltc);
1239         assertEquals(eltc,eltb);
1240     }
1241     
1242      public void testTransformedCollection() {
1243         Transformer transformer = TransformerUtils.nopTransformer();
1244         Collection collection = 
1245             CollectionUtils.transformedCollection(new ArrayList(), transformer);
1246         assertTrue("returned object should be a TransformedCollection",
1247             collection instanceof TransformedCollection);
1248         try { 
1249            collection = 
1250                 CollectionUtils.transformedCollection(new ArrayList(), null); 
1251            fail("Expecting IllegalArgumentException for null transformer.");
1252         } catch (IllegalArgumentException ex) {
1253             // expected
1254         }
1255         try { 
1256            collection = 
1257                 CollectionUtils.transformedCollection(null, transformer); 
1258            fail("Expecting IllegalArgumentException for null collection.");
1259         } catch (IllegalArgumentException ex) {
1260             // expected
1261         }             
1262     }
1263 
1264     public void testTransformedCollection_2() {
1265         List list = new ArrayList();
1266         list.add("1");
1267         list.add("2");
1268         list.add("3");
1269         Collection result = CollectionUtils.transformedCollection(list, TRANSFORM_TO_INTEGER);
1270         assertEquals(true, result.contains("1"));  // untransformed
1271         assertEquals(true, result.contains("2"));  // untransformed
1272         assertEquals(true, result.contains("3"));  // untransformed
1273     }
1274 
1275     public void testSynchronizedCollection() {
1276         Collection col = CollectionUtils.synchronizedCollection(new ArrayList());
1277         assertTrue("Returned object should be a SynchronizedCollection.",
1278             col instanceof SynchronizedCollection);
1279         try {
1280             col = CollectionUtils.synchronizedCollection(null);
1281             fail("Expecting IllegalArgumentException for null collection.");
1282         } catch (IllegalArgumentException ex) {
1283             // expected
1284         }  
1285     }
1286     
1287     public void testUnmodifiableCollection() {
1288         Collection col = CollectionUtils.unmodifiableCollection(new ArrayList());
1289         assertTrue("Returned object should be a UnmodifiableCollection.",
1290             col instanceof UnmodifiableCollection);
1291         try {
1292             col = CollectionUtils.unmodifiableCollection(null);
1293             fail("Expecting IllegalArgumentException for null collection.");
1294         } catch (IllegalArgumentException ex) {
1295             // expected
1296         }  
1297     }
1298     
1299 }