Source Code Cross Referenced for TestCollectionUtils.java in  » Library » Apache-common-Collections » org » apache » commons » collections » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache common Collections » org.apache.commons.collections 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         *  Copyright 2001-2005 The Apache Software Foundation
0003:         *
0004:         *  Licensed under the Apache License, Version 2.0 (the "License");
0005:         *  you may not use this file except in compliance with the License.
0006:         *  You may obtain a copy of the License at
0007:         *
0008:         *      http://www.apache.org/licenses/LICENSE-2.0
0009:         *
0010:         *  Unless required by applicable law or agreed to in writing, software
0011:         *  distributed under the License is distributed on an "AS IS" BASIS,
0012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013:         *  See the License for the specific language governing permissions and
0014:         *  limitations under the License.
0015:         */
0016:        package org.apache.commons.collections;
0017:
0018:        import java.util.ArrayList;
0019:        import java.util.Collection;
0020:        import java.util.Enumeration;
0021:        import java.util.HashMap;
0022:        import java.util.HashSet;
0023:        import java.util.Iterator;
0024:        import java.util.LinkedList;
0025:        import java.util.List;
0026:        import java.util.Map;
0027:        import java.util.Set;
0028:        import java.util.SortedMap;
0029:        import java.util.TreeMap;
0030:        import java.util.Vector;
0031:
0032:        import junit.framework.Test;
0033:        import junit.framework.TestCase;
0034:        import junit.framework.TestSuite;
0035:
0036:        import org.apache.commons.collections.bag.HashBag;
0037:        import org.apache.commons.collections.buffer.BoundedFifoBuffer;
0038:        import org.apache.commons.collections.collection.AbstractTestCollection;
0039:        import org.apache.commons.collections.collection.PredicatedCollection;
0040:        import org.apache.commons.collections.collection.SynchronizedCollection;
0041:        import org.apache.commons.collections.collection.TransformedCollection;
0042:        import org.apache.commons.collections.collection.UnmodifiableCollection;
0043:
0044:        /**
0045:         * Tests for CollectionUtils.
0046:         * 
0047:         * @author Rodney Waldhoff
0048:         * @author Matthew Hawthorne
0049:         * @author Stephen Colebourne
0050:         * @author Phil Steitz
0051:         * @author Steven Melzer
0052:         * @author Neil O'Toole
0053:         * @author Stephen Smith
0054:         * 
0055:         * @version $Revision: 230513 $ $Date: 2005-08-06 00:07:00 +0100 (Sat, 06 Aug 2005) $
0056:         */
0057:        public class TestCollectionUtils extends TestCase {
0058:
0059:            public TestCollectionUtils(String testName) {
0060:                super (testName);
0061:            }
0062:
0063:            public static Test suite() {
0064:                return new TestSuite(TestCollectionUtils.class);
0065:            }
0066:
0067:            public static void main(String args[]) {
0068:                String[] testCaseName = { TestCollectionUtils.class.getName() };
0069:                junit.textui.TestRunner.main(testCaseName);
0070:            }
0071:
0072:            private Collection collectionA = null;
0073:            private Collection collectionB = null;
0074:
0075:            public void setUp() {
0076:                collectionA = new ArrayList();
0077:                collectionA.add("a");
0078:                collectionA.add("b");
0079:                collectionA.add("b");
0080:                collectionA.add("c");
0081:                collectionA.add("c");
0082:                collectionA.add("c");
0083:                collectionA.add("d");
0084:                collectionA.add("d");
0085:                collectionA.add("d");
0086:                collectionA.add("d");
0087:                collectionB = new LinkedList();
0088:                collectionB.add("e");
0089:                collectionB.add("d");
0090:                collectionB.add("d");
0091:                collectionB.add("c");
0092:                collectionB.add("c");
0093:                collectionB.add("c");
0094:                collectionB.add("b");
0095:                collectionB.add("b");
0096:                collectionB.add("b");
0097:                collectionB.add("b");
0098:
0099:            }
0100:
0101:            public void testGetCardinalityMap() {
0102:                Map freq = CollectionUtils.getCardinalityMap(collectionA);
0103:                assertEquals(new Integer(1), freq.get("a"));
0104:                assertEquals(new Integer(2), freq.get("b"));
0105:                assertEquals(new Integer(3), freq.get("c"));
0106:                assertEquals(new Integer(4), freq.get("d"));
0107:                assertNull(freq.get("e"));
0108:
0109:                freq = CollectionUtils.getCardinalityMap(collectionB);
0110:                assertNull(freq.get("a"));
0111:                assertEquals(new Integer(4), freq.get("b"));
0112:                assertEquals(new Integer(3), freq.get("c"));
0113:                assertEquals(new Integer(2), freq.get("d"));
0114:                assertEquals(new Integer(1), freq.get("e"));
0115:            }
0116:
0117:            public void testCardinality() {
0118:                assertEquals(1, CollectionUtils.cardinality("a", collectionA));
0119:                assertEquals(2, CollectionUtils.cardinality("b", collectionA));
0120:                assertEquals(3, CollectionUtils.cardinality("c", collectionA));
0121:                assertEquals(4, CollectionUtils.cardinality("d", collectionA));
0122:                assertEquals(0, CollectionUtils.cardinality("e", collectionA));
0123:
0124:                assertEquals(0, CollectionUtils.cardinality("a", collectionB));
0125:                assertEquals(4, CollectionUtils.cardinality("b", collectionB));
0126:                assertEquals(3, CollectionUtils.cardinality("c", collectionB));
0127:                assertEquals(2, CollectionUtils.cardinality("d", collectionB));
0128:                assertEquals(1, CollectionUtils.cardinality("e", collectionB));
0129:
0130:                Set set = new HashSet();
0131:                set.add("A");
0132:                set.add("C");
0133:                set.add("E");
0134:                set.add("E");
0135:                assertEquals(1, CollectionUtils.cardinality("A", set));
0136:                assertEquals(0, CollectionUtils.cardinality("B", set));
0137:                assertEquals(1, CollectionUtils.cardinality("C", set));
0138:                assertEquals(0, CollectionUtils.cardinality("D", set));
0139:                assertEquals(1, CollectionUtils.cardinality("E", set));
0140:
0141:                Bag bag = new HashBag();
0142:                bag.add("A", 3);
0143:                bag.add("C");
0144:                bag.add("E");
0145:                bag.add("E");
0146:                assertEquals(3, CollectionUtils.cardinality("A", bag));
0147:                assertEquals(0, CollectionUtils.cardinality("B", bag));
0148:                assertEquals(1, CollectionUtils.cardinality("C", bag));
0149:                assertEquals(0, CollectionUtils.cardinality("D", bag));
0150:                assertEquals(2, CollectionUtils.cardinality("E", bag));
0151:            }
0152:
0153:            public void testCardinalityOfNull() {
0154:                List list = new ArrayList();
0155:                assertEquals(0, CollectionUtils.cardinality(null, list));
0156:                {
0157:                    Map freq = CollectionUtils.getCardinalityMap(list);
0158:                    assertNull(freq.get(null));
0159:                }
0160:                list.add("A");
0161:                assertEquals(0, CollectionUtils.cardinality(null, list));
0162:                {
0163:                    Map freq = CollectionUtils.getCardinalityMap(list);
0164:                    assertNull(freq.get(null));
0165:                }
0166:                list.add(null);
0167:                assertEquals(1, CollectionUtils.cardinality(null, list));
0168:                {
0169:                    Map freq = CollectionUtils.getCardinalityMap(list);
0170:                    assertEquals(new Integer(1), freq.get(null));
0171:                }
0172:                list.add("B");
0173:                assertEquals(1, CollectionUtils.cardinality(null, list));
0174:                {
0175:                    Map freq = CollectionUtils.getCardinalityMap(list);
0176:                    assertEquals(new Integer(1), freq.get(null));
0177:                }
0178:                list.add(null);
0179:                assertEquals(2, CollectionUtils.cardinality(null, list));
0180:                {
0181:                    Map freq = CollectionUtils.getCardinalityMap(list);
0182:                    assertEquals(new Integer(2), freq.get(null));
0183:                }
0184:                list.add("B");
0185:                assertEquals(2, CollectionUtils.cardinality(null, list));
0186:                {
0187:                    Map freq = CollectionUtils.getCardinalityMap(list);
0188:                    assertEquals(new Integer(2), freq.get(null));
0189:                }
0190:                list.add(null);
0191:                assertEquals(3, CollectionUtils.cardinality(null, list));
0192:                {
0193:                    Map freq = CollectionUtils.getCardinalityMap(list);
0194:                    assertEquals(new Integer(3), freq.get(null));
0195:                }
0196:            }
0197:
0198:            public void testContainsAny() {
0199:                Collection empty = new ArrayList(0);
0200:                Collection one = new ArrayList(1);
0201:                one.add("1");
0202:                Collection two = new ArrayList(1);
0203:                two.add("2");
0204:                Collection three = new ArrayList(1);
0205:                three.add("3");
0206:                Collection odds = new ArrayList(2);
0207:                odds.add("1");
0208:                odds.add("3");
0209:
0210:                assertTrue("containsAny({1},{1,3}) should return true.",
0211:                        CollectionUtils.containsAny(one, odds));
0212:                assertTrue("containsAny({1,3},{1}) should return true.",
0213:                        CollectionUtils.containsAny(odds, one));
0214:                assertTrue("containsAny({3},{1,3}) should return true.",
0215:                        CollectionUtils.containsAny(three, odds));
0216:                assertTrue("containsAny({1,3},{3}) should return true.",
0217:                        CollectionUtils.containsAny(odds, three));
0218:                assertTrue("containsAny({2},{2}) should return true.",
0219:                        CollectionUtils.containsAny(two, two));
0220:                assertTrue("containsAny({1,3},{1,3}) should return true.",
0221:                        CollectionUtils.containsAny(odds, odds));
0222:
0223:                assertTrue("containsAny({2},{1,3}) should return false.",
0224:                        !CollectionUtils.containsAny(two, odds));
0225:                assertTrue("containsAny({1,3},{2}) should return false.",
0226:                        !CollectionUtils.containsAny(odds, two));
0227:                assertTrue("containsAny({1},{3}) should return false.",
0228:                        !CollectionUtils.containsAny(one, three));
0229:                assertTrue("containsAny({3},{1}) should return false.",
0230:                        !CollectionUtils.containsAny(three, one));
0231:                assertTrue("containsAny({1,3},{}) should return false.",
0232:                        !CollectionUtils.containsAny(odds, empty));
0233:                assertTrue("containsAny({},{1,3}) should return false.",
0234:                        !CollectionUtils.containsAny(empty, odds));
0235:                assertTrue("containsAny({},{}) should return false.",
0236:                        !CollectionUtils.containsAny(empty, empty));
0237:            }
0238:
0239:            public void testUnion() {
0240:                Collection col = CollectionUtils
0241:                        .union(collectionA, collectionB);
0242:                Map freq = CollectionUtils.getCardinalityMap(col);
0243:                assertEquals(new Integer(1), freq.get("a"));
0244:                assertEquals(new Integer(4), freq.get("b"));
0245:                assertEquals(new Integer(3), freq.get("c"));
0246:                assertEquals(new Integer(4), freq.get("d"));
0247:                assertEquals(new Integer(1), freq.get("e"));
0248:
0249:                Collection col2 = CollectionUtils.union(collectionB,
0250:                        collectionA);
0251:                Map freq2 = CollectionUtils.getCardinalityMap(col2);
0252:                assertEquals(new Integer(1), freq2.get("a"));
0253:                assertEquals(new Integer(4), freq2.get("b"));
0254:                assertEquals(new Integer(3), freq2.get("c"));
0255:                assertEquals(new Integer(4), freq2.get("d"));
0256:                assertEquals(new Integer(1), freq2.get("e"));
0257:            }
0258:
0259:            public void testIntersection() {
0260:                Collection col = CollectionUtils.intersection(collectionA,
0261:                        collectionB);
0262:                Map freq = CollectionUtils.getCardinalityMap(col);
0263:                assertNull(freq.get("a"));
0264:                assertEquals(new Integer(2), freq.get("b"));
0265:                assertEquals(new Integer(3), freq.get("c"));
0266:                assertEquals(new Integer(2), freq.get("d"));
0267:                assertNull(freq.get("e"));
0268:
0269:                Collection col2 = CollectionUtils.intersection(collectionB,
0270:                        collectionA);
0271:                Map freq2 = CollectionUtils.getCardinalityMap(col2);
0272:                assertNull(freq2.get("a"));
0273:                assertEquals(new Integer(2), freq2.get("b"));
0274:                assertEquals(new Integer(3), freq2.get("c"));
0275:                assertEquals(new Integer(2), freq2.get("d"));
0276:                assertNull(freq2.get("e"));
0277:            }
0278:
0279:            public void testDisjunction() {
0280:                Collection col = CollectionUtils.disjunction(collectionA,
0281:                        collectionB);
0282:                Map freq = CollectionUtils.getCardinalityMap(col);
0283:                assertEquals(new Integer(1), freq.get("a"));
0284:                assertEquals(new Integer(2), freq.get("b"));
0285:                assertNull(freq.get("c"));
0286:                assertEquals(new Integer(2), freq.get("d"));
0287:                assertEquals(new Integer(1), freq.get("e"));
0288:
0289:                Collection col2 = CollectionUtils.disjunction(collectionB,
0290:                        collectionA);
0291:                Map freq2 = CollectionUtils.getCardinalityMap(col2);
0292:                assertEquals(new Integer(1), freq2.get("a"));
0293:                assertEquals(new Integer(2), freq2.get("b"));
0294:                assertNull(freq2.get("c"));
0295:                assertEquals(new Integer(2), freq2.get("d"));
0296:                assertEquals(new Integer(1), freq2.get("e"));
0297:            }
0298:
0299:            public void testDisjunctionAsUnionMinusIntersection() {
0300:                Collection dis = CollectionUtils.disjunction(collectionA,
0301:                        collectionB);
0302:                Collection un = CollectionUtils.union(collectionA, collectionB);
0303:                Collection inter = CollectionUtils.intersection(collectionA,
0304:                        collectionB);
0305:                assertTrue(CollectionUtils.isEqualCollection(dis,
0306:                        CollectionUtils.subtract(un, inter)));
0307:            }
0308:
0309:            public void testDisjunctionAsSymmetricDifference() {
0310:                Collection dis = CollectionUtils.disjunction(collectionA,
0311:                        collectionB);
0312:                Collection amb = CollectionUtils.subtract(collectionA,
0313:                        collectionB);
0314:                Collection bma = CollectionUtils.subtract(collectionB,
0315:                        collectionA);
0316:                assertTrue(CollectionUtils.isEqualCollection(dis,
0317:                        CollectionUtils.union(amb, bma)));
0318:            }
0319:
0320:            public void testSubtract() {
0321:                Collection col = CollectionUtils.subtract(collectionA,
0322:                        collectionB);
0323:                Map freq = CollectionUtils.getCardinalityMap(col);
0324:                assertEquals(new Integer(1), freq.get("a"));
0325:                assertNull(freq.get("b"));
0326:                assertNull(freq.get("c"));
0327:                assertEquals(new Integer(2), freq.get("d"));
0328:                assertNull(freq.get("e"));
0329:
0330:                Collection col2 = CollectionUtils.subtract(collectionB,
0331:                        collectionA);
0332:                Map freq2 = CollectionUtils.getCardinalityMap(col2);
0333:                assertEquals(new Integer(1), freq2.get("e"));
0334:                assertNull(freq2.get("d"));
0335:                assertNull(freq2.get("c"));
0336:                assertEquals(new Integer(2), freq2.get("b"));
0337:                assertNull(freq2.get("a"));
0338:            }
0339:
0340:            public void testIsSubCollectionOfSelf() {
0341:                assertTrue(CollectionUtils.isSubCollection(collectionA,
0342:                        collectionA));
0343:                assertTrue(CollectionUtils.isSubCollection(collectionB,
0344:                        collectionB));
0345:            }
0346:
0347:            public void testIsSubCollection() {
0348:                assertTrue(!CollectionUtils.isSubCollection(collectionA,
0349:                        collectionB));
0350:                assertTrue(!CollectionUtils.isSubCollection(collectionB,
0351:                        collectionA));
0352:            }
0353:
0354:            public void testIsSubCollection2() {
0355:                Collection c = new ArrayList();
0356:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0357:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0358:                c.add("a");
0359:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0360:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0361:                c.add("b");
0362:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0363:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0364:                c.add("b");
0365:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0366:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0367:                c.add("c");
0368:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0369:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0370:                c.add("c");
0371:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0372:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0373:                c.add("c");
0374:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0375:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0376:                c.add("d");
0377:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0378:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0379:                c.add("d");
0380:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0381:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0382:                c.add("d");
0383:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0384:                assertTrue(!CollectionUtils.isSubCollection(collectionA, c));
0385:                c.add("d");
0386:                assertTrue(CollectionUtils.isSubCollection(c, collectionA));
0387:                assertTrue(CollectionUtils.isSubCollection(collectionA, c));
0388:                c.add("e");
0389:                assertTrue(!CollectionUtils.isSubCollection(c, collectionA));
0390:                assertTrue(CollectionUtils.isSubCollection(collectionA, c));
0391:            }
0392:
0393:            public void testIsEqualCollectionToSelf() {
0394:                assertTrue(CollectionUtils.isEqualCollection(collectionA,
0395:                        collectionA));
0396:                assertTrue(CollectionUtils.isEqualCollection(collectionB,
0397:                        collectionB));
0398:            }
0399:
0400:            public void testIsEqualCollection() {
0401:                assertTrue(!CollectionUtils.isEqualCollection(collectionA,
0402:                        collectionB));
0403:                assertTrue(!CollectionUtils.isEqualCollection(collectionB,
0404:                        collectionA));
0405:            }
0406:
0407:            public void testIsEqualCollection2() {
0408:                Collection a = new ArrayList();
0409:                Collection b = new ArrayList();
0410:                assertTrue(CollectionUtils.isEqualCollection(a, b));
0411:                assertTrue(CollectionUtils.isEqualCollection(b, a));
0412:                a.add("1");
0413:                assertTrue(!CollectionUtils.isEqualCollection(a, b));
0414:                assertTrue(!CollectionUtils.isEqualCollection(b, a));
0415:                b.add("1");
0416:                assertTrue(CollectionUtils.isEqualCollection(a, b));
0417:                assertTrue(CollectionUtils.isEqualCollection(b, a));
0418:                a.add("2");
0419:                assertTrue(!CollectionUtils.isEqualCollection(a, b));
0420:                assertTrue(!CollectionUtils.isEqualCollection(b, a));
0421:                b.add("2");
0422:                assertTrue(CollectionUtils.isEqualCollection(a, b));
0423:                assertTrue(CollectionUtils.isEqualCollection(b, a));
0424:                a.add("1");
0425:                assertTrue(!CollectionUtils.isEqualCollection(a, b));
0426:                assertTrue(!CollectionUtils.isEqualCollection(b, a));
0427:                b.add("1");
0428:                assertTrue(CollectionUtils.isEqualCollection(a, b));
0429:                assertTrue(CollectionUtils.isEqualCollection(b, a));
0430:            }
0431:
0432:            public void testIsProperSubCollection() {
0433:                Collection a = new ArrayList();
0434:                Collection b = new ArrayList();
0435:                assertTrue(!CollectionUtils.isProperSubCollection(a, b));
0436:                b.add("1");
0437:                assertTrue(CollectionUtils.isProperSubCollection(a, b));
0438:                assertTrue(!CollectionUtils.isProperSubCollection(b, a));
0439:                assertTrue(!CollectionUtils.isProperSubCollection(b, b));
0440:                assertTrue(!CollectionUtils.isProperSubCollection(a, a));
0441:                a.add("1");
0442:                a.add("2");
0443:                b.add("2");
0444:                assertTrue(!CollectionUtils.isProperSubCollection(b, a));
0445:                assertTrue(!CollectionUtils.isProperSubCollection(a, b));
0446:                a.add("1");
0447:                assertTrue(CollectionUtils.isProperSubCollection(b, a));
0448:                assertTrue(CollectionUtils.isProperSubCollection(
0449:                        CollectionUtils.intersection(collectionA, collectionB),
0450:                        collectionA));
0451:                assertTrue(CollectionUtils.isProperSubCollection(
0452:                        CollectionUtils.subtract(a, b), a));
0453:                assertTrue(!CollectionUtils.isProperSubCollection(a,
0454:                        CollectionUtils.subtract(a, b)));
0455:            }
0456:
0457:            public void testFind() {
0458:                Predicate testPredicate = PredicateUtils.equalPredicate("d");
0459:                Object test = CollectionUtils.find(collectionA, testPredicate);
0460:                assertTrue(test.equals("d"));
0461:                testPredicate = PredicateUtils.equalPredicate("de");
0462:                test = CollectionUtils.find(collectionA, testPredicate);
0463:                assertTrue(test == null);
0464:                assertEquals(CollectionUtils.find(null, testPredicate), null);
0465:                assertEquals(CollectionUtils.find(collectionA, null), null);
0466:            }
0467:
0468:            public void testForAllDo() {
0469:                Closure testClosure = ClosureUtils.invokerClosure("clear");
0470:                Collection col = new ArrayList();
0471:                col.add(collectionA);
0472:                col.add(collectionB);
0473:                CollectionUtils.forAllDo(col, testClosure);
0474:                assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
0475:                CollectionUtils.forAllDo(col, null);
0476:                assertTrue(collectionA.isEmpty() && collectionB.isEmpty());
0477:                CollectionUtils.forAllDo(null, testClosure);
0478:                col.add(null);
0479:                // null should be OK
0480:                CollectionUtils.forAllDo(col, testClosure);
0481:                col.add("x");
0482:                // This will lead to FunctorException
0483:                try {
0484:                    CollectionUtils.forAllDo(col, testClosure);
0485:                    fail("Expecting FunctorException");
0486:                } catch (FunctorException ex) {
0487:                    // expected from invoker -- method not found
0488:                }
0489:            }
0490:
0491:            public void testIndex() {
0492:                // normal map behavior when index is an Integer and a key
0493:                Map map = new HashMap();
0494:                map.put(new Integer(0), "zero");
0495:                map.put(new Integer(-1), "minusOne");
0496:                Object test = CollectionUtils.index(map, 0);
0497:                assertTrue(test.equals("zero"));
0498:                test = CollectionUtils.index(map, new Integer(-1));
0499:                assertTrue(test.equals("minusOne"));
0500:
0501:                // map, non-integer key that does not exist -- map returned
0502:                test = CollectionUtils.index(map, "missing");
0503:                assertTrue(test.equals(map));
0504:
0505:                // map, integer not a key, valid index -- "some" element of keyset returned
0506:                test = CollectionUtils.index(map, new Integer(1));
0507:                assertTrue(map.keySet().contains(test));
0508:
0509:                // map, integer not a key, not valid index -- "dead" keyset iterator returned
0510:                test = CollectionUtils.index(map, new Integer(4));
0511:                assertTrue((test instanceof  Iterator)
0512:                        && !((Iterator) test).hasNext());
0513:
0514:                // sorted map, integer not a key, valid index -- ith key returned
0515:                SortedMap map2 = new TreeMap();
0516:                map2.put(new Integer(23), "u");
0517:                map2.put(new Integer(21), "x");
0518:                map2.put(new Integer(17), "v");
0519:                map2.put(new Integer(42), "w");
0520:                Integer val = (Integer) CollectionUtils.index(map2, 0);
0521:                assertTrue(val.intValue() == 17);
0522:                val = (Integer) CollectionUtils.index(map2, 1);
0523:                assertTrue(val.intValue() == 21);
0524:                val = (Integer) CollectionUtils.index(map2, 2);
0525:                assertTrue(val.intValue() == 23);
0526:                val = (Integer) CollectionUtils.index(map2, 3);
0527:                assertTrue(val.intValue() == 42);
0528:
0529:                // list, entry exists
0530:                List list = new ArrayList();
0531:                list.add("zero");
0532:                list.add("one");
0533:                test = CollectionUtils.index(list, 0);
0534:                assertTrue(test.equals("zero"));
0535:                test = CollectionUtils.index(list, 1);
0536:                assertTrue(test.equals("one"));
0537:
0538:                // list, non-existent entry -- IndexOutOfBoundsException
0539:                try {
0540:                    test = CollectionUtils.index(list, 2);
0541:                    fail("Expecting IndexOutOfBoundsException");
0542:                } catch (IndexOutOfBoundsException e) {
0543:                    // expected
0544:                }
0545:
0546:                // iterator, entry exists
0547:                Iterator iterator = list.iterator();
0548:                test = CollectionUtils.index(iterator, 0);
0549:                assertTrue(test.equals("zero"));
0550:                iterator = list.iterator();
0551:                test = CollectionUtils.index(iterator, 1);
0552:                assertTrue(test.equals("one"));
0553:
0554:                // iterator, non-existent entry -- "dead" iterator returned
0555:                test = CollectionUtils.index(iterator, 3);
0556:                assertTrue(test.equals(iterator) && !iterator.hasNext());
0557:
0558:                // Enumeration, entry exists
0559:                Vector vector = new Vector(list);
0560:                Enumeration en = vector.elements();
0561:                test = CollectionUtils.index(en, 0);
0562:                assertTrue(test.equals("zero"));
0563:                en = vector.elements();
0564:                test = CollectionUtils.index(en, 1);
0565:                assertTrue(test.equals("one"));
0566:
0567:                // Enumeration, non-existent entry -- "dead" enumerator returned
0568:                test = CollectionUtils.index(en, 3);
0569:                assertTrue(test.equals(en) && !en.hasMoreElements());
0570:
0571:                // Collection, entry exists
0572:                Bag bag = new HashBag();
0573:                bag.add("element", 1);
0574:                test = CollectionUtils.index(bag, 0);
0575:                assertTrue(test.equals("element"));
0576:
0577:                // Collection, non-existent entry -- "dead" iterator returned
0578:                test = CollectionUtils.index(bag, 2);
0579:                assertTrue((test instanceof  Iterator)
0580:                        && !((Iterator) test).hasNext());
0581:
0582:                // Object array, entry exists
0583:                Object[] objArray = new Object[2];
0584:                objArray[0] = "zero";
0585:                objArray[1] = "one";
0586:                test = CollectionUtils.index(objArray, 0);
0587:                assertTrue(test.equals("zero"));
0588:                test = CollectionUtils.index(objArray, 1);
0589:                assertTrue(test.equals("one"));
0590:
0591:                // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
0592:                try {
0593:                    test = CollectionUtils.index(objArray, 2);
0594:                    fail("Expecting ArrayIndexOutOfBoundsException.");
0595:                } catch (ArrayIndexOutOfBoundsException ex) {
0596:                    // expected
0597:                }
0598:
0599:                // Non-collection object -- returned unchanged
0600:                Object obj = new Object();
0601:                test = CollectionUtils.index(obj, obj);
0602:                assertTrue(test.equals(obj));
0603:            }
0604:
0605:            public void testGet() {
0606:                {
0607:                    // Unordered map, entries exist
0608:                    Map expected = new HashMap();
0609:                    expected.put("zeroKey", "zero");
0610:                    expected.put("oneKey", "one");
0611:
0612:                    Map found = new HashMap();
0613:                    Map.Entry entry = (Map.Entry) (CollectionUtils.get(
0614:                            expected, 0));
0615:                    found.put(entry.getKey(), entry.getValue());
0616:                    entry = (Map.Entry) (CollectionUtils.get(expected, 1));
0617:                    found.put(entry.getKey(), entry.getValue());
0618:                    assertEquals(expected, found);
0619:
0620:                    // Map index out of range
0621:                    try {
0622:                        CollectionUtils.get(expected, 2);
0623:                        fail("Expecting IndexOutOfBoundsException.");
0624:                    } catch (IndexOutOfBoundsException e) {
0625:                        // expected
0626:                    }
0627:                    try {
0628:                        CollectionUtils.get(expected, -2);
0629:                        fail("Expecting IndexOutOfBoundsException.");
0630:                    } catch (IndexOutOfBoundsException e) {
0631:                        // expected
0632:                    }
0633:                }
0634:
0635:                {
0636:                    // Sorted map, entries exist, should respect order
0637:                    SortedMap map = new TreeMap();
0638:                    map.put("zeroKey", "zero");
0639:                    map.put("oneKey", "one");
0640:                    Object test = CollectionUtils.get(map, 1);
0641:                    assertEquals("zeroKey", ((Map.Entry) test).getKey());
0642:                    assertEquals("zero", ((Map.Entry) test).getValue());
0643:                    test = CollectionUtils.get(map, 0);
0644:                    assertEquals("oneKey", ((Map.Entry) test).getKey());
0645:                    assertEquals("one", ((Map.Entry) test).getValue());
0646:                }
0647:
0648:                {
0649:                    // List, entry exists
0650:                    List list = new ArrayList();
0651:                    list.add("zero");
0652:                    list.add("one");
0653:                    assertEquals("zero", CollectionUtils.get(list, 0));
0654:                    assertEquals("one", CollectionUtils.get(list, 1));
0655:                    // list, non-existent entry -- IndexOutOfBoundsException
0656:                    try {
0657:                        CollectionUtils.get(list, 2);
0658:                        fail("Expecting IndexOutOfBoundsException");
0659:                    } catch (IndexOutOfBoundsException e) {
0660:                        // expected
0661:                    }
0662:
0663:                    // Iterator, entry exists
0664:                    Iterator iterator = list.iterator();
0665:                    assertEquals("zero", CollectionUtils.get(iterator, 0));
0666:                    iterator = list.iterator();
0667:                    assertEquals("one", CollectionUtils.get(iterator, 1));
0668:
0669:                    // Iterator, non-existent entry 
0670:                    try {
0671:                        CollectionUtils.get(iterator, 3);
0672:                        fail("Expecting IndexOutOfBoundsException.");
0673:                    } catch (IndexOutOfBoundsException e) {
0674:                        // expected
0675:                    }
0676:                    assertTrue(!iterator.hasNext());
0677:                }
0678:
0679:                {
0680:                    // Enumeration, entry exists
0681:                    Vector vector = new Vector();
0682:                    vector.addElement("zero");
0683:                    vector.addElement("one");
0684:                    Enumeration en = vector.elements();
0685:                    assertEquals("zero", CollectionUtils.get(en, 0));
0686:                    en = vector.elements();
0687:                    assertEquals("one", CollectionUtils.get(en, 1));
0688:
0689:                    // Enumerator, non-existent entry 
0690:                    try {
0691:                        CollectionUtils.get(en, 3);
0692:                        fail("Expecting IndexOutOfBoundsException.");
0693:                    } catch (IndexOutOfBoundsException e) {
0694:                        // expected
0695:                    }
0696:                    assertTrue(!en.hasMoreElements());
0697:                }
0698:
0699:                {
0700:                    // Collection, entry exists
0701:                    Bag bag = new HashBag();
0702:                    bag.add("element", 1);
0703:                    assertEquals("element", CollectionUtils.get(bag, 0));
0704:
0705:                    // Collection, non-existent entry
0706:                    try {
0707:                        CollectionUtils.get(bag, 1);
0708:                        fail("Expceting IndexOutOfBoundsException.");
0709:                    } catch (IndexOutOfBoundsException e) {
0710:                        // expected
0711:                    }
0712:                }
0713:
0714:                {
0715:                    // Object array, entry exists
0716:                    Object[] objArray = new Object[2];
0717:                    objArray[0] = "zero";
0718:                    objArray[1] = "one";
0719:                    assertEquals("zero", CollectionUtils.get(objArray, 0));
0720:                    assertEquals("one", CollectionUtils.get(objArray, 1));
0721:
0722:                    // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
0723:                    try {
0724:                        CollectionUtils.get(objArray, 2);
0725:                        fail("Expecting IndexOutOfBoundsException.");
0726:                    } catch (IndexOutOfBoundsException ex) {
0727:                        // expected
0728:                    }
0729:                }
0730:
0731:                {
0732:                    // Primitive array, entry exists
0733:                    int[] array = new int[2];
0734:                    array[0] = 10;
0735:                    array[1] = 20;
0736:                    assertEquals(new Integer(10), CollectionUtils.get(array, 0));
0737:                    assertEquals(new Integer(20), CollectionUtils.get(array, 1));
0738:
0739:                    // Object array, non-existent entry -- ArrayIndexOutOfBoundsException
0740:                    try {
0741:                        CollectionUtils.get(array, 2);
0742:                        fail("Expecting IndexOutOfBoundsException.");
0743:                    } catch (IndexOutOfBoundsException ex) {
0744:                        // expected
0745:                    }
0746:                }
0747:
0748:                {
0749:                    // Invalid object
0750:                    Object obj = new Object();
0751:                    try {
0752:                        CollectionUtils.get(obj, 0);
0753:                        fail("Expecting IllegalArgumentException.");
0754:                    } catch (IllegalArgumentException e) {
0755:                        // expected
0756:                    }
0757:                    try {
0758:                        CollectionUtils.get(null, 0);
0759:                        fail("Expecting IllegalArgumentException.");
0760:                    } catch (IllegalArgumentException e) {
0761:                        // expected
0762:                    }
0763:                }
0764:            }
0765:
0766:            //-----------------------------------------------------------------------
0767:            public void testSize_List() {
0768:                List list = new ArrayList();
0769:                assertEquals(0, CollectionUtils.size(list));
0770:                list.add("a");
0771:                assertEquals(1, CollectionUtils.size(list));
0772:                list.add("b");
0773:                assertEquals(2, CollectionUtils.size(list));
0774:            }
0775:
0776:            public void testSize_Map() {
0777:                Map map = new HashMap();
0778:                assertEquals(0, CollectionUtils.size(map));
0779:                map.put("1", "a");
0780:                assertEquals(1, CollectionUtils.size(map));
0781:                map.put("2", "b");
0782:                assertEquals(2, CollectionUtils.size(map));
0783:            }
0784:
0785:            public void testSize_Array() {
0786:                Object[] objectArray = new Object[0];
0787:                assertEquals(0, CollectionUtils.size(objectArray));
0788:
0789:                String[] stringArray = new String[3];
0790:                assertEquals(3, CollectionUtils.size(stringArray));
0791:                stringArray[0] = "a";
0792:                stringArray[1] = "b";
0793:                stringArray[2] = "c";
0794:                assertEquals(3, CollectionUtils.size(stringArray));
0795:            }
0796:
0797:            public void testSize_PrimitiveArray() {
0798:                int[] intArray = new int[0];
0799:                assertEquals(0, CollectionUtils.size(intArray));
0800:
0801:                double[] doubleArray = new double[3];
0802:                assertEquals(3, CollectionUtils.size(doubleArray));
0803:                doubleArray[0] = 0.0d;
0804:                doubleArray[1] = 1.0d;
0805:                doubleArray[2] = 2.5d;
0806:                assertEquals(3, CollectionUtils.size(doubleArray));
0807:            }
0808:
0809:            public void testSize_Enumeration() {
0810:                Vector list = new Vector();
0811:                assertEquals(0, CollectionUtils.size(list.elements()));
0812:                list.add("a");
0813:                assertEquals(1, CollectionUtils.size(list.elements()));
0814:                list.add("b");
0815:                assertEquals(2, CollectionUtils.size(list.elements()));
0816:            }
0817:
0818:            public void testSize_Iterator() {
0819:                List list = new ArrayList();
0820:                assertEquals(0, CollectionUtils.size(list.iterator()));
0821:                list.add("a");
0822:                assertEquals(1, CollectionUtils.size(list.iterator()));
0823:                list.add("b");
0824:                assertEquals(2, CollectionUtils.size(list.iterator()));
0825:            }
0826:
0827:            public void testSize_Other() {
0828:                try {
0829:                    CollectionUtils.size(null);
0830:                    fail("Expecting IllegalArgumentException");
0831:                } catch (IllegalArgumentException e) {
0832:                }
0833:                try {
0834:                    CollectionUtils.size("not a list");
0835:                    fail("Expecting IllegalArgumentException");
0836:                } catch (IllegalArgumentException e) {
0837:                }
0838:            }
0839:
0840:            //-----------------------------------------------------------------------
0841:            public void testSizeIsEmpty_List() {
0842:                List list = new ArrayList();
0843:                assertEquals(true, CollectionUtils.sizeIsEmpty(list));
0844:                list.add("a");
0845:                assertEquals(false, CollectionUtils.sizeIsEmpty(list));
0846:            }
0847:
0848:            public void testSizeIsEmpty_Map() {
0849:                Map map = new HashMap();
0850:                assertEquals(true, CollectionUtils.sizeIsEmpty(map));
0851:                map.put("1", "a");
0852:                assertEquals(false, CollectionUtils.sizeIsEmpty(map));
0853:            }
0854:
0855:            public void testSizeIsEmpty_Array() {
0856:                Object[] objectArray = new Object[0];
0857:                assertEquals(true, CollectionUtils.sizeIsEmpty(objectArray));
0858:
0859:                String[] stringArray = new String[3];
0860:                assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
0861:                stringArray[0] = "a";
0862:                stringArray[1] = "b";
0863:                stringArray[2] = "c";
0864:                assertEquals(false, CollectionUtils.sizeIsEmpty(stringArray));
0865:            }
0866:
0867:            public void testSizeIsEmpty_PrimitiveArray() {
0868:                int[] intArray = new int[0];
0869:                assertEquals(true, CollectionUtils.sizeIsEmpty(intArray));
0870:
0871:                double[] doubleArray = new double[3];
0872:                assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
0873:                doubleArray[0] = 0.0d;
0874:                doubleArray[1] = 1.0d;
0875:                doubleArray[2] = 2.5d;
0876:                assertEquals(false, CollectionUtils.sizeIsEmpty(doubleArray));
0877:            }
0878:
0879:            public void testSizeIsEmpty_Enumeration() {
0880:                Vector list = new Vector();
0881:                assertEquals(true, CollectionUtils.sizeIsEmpty(list.elements()));
0882:                list.add("a");
0883:                assertEquals(false, CollectionUtils
0884:                        .sizeIsEmpty(list.elements()));
0885:                Enumeration en = list.elements();
0886:                en.nextElement();
0887:                assertEquals(true, CollectionUtils.sizeIsEmpty(en));
0888:            }
0889:
0890:            public void testSizeIsEmpty_Iterator() {
0891:                List list = new ArrayList();
0892:                assertEquals(true, CollectionUtils.sizeIsEmpty(list.iterator()));
0893:                list.add("a");
0894:                assertEquals(false, CollectionUtils
0895:                        .sizeIsEmpty(list.iterator()));
0896:                Iterator it = list.iterator();
0897:                it.next();
0898:                assertEquals(true, CollectionUtils.sizeIsEmpty(it));
0899:            }
0900:
0901:            public void testSizeIsEmpty_Other() {
0902:                try {
0903:                    CollectionUtils.sizeIsEmpty(null);
0904:                    fail("Expecting IllegalArgumentException");
0905:                } catch (IllegalArgumentException ex) {
0906:                }
0907:                try {
0908:                    CollectionUtils.sizeIsEmpty("not a list");
0909:                    fail("Expecting IllegalArgumentException");
0910:                } catch (IllegalArgumentException ex) {
0911:                }
0912:            }
0913:
0914:            //-----------------------------------------------------------------------
0915:            public void testIsEmptyWithEmptyCollection() {
0916:                Collection coll = new ArrayList();
0917:                assertEquals(true, CollectionUtils.isEmpty(coll));
0918:            }
0919:
0920:            public void testIsEmptyWithNonEmptyCollection() {
0921:                Collection coll = new ArrayList();
0922:                coll.add("item");
0923:                assertEquals(false, CollectionUtils.isEmpty(coll));
0924:            }
0925:
0926:            public void testIsEmptyWithNull() {
0927:                Collection coll = null;
0928:                assertEquals(true, CollectionUtils.isEmpty(coll));
0929:            }
0930:
0931:            public void testIsNotEmptyWithEmptyCollection() {
0932:                Collection coll = new ArrayList();
0933:                assertEquals(false, CollectionUtils.isNotEmpty(coll));
0934:            }
0935:
0936:            public void testIsNotEmptyWithNonEmptyCollection() {
0937:                Collection coll = new ArrayList();
0938:                coll.add("item");
0939:                assertEquals(true, CollectionUtils.isNotEmpty(coll));
0940:            }
0941:
0942:            public void testIsNotEmptyWithNull() {
0943:                Collection coll = null;
0944:                assertEquals(false, CollectionUtils.isNotEmpty(coll));
0945:            }
0946:
0947:            //-----------------------------------------------------------------------
0948:            private static Predicate EQUALS_TWO = new Predicate() {
0949:                public boolean evaluate(Object input) {
0950:                    return (input.equals("Two"));
0951:                }
0952:            };
0953:
0954:            public void testFilter() {
0955:                List list = new ArrayList();
0956:                list.add("One");
0957:                list.add("Two");
0958:                list.add("Three");
0959:                list.add("Four");
0960:                CollectionUtils.filter(list, EQUALS_TWO);
0961:                assertEquals(1, list.size());
0962:                assertEquals("Two", list.get(0));
0963:
0964:                list = new ArrayList();
0965:                list.add("One");
0966:                list.add("Two");
0967:                list.add("Three");
0968:                list.add("Four");
0969:                CollectionUtils.filter(list, null);
0970:                assertEquals(4, list.size());
0971:                CollectionUtils.filter(null, EQUALS_TWO);
0972:                assertEquals(4, list.size());
0973:                CollectionUtils.filter(null, null);
0974:                assertEquals(4, list.size());
0975:            }
0976:
0977:            public void testCountMatches() {
0978:                List list = new ArrayList();
0979:                list.add("One");
0980:                list.add("Two");
0981:                list.add("Three");
0982:                list.add("Four");
0983:                int count = CollectionUtils.countMatches(list, EQUALS_TWO);
0984:                assertEquals(4, list.size());
0985:                assertEquals(1, count);
0986:                assertEquals(0, CollectionUtils.countMatches(list, null));
0987:                assertEquals(0, CollectionUtils.countMatches(null, EQUALS_TWO));
0988:                assertEquals(0, CollectionUtils.countMatches(null, null));
0989:            }
0990:
0991:            public void testExists() {
0992:                List list = new ArrayList();
0993:                assertEquals(false, CollectionUtils.exists(null, null));
0994:                assertEquals(false, CollectionUtils.exists(list, null));
0995:                assertEquals(false, CollectionUtils.exists(null, EQUALS_TWO));
0996:                assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
0997:                list.add("One");
0998:                list.add("Three");
0999:                list.add("Four");
1000:                assertEquals(false, CollectionUtils.exists(list, EQUALS_TWO));
1001:
1002:                list.add("Two");
1003:                assertEquals(true, CollectionUtils.exists(list, EQUALS_TWO));
1004:            }
1005:
1006:            public void testSelect() {
1007:                List list = new ArrayList();
1008:                list.add("One");
1009:                list.add("Two");
1010:                list.add("Three");
1011:                list.add("Four");
1012:                Collection output = CollectionUtils.select(list, EQUALS_TWO);
1013:                assertEquals(4, list.size());
1014:                assertEquals(1, output.size());
1015:                assertEquals("Two", output.iterator().next());
1016:            }
1017:
1018:            public void testSelectRejected() {
1019:                List list = new ArrayList();
1020:                list.add("One");
1021:                list.add("Two");
1022:                list.add("Three");
1023:                list.add("Four");
1024:                Collection output = CollectionUtils.selectRejected(list,
1025:                        EQUALS_TWO);
1026:                assertEquals(4, list.size());
1027:                assertEquals(3, output.size());
1028:                assertTrue(output.contains("One"));
1029:                assertTrue(output.contains("Three"));
1030:                assertTrue(output.contains("Four"));
1031:            }
1032:
1033:            public void testCollect() {
1034:                Transformer transformer = TransformerUtils
1035:                        .constantTransformer("z");
1036:                Collection collection = CollectionUtils.collect(collectionA,
1037:                        transformer);
1038:                assertTrue(collection.size() == collectionA.size());
1039:                assertTrue(collectionA.contains("a")
1040:                        && !collectionA.contains("z"));
1041:                assertTrue(collection.contains("z")
1042:                        && !collection.contains("a"));
1043:
1044:                collection = new ArrayList();
1045:                CollectionUtils.collect(collectionA, transformer, collection);
1046:                assertTrue(collection.size() == collectionA.size());
1047:                assertTrue(collectionA.contains("a")
1048:                        && !collectionA.contains("z"));
1049:                assertTrue(collection.contains("z")
1050:                        && !collection.contains("a"));
1051:
1052:                Iterator iterator = null;
1053:                collection = new ArrayList();
1054:                CollectionUtils.collect(iterator, transformer, collection);
1055:
1056:                iterator = collectionA.iterator();
1057:                CollectionUtils.collect(iterator, transformer, collection);
1058:                assertTrue(collection.size() == collectionA.size());
1059:                assertTrue(collectionA.contains("a")
1060:                        && !collectionA.contains("z"));
1061:                assertTrue(collection.contains("z")
1062:                        && !collection.contains("a"));
1063:
1064:                iterator = collectionA.iterator();
1065:                collection = CollectionUtils.collect(iterator, transformer);
1066:                assertTrue(collection.size() == collectionA.size());
1067:                assertTrue(collection.contains("z")
1068:                        && !collection.contains("a"));
1069:                collection = CollectionUtils.collect((Iterator) null,
1070:                        (Transformer) null);
1071:                assertTrue(collection.size() == 0);
1072:
1073:                int size = collectionA.size();
1074:                CollectionUtils.collect((Collection) null, transformer,
1075:                        collectionA);
1076:                assertTrue(collectionA.size() == size
1077:                        && collectionA.contains("a"));
1078:                CollectionUtils.collect(collectionB, null, collectionA);
1079:                assertTrue(collectionA.size() == size
1080:                        && collectionA.contains("a"));
1081:
1082:            }
1083:
1084:            Transformer TRANSFORM_TO_INTEGER = new Transformer() {
1085:                public Object transform(Object input) {
1086:                    return new Integer((String) input);
1087:                }
1088:            };
1089:
1090:            public void testTransform1() {
1091:                List list = new ArrayList();
1092:                list.add("1");
1093:                list.add("2");
1094:                list.add("3");
1095:                CollectionUtils.transform(list, TRANSFORM_TO_INTEGER);
1096:                assertEquals(3, list.size());
1097:                assertEquals(new Integer(1), list.get(0));
1098:                assertEquals(new Integer(2), list.get(1));
1099:                assertEquals(new Integer(3), list.get(2));
1100:
1101:                list = new ArrayList();
1102:                list.add("1");
1103:                list.add("2");
1104:                list.add("3");
1105:                CollectionUtils.transform(null, TRANSFORM_TO_INTEGER);
1106:                assertEquals(3, list.size());
1107:                CollectionUtils.transform(list, null);
1108:                assertEquals(3, list.size());
1109:                CollectionUtils.transform(null, null);
1110:                assertEquals(3, list.size());
1111:            }
1112:
1113:            public void testTransform2() {
1114:                Set set = new HashSet();
1115:                set.add("1");
1116:                set.add("2");
1117:                set.add("3");
1118:                CollectionUtils.transform(set, new Transformer() {
1119:                    public Object transform(Object input) {
1120:                        return new Integer(4);
1121:                    }
1122:                });
1123:                assertEquals(1, set.size());
1124:                assertEquals(new Integer(4), set.iterator().next());
1125:            }
1126:
1127:            //-----------------------------------------------------------------------
1128:            public void testAddIgnoreNull() {
1129:                Set set = new HashSet();
1130:                set.add("1");
1131:                set.add("2");
1132:                set.add("3");
1133:                assertEquals(false, CollectionUtils.addIgnoreNull(set, null));
1134:                assertEquals(3, set.size());
1135:                assertEquals(false, CollectionUtils.addIgnoreNull(set, "1"));
1136:                assertEquals(3, set.size());
1137:                assertEquals(true, CollectionUtils.addIgnoreNull(set, "4"));
1138:                assertEquals(4, set.size());
1139:                assertEquals(true, set.contains("4"));
1140:            }
1141:
1142:            //-----------------------------------------------------------------------
1143:            public void testPredicatedCollection() {
1144:                Predicate predicate = new Predicate() {
1145:                    public boolean evaluate(Object o) {
1146:                        return o instanceof  String;
1147:                    }
1148:                };
1149:                Collection collection = CollectionUtils.predicatedCollection(
1150:                        new ArrayList(), predicate);
1151:                assertTrue("returned object should be a PredicatedCollection",
1152:                        collection instanceof  PredicatedCollection);
1153:                try {
1154:                    collection = CollectionUtils.predicatedCollection(
1155:                            new ArrayList(), null);
1156:                    fail("Expecting IllegalArgumentException for null predicate.");
1157:                } catch (IllegalArgumentException ex) {
1158:                    // expected
1159:                }
1160:                try {
1161:                    collection = CollectionUtils.predicatedCollection(null,
1162:                            predicate);
1163:                    fail("Expecting IllegalArgumentException for null collection.");
1164:                } catch (IllegalArgumentException ex) {
1165:                    // expected
1166:                }
1167:            }
1168:
1169:            public BulkTest bulkTestTypedCollection() {
1170:                return new TestTypedCollection("") {
1171:                    public Collection typedCollection() {
1172:                        return CollectionUtils.typedCollection(new ArrayList(),
1173:                                super .getType());
1174:                    }
1175:
1176:                    public BulkTest bulkTestAll() {
1177:                        return new AbstractTestCollection("") {
1178:                            public Collection makeCollection() {
1179:                                return typedCollection();
1180:                            }
1181:
1182:                            public Collection makeConfirmedCollection() {
1183:                                return new ArrayList();
1184:                            }
1185:
1186:                            public Collection makeConfirmedFullCollection() {
1187:                                ArrayList list = new ArrayList();
1188:                                list.addAll(java.util.Arrays
1189:                                        .asList(getFullElements()));
1190:                                return list;
1191:                            }
1192:
1193:                            public Object[] getFullElements() {
1194:                                return getFullNonNullStringElements();
1195:                            }
1196:
1197:                            public Object[] getOtherElements() {
1198:                                return getOtherNonNullStringElements();
1199:                            }
1200:
1201:                        };
1202:                    }
1203:                };
1204:            }
1205:
1206:            public void testIsFull() {
1207:                Set set = new HashSet();
1208:                set.add("1");
1209:                set.add("2");
1210:                set.add("3");
1211:                try {
1212:                    CollectionUtils.isFull(null);
1213:                    fail();
1214:                } catch (NullPointerException ex) {
1215:                }
1216:                assertEquals(false, CollectionUtils.isFull(set));
1217:
1218:                BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
1219:                assertEquals(true, CollectionUtils.isFull(buf));
1220:                buf.remove("2");
1221:                assertEquals(false, CollectionUtils.isFull(buf));
1222:                buf.add("2");
1223:                assertEquals(true, CollectionUtils.isFull(buf));
1224:
1225:                Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
1226:                assertEquals(true, CollectionUtils.isFull(buf2));
1227:                buf2.remove("2");
1228:                assertEquals(false, CollectionUtils.isFull(buf2));
1229:                buf2.add("2");
1230:                assertEquals(true, CollectionUtils.isFull(buf2));
1231:            }
1232:
1233:            public void testMaxSize() {
1234:                Set set = new HashSet();
1235:                set.add("1");
1236:                set.add("2");
1237:                set.add("3");
1238:                try {
1239:                    CollectionUtils.maxSize(null);
1240:                    fail();
1241:                } catch (NullPointerException ex) {
1242:                }
1243:                assertEquals(-1, CollectionUtils.maxSize(set));
1244:
1245:                BoundedFifoBuffer buf = new BoundedFifoBuffer(set);
1246:                assertEquals(3, CollectionUtils.maxSize(buf));
1247:                buf.remove("2");
1248:                assertEquals(3, CollectionUtils.maxSize(buf));
1249:                buf.add("2");
1250:                assertEquals(3, CollectionUtils.maxSize(buf));
1251:
1252:                Buffer buf2 = BufferUtils.synchronizedBuffer(buf);
1253:                assertEquals(3, CollectionUtils.maxSize(buf2));
1254:                buf2.remove("2");
1255:                assertEquals(3, CollectionUtils.maxSize(buf2));
1256:                buf2.add("2");
1257:                assertEquals(3, CollectionUtils.maxSize(buf2));
1258:            }
1259:
1260:            public void testIntersectionUsesMethodEquals() {
1261:                // Let elta and eltb be objects...
1262:                Object elta = new Integer(17);
1263:                Object eltb = new Integer(17);
1264:
1265:                // ...which are equal...
1266:                assertEquals(elta, eltb);
1267:                assertEquals(eltb, elta);
1268:
1269:                // ...but not the same (==).
1270:                assertTrue(elta != eltb);
1271:
1272:                // Let cola and colb be collections...
1273:                Collection cola = new ArrayList();
1274:                Collection colb = new ArrayList();
1275:
1276:                // ...which contain elta and eltb, 
1277:                // respectively.
1278:                cola.add(elta);
1279:                colb.add(eltb);
1280:
1281:                // Then the intersection of the two
1282:                // should contain one element.
1283:                Collection intersection = CollectionUtils.intersection(cola,
1284:                        colb);
1285:                assertEquals(1, intersection.size());
1286:
1287:                // In practice, this element will be the same (==) as elta
1288:                // or eltb, although this isn't strictly part of the
1289:                // contract.
1290:                Object eltc = intersection.iterator().next();
1291:                assertTrue((eltc == elta && eltc != eltb)
1292:                        || (eltc != elta && eltc == eltb));
1293:
1294:                // In any event, this element remains equal,
1295:                // to both elta and eltb.
1296:                assertEquals(elta, eltc);
1297:                assertEquals(eltc, elta);
1298:                assertEquals(eltb, eltc);
1299:                assertEquals(eltc, eltb);
1300:            }
1301:
1302:            public void testTransformedCollection() {
1303:                Transformer transformer = TransformerUtils.nopTransformer();
1304:                Collection collection = CollectionUtils.transformedCollection(
1305:                        new ArrayList(), transformer);
1306:                assertTrue("returned object should be a TransformedCollection",
1307:                        collection instanceof  TransformedCollection);
1308:                try {
1309:                    collection = CollectionUtils.transformedCollection(
1310:                            new ArrayList(), null);
1311:                    fail("Expecting IllegalArgumentException for null transformer.");
1312:                } catch (IllegalArgumentException ex) {
1313:                    // expected
1314:                }
1315:                try {
1316:                    collection = CollectionUtils.transformedCollection(null,
1317:                            transformer);
1318:                    fail("Expecting IllegalArgumentException for null collection.");
1319:                } catch (IllegalArgumentException ex) {
1320:                    // expected
1321:                }
1322:            }
1323:
1324:            public void testTransformedCollection_2() {
1325:                List list = new ArrayList();
1326:                list.add("1");
1327:                list.add("2");
1328:                list.add("3");
1329:                Collection result = CollectionUtils.transformedCollection(list,
1330:                        TRANSFORM_TO_INTEGER);
1331:                assertEquals(true, result.contains("1")); // untransformed
1332:                assertEquals(true, result.contains("2")); // untransformed
1333:                assertEquals(true, result.contains("3")); // untransformed
1334:            }
1335:
1336:            public void testSynchronizedCollection() {
1337:                Collection col = CollectionUtils
1338:                        .synchronizedCollection(new ArrayList());
1339:                assertTrue(
1340:                        "Returned object should be a SynchronizedCollection.",
1341:                        col instanceof  SynchronizedCollection);
1342:                try {
1343:                    col = CollectionUtils.synchronizedCollection(null);
1344:                    fail("Expecting IllegalArgumentException for null collection.");
1345:                } catch (IllegalArgumentException ex) {
1346:                    // expected
1347:                }
1348:            }
1349:
1350:            public void testUnmodifiableCollection() {
1351:                Collection col = CollectionUtils
1352:                        .unmodifiableCollection(new ArrayList());
1353:                assertTrue(
1354:                        "Returned object should be a UnmodifiableCollection.",
1355:                        col instanceof  UnmodifiableCollection);
1356:                try {
1357:                    col = CollectionUtils.unmodifiableCollection(null);
1358:                    fail("Expecting IllegalArgumentException for null collection.");
1359:                } catch (IllegalArgumentException ex) {
1360:                    // expected
1361:                }
1362:            }
1363:
1364:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.