1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import com.google.common.base.Function;
20 import com.google.common.testing.GcFinalization;
21 import com.google.common.testing.NullPointerTester;
22
23 import junit.framework.TestCase;
24
25 import java.lang.ref.WeakReference;
26
27
28
29
30
31
32 public class InternersTest extends TestCase {
33
34 public void testStrong_simplistic() {
35 String canonical = "a";
36 String not = new String("a");
37
38 Interner<String> pool = Interners.newStrongInterner();
39 assertSame(canonical, pool.intern(canonical));
40 assertSame(canonical, pool.intern(not));
41 }
42
43 public void testStrong_null() {
44 Interner<String> pool = Interners.newStrongInterner();
45 try {
46 pool.intern(null);
47 fail();
48 } catch (NullPointerException ok) {}
49 }
50
51 public void testWeak_simplistic() {
52 String canonical = "a";
53 String not = new String("a");
54
55 Interner<String> pool = Interners.newWeakInterner();
56 assertSame(canonical, pool.intern(canonical));
57 assertSame(canonical, pool.intern(not));
58 }
59
60 public void testWeak_null() {
61 Interner<String> pool = Interners.newWeakInterner();
62 try {
63 pool.intern(null);
64 fail();
65 } catch (NullPointerException ok) {}
66 }
67
68 public void testWeak_afterGC() throws InterruptedException {
69 Integer canonical = new Integer(5);
70 Integer not = new Integer(5);
71
72 Interner<Integer> pool = Interners.newWeakInterner();
73 assertSame(canonical, pool.intern(canonical));
74
75 WeakReference<Integer> signal = new WeakReference<Integer>(canonical);
76 canonical = null;
77
78 GcFinalization.awaitClear(signal);
79 assertSame(not, pool.intern(not));
80 }
81
82 public void testAsFunction_simplistic() {
83 String canonical = "a";
84 String not = new String("a");
85
86 Function<String, String> internerFunction =
87 Interners.asFunction(Interners.<String>newStrongInterner());
88
89 assertSame(canonical, internerFunction.apply(canonical));
90 assertSame(canonical, internerFunction.apply(not));
91 }
92
93 public void testNullPointerExceptions() {
94 new NullPointerTester().testAllPublicStaticMethods(Interners.class);
95 }
96 }