Debian Codegraph @

src/test/java/org/jboss/modules/ref/PhantomReferenceTestCase.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.assertEquals;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertSame;
24
25 import java.lang.ref.ReferenceQueue;
26 import java.util.ArrayList;
27 import java.util.Collection;
28
29 import org.jboss.modules.ref.Reference.Type;
30 import org.jboss.modules.ref.util.TestReaper;
31 import org.junit.Test;
32
33 /**
34 * Test for PhantomReference.
35 *
36 * @author <a href="mailto:[email protected]">Flavia Rainone</a>
37 * @see PhantomReference
38 */
39 public class PhantomReferenceTestCase extends AbstractReapableReferenceTest {
40
41 @Test
42 public void plainPhantomReference() {
43 final Reference<String, String> reference = new PhantomReference<String, String>("referent", "attachment",
44 (ReferenceQueue<String>) null);
45 assertReference(reference, null, "attachment", null);
46 }
47
48 @Test
49 public void nullPhantomReference() throws Exception {
50 final Reference<Thing, Integer> reference = new PhantomReference<Thing, Integer>(null, Integer.valueOf(0), new ReferenceQueue<Thing>());
51 assertReference(reference, null, Integer.valueOf(0), null);
52 }
53
54 @Test
55 public void phantomReferenceWithReferenceQueue() throws Exception {
56 final ReferenceQueue<Collection<Object>> referenceQueue = new ReferenceQueue<Collection<Object>>();
57 Collection<Object> collection = new ArrayList<Object>();
58 final Reference<Collection<Object>, String> reference = new PhantomReference<Collection<Object>, String>(collection, "collection", referenceQueue);
59 assertReference(reference, null, "collection", null);
60 collection = null;
61 System.gc();
62 assertSame(reference, referenceQueue.remove(300));
63 }
64
65 @Test
66 public void phantomReferenceWithReaper() throws Exception {
67 Thing thing = new Thing();
68 final TestReaper<Thing, Void> reaper = new TestReaper<Thing, Void>();
69 final Reference<Thing, Void> reference = new PhantomReference<Thing, Void>(thing, null, reaper);
70 assertReference(reference, null, null, reaper);
71 thing = null;
72 System.gc();
73 assertSame(reference, reaper.getReapedReference());
74 }
75
76 @Override @Test
77 public void clearReference() {
78 final Reference<Boolean, String> reference = createReference(Boolean.TRUE, "attachment for true");
79 assertNull(reference.get());
80 assertEquals("attachment for true", reference.getAttachment());
81
82 reference.clear();
83 assertNull(reference.get());
84 assertEquals("attachment for true", reference.getAttachment());
85 }
86
87 @Override
88 <T, A> Reference<T, A> createReference(T value, A attachment) {
89 return new PhantomReference<T, A>(value, attachment, new TestReaper<T, A>());
90 }
91
92 @Override
93 Type getTestedReferenceType() {
94 return Type.PHANTOM;
95 }
96 }