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.map;
017:
018: import java.io.IOException;
019: import java.io.Serializable;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import junit.framework.Test;
024: import junit.framework.TestSuite;
025: import junit.textui.TestRunner;
026:
027: import org.apache.commons.collections.AbstractTestObject;
028: import org.apache.commons.collections.IterableMap;
029:
030: /**
031: * JUnit tests.
032: *
033: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
034: *
035: * @author Stephen Colebourne
036: */
037: public class TestIdentityMap extends AbstractTestObject {
038:
039: private static final Integer I1A = new Integer(1);
040: private static final Integer I1B = new Integer(1);
041: private static final Integer I2A = new Integer(2);
042: private static final Integer I2B = new Integer(2);
043:
044: public TestIdentityMap(String testName) {
045: super (testName);
046: }
047:
048: public static void main(String[] args) {
049: TestRunner.run(suite());
050: }
051:
052: public static Test suite() {
053: return new TestSuite(TestIdentityMap.class);
054: // return BulkTest.makeSuite(TestIdentityMap.class); // causes race condition!
055: }
056:
057: public Object makeObject() {
058: return new IdentityMap();
059: }
060:
061: public String getCompatibilityVersion() {
062: return "3";
063: }
064:
065: //-----------------------------------------------------------------------
066: public void testBasics() {
067: IterableMap map = new IdentityMap();
068: assertEquals(0, map.size());
069:
070: map.put(I1A, I2A);
071: assertEquals(1, map.size());
072: assertSame(I2A, map.get(I1A));
073: assertSame(null, map.get(I1B));
074: assertEquals(true, map.containsKey(I1A));
075: assertEquals(false, map.containsKey(I1B));
076: assertEquals(true, map.containsValue(I2A));
077: assertEquals(false, map.containsValue(I2B));
078:
079: map.put(I1A, I2B);
080: assertEquals(1, map.size());
081: assertSame(I2B, map.get(I1A));
082: assertSame(null, map.get(I1B));
083: assertEquals(true, map.containsKey(I1A));
084: assertEquals(false, map.containsKey(I1B));
085: assertEquals(false, map.containsValue(I2A));
086: assertEquals(true, map.containsValue(I2B));
087:
088: map.put(I1B, I2B);
089: assertEquals(2, map.size());
090: assertSame(I2B, map.get(I1A));
091: assertSame(I2B, map.get(I1B));
092: assertEquals(true, map.containsKey(I1A));
093: assertEquals(true, map.containsKey(I1B));
094: assertEquals(false, map.containsValue(I2A));
095: assertEquals(true, map.containsValue(I2B));
096: }
097:
098: //-----------------------------------------------------------------------
099: public void testHashEntry() {
100: IterableMap map = new IdentityMap();
101:
102: map.put(I1A, I2A);
103: map.put(I1B, I2A);
104:
105: Map.Entry entry1 = (Map.Entry) map.entrySet().iterator().next();
106: Iterator it = map.entrySet().iterator();
107: Map.Entry entry2 = (Map.Entry) it.next();
108: Map.Entry entry3 = (Map.Entry) it.next();
109:
110: assertEquals(true, entry1.equals(entry2));
111: assertEquals(true, entry2.equals(entry1));
112: assertEquals(false, entry1.equals(entry3));
113: }
114:
115: /**
116: * Compare the current serialized form of the Map
117: * against the canonical version in CVS.
118: */
119: public void testEmptyMapCompatibility() throws IOException,
120: ClassNotFoundException {
121: // test to make sure the canonical form has been preserved
122: Map map = (Map) makeObject();
123: if (map instanceof Serializable
124: && !skipSerializedCanonicalTests()) {
125: Map map2 = (Map) readExternalFormFromDisk(getCanonicalEmptyCollectionName(map));
126: assertEquals("Map is empty", 0, map2.size());
127: }
128: }
129:
130: public void testClone() {
131: IdentityMap map = new IdentityMap(10);
132: map.put("1", "1");
133: Map cloned = (Map) map.clone();
134: assertEquals(map.size(), cloned.size());
135: assertSame(map.get("1"), cloned.get("1"));
136: }
137:
138: // public void testCreate() throws Exception {
139: // Map map = new IdentityMap();
140: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/IdentityMap.emptyCollection.version3.obj");
141: // map = new IdentityMap();
142: // map.put(I1A, I2A);
143: // map.put(I1B, I2A);
144: // map.put(I2A, I2B);
145: // writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/IdentityMap.fullCollection.version3.obj");
146: // }
147: }
|