1 16 package org.apache.commons.collections.map; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.Iterator ; 21 import java.util.Map ; 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 37 public class TestIdentityMap extends AbstractTestObject { 38 39 private static final Integer I1A = new Integer (1); 40 private static final Integer I1B = new Integer (1); 41 private static final Integer I2A = new Integer (2); 42 private static final Integer I2B = new Integer (2); 43 44 public TestIdentityMap(String testName) { 45 super(testName); 46 } 47 48 public static void main(String [] args) { 49 TestRunner.run(suite()); 50 } 51 52 public static Test suite() { 53 return new TestSuite(TestIdentityMap.class); 54 } 56 57 public Object makeObject() { 58 return new IdentityMap(); 59 } 60 61 public String getCompatibilityVersion() { 62 return "3"; 63 } 64 65 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 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 119 public void testEmptyMapCompatibility() throws IOException , ClassNotFoundException { 120 Map map = (Map ) makeObject(); 122 if (map instanceof Serializable && !skipSerializedCanonicalTests()) { 123 Map map2 = (Map ) 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 cloned = (Map ) map.clone(); 132 assertEquals(map.size(), cloned.size()); 133 assertSame(map.get("1"), cloned.get("1")); 134 } 135 136 } 146 | Popular Tags |