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.testing.NullPointerTester; 21 22 import junit.framework.TestCase; 23 24 import java.lang.ref.WeakReference; 25 26 /** 27 * Unit test for {@link Interners}. 28 * 29 * @author Kevin Bourrillion 30 */ 31 public class InternersTest extends TestCase { 32 testStrong_simplistic()33 public void testStrong_simplistic() { 34 String canonical = "a"; 35 String not = new String("a"); 36 37 Interner<String> pool = Interners.newStrongInterner(); 38 assertSame(canonical, pool.intern(canonical)); 39 assertSame(canonical, pool.intern(not)); 40 } 41 testStrong_null()42 public void testStrong_null() { 43 Interner<String> pool = Interners.newStrongInterner(); 44 try { 45 pool.intern(null); 46 fail(); 47 } catch (NullPointerException ok) {} 48 } 49 testWeak_simplistic()50 public void testWeak_simplistic() { 51 String canonical = "a"; 52 String not = new String("a"); 53 54 Interner<String> pool = Interners.newWeakInterner(); 55 assertSame(canonical, pool.intern(canonical)); 56 assertSame(canonical, pool.intern(not)); 57 } 58 testWeak_null()59 public void testWeak_null() { 60 Interner<String> pool = Interners.newWeakInterner(); 61 try { 62 pool.intern(null); 63 fail(); 64 } catch (NullPointerException ok) {} 65 } 66 testWeak_afterGC()67 public void testWeak_afterGC() throws InterruptedException { 68 Integer canonical = new Integer(5); 69 Integer not = new Integer(5); 70 71 Interner<Integer> pool = Interners.newWeakInterner(); 72 assertSame(canonical, pool.intern(canonical)); 73 74 WeakReference<Integer> signal = new WeakReference<Integer>(canonical); 75 canonical = null; 76 77 for (int i = 0; i < 3000; i++) { 78 System.gc(); 79 if (signal.get() == null) { // it was collected 80 assertSame(not, pool.intern(not)); 81 return; 82 } 83 Thread.sleep(1); 84 } 85 fail("reference didn't get cleaned up"); 86 } 87 testAsFunction_simplistic()88 public void testAsFunction_simplistic() { 89 String canonical = "a"; 90 String not = new String("a"); 91 92 Function<String, String> internerFunction = 93 Interners.asFunction(Interners.<String>newStrongInterner()); 94 95 assertSame(canonical, internerFunction.apply(canonical)); 96 assertSame(canonical, internerFunction.apply(not)); 97 } 98 testNullPointerExceptions()99 public void testNullPointerExceptions() throws Exception { 100 new NullPointerTester().testAllPublicStaticMethods(Interners.class); 101 } 102 } 103