• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.base.Function;
20 import com.google.common.collect.Interners.InternerImpl;
21 import com.google.common.collect.MapMakerInternalMap.Strength;
22 import com.google.common.testing.GcFinalization;
23 import com.google.common.testing.NullPointerTester;
24 import java.lang.ref.WeakReference;
25 import junit.framework.TestCase;
26 
27 /**
28  * Unit test for {@link Interners}.
29  *
30  * @author Kevin Bourrillion
31  */
32 public class InternersTest extends TestCase {
33 
testStrong_simplistic()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 
testStrong_null()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 
testStrong_builder()52   public void testStrong_builder() {
53     int concurrencyLevel = 42;
54     Interner<Object> interner =
55         Interners.newBuilder().strong().concurrencyLevel(concurrencyLevel).build();
56     InternerImpl<Object> internerImpl = (InternerImpl<Object>) interner;
57     assertEquals(Strength.STRONG, internerImpl.map.keyStrength());
58   }
59 
testWeak_simplistic()60   public void testWeak_simplistic() {
61     String canonical = "a";
62     String not = new String("a");
63 
64     Interner<String> pool = Interners.newWeakInterner();
65     assertSame(canonical, pool.intern(canonical));
66     assertSame(canonical, pool.intern(not));
67   }
68 
testWeak_null()69   public void testWeak_null() {
70     Interner<String> pool = Interners.newWeakInterner();
71     try {
72       pool.intern(null);
73       fail();
74     } catch (NullPointerException ok) {
75     }
76   }
77 
testWeak_builder()78   public void testWeak_builder() {
79     int concurrencyLevel = 42;
80     Interner<Object> interner =
81         Interners.newBuilder().weak().concurrencyLevel(concurrencyLevel).build();
82     InternerImpl<Object> internerImpl = (InternerImpl<Object>) interner;
83     assertEquals(Strength.WEAK, internerImpl.map.keyStrength());
84     assertEquals(concurrencyLevel, internerImpl.map.concurrencyLevel);
85   }
86 
87 
testWeak_afterGC()88   public void testWeak_afterGC() throws InterruptedException {
89     Integer canonical = new Integer(5);
90     Integer not = new Integer(5);
91 
92     Interner<Integer> pool = Interners.newWeakInterner();
93     assertSame(canonical, pool.intern(canonical));
94 
95     WeakReference<Integer> signal = new WeakReference<>(canonical);
96     canonical = null; // Hint to the JIT that canonical is unreachable
97 
98     GcFinalization.awaitClear(signal);
99     assertSame(not, pool.intern(not));
100   }
101 
testAsFunction_simplistic()102   public void testAsFunction_simplistic() {
103     String canonical = "a";
104     String not = new String("a");
105 
106     Function<String, String> internerFunction =
107         Interners.asFunction(Interners.<String>newStrongInterner());
108 
109     assertSame(canonical, internerFunction.apply(canonical));
110     assertSame(canonical, internerFunction.apply(not));
111   }
112 
testNullPointerExceptions()113   public void testNullPointerExceptions() {
114     new NullPointerTester().testAllPublicStaticMethods(Interners.class);
115   }
116 
testConcurrencyLevel_Zero()117   public void testConcurrencyLevel_Zero() {
118     Interners.InternerBuilder builder = Interners.newBuilder();
119     try {
120       builder.concurrencyLevel(0);
121       fail();
122     } catch (IllegalArgumentException expected) {
123     }
124   }
125 
testConcurrencyLevel_Negative()126   public void testConcurrencyLevel_Negative() {
127     Interners.InternerBuilder builder = Interners.newBuilder();
128     try {
129       builder.concurrencyLevel(-42);
130       fail();
131     } catch (IllegalArgumentException expected) {
132     }
133   }
134 }
135