Home | History | Annotate | Download | only in cache
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.common.cache;
     16 
     17 import static com.google.common.cache.CacheTesting.checkEmpty;
     18 import static com.google.common.cache.TestingCacheLoaders.constantLoader;
     19 import static com.google.common.cache.TestingCacheLoaders.exceptionLoader;
     20 import static com.google.common.cache.TestingRemovalListeners.queuingRemovalListener;
     21 import static java.util.concurrent.TimeUnit.SECONDS;
     22 
     23 import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
     24 import com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener;
     25 import com.google.common.util.concurrent.UncheckedExecutionException;
     26 
     27 import junit.framework.TestCase;
     28 
     29 /**
     30  * {@link LoadingCache} tests for caches with a maximum size of zero.
     31  *
     32  * @author mike nonemacher
     33  */
     34 public class NullCacheTest extends TestCase {
     35   QueuingRemovalListener<Object, Object> listener;
     36 
     37   @Override
     38   protected void setUp() {
     39     listener = queuingRemovalListener();
     40   }
     41 
     42   public void testGet() {
     43     Object computed = new Object();
     44     LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
     45         .maximumSize(0)
     46         .removalListener(listener)
     47         .build(constantLoader(computed));
     48 
     49     Object key = new Object();
     50     assertSame(computed, cache.getUnchecked(key));
     51     RemovalNotification<Object, Object> notification = listener.remove();
     52     assertSame(key, notification.getKey());
     53     assertSame(computed, notification.getValue());
     54     assertSame(RemovalCause.SIZE, notification.getCause());
     55     assertTrue(listener.isEmpty());
     56     checkEmpty(cache);
     57   }
     58 
     59   public void testGet_expireAfterWrite() {
     60     Object computed = new Object();
     61     LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
     62         .expireAfterWrite(0, SECONDS)
     63         .removalListener(listener)
     64         .build(constantLoader(computed));
     65 
     66     Object key = new Object();
     67     assertSame(computed, cache.getUnchecked(key));
     68     RemovalNotification<Object, Object> notification = listener.remove();
     69     assertSame(key, notification.getKey());
     70     assertSame(computed, notification.getValue());
     71     assertSame(RemovalCause.SIZE, notification.getCause());
     72     assertTrue(listener.isEmpty());
     73     checkEmpty(cache);
     74   }
     75 
     76   public void testGet_expireAfterAccess() {
     77     Object computed = new Object();
     78     LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
     79         .expireAfterAccess(0, SECONDS)
     80         .removalListener(listener)
     81         .build(constantLoader(computed));
     82 
     83     Object key = new Object();
     84     assertSame(computed, cache.getUnchecked(key));
     85     RemovalNotification<Object, Object> notification = listener.remove();
     86     assertSame(key, notification.getKey());
     87     assertSame(computed, notification.getValue());
     88     assertSame(RemovalCause.SIZE, notification.getCause());
     89     assertTrue(listener.isEmpty());
     90     checkEmpty(cache);
     91   }
     92 
     93   public void testGet_computeNull() {
     94     LoadingCache<Object, Object> cache = CacheBuilder.newBuilder()
     95         .maximumSize(0)
     96         .removalListener(listener)
     97         .build(constantLoader(null));
     98 
     99     try {
    100       cache.getUnchecked(new Object());
    101       fail();
    102     } catch (InvalidCacheLoadException e) { /* expected */}
    103 
    104     assertTrue(listener.isEmpty());
    105     checkEmpty(cache);
    106   }
    107 
    108   public void testGet_runtimeException() {
    109     final RuntimeException e = new RuntimeException();
    110     LoadingCache<Object, Object> map = CacheBuilder.newBuilder()
    111         .maximumSize(0)
    112         .removalListener(listener)
    113         .build(exceptionLoader(e));
    114 
    115     try {
    116       map.getUnchecked(new Object());
    117       fail();
    118     } catch (UncheckedExecutionException uee) {
    119       assertSame(e, uee.getCause());
    120     }
    121     assertTrue(listener.isEmpty());
    122     checkEmpty(map);
    123   }
    124 }
    125