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 
24 import com.google.common.cache.CacheLoader.InvalidCacheLoadException;
25 import com.google.common.cache.TestingRemovalListeners.QueuingRemovalListener;
26 import com.google.common.util.concurrent.UncheckedExecutionException;
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
setUp()38   protected void setUp() {
39     listener = queuingRemovalListener();
40   }
41 
testGet()42   public void testGet() {
43     Object computed = new Object();
44     LoadingCache<Object, Object> cache =
45         CacheBuilder.newBuilder()
46             .maximumSize(0)
47             .removalListener(listener)
48             .build(constantLoader(computed));
49 
50     Object key = new Object();
51     assertSame(computed, cache.getUnchecked(key));
52     RemovalNotification<Object, Object> notification = listener.remove();
53     assertSame(key, notification.getKey());
54     assertSame(computed, notification.getValue());
55     assertSame(RemovalCause.SIZE, notification.getCause());
56     assertTrue(listener.isEmpty());
57     checkEmpty(cache);
58   }
59 
testGet_expireAfterWrite()60   public void testGet_expireAfterWrite() {
61     Object computed = new Object();
62     LoadingCache<Object, Object> cache =
63         CacheBuilder.newBuilder()
64             .expireAfterWrite(0, SECONDS)
65             .removalListener(listener)
66             .build(constantLoader(computed));
67 
68     Object key = new Object();
69     assertSame(computed, cache.getUnchecked(key));
70     RemovalNotification<Object, Object> notification = listener.remove();
71     assertSame(key, notification.getKey());
72     assertSame(computed, notification.getValue());
73     assertSame(RemovalCause.SIZE, notification.getCause());
74     assertTrue(listener.isEmpty());
75     checkEmpty(cache);
76   }
77 
testGet_expireAfterAccess()78   public void testGet_expireAfterAccess() {
79     Object computed = new Object();
80     LoadingCache<Object, Object> cache =
81         CacheBuilder.newBuilder()
82             .expireAfterAccess(0, SECONDS)
83             .removalListener(listener)
84             .build(constantLoader(computed));
85 
86     Object key = new Object();
87     assertSame(computed, cache.getUnchecked(key));
88     RemovalNotification<Object, Object> notification = listener.remove();
89     assertSame(key, notification.getKey());
90     assertSame(computed, notification.getValue());
91     assertSame(RemovalCause.SIZE, notification.getCause());
92     assertTrue(listener.isEmpty());
93     checkEmpty(cache);
94   }
95 
testGet_computeNull()96   public void testGet_computeNull() {
97     LoadingCache<Object, Object> cache =
98         CacheBuilder.newBuilder()
99             .maximumSize(0)
100             .removalListener(listener)
101             .build(constantLoader(null));
102 
103     try {
104       cache.getUnchecked(new Object());
105       fail();
106     } catch (InvalidCacheLoadException e) {
107       /* expected */
108     }
109 
110     assertTrue(listener.isEmpty());
111     checkEmpty(cache);
112   }
113 
testGet_runtimeException()114   public void testGet_runtimeException() {
115     final RuntimeException e = new RuntimeException();
116     LoadingCache<Object, Object> map =
117         CacheBuilder.newBuilder()
118             .maximumSize(0)
119             .removalListener(listener)
120             .build(exceptionLoader(e));
121 
122     try {
123       map.getUnchecked(new Object());
124       fail();
125     } catch (UncheckedExecutionException uee) {
126       assertThat(uee).hasCauseThat().isSameInstanceAs(e);
127     }
128     assertTrue(listener.isEmpty());
129     checkEmpty(map);
130   }
131 }
132