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: }
|