Java example source code file (WeakKeySetTest.java)
This example Java source code file (WeakKeySetTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
/**
* Copyright (C) 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.inject.internal;
import static com.google.inject.Asserts.awaitClear;
import static com.google.inject.Asserts.awaitFullGc;
import static com.google.inject.internal.WeakKeySetUtils.assertBlacklisted;
import static com.google.inject.internal.WeakKeySetUtils.assertInSet;
import static com.google.inject.internal.WeakKeySetUtils.assertNotBlacklisted;
import static com.google.inject.internal.WeakKeySetUtils.assertNotInSet;
import static com.google.inject.internal.WeakKeySetUtils.assertSourceNotInSet;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
import com.google.inject.Binding;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Scope;
import com.google.inject.TypeLiteral;
import com.google.inject.spi.ModuleAnnotatedMethodScannerBinding;
import com.google.inject.spi.ProvisionListenerBinding;
import com.google.inject.spi.ScopeBinding;
import com.google.inject.spi.TypeConverterBinding;
import com.google.inject.spi.TypeListenerBinding;
import junit.framework.TestCase;
import java.lang.annotation.Annotation;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Tests for {@link WeakKeySet}.
* <p>
* Multibinding specific tests can be found in MultibinderTest and MapBinderTest.
*
* @author dweis@google.com (Daniel Weis)
*/
public class WeakKeySetTest extends TestCase {
private WeakKeySet set;
@Override
protected void setUp() throws Exception {
set = new WeakKeySet(new Object());
}
public void testEviction() {
TestState state = new TestState();
Key<Integer> key = Key.get(Integer.class);
Object source = new Object();
WeakReference<Key weakKeyRef = new WeakReference>(key);
set.add(key, state, source);
assertInSet(set, key, 1, source);
state = null;
awaitFullGc();
assertNotInSet(set, Key.get(Integer.class));
// Ensure there are no hanging references.
key = null;
awaitClear(weakKeyRef);
}
public void testEviction_nullSource() {
TestState state = new TestState();
Key<Integer> key = Key.get(Integer.class);
Object source = null;
WeakReference<Key weakKeyRef = new WeakReference>(key);
set.add(key, state, source);
assertInSet(set, key, 1, source);
state = null;
awaitFullGc();
assertNotInSet(set, Key.get(Integer.class));
// Ensure there are no hanging references.
key = null;
awaitClear(weakKeyRef);
}
public void testEviction_keyOverlap_2x() {
TestState state1 = new TestState();
TestState state2 = new TestState();
Key<Integer> key1 = Key.get(Integer.class);
Key<Integer> key2 = Key.get(Integer.class);
Object source1 = new Object();
Object source2 = new Object();
set.add(key1, state1, source1);
assertInSet(set, key1, 1, source1);
set.add(key2, state2, source2);
assertInSet(set, key2, 2, source1, source2);
WeakReference<Key weakKey1Ref = new WeakReference>(key1);
WeakReference<Key weakKey2Ref = new WeakReference>(key2);
WeakReference<Object> weakSource1Ref = new WeakReference
Other Java examples (source code examples)
Here is a short list of links related to this Java WeakKeySetTest.java source code file: