Source Code Cross Referenced for TestSequencedHashMap.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) 


001:        /*
002:         *  Copyright 2001-2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */
016:        package org.apache.commons.collections;
017:
018:        import java.io.IOException;
019:        import java.io.Serializable;
020:        import java.util.Iterator;
021:        import java.util.List;
022:        import java.util.Map;
023:
024:        import junit.framework.Test;
025:
026:        import org.apache.commons.collections.map.AbstractTestMap;
027:
028:        /**
029:         * Unit tests 
030:         * {@link org.apache.commons.collections.SequencedHashMap}.
031:         * Be sure to use the "labRat" instance whenever possible,
032:         * so that subclasses will be tested correctly.
033:         * 
034:         * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
035:         * 
036:         * @author Morgan Delagrange
037:         * @author Daniel Rall
038:         * @author Henning P. Schmiedehausen
039:         * @author James Strachan
040:         */
041:        public class TestSequencedHashMap extends AbstractTestMap {
042:            /**
043:             * The instance to experiment on.
044:             */
045:            protected SequencedHashMap labRat;
046:
047:            public TestSequencedHashMap(String name) {
048:                super (name);
049:            }
050:
051:            public static Test suite() {
052:                return BulkTest.makeSuite(TestSequencedHashMap.class);
053:            }
054:
055:            // current versions of SequencedHashMap and subclasses are not
056:            // compatible with Collections 1.x
057:            public String getCompatibilityVersion() {
058:                return "2";
059:            }
060:
061:            public static void main(String[] args) {
062:                String[] testCaseName = { TestSequencedHashMap.class.getName() };
063:                junit.textui.TestRunner.main(testCaseName);
064:            }
065:
066:            public void setUp() throws Exception {
067:                super .setUp();
068:                // use makeMap and cast the result to a SeqHashMap
069:                // so that subclasses of SeqHashMap can share these tests
070:                labRat = (SequencedHashMap) makeEmptyMap();
071:            }
072:
073:            public Map makeEmptyMap() {
074:                return new SequencedHashMap();
075:            }
076:
077:            protected Object[] getKeys() {
078:                return new Object[] { "foo", "baz", "eek" };
079:            }
080:
081:            protected Object[] getValues() {
082:                return new Object[] { "bar", "frob", new Object() };
083:            }
084:
085:            public void testSequenceMap() throws Throwable {
086:                Object[] keys = getKeys();
087:                int expectedSize = keys.length;
088:                Object[] values = getValues();
089:                for (int i = 0; i < expectedSize; i++) {
090:                    labRat.put(keys[i], values[i]);
091:                }
092:
093:                // Test size().
094:                assertEquals("size() does not match expected size",
095:                        expectedSize, labRat.size());
096:
097:                // Test clone(), iterator(), and get(Object).
098:                SequencedHashMap clone = (SequencedHashMap) labRat.clone();
099:                assertEquals("Size of clone does not match original", labRat
100:                        .size(), clone.size());
101:                Iterator origEntries = labRat.entrySet().iterator();
102:                Iterator copiedEntries = clone.entrySet().iterator();
103:                while (origEntries.hasNext()) {
104:                    Map.Entry origEntry = (Map.Entry) origEntries.next();
105:                    Map.Entry copiedEntry = (Map.Entry) copiedEntries.next();
106:                    assertEquals("Cloned key does not match original",
107:                            origEntry.getKey(), copiedEntry.getKey());
108:                    assertEquals("Cloned value does not match original",
109:                            origEntry.getValue(), copiedEntry.getValue());
110:                    assertEquals("Cloned entry does not match original",
111:                            origEntry, copiedEntry);
112:                }
113:                assertTrue(
114:                        "iterator() returned different number of elements than keys()",
115:                        !copiedEntries.hasNext());
116:
117:                // Test sequence()
118:                List seq = labRat.sequence();
119:                assertEquals("sequence() returns more keys than in the Map",
120:                        expectedSize, seq.size());
121:
122:                for (int i = 0; i < seq.size(); i++) {
123:                    assertEquals("Key " + i
124:                            + " is not the same as the key in the Map",
125:                            keys[i], seq.get(i));
126:                }
127:            }
128:
129:            public void testYoungest() {
130:                labRat.put(new Integer(1), "foo");
131:                labRat.put(new Integer(2), "bar");
132:                assertTrue("first key is correct", labRat.get(0).equals(
133:                        new Integer(1)));
134:                labRat.put(new Integer(1), "boo");
135:                assertTrue("second key is reassigned to first", labRat.get(0)
136:                        .equals(new Integer(2)));
137:            }
138:
139:            public void testYoungestReplaceNullWithValue() {
140:                labRat.put(new Integer(1), null);
141:                labRat.put(new Integer(2), "foo");
142:                assertTrue("first key is correct", labRat.get(0).equals(
143:                        new Integer(1)));
144:                labRat.put(new Integer(1), "bar");
145:                assertTrue("second key is reassigned to first", labRat.get(0)
146:                        .equals(new Integer(2)));
147:            }
148:
149:            public void testYoungestReplaceValueWithNull() {
150:                labRat.put(new Integer(1), "bar");
151:                labRat.put(new Integer(2), "foo");
152:                assertTrue("first key is correct", labRat.get(0).equals(
153:                        new Integer(1)));
154:                labRat.put(new Integer(1), null);
155:                assertTrue("second key is reassigned to first", labRat.get(0)
156:                        .equals(new Integer(2)));
157:            }
158:
159:            // override TestMap method with more specific tests
160:            public void testFullMapSerialization() throws IOException,
161:                    ClassNotFoundException {
162:                SequencedHashMap map = (SequencedHashMap) makeFullMap();
163:
164:                if (!(map instanceof  Serializable))
165:                    return;
166:
167:                byte[] objekt = writeExternalFormToBytes((Serializable) map);
168:                SequencedHashMap map2 = (SequencedHashMap) readExternalFormFromBytes(objekt);
169:
170:                assertEquals("Both maps are same size", map.size(),
171:                        getSampleKeys().length);
172:                assertEquals("Both maps are same size", map2.size(),
173:                        getSampleKeys().length);
174:
175:                assertEquals("Both maps have the same first key", map
176:                        .getFirstKey(), getSampleKeys()[0]);
177:                assertEquals("Both maps have the same first key", map2
178:                        .getFirstKey(), getSampleKeys()[0]);
179:                assertEquals("Both maps have the same last key", map
180:                        .getLastKey(),
181:                        getSampleKeys()[getSampleKeys().length - 1]);
182:                assertEquals("Both maps have the same last key", map2
183:                        .getLastKey(),
184:                        getSampleKeys()[getSampleKeys().length - 1]);
185:            }
186:
187:            public void testIndexOf() throws Exception {
188:                Object[] keys = getKeys();
189:                int expectedSize = keys.length;
190:                Object[] values = getValues();
191:                for (int i = 0; i < expectedSize; i++) {
192:                    labRat.put(keys[i], values[i]);
193:                }
194:                // test that the index returned are in the same order that they were 
195:                // placed in the map
196:                for (int i = 0; i < keys.length; i++) {
197:                    assertEquals("indexOf with existing key failed", i, labRat
198:                            .indexOf(keys[i]));
199:                }
200:                // test non existing key..
201:                assertEquals("test with non-existing key failed", -1, labRat
202:                        .indexOf("NonExistingKey"));
203:            }
204:
205:            public void tearDown() throws Exception {
206:                labRat = null;
207:            }
208:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.