Debian Codegraph @

src/test/java/org/jboss/modules/ref/SoftReferenceTestCase.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 java.lang.ref.ReferenceQueue;
22 import java.util.ArrayList;
23 import java.util.Collection;
24
25 import org.jboss.modules.ref.Reference.Type;
26 import org.jboss.modules.ref.util.TestReaper;
27 import org.junit.Test;
28
29 /**
30 * Test for {@link SoftReference}.
31 *
32 * @author <a href="mailto:[email protected]">Flavia Rainone</a>
33 */
34 public class SoftReferenceTestCase extends AbstractReapableReferenceTest {
35
36 @Test
37 public void plainSoftReference() {
38 final Reference<String, String> reference = new SoftReference<String, String>("referent", "attachment");
39 assertReference(reference, "referent", "attachment", null);
40 }
41
42 @Test
43 public void softReferenceWithoutAttachment() {
44 final Reference<String, String> reference = new SoftReference<String, String>("referent");
45 assertReference(reference, "referent", null, null);
46 }
47
48 @Test
49 public void nullSoftReference() throws Exception {
50 final Reference<Thing, Integer> reference = new SoftReference<Thing, Integer>(null, Integer.valueOf(0));
51 assertReference(reference, null, Integer.valueOf(0), null);
52 }
53
54 @Test
55 public void softReferenceWithReferenceQueue() throws Exception {
56 final ReferenceQueue<Collection<Object>> referenceQueue = new ReferenceQueue<Collection<Object>>();
57 final Collection<Object> collection = new ArrayList<Object>();
58 final Reference<Collection<Object>, String> reference = new SoftReference<Collection<Object>, String>(collection, "collection", referenceQueue);
59 assertReference(reference, collection, "collection", null);
60 }
61
62 @Test
63 public void softReferenceWithReaper() throws Exception {
64 final Thing thing = new Thing();
65 final TestReaper<Thing, Void> reaper = new TestReaper<Thing, Void>();
66 final Reference<Thing, Void> reference = new SoftReference<Thing, Void>(thing, null, reaper);
67 assertReference(reference, thing, null, reaper);
68 }
69
70 @Override
71 <T, A> Reference<T, A> createReference(T value, A attachment) {
72 return new SoftReference<T, A>(value, attachment);
73 }
74
75 @Override
76 Type getTestedReferenceType() {
77 return Type.SOFT;
78 }
79 }