001: /**
002: * Copyright (C) 2006 Google Inc.
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: */package com.google.inject.util;
016:
017: import junit.framework.Test;
018: import junit.framework.TestCase;
019: import junit.framework.TestSuite;
020:
021: import java.io.ByteArrayInputStream;
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.lang.reflect.Method;
027: import java.util.Arrays;
028: import java.util.Collections;
029: import java.util.HashSet;
030: import java.util.Map;
031: import java.util.Set;
032:
033: /**
034: * @author crazybob@google.com (Bob Lee)
035: */
036: @SuppressWarnings({"unchecked"})
037: public class ReferenceMapTestSuite {
038:
039: public static Test suite() {
040: TestSuite suite = new TestSuite();
041:
042: Set<ReferenceType> referenceTypes = new HashSet<ReferenceType>();
043: referenceTypes.addAll(Arrays.asList(ReferenceType.values()));
044: referenceTypes.remove(ReferenceType.PHANTOM);
045:
046: // create test cases for each key and value type.
047: for (Method method : MapTest.class.getMethods()) {
048: String name = method.getName();
049: if (name.startsWith("test")) {
050: for (ReferenceType keyType : referenceTypes) {
051: for (ReferenceType valueType : referenceTypes) {
052: suite.addTest(new MapTest(name, keyType,
053: valueType));
054: }
055: }
056: }
057: }
058:
059: return suite;
060: }
061:
062: public static class MapTest extends TestCase {
063:
064: final ReferenceType keyType;
065: final ReferenceType valueType;
066:
067: public MapTest(String name, ReferenceType keyType,
068: ReferenceType valueType) {
069: super (name);
070: this .keyType = keyType;
071: this .valueType = valueType;
072: }
073:
074: public String getName() {
075: return super .getName()
076: + "For"
077: + Strings.capitalize(keyType.toString()
078: .toLowerCase())
079: + Strings.capitalize(valueType.toString()
080: .toLowerCase());
081: }
082:
083: ReferenceMap newInstance() {
084: return new ReferenceMap(keyType, valueType);
085: }
086:
087: public void testContainsKey() {
088: ReferenceMap map = newInstance();
089: Object k = "key";
090: map.put(k, "value");
091: assertTrue(map.containsKey(k));
092: }
093:
094: public void testClear() {
095: ReferenceMap map = newInstance();
096: String k = "key";
097: map.put(k, "value");
098: assertFalse(map.isEmpty());
099: 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(map.new Entry("a",
151: "1"), map.new Entry("b", "2")));
152: assertEquals(expected, map.entrySet());
153: }
154:
155: public void testPutAll() {
156: ReferenceMap map = newInstance();
157: Object k = "key";
158: Object v = "value";
159: map.putAll(Collections.singletonMap(k, v));
160: assertSame(v, map.get(k));
161: }
162:
163: public void testRemove() {
164: ReferenceMap map = newInstance();
165: Object k = "key";
166: map.put(k, "value");
167: map.remove(k);
168: assertFalse(map.containsKey(k));
169: }
170:
171: public void testPutGet() {
172: final Object k = new Object();
173: final Object v = new Object();
174: ReferenceMap map = newInstance();
175: map.put(k, v);
176: assertEquals(1, map.size());
177: assertSame(v, map.get(k));
178: assertEquals(1, map.size());
179: assertNull(map.get(new Object()));
180: }
181:
182: public void testCreate() {
183: final Object k = new Object();
184: final Object v = new Object();
185: ReferenceMap map = new ReferenceCache(keyType, valueType) {
186: @Override
187: protected Object create(Object key) {
188: return key == k ? v : null;
189: }
190: };
191: assertEquals(0, map.size());
192: assertSame(v, map.get(k));
193: assertSame(v, map.get(k));
194: assertEquals(1, map.size());
195:
196: try {
197: // create can't return null.
198: map.get(new Object());
199: fail();
200: } catch (NullPointerException e) {
201: }
202: }
203:
204: public void testReferenceMapSerialization() throws IOException,
205: ClassNotFoundException {
206: Map map = newInstance();
207: map.put(Key.FOO, Value.FOO);
208: map = (Map) serializeAndDeserialize(map);
209: map.put(Key.BAR, Value.BAR);
210: assertSame(Value.FOO, map.get(Key.FOO));
211: assertSame(Value.BAR, map.get(Key.BAR));
212: assertNull(map.get(Key.TEE));
213: }
214:
215: static class MockReferenceCache extends ReferenceCache {
216:
217: int count;
218:
219: public MockReferenceCache(ReferenceType keyReferenceType,
220: ReferenceType valueReferenceType) {
221: super (keyReferenceType, valueReferenceType);
222: }
223:
224: protected Object create(Object key) {
225: count++;
226: return Value.valueOf(((Key) key).toString());
227: }
228: }
229:
230: public void testReferenceCacheSerialization()
231: throws IOException, ClassNotFoundException {
232: MockReferenceCache map = new MockReferenceCache(keyType,
233: 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)
245: throws IOException, ClassNotFoundException {
246: ByteArrayOutputStream out = new ByteArrayOutputStream();
247: new ObjectOutputStream(out).writeObject(o);
248: return new ObjectInputStream(new ByteArrayInputStream(out
249: .toByteArray())).readObject();
250: }
251: }
252:
253: /**
254: * Enums conveniently maintain instance identity across serialization.
255: */
256: enum Key {
257: FOO, BAR, TEE;
258: }
259:
260: enum Value {
261: FOO, BAR, TEE;
262: }
263: }
|