Debian Codegraph @

src/test/java/org/jboss/modules/ref/WeakReferenceTestCase.java

raw
1 /*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2014 Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.jboss.modules.ref;
20
21 import static org.junit.Assert.assertSame;
22
23 import java.lang.ref.ReferenceQueue;
24 import java.util.ArrayList;
25 import java.util.Collection;
26
27 import org.jboss.modules.ref.Reference.Type;
28 import org.jboss.modules.ref.util.TestReaper;
29 import org.junit.Test;
30
31 /**
32 * Test for {@link WeakReference}.
33 *
34 * @author <a href="mailto:[email protected]">Flavia Rainone</a>
35 */
36 public class WeakReferenceTestCase extends AbstractReapableReferenceTest {
37
38 @Test
39 public void plainWeakReference() {
40 final Reference<String, String> reference = new WeakReference<String, String>("referent", "attachment");
41 assertReference(reference, "referent", "attachment", null);
42 }
43
44 @Test
45 public void weakReferenceWithoutAttachment() {
46 final Reference<String, String> reference = new WeakReference<String, String>("referent");
47 assertReference(reference, "referent", null, null);
48 }
49
50 @Test
51 public void nullWeakReference() throws Exception {
52 final Reference<Thing, Integer> reference = new WeakReference<Thing, Integer>(null, Integer.valueOf(0));
53 assertReference(reference, null, Integer.valueOf(0));
54 }
55
56 @Test
57 public void weakReferenceWithReferenceQueue() throws Exception {
58 final ReferenceQueue<Collection<Object>> referenceQueue = new ReferenceQueue<Collection<Object>>();
59 Collection<Object> collection = new ArrayList<Object>();
60 final Reference<Collection<Object>, String> reference = new WeakReference<Collection<Object>, String>(collection, "collection", referenceQueue);
61 assertReference(reference, collection, "collection", null);
62 collection = null;
63 System.gc();
64 assertSame(reference, referenceQueue.remove(300));
65 }
66
67 @Test
68 public void weakReferenceWithReaper() throws Exception {
69 Thing thing = new Thing();
70 final TestReaper<Thing, Void> reaper = new TestReaper<Thing, Void>();
71 final Reference<Thing, Void> reference = new WeakReference<Thing, Void>(thing, null, reaper);
72 assertReference(reference, thing, null, reaper);
73 thing = null;
74 System.gc();
75 assertSame(reference, reaper.getReapedReference());
76 }
77
78 @Override
79 <T, A> Reference<T, A> createReference(T value, A attachment) {
80 return new WeakReference<T, A>(value, attachment);
81 }
82
83 @Override
84 Type getTestedReferenceType() {
85 return Type.WEAK;
86 }
87 }