Source Code Cross Referenced for TestSetUniqueList.java in  » Library » Apache-common-Collections » org » apache » commons » collections » list » 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.list 
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.list;
017:
018:        import java.util.ArrayList;
019:        import java.util.Arrays;
020:        import java.util.HashSet;
021:        import java.util.LinkedList;
022:        import java.util.List;
023:        import java.util.ListIterator;
024:
025:        import junit.framework.Test;
026:        import junit.framework.TestSuite;
027:        import junit.textui.TestRunner;
028:
029:        /**
030:         * JUnit tests.
031:         *
032:         * @since Commons Collections 3.0
033:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034:         * 
035:         * @author Matthew Hawthorne
036:         * @author Tom Dunham
037:         */
038:        public class TestSetUniqueList extends AbstractTestList {
039:
040:            public static void main(String[] args) {
041:                TestRunner.run(suite());
042:            }
043:
044:            public static Test suite() {
045:                return new TestSuite(TestSetUniqueList.class);
046:            }
047:
048:            public TestSetUniqueList(String testName) {
049:                super (testName);
050:            }
051:
052:            //-----------------------------------------------------------------------
053:            public List makeEmptyList() {
054:                return new SetUniqueList(new ArrayList(), new HashSet());
055:            }
056:
057:            public void testListIteratorSet() {
058:                // override to block
059:                resetFull();
060:                ListIterator it = getList().listIterator();
061:                it.next();
062:                try {
063:                    it.set(null);
064:                    fail();
065:                } catch (UnsupportedOperationException ex) {
066:                }
067:            }
068:
069:            public Object[] getFullNonNullElements() {
070:                // override to avoid duplicate "One"
071:                return new Object[] { new String(""), new String("One"),
072:                        new Integer(2), "Three", new Integer(4), new Double(5),
073:                        new Float(6), "Seven", "Eight", new String("Nine"),
074:                        new Integer(10), new Short((short) 11), new Long(12),
075:                        "Thirteen", "14", "15", new Byte((byte) 16) };
076:            }
077:
078:            public void testListIteratorAdd() {
079:                // override to cope with Set behaviour
080:                resetEmpty();
081:                List list1 = getList();
082:                List list2 = getConfirmedList();
083:
084:                Object[] elements = getOtherElements(); // changed here
085:                ListIterator iter1 = list1.listIterator();
086:                ListIterator iter2 = list2.listIterator();
087:
088:                for (int i = 0; i < elements.length; i++) {
089:                    iter1.add(elements[i]);
090:                    iter2.add(elements[i]);
091:                    super .verify(); // changed here
092:                }
093:
094:                resetFull();
095:                iter1 = getList().listIterator();
096:                iter2 = getConfirmedList().listIterator();
097:                for (int i = 0; i < elements.length; i++) {
098:                    iter1.next();
099:                    iter2.next();
100:                    iter1.add(elements[i]);
101:                    iter2.add(elements[i]);
102:                    super .verify(); // changed here
103:                }
104:            }
105:
106:            public void testCollectionAddAll() {
107:                // override for set behaviour
108:                resetEmpty();
109:                Object[] elements = getFullElements();
110:                boolean r = collection.addAll(Arrays.asList(elements));
111:                confirmed.addAll(Arrays.asList(elements));
112:                verify();
113:                assertTrue("Empty collection should change after addAll", r);
114:                for (int i = 0; i < elements.length; i++) {
115:                    assertTrue("Collection should contain added element",
116:                            collection.contains(elements[i]));
117:                }
118:
119:                resetFull();
120:                int size = collection.size();
121:                elements = getOtherElements();
122:                r = collection.addAll(Arrays.asList(elements));
123:                confirmed.addAll(Arrays.asList(elements));
124:                verify();
125:                assertTrue("Full collection should change after addAll", r);
126:                for (int i = 0; i < elements.length; i++) {
127:                    assertTrue("Full collection should contain added element "
128:                            + i, collection.contains(elements[i]));
129:                }
130:                assertEquals("Size should increase after addAll", size
131:                        + elements.length, collection.size());
132:            }
133:
134:            public void testListSetByIndex() {
135:                // override for set behaviour
136:                resetFull();
137:                int size = collection.size();
138:                getList().set(0, new Long(1000));
139:                assertEquals(size, collection.size());
140:
141:                getList().set(2, new Long(1000));
142:                assertEquals(size - 1, collection.size());
143:                assertEquals(new Long(1000), getList().get(1)); // set into 2, but shifted down to 1
144:            }
145:
146:            boolean extraVerify = true;
147:
148:            public void testCollectionIteratorRemove() {
149:                try {
150:                    extraVerify = false;
151:                    super .testCollectionIteratorRemove();
152:                } finally {
153:                    extraVerify = true;
154:                }
155:            }
156:
157:            public void verify() {
158:                super .verify();
159:
160:                if (extraVerify) {
161:                    int size = collection.size();
162:                    getList().add(new Long(1000));
163:                    assertEquals(size + 1, collection.size());
164:
165:                    getList().add(new Long(1000));
166:                    assertEquals(size + 1, collection.size());
167:                    assertEquals(new Long(1000), getList().get(size));
168:
169:                    getList().remove(size);
170:                }
171:            }
172:
173:            //-----------------------------------------------------------------------
174:            public void testFactory() {
175:                Integer[] array = new Integer[] { new Integer(1),
176:                        new Integer(2), new Integer(1) };
177:                ArrayList list = new ArrayList(Arrays.asList(array));
178:                final SetUniqueList lset = SetUniqueList.decorate(list);
179:
180:                assertEquals("Duplicate element was added.", 2, lset.size());
181:                assertEquals(new Integer(1), lset.get(0));
182:                assertEquals(new Integer(2), lset.get(1));
183:                assertEquals(new Integer(1), list.get(0));
184:                assertEquals(new Integer(2), list.get(1));
185:            }
186:
187:            public void testAdd() {
188:                final SetUniqueList lset = new SetUniqueList(new ArrayList(),
189:                        new HashSet());
190:
191:                // Duplicate element
192:                final Object obj = new Integer(1);
193:                lset.add(obj);
194:                lset.add(obj);
195:                assertEquals("Duplicate element was added.", 1, lset.size());
196:
197:                // Unique element
198:                lset.add(new Integer(2));
199:                assertEquals("Unique element was not added.", 2, lset.size());
200:            }
201:
202:            public void testAddAll() {
203:                final SetUniqueList lset = new SetUniqueList(new ArrayList(),
204:                        new HashSet());
205:
206:                lset.addAll(Arrays.asList(new Integer[] { new Integer(1),
207:                        new Integer(1) }));
208:
209:                assertEquals("Duplicate element was added.", 1, lset.size());
210:            }
211:
212:            public void testSet() {
213:                final SetUniqueList lset = new SetUniqueList(new ArrayList(),
214:                        new HashSet());
215:
216:                // Duplicate element
217:                final Object obj1 = new Integer(1);
218:                final Object obj2 = new Integer(2);
219:                final Object obj3 = new Integer(3);
220:
221:                lset.add(obj1);
222:                lset.add(obj2);
223:                lset.set(0, obj1);
224:                assertEquals(2, lset.size());
225:                assertSame(obj1, lset.get(0));
226:                assertSame(obj2, lset.get(1));
227:
228:                lset.clear();
229:                lset.add(obj1);
230:                lset.add(obj2);
231:                lset.set(0, obj2);
232:                assertEquals(1, lset.size());
233:                assertSame(obj2, lset.get(0));
234:
235:                lset.clear();
236:                lset.add(obj1);
237:                lset.add(obj2);
238:                lset.set(0, obj3);
239:                assertEquals(2, lset.size());
240:                assertSame(obj3, lset.get(0));
241:                assertSame(obj2, lset.get(1));
242:
243:                lset.clear();
244:                lset.add(obj1);
245:                lset.add(obj2);
246:                lset.set(1, obj1);
247:                assertEquals(1, lset.size());
248:                assertSame(obj1, lset.get(0));
249:            }
250:
251:            public void testListIterator() {
252:                final SetUniqueList lset = new SetUniqueList(new ArrayList(),
253:                        new HashSet());
254:
255:                final Object obj1 = new Integer(1);
256:                final Object obj2 = new Integer(2);
257:                lset.add(obj1);
258:                lset.add(obj2);
259:
260:                // Attempts to add a duplicate object
261:                for (final ListIterator it = lset.listIterator(); it.hasNext();) {
262:                    it.next();
263:
264:                    if (!it.hasNext()) {
265:                        it.add(obj1);
266:                        break;
267:                    }
268:                }
269:
270:                assertEquals("Duplicate element was added", 2, lset.size());
271:            }
272:
273:            public void testUniqueListReInsert() {
274:                List l = SetUniqueList.decorate(new LinkedList());
275:                l.add(new Object());
276:                l.add(new Object());
277:
278:                Object a = l.get(0);
279:
280:                // duplicate is removed
281:                l.set(0, l.get(1));
282:                assertEquals(1, l.size());
283:
284:                // old object is added back in 
285:                l.add(1, a);
286:                assertEquals(2, l.size());
287:            }
288:
289:            public void testUniqueListDoubleInsert() {
290:                List l = SetUniqueList.decorate(new LinkedList());
291:                l.add(new Object());
292:                l.add(new Object());
293:
294:                // duplicate is removed
295:                l.set(0, l.get(1));
296:                assertEquals(1, l.size());
297:
298:                // duplicate should be removed again
299:                l.add(1, l.get(0));
300:                assertEquals(1, l.size());
301:            }
302:
303:            public void testSetDownwardsInList() {
304:                /*
305:                 * Checks the following semantics
306:                 * [a,b]
307:                 * set(0,b): [b]->a
308:                 * So UniqList contains [b] and a is returned
309:                 */
310:                ArrayList l = new ArrayList();
311:                HashSet s = new HashSet();
312:                final SetUniqueList ul = new SetUniqueList(l, s);
313:
314:                Object a = new Object();
315:                Object b = new Object();
316:                ul.add(a);
317:                ul.add(b);
318:                assertEquals(a, l.get(0));
319:                assertEquals(b, l.get(1));
320:                assertTrue(s.contains(a));
321:                assertTrue(s.contains(b));
322:
323:                assertEquals(a, ul.set(0, b));
324:                assertEquals(1, s.size());
325:                assertEquals(1, l.size());
326:                assertEquals(b, l.get(0));
327:                assertTrue(s.contains(b));
328:                assertFalse(s.contains(a));
329:            }
330:
331:            public void testSetInBiggerList() {
332:                /*
333:                 * Checks the following semantics
334:                 * [a,b,c]
335:                 * set(0,b): [b,c]->a
336:                 * So UniqList contains [b,c] and a is returned
337:                 */
338:                ArrayList l = new ArrayList();
339:                HashSet s = new HashSet();
340:                final SetUniqueList ul = new SetUniqueList(l, s);
341:
342:                Object a = new Object();
343:                Object b = new Object();
344:                Object c = new Object();
345:
346:                ul.add(a);
347:                ul.add(b);
348:                ul.add(c);
349:                assertEquals(a, l.get(0));
350:                assertEquals(b, l.get(1));
351:                assertEquals(c, l.get(2));
352:                assertTrue(s.contains(a));
353:                assertTrue(s.contains(b));
354:                assertTrue(s.contains(c));
355:
356:                assertEquals(a, ul.set(0, b));
357:                assertEquals(2, s.size());
358:                assertEquals(2, l.size());
359:                assertEquals(b, l.get(0));
360:                assertEquals(c, l.get(1));
361:                assertFalse(s.contains(a));
362:                assertTrue(s.contains(b));
363:                assertTrue(s.contains(c));
364:            }
365:
366:            public void testSetUpwardsInList() {
367:                /*
368:                 * Checks the following semantics
369:                 * [a,b,c]
370:                 * set(1,a): [a,c]->b
371:                 * So UniqList contains [a,c] and b is returned
372:                 */
373:                ArrayList l = new ArrayList();
374:                HashSet s = new HashSet();
375:                final SetUniqueList ul = new SetUniqueList(l, s);
376:
377:                Object a = new String("A");
378:                Object b = new String("B");
379:                Object c = new String("C");
380:
381:                ul.add(a);
382:                ul.add(b);
383:                ul.add(c);
384:                assertEquals(a, l.get(0));
385:                assertEquals(b, l.get(1));
386:                assertEquals(c, l.get(2));
387:                assertTrue(s.contains(a));
388:                assertTrue(s.contains(b));
389:                assertTrue(s.contains(c));
390:
391:                assertEquals(b, ul.set(1, a));
392:                assertEquals(2, s.size());
393:                assertEquals(2, l.size());
394:                assertEquals(a, l.get(0));
395:                assertEquals(c, l.get(1));
396:                assertTrue(s.contains(a));
397:                assertFalse(s.contains(b));
398:                assertTrue(s.contains(c));
399:            }
400:
401:            //-----------------------------------------------------------------------
402:            public String getCompatibilityVersion() {
403:                return "3.1";
404:            }
405:
406:            //    public void testCreate() throws Exception {
407:            //        resetEmpty();
408:            //        writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SetUniqueList.emptyCollection.version3.1.obj");
409:            //        resetFull();
410:            //        writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/SetUniqueList.fullCollection.version3.1.obj");
411:            //    }
412:
413:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.