Source Code Cross Referenced for TestIteratorChain.java in  » Library » Apache-common-Collections » org » apache » commons » collections » iterators » 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.iterators 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Copyright 2001-2005 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections.iterators;
017:
018:        import java.util.ArrayList;
019:        import java.util.Iterator;
020:        import java.util.List;
021:        import java.util.NoSuchElementException;
022:
023:        import junit.framework.Test;
024:        import junit.framework.TestSuite;
025:
026:        import org.apache.commons.collections.IteratorUtils;
027:        import org.apache.commons.collections.Predicate;
028:
029:        /**
030:         * Tests the IteratorChain class.
031:         * 
032:         * @version $Revision: 171347 $ $Date: 2005-05-22 18:27:34 +0100 (Sun, 22 May 2005) $
033:         * 
034:         * @author James Strachan
035:         * @author Mauricio S. Moura
036:         * @author Morgan Delagrange
037:         */
038:        public class TestIteratorChain extends AbstractTestIterator {
039:
040:            protected String[] testArray = { "One", "Two", "Three", "Four",
041:                    "Five", "Six" };
042:
043:            protected List list1 = null;
044:            protected List list2 = null;
045:            protected List list3 = null;
046:
047:            public static Test suite() {
048:                return new TestSuite(TestIteratorChain.class);
049:            }
050:
051:            public TestIteratorChain(String testName) {
052:                super (testName);
053:            }
054:
055:            public void setUp() {
056:                list1 = new ArrayList();
057:                list1.add("One");
058:                list1.add("Two");
059:                list1.add("Three");
060:                list2 = new ArrayList();
061:                list2.add("Four");
062:                list3 = new ArrayList();
063:                list3.add("Five");
064:                list3.add("Six");
065:            }
066:
067:            public Iterator makeEmptyIterator() {
068:                ArrayList list = new ArrayList();
069:                return new IteratorChain(list.iterator());
070:            }
071:
072:            public Iterator makeFullIterator() {
073:                IteratorChain chain = new IteratorChain();
074:
075:                chain.addIterator(list1.iterator());
076:                chain.addIterator(list2.iterator());
077:                chain.addIterator(list3.iterator());
078:                return chain;
079:            }
080:
081:            public void testIterator() {
082:                Iterator iter = (Iterator) makeFullIterator();
083:                for (int i = 0; i < testArray.length; i++) {
084:                    Object testValue = testArray[i];
085:                    Object iterValue = iter.next();
086:
087:                    assertEquals("Iteration value is correct", testValue,
088:                            iterValue);
089:                }
090:
091:                assertTrue("Iterator should now be empty", !iter.hasNext());
092:
093:                try {
094:                    Object testValue = iter.next();
095:                } catch (Exception e) {
096:                    assertTrue("NoSuchElementException must be thrown", e
097:                            .getClass().equals(
098:                                    (new NoSuchElementException()).getClass()));
099:                }
100:            }
101:
102:            public void testRemoveFromFilteredIterator() {
103:
104:                final Predicate myPredicate = new Predicate() {
105:                    public boolean evaluate(Object object) {
106:                        Integer i = (Integer) object;
107:                        if (i.compareTo(new Integer(4)) < 0)
108:                            return true;
109:                        return false;
110:                    }
111:                };
112:
113:                List list1 = new ArrayList();
114:                List list2 = new ArrayList();
115:
116:                list1.add(new Integer(1));
117:                list1.add(new Integer(2));
118:                list2.add(new Integer(3));
119:                list2.add(new Integer(4)); // will be ignored by the predicate
120:
121:                Iterator it1 = IteratorUtils.filteredIterator(list1.iterator(),
122:                        myPredicate);
123:                Iterator it2 = IteratorUtils.filteredIterator(list2.iterator(),
124:                        myPredicate);
125:
126:                Iterator it = IteratorUtils.chainedIterator(it1, it2);
127:                while (it.hasNext()) {
128:                    it.next();
129:                    it.remove();
130:                }
131:                assertEquals(0, list1.size());
132:                assertEquals(1, list2.size());
133:
134:            }
135:
136:            public void testRemove() {
137:                Iterator iter = (Iterator) makeFullIterator();
138:
139:                try {
140:                    iter.remove();
141:                    fail("Calling remove before the first call to next() should throw an exception");
142:                } catch (IllegalStateException e) {
143:
144:                }
145:
146:                for (int i = 0; i < testArray.length; i++) {
147:                    Object testValue = testArray[i];
148:                    Object iterValue = iter.next();
149:
150:                    assertEquals("Iteration value is correct", testValue,
151:                            iterValue);
152:
153:                    if (!iterValue.equals("Four")) {
154:                        iter.remove();
155:                    }
156:                }
157:
158:                assertTrue("List is empty", list1.size() == 0);
159:                assertTrue("List is empty", list2.size() == 1);
160:                assertTrue("List is empty", list3.size() == 0);
161:            }
162:
163:            public void testFirstIteratorIsEmptyBug() {
164:                List empty = new ArrayList();
165:                List notEmpty = new ArrayList();
166:                notEmpty.add("A");
167:                notEmpty.add("B");
168:                notEmpty.add("C");
169:                IteratorChain chain = new IteratorChain();
170:                chain.addIterator(empty.iterator());
171:                chain.addIterator(notEmpty.iterator());
172:                assertTrue("should have next", chain.hasNext());
173:                assertEquals("A", chain.next());
174:                assertTrue("should have next", chain.hasNext());
175:                assertEquals("B", chain.next());
176:                assertTrue("should have next", chain.hasNext());
177:                assertEquals("C", chain.next());
178:                assertTrue("should not have next", !chain.hasNext());
179:            }
180:
181:            public void testEmptyChain() {
182:                IteratorChain chain = new IteratorChain();
183:                assertEquals(false, chain.hasNext());
184:                try {
185:                    chain.next();
186:                    fail();
187:                } catch (NoSuchElementException ex) {
188:                }
189:                try {
190:                    chain.remove();
191:                    fail();
192:                } catch (IllegalStateException ex) {
193:                }
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.