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 testWeak_afterGC()87 public void testWeak_afterGC() throws InterruptedException { 88 Integer canonical = new Integer(5); 89 Integer not = new Integer(5); 90 91 Interner<Integer> pool = Interners.newWeakInterner(); 92 assertSame(canonical, pool.intern(canonical)); 93 94 WeakReference<Integer> signal = new WeakReference<>(canonical); 95 canonical = null; // Hint to the JIT that canonical is unreachable 96 97 GcFinalization.awaitClear(signal); 98 assertSame(not, pool.intern(not)); 99 } 100 testAsFunction_simplistic()101 public void testAsFunction_simplistic() { 102 String canonical = "a"; 103 String not = new String("a"); 104 105 Function<String, String> internerFunction = 106 Interners.asFunction(Interners.<String>newStrongInterner()); 107 108 assertSame(canonical, internerFunction.apply(canonical)); 109 assertSame(canonical, internerFunction.apply(not)); 110 } 111 testNullPointerExceptions()112 public void testNullPointerExceptions() { 113 new NullPointerTester().testAllPublicStaticMethods(Interners.class); 114 } 115 testConcurrencyLevel_Zero()116 public void testConcurrencyLevel_Zero() { 117 Interners.InternerBuilder builder = Interners.newBuilder(); 118 try { 119 builder.concurrencyLevel(0); 120 fail(); 121 } catch (IllegalArgumentException expected) { 122 } 123 } 124 testConcurrencyLevel_Negative()125 public void testConcurrencyLevel_Negative() { 126 Interners.InternerBuilder builder = Interners.newBuilder(); 127 try { 128 builder.concurrencyLevel(-42); 129 fail(); 130 } catch (IllegalArgumentException expected) { 131 } 132 } 133 } 134