KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TestIdentityMap


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.map;
17
18 import java.io.IOException JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25 import junit.textui.TestRunner;
26
27 import org.apache.commons.collections.AbstractTestObject;
28 import org.apache.commons.collections.IterableMap;
29
30 /**
31  * JUnit tests.
32  *
33  * @version $Revision: 1.7 $ $Date: 2004/02/27 00:25:14 $
34  *
35  * @author Stephen Colebourne
36  */

37 public class TestIdentityMap extends AbstractTestObject {
38     
39     private static final Integer JavaDoc I1A = new Integer JavaDoc(1);
40     private static final Integer JavaDoc I1B = new Integer JavaDoc(1);
41     private static final Integer JavaDoc I2A = new Integer JavaDoc(2);
42     private static final Integer JavaDoc I2B = new Integer JavaDoc(2);
43
44     public TestIdentityMap(String JavaDoc testName) {
45         super(testName);
46     }
47
48     public static void main(String JavaDoc[] args) {
49         TestRunner.run(suite());
50     }
51     
52     public static Test suite() {
53         return new TestSuite(TestIdentityMap.class);
54 // return BulkTest.makeSuite(TestIdentityMap.class); // causes race condition!
55
}
56     
57     public Object JavaDoc makeObject() {
58         return new IdentityMap();
59     }
60     
61     public String JavaDoc getCompatibilityVersion() {
62         return "3";
63     }
64     
65     //-----------------------------------------------------------------------
66
public void testBasics() {
67         IterableMap map = new IdentityMap();
68         assertEquals(0, map.size());
69         
70         map.put(I1A, I2A);
71         assertEquals(1, map.size());
72         assertSame(I2A, map.get(I1A));
73         assertSame(null, map.get(I1B));
74         assertEquals(true, map.containsKey(I1A));
75         assertEquals(false, map.containsKey(I1B));
76         assertEquals(true, map.containsValue(I2A));
77         assertEquals(false, map.containsValue(I2B));
78         
79         map.put(I1A, I2B);
80         assertEquals(1, map.size());
81         assertSame(I2B, map.get(I1A));
82         assertSame(null, map.get(I1B));
83         assertEquals(true, map.containsKey(I1A));
84         assertEquals(false, map.containsKey(I1B));
85         assertEquals(false, map.containsValue(I2A));
86         assertEquals(true, map.containsValue(I2B));
87         
88         map.put(I1B, I2B);
89         assertEquals(2, map.size());
90         assertSame(I2B, map.get(I1A));
91         assertSame(I2B, map.get(I1B));
92         assertEquals(true, map.containsKey(I1A));
93         assertEquals(true, map.containsKey(I1B));
94         assertEquals(false, map.containsValue(I2A));
95         assertEquals(true, map.containsValue(I2B));
96     }
97     
98     //-----------------------------------------------------------------------
99
public void testHashEntry() {
100         IterableMap map = new IdentityMap();
101         
102         map.put(I1A, I2A);
103         map.put(I1B, I2A);
104         
105         Map.Entry JavaDoc entry1 = (Map.Entry JavaDoc) map.entrySet().iterator().next();
106         Iterator JavaDoc it = map.entrySet().iterator();
107         Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc) it.next();
108         Map.Entry JavaDoc entry3 = (Map.Entry JavaDoc) 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 JavaDoc, ClassNotFoundException JavaDoc {
120         // test to make sure the canonical form has been preserved
121
Map JavaDoc map = (Map JavaDoc) makeObject();
122         if (map instanceof Serializable JavaDoc && !skipSerializedCanonicalTests()) {
123             Map JavaDoc map2 = (Map JavaDoc) readExternalFormFromDisk(getCanonicalEmptyCollectionName(map));
124             assertEquals("Map is empty", 0, map2.size());
125         }
126     }
127
128     public void testClone() {
129         IdentityMap map = new IdentityMap(10);
130         map.put("1", "1");
131         Map JavaDoc cloned = (Map JavaDoc) map.clone();
132         assertEquals(map.size(), cloned.size());
133         assertSame(map.get("1"), cloned.get("1"));
134     }
135     
136 // public void testCreate() throws Exception {
137
// Map map = new IdentityMap();
138
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/IdentityMap.emptyCollection.version3.obj");
139
// map = new IdentityMap();
140
// map.put(I1A, I2A);
141
// map.put(I1B, I2A);
142
// map.put(I2A, I2B);
143
// writeExternalFormToDisk((java.io.Serializable) map, "D:/dev/collections/data/test/IdentityMap.fullCollection.version3.obj");
144
// }
145
}
146
Popular Tags