1 17 package org.apache.geronimo.kernel.basic; 18 19 import junit.framework.TestCase; 20 21 public class BasicProxyMapTest extends TestCase { 22 private BasicProxyMap map; 23 24 public void testPut() { 25 Object key1 = new Object (); 26 Object value1 = new Object (); 27 Object key2 = new Object (); 28 Object value2 = new Object (); 29 30 assertEquals(0,map.size()); 32 assertTrue(map.isEmpty()); 33 assertNull(map.get(key1)); 34 assertFalse(map.containsKey(key1)); 35 assertFalse(map.containsValue(value1)); 36 37 assertNull(map.put(key1, value1)); 39 assertEquals(1,map.size()); 40 assertFalse(map.isEmpty()); 41 assertSame(value1, map.put(key1,value2)); 42 assertSame(value2, map.put(key1,value1)); 43 assertNull(map.put(key2, value2)); 44 assertEquals(2,map.size()); 45 } 46 47 public void testContains() { 48 Object key1 = new Object (); 49 Object value1 = new Object (); 50 Object key2 = new Object (); 51 Object value2 = new Object (); 52 53 map.put(key1,value1); 54 map.put(key2, value2); 55 56 assertTrue(map.containsKey(key1)); 57 assertTrue(map.containsValue(value1)); 58 assertTrue(map.containsKey(key2)); 59 assertTrue(map.containsValue(value2)); 60 assertFalse(map.containsKey(new Object ())); 61 assertFalse(map.containsValue(new Object ())); 62 } 63 64 public void testGet() { 65 Object key1 = new Object (); 66 Object value1 = new Object (); 67 Object key2 = new Object (); 68 Object value2 = new Object (); 69 70 map.put(key1,value1); 71 map.put(key2, value2); 72 73 assertSame(value1,map.get(key1)); 74 assertSame(value2,map.get(key2)); 75 assertNull(map.get(new Object ())); 76 } 77 78 public void testRemove() { 79 Object key1 = new Object (); 80 Object value1 = new Object (); 81 Object key2 = new Object (); 82 Object value2 = new Object (); 83 84 map.put(key1,value1); 85 map.put(key2, value2); 86 87 assertSame(value1,map.remove(key1)); 88 assertEquals(1,map.size()); 89 assertFalse(map.isEmpty()); 90 assertFalse(map.containsKey(key1)); 91 assertFalse(map.containsValue(value1)); 92 assertNull(map.get(key1)); 93 assertNull(map.put(key1, value1)); 94 } 95 96 public void testClear() { 97 Object key1 = new Object (); 98 Object value1 = new Object (); 99 Object key2 = new Object (); 100 Object value2 = new Object (); 101 102 map.put(key1,value1); 103 map.put(key2, value2); 104 map.clear(); 105 106 assertEquals(0,map.size()); 107 assertTrue(map.isEmpty()); 108 assertNull(map.get(key1)); 109 assertFalse(map.containsKey(key1)); 110 assertFalse(map.containsValue(value1)); 111 } 112 113 public void testIdentitySemantic() { 114 Object value1 = new Object (); 115 Object value2 = new Object (); 116 117 class IdentityTest1 { 118 public int hashCode() { return 1; } 119 public boolean equals(Object o) { return true; } 120 } 121 class IdentityTest2 { 122 public int hashCode() { return 1; } 123 public boolean equals(Object o) { return false; } 124 } 125 126 Object key1 = new IdentityTest1(); 127 Object key2 = new IdentityTest1(); 128 129 assertNull(map.put(key1, value1)); 130 assertNull(map.get(key2)); 131 assertNull(map.put(key2, value2)); 132 assertSame(value1, map.get(key1)); 133 assertSame(value2, map.get(key2)); 134 map.clear(); 135 136 key1 = new IdentityTest2(); 137 key2 = new IdentityTest2(); 138 139 assertNull(map.put(key1, value1)); 140 assertNull(map.get(key2)); 141 assertNull(map.put(key2, value2)); 142 assertSame(value1, map.get(key1)); 143 assertSame(value2, map.get(key2)); 144 } 145 146 public void testWeakSemantic() { 147 int MAX = 256; 148 Object [] keys = new Object [MAX]; 149 Object [] values = new Object [MAX]; 150 for (int i=0;i<MAX;i++) { 151 keys[i] = new Object (); values[i] = new Object (); 152 assertNull(map.put(keys[i],values[i])); 153 } 154 for (int i=0;i<MAX;i++) { 155 assertEquals(values[i], map.get(keys[i])); 156 } 157 158 for (int i=0;i<MAX;i++) { 160 assertNull(map.put(new Object (), new Object ())); 161 } 162 System.gc(); 163 try {Thread.sleep(1000);} catch (InterruptedException ie) {} 164 assertEquals(MAX,map.size()); 165 for (int i=0;i<MAX;i++) { 166 assertSame(values[i], map.get(keys[i])); 167 } 168 } 169 170 protected void setUp() throws Exception { 171 super.setUp(); 172 map = new BasicProxyMap(); 173 } 174 } 175 | Popular Tags |