1 16 17 package com.google.inject.util; 18 19 import junit.framework.Test; 20 import junit.framework.TestCase; 21 import junit.framework.TestSuite; 22 23 import java.io.ByteArrayInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.IOException ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.lang.reflect.Method ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.Map ; 33 import java.util.Set ; 34 35 38 @SuppressWarnings ({"unchecked"}) 39 public class ReferenceMapTestSuite { 40 41 public static Test suite() { 42 TestSuite suite = new TestSuite(); 43 44 Set <ReferenceType> referenceTypes = 45 new HashSet <ReferenceType>(); 46 referenceTypes.addAll(Arrays.asList(ReferenceType.values())); 47 referenceTypes.remove(ReferenceType.PHANTOM); 48 49 for (Method method : MapTest.class.getMethods()) { 51 String name = method.getName(); 52 if (name.startsWith("test")) { 53 for (ReferenceType keyType : referenceTypes) { 54 for (ReferenceType valueType : referenceTypes) { 55 suite.addTest(new MapTest(name, keyType, valueType)); 56 } 57 } 58 } 59 } 60 61 return suite; 62 } 63 64 public static class MapTest extends TestCase { 65 66 final ReferenceType keyType; 67 final ReferenceType valueType; 68 69 public MapTest(String name, ReferenceType keyType, 70 ReferenceType valueType) { 71 super(name); 72 this.keyType = keyType; 73 this.valueType = valueType; 74 } 75 76 public String getName() { 77 return super.getName() 78 + "For" 79 + Strings.capitalize(keyType.toString().toLowerCase()) 80 + Strings.capitalize(valueType.toString().toLowerCase()); 81 } 82 83 ReferenceMap newInstance() { 84 return new ReferenceMap(keyType, valueType); 85 } 86 87 public void testContainsKey() { 88 ReferenceMap map = newInstance(); 89 Object k = "key"; 90 map.put(k, "value"); 91 assertTrue(map.containsKey(k)); 92 } 93 94 public void testClear() { 95 ReferenceMap map = newInstance(); 96 String k = "key"; 97 map.put(k, "value"); 98 assertFalse(map.isEmpty()); 99 map.clear(); 100 assertTrue(map.isEmpty()); 101 assertNull(map.get(k)); 102 } 103 104 public void testKeySet() { 105 ReferenceMap map = newInstance(); 106 map.put("a", "foo"); 107 map.put("b", "foo"); 108 Set expected = new HashSet (Arrays.asList("a", "b")); 109 assertEquals(expected, map.keySet()); 110 } 111 112 public void testValues() { 113 ReferenceMap map = newInstance(); 114 map.put("a", "1"); 115 map.put("b", "2"); 116 Set expected = new HashSet (Arrays.asList("1", "2")); 117 Set actual = new HashSet (); 118 actual.addAll(map.values()); 119 assertEquals(expected, actual); 120 } 121 122 public void testPutIfAbsent() { 123 ReferenceMap map = newInstance(); 124 map.putIfAbsent("a", "1"); 125 assertEquals("1", map.get("a")); 126 map.putIfAbsent("a", "2"); 127 assertEquals("1", map.get("a")); 128 } 129 130 public void testReplace() { 131 ReferenceMap map = newInstance(); 132 map.put("a", "1"); 133 map.replace("a", "2", "2"); 134 assertEquals("1", map.get("a")); 135 map.replace("a", "1", "2"); 136 assertEquals("2", map.get("a")); 137 } 138 139 public void testContainsValue() { 140 ReferenceMap map = newInstance(); 141 Object v = "value"; 142 map.put("key", v); 143 assertTrue(map.containsValue(v)); 144 } 145 146 public void testEntrySet() { 147 final ReferenceMap map = newInstance(); 148 map.put("a", "1"); 149 map.put("b", "2"); 150 Set expected = new HashSet (Arrays.asList( 151 map.new Entry("a", "1"), 152 map.new Entry("b", "2") 153 )); 154 assertEquals(expected, map.entrySet()); 155 } 156 157 public void testPutAll() { 158 ReferenceMap map = newInstance(); 159 Object k = "key"; 160 Object v = "value"; 161 map.putAll(Collections.singletonMap(k, v)); 162 assertSame(v, map.get(k)); 163 } 164 165 public void testRemove() { 166 ReferenceMap map = newInstance(); 167 Object k = "key"; 168 map.put(k, "value"); 169 map.remove(k); 170 assertFalse(map.containsKey(k)); 171 } 172 173 public void testPutGet() { 174 final Object k = new Object (); 175 final Object v = new Object (); 176 ReferenceMap map = newInstance(); 177 map.put(k, v); 178 assertEquals(1, map.size()); 179 assertSame(v, map.get(k)); 180 assertEquals(1, map.size()); 181 assertNull(map.get(new Object ())); 182 } 183 184 public void testCreate() { 185 final Object k = new Object (); 186 final Object v = new Object (); 187 ReferenceMap map = new ReferenceCache( 188 keyType, valueType) { 189 @Override protected Object create(Object key) { 190 return key == k ? v : null; 191 } 192 }; 193 assertEquals(0, map.size()); 194 assertSame(v, map.get(k)); 195 assertSame(v, map.get(k)); 196 assertEquals(1, map.size()); 197 198 try { 199 map.get(new Object ()); 201 fail(); 202 } catch (NullPointerException e) {} 203 } 204 205 public void testReferenceMapSerialization() throws IOException , 206 ClassNotFoundException { 207 Map map = newInstance(); 208 map.put(Key.FOO, Value.FOO); 209 map = (Map ) serializeAndDeserialize(map); 210 map.put(Key.BAR, Value.BAR); 211 assertSame(Value.FOO, map.get(Key.FOO)); 212 assertSame(Value.BAR, map.get(Key.BAR)); 213 assertNull(map.get(Key.TEE)); 214 } 215 216 static class MockReferenceCache extends ReferenceCache { 217 218 int count; 219 220 public MockReferenceCache(ReferenceType keyReferenceType, 221 ReferenceType valueReferenceType) { 222 super(keyReferenceType, valueReferenceType); 223 } 224 225 protected Object create(Object key) { 226 count++; 227 return Value.valueOf(((Key) key).toString()); 228 } 229 } 230 231 public void testReferenceCacheSerialization() throws IOException , 232 ClassNotFoundException { 233 MockReferenceCache map = new MockReferenceCache(keyType, valueType); 234 assertSame(Value.FOO, map.get(Key.FOO)); 235 assertSame(Value.BAR, map.get(Key.BAR)); 236 map = (MockReferenceCache) serializeAndDeserialize(map); 237 assertSame(Value.FOO, map.get(Key.FOO)); 238 assertSame(Value.BAR, map.get(Key.BAR)); 239 assertSame(Value.TEE, map.get(Key.TEE)); 240 assertEquals(3, map.count); 241 } 242 243 @SuppressWarnings ("IOResourceOpenedButNotSafelyClosed") 244 public Object serializeAndDeserialize(Object o) throws IOException , 245 ClassNotFoundException { 246 ByteArrayOutputStream out = new ByteArrayOutputStream (); 247 new ObjectOutputStream (out).writeObject(o); 248 return new ObjectInputStream (new ByteArrayInputStream (out.toByteArray())) 249 .readObject(); 250 } 251 } 252 253 256 enum Key { 257 FOO, BAR, TEE; 258 } 259 260 enum Value { 261 FOO, BAR, TEE; 262 } 263 } | Popular Tags |