KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestSequencedHashMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import junit.framework.Test;
25
26 import org.apache.commons.collections.map.AbstractTestMap;
27
28 /**
29  * Unit tests
30  * {@link org.apache.commons.collections.SequencedHashMap}.
31  * Be sure to use the "labRat" instance whenever possible,
32  * so that subclasses will be tested correctly.
33  *
34  * @version $Revision: 1.26 $ $Date: 2004/02/18 01:20:35 $
35  *
36  * @author Morgan Delagrange
37  * @author Daniel Rall
38  * @author Henning P. Schmiedehausen
39  * @author James Strachan
40  */

41 public class TestSequencedHashMap extends AbstractTestMap {
42     /**
43      * The instance to experiment on.
44      */

45     protected SequencedHashMap labRat;
46
47     public TestSequencedHashMap(String JavaDoc name) {
48         super(name);
49     }
50
51     public static Test suite() {
52         return BulkTest.makeSuite(TestSequencedHashMap.class);
53     }
54
55     // current versions of SequencedHashMap and subclasses are not
56
// compatible with Collections 1.x
57
public String JavaDoc getCompatibilityVersion() {
58         return "2";
59     }
60
61     public static void main(String JavaDoc[] args) {
62         String JavaDoc[] testCaseName = { TestSequencedHashMap.class.getName() };
63         junit.textui.TestRunner.main(testCaseName);
64     }
65
66     public void setUp() throws Exception JavaDoc {
67         super.setUp();
68         // use makeMap and cast the result to a SeqHashMap
69
// so that subclasses of SeqHashMap can share these tests
70
labRat = (SequencedHashMap) makeEmptyMap();
71     }
72
73     public Map JavaDoc makeEmptyMap() {
74         return new SequencedHashMap();
75     }
76
77     protected Object JavaDoc[] getKeys() {
78         return new Object JavaDoc[] { "foo", "baz", "eek" };
79     }
80
81     protected Object JavaDoc[] getValues() {
82         return new Object JavaDoc[] { "bar", "frob", new Object JavaDoc() };
83     }
84  
85     public void testSequenceMap() throws Throwable JavaDoc {
86         Object JavaDoc[] keys = getKeys();
87         int expectedSize = keys.length;
88         Object JavaDoc[] values = getValues();
89         for (int i = 0; i < expectedSize; i++) {
90             labRat.put(keys[i], values[i]);
91         }
92
93         // Test size().
94
assertEquals("size() does not match expected size",
95                      expectedSize, labRat.size());
96
97         // Test clone(), iterator(), and get(Object).
98
SequencedHashMap clone = (SequencedHashMap) labRat.clone();
99         assertEquals("Size of clone does not match original",
100                      labRat.size(), clone.size());
101         Iterator JavaDoc origEntries = labRat.entrySet().iterator();
102         Iterator JavaDoc copiedEntries = clone.entrySet().iterator();
103         while (origEntries.hasNext()) {
104             Map.Entry JavaDoc origEntry = (Map.Entry JavaDoc)origEntries.next();
105             Map.Entry JavaDoc copiedEntry = (Map.Entry JavaDoc)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("iterator() returned different number of elements than keys()",
114                !copiedEntries.hasNext());
115
116         // Test sequence()
117
List JavaDoc seq = labRat.sequence();
118         assertEquals("sequence() returns more keys than in the Map",
119                      expectedSize, seq.size());
120
121         for (int i = 0; i < seq.size(); i++) {
122             assertEquals("Key " + i + " is not the same as the key in the Map",
123                          keys[i], seq.get(i));
124         }
125     }
126
127     public void testYoungest() {
128         labRat.put(new Integer JavaDoc(1),"foo");
129         labRat.put(new Integer JavaDoc(2),"bar");
130         assertTrue("first key is correct",labRat.get(0).equals(new Integer JavaDoc(1)));
131         labRat.put(new Integer JavaDoc(1),"boo");
132         assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer JavaDoc(2)));
133     }
134
135     public void testYoungestReplaceNullWithValue() {
136         labRat.put(new Integer JavaDoc(1),null);
137         labRat.put(new Integer JavaDoc(2),"foo");
138         assertTrue("first key is correct",labRat.get(0).equals(new Integer JavaDoc(1)));
139         labRat.put(new Integer JavaDoc(1),"bar");
140         assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer JavaDoc(2)));
141     }
142
143     public void testYoungestReplaceValueWithNull() {
144         labRat.put(new Integer JavaDoc(1),"bar");
145         labRat.put(new Integer JavaDoc(2),"foo");
146         assertTrue("first key is correct",labRat.get(0).equals(new Integer JavaDoc(1)));
147         labRat.put(new Integer JavaDoc(1),null);
148         assertTrue("second key is reassigned to first",labRat.get(0).equals(new Integer JavaDoc(2)));
149     }
150
151     // override TestMap method with more specific tests
152
public void testFullMapSerialization()
153     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
154         SequencedHashMap map = (SequencedHashMap) makeFullMap();
155
156         if (!(map instanceof Serializable JavaDoc)) return;
157
158         byte[] objekt = writeExternalFormToBytes((Serializable JavaDoc) map);
159         SequencedHashMap map2 = (SequencedHashMap) readExternalFormFromBytes(objekt);
160
161         assertEquals("Both maps are same size",map.size(), getSampleKeys().length);
162         assertEquals("Both maps are same size",map2.size(),getSampleKeys().length);
163
164         assertEquals("Both maps have the same first key",
165                      map.getFirstKey(),getSampleKeys()[0]);
166         assertEquals("Both maps have the same first key",
167                      map2.getFirstKey(),getSampleKeys()[0]);
168         assertEquals("Both maps have the same last key",
169                      map.getLastKey(),getSampleKeys()[getSampleKeys().length - 1]);
170         assertEquals("Both maps have the same last key",
171                      map2.getLastKey(),getSampleKeys()[getSampleKeys().length - 1]);
172     }
173
174     public void testIndexOf() throws Exception JavaDoc {
175         Object JavaDoc[] keys = getKeys();
176         int expectedSize = keys.length;
177         Object JavaDoc[] values = getValues();
178         for (int i = 0; i < expectedSize; i++) {
179             labRat.put(keys[i], values[i]);
180         }
181         // test that the index returned are in the same order that they were
182
// placed in the map
183
for (int i = 0; i < keys.length; i++) {
184             assertEquals("indexOf with existing key failed", i, labRat.indexOf(keys[i]));
185         }
186         // test non existing key..
187
assertEquals("test with non-existing key failed", -1, labRat.indexOf("NonExistingKey"));
188     }
189     
190     public void tearDown() throws Exception JavaDoc {
191         labRat = null;
192     }
193 }
194
Popular Tags