• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.google.common.truth.Truth.assertThat;
22 import static java.util.concurrent.TimeUnit.SECONDS;
23 import static org.junit.Assert.assertThrows;
24 
25 import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
26 import com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener;
27 import com.google.common.util.concurrent.UncheckedExecutionException;
28 import junit.framework.TestCase;
29 
30 /**
31  * {@link LoadingCache} tests for caches with a maximum size of zero.
32  *
33  * @author mike nonemacher
34  */
35 public class NullCacheTest extends TestCase {
36   QueuingRemovalListener<Object, Object> listener;
37 
38   @Override
setUp()39   protected void setUp() {
40     listener = queuingRemovalListener();
41   }
42 
testGet()43   public void testGet() {
44     Object computed = new Object();
45     LoadingCache<Object, Object> cache =
46         CacheBuilder.newBuilder()
47             .maximumSize(0)
48             .removalListener(listener)
49             .build(constantLoader(computed));
50 
51     Object key = new Object();
52     assertSame(computed, cache.getUnchecked(key));
53     RemovalNotification<Object, Object> notification = listener.remove();
54     assertSame(key, notification.getKey());
55     assertSame(computed, notification.getValue());
56     assertSame(RemovalCause.SIZE, notification.getCause());
57     assertTrue(listener.isEmpty());
58     checkEmpty(cache);
59   }
60 
testGet_expireAfterWrite()61   public void testGet_expireAfterWrite() {
62     Object computed = new Object();
63     LoadingCache<Object, Object> cache =
64         CacheBuilder.newBuilder()
65             .expireAfterWrite(0, SECONDS)
66             .removalListener(listener)
67             .build(constantLoader(computed));
68 
69     Object key = new Object();
70     assertSame(computed, cache.getUnchecked(key));
71     RemovalNotification<Object, Object> notification = listener.remove();
72     assertSame(key, notification.getKey());
73     assertSame(computed, notification.getValue());
74     assertSame(RemovalCause.SIZE, notification.getCause());
75     assertTrue(listener.isEmpty());
76     checkEmpty(cache);
77   }
78 
testGet_expireAfterAccess()79   public void testGet_expireAfterAccess() {
80     Object computed = new Object();
81     LoadingCache<Object, Object> cache =
82         CacheBuilder.newBuilder()
83             .expireAfterAccess(0, SECONDS)
84             .removalListener(listener)
85             .build(constantLoader(computed));
86 
87     Object key = new Object();
88     assertSame(computed, cache.getUnchecked(key));
89     RemovalNotification<Object, Object> notification = listener.remove();
90     assertSame(key, notification.getKey());
91     assertSame(computed, notification.getValue());
92     assertSame(RemovalCause.SIZE, notification.getCause());
93     assertTrue(listener.isEmpty());
94     checkEmpty(cache);
95   }
96 
testGet_computeNull()97   public void testGet_computeNull() {
98     LoadingCache<Object, Object> cache =
99         CacheBuilder.newBuilder()
100             .maximumSize(0)
101             .removalListener(listener)
102             .build(constantLoader(null));
103 
104     assertThrows(InvalidCacheLoadException.class, () -> cache.getUnchecked(new Object()));
105 
106     assertTrue(listener.isEmpty());
107     checkEmpty(cache);
108   }
109 
testGet_runtimeException()110   public void testGet_runtimeException() {
111     final RuntimeException e = new RuntimeException();
112     LoadingCache<Object, Object> map =
113         CacheBuilder.newBuilder()
114             .maximumSize(0)
115             .removalListener(listener)
116             .build(exceptionLoader(e));
117 
118     UncheckedExecutionException uee =
119         assertThrows(UncheckedExecutionException.class, () -> map.getUnchecked(new Object()));
120     assertThat(uee).hasCauseThat().isSameInstanceAs(e);
121     assertTrue(listener.isEmpty());
122     checkEmpty(map);
123   }
124 }
125