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 static org.junit.Assert.assertThrows; 20 21 import com.google.common.base.Function; 22 import com.google.common.collect.Interners.InternerImpl; 23 import com.google.common.collect.MapMakerInternalMap.Strength; 24 import com.google.common.testing.GcFinalization; 25 import com.google.common.testing.NullPointerTester; 26 import java.lang.ref.WeakReference; 27 import junit.framework.TestCase; 28 29 /** 30 * Unit test for {@link Interners}. 31 * 32 * @author Kevin Bourrillion 33 */ 34 public class InternersTest extends TestCase { 35 testStrong_simplistic()36 public void testStrong_simplistic() { 37 String canonical = "a"; 38 String not = new String("a"); 39 40 Interner<String> pool = Interners.newStrongInterner(); 41 assertSame(canonical, pool.intern(canonical)); 42 assertSame(canonical, pool.intern(not)); 43 } 44 testStrong_null()45 public void testStrong_null() { 46 Interner<String> pool = Interners.newStrongInterner(); 47 assertThrows(NullPointerException.class, () -> pool.intern(null)); 48 } 49 testStrong_builder()50 public void testStrong_builder() { 51 int concurrencyLevel = 42; 52 Interner<Object> interner = 53 Interners.newBuilder().strong().concurrencyLevel(concurrencyLevel).build(); 54 InternerImpl<Object> internerImpl = (InternerImpl<Object>) interner; 55 assertEquals(Strength.STRONG, internerImpl.map.keyStrength()); 56 } 57 testWeak_simplistic()58 public void testWeak_simplistic() { 59 String canonical = "a"; 60 String not = new String("a"); 61 62 Interner<String> pool = Interners.newWeakInterner(); 63 assertSame(canonical, pool.intern(canonical)); 64 assertSame(canonical, pool.intern(not)); 65 } 66 testWeak_null()67 public void testWeak_null() { 68 Interner<String> pool = Interners.newWeakInterner(); 69 assertThrows(NullPointerException.class, () -> pool.intern(null)); 70 } 71 testWeak_builder()72 public void testWeak_builder() { 73 int concurrencyLevel = 42; 74 Interner<Object> interner = 75 Interners.newBuilder().weak().concurrencyLevel(concurrencyLevel).build(); 76 InternerImpl<Object> internerImpl = (InternerImpl<Object>) interner; 77 assertEquals(Strength.WEAK, internerImpl.map.keyStrength()); 78 assertEquals(concurrencyLevel, internerImpl.map.concurrencyLevel); 79 } 80 81 testWeak_afterGC()82 public void testWeak_afterGC() throws InterruptedException { 83 Integer canonical = new Integer(5); 84 Integer not = new Integer(5); 85 86 Interner<Integer> pool = Interners.newWeakInterner(); 87 assertSame(canonical, pool.intern(canonical)); 88 89 WeakReference<Integer> signal = new WeakReference<>(canonical); 90 canonical = null; // Hint to the JIT that canonical is unreachable 91 92 GcFinalization.awaitClear(signal); 93 assertSame(not, pool.intern(not)); 94 } 95 testAsFunction_simplistic()96 public void testAsFunction_simplistic() { 97 String canonical = "a"; 98 String not = new String("a"); 99 100 Function<String, String> internerFunction = 101 Interners.asFunction(Interners.<String>newStrongInterner()); 102 103 assertSame(canonical, internerFunction.apply(canonical)); 104 assertSame(canonical, internerFunction.apply(not)); 105 } 106 testNullPointerExceptions()107 public void testNullPointerExceptions() { 108 new NullPointerTester().testAllPublicStaticMethods(Interners.class); 109 } 110 testConcurrencyLevel_zero()111 public void testConcurrencyLevel_zero() { 112 Interners.InternerBuilder builder = Interners.newBuilder(); 113 assertThrows(IllegalArgumentException.class, () -> builder.concurrencyLevel(0)); 114 } 115 testConcurrencyLevel_negative()116 public void testConcurrencyLevel_negative() { 117 Interners.InternerBuilder builder = Interners.newBuilder(); 118 assertThrows(IllegalArgumentException.class, () -> builder.concurrencyLevel(-42)); 119 } 120 } 121