Home | History | Annotate | Download | only in collect
      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.GcFinalization;
     21 import com.google.common.testing.NullPointerTester;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.lang.ref.WeakReference;
     26 
     27 /**
     28  * Unit test for {@link Interners}.
     29  *
     30  * @author Kevin Bourrillion
     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;  // Hint to the JIT that canonical is unreachable
     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 }
     97