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 static com.google.inject.util.ReferenceType.SOFT;
018: import static com.google.inject.util.ReferenceType.STRONG;
019: import static com.google.inject.util.ReferenceType.WEAK;
020:
021: import junit.framework.TestCase;
022:
023: import java.lang.ref.Reference;
024: import java.util.Iterator;
025: import java.util.concurrent.ConcurrentMap;
026:
027: /**
028: * Tests for {@link ReferenceMap}.
029: *
030: * @author crazybob@google.com (Bob Lee)
031: * @author mbostock@google.com (Mike Bostock)
032: */
033: @SuppressWarnings({"unchecked"})
034: public class ReferenceMapTest extends TestCase {
035:
036: private enum CleanupMode {
037: ENQUEUE_KEY, ENQUEUE_VALUE, GC;
038: }
039:
040: public void testValueCleanupWithWeakKey() {
041: ReferenceMap map = new ReferenceMap(WEAK, STRONG);
042: map.put(new Object(), new Object());
043: assertCleanup(map, CleanupMode.GC);
044: }
045:
046: public void testKeyCleanupWithWeakValue() {
047: ReferenceMap map = new ReferenceMap(STRONG, WEAK);
048: map.put(new Object(), new Object());
049: assertCleanup(map, CleanupMode.GC);
050: }
051:
052: public void testInternedValueCleanupWithWeakKey() {
053: ReferenceMap map = new ReferenceMap(WEAK, STRONG);
054: map.put(5, "foo");
055: assertCleanup(map, CleanupMode.ENQUEUE_KEY);
056: }
057:
058: public void testInternedValueCleanupWithSoftKey() {
059: ReferenceMap map = new ReferenceMap(SOFT, STRONG);
060: map.put(5, "foo");
061: assertCleanup(map, CleanupMode.ENQUEUE_KEY);
062: }
063:
064: public void testInternedKeyCleanupWithWeakValue() {
065: ReferenceMap map = new ReferenceMap(STRONG, WEAK);
066: map.put(5, "foo");
067: assertCleanup(map, CleanupMode.ENQUEUE_VALUE);
068: }
069:
070: public void testInternedKeyCleanupWithSoftValue() {
071: ReferenceMap map = new ReferenceMap(STRONG, SOFT);
072: map.put(5, "foo");
073: assertCleanup(map, CleanupMode.ENQUEUE_VALUE);
074: }
075:
076: private static void assertCleanup(ReferenceMap<?, ?> map,
077: CleanupMode mode) {
078: assertEquals(1, map.delegate.size());
079:
080: switch (mode) {
081: case ENQUEUE_KEY: {
082: ConcurrentMap delegate = map.delegate;
083: Iterator keyIterator = delegate.keySet().iterator();
084: Reference reference = ((Reference) keyIterator.next());
085: reference.enqueue();
086: break;
087: }
088: case ENQUEUE_VALUE: {
089: ConcurrentMap delegate = map.delegate;
090: Iterator valueIterator = delegate.values().iterator();
091: Reference reference = ((Reference) valueIterator.next());
092: reference.enqueue();
093: break;
094: }
095: }
096:
097: // wait up to 5s
098: for (int i = 0; i < 500; i++) {
099: if (mode == CleanupMode.GC) {
100: System.gc();
101: }
102: if (map.size() == 0) {
103: return;
104: }
105: try {
106: Thread.sleep(10);
107: } catch (InterruptedException e) { /* ignore */
108: }
109: }
110: fail();
111: }
112: }
|