• 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");
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.cache;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.util.concurrent.ExecutionError;
22 import com.google.common.util.concurrent.UncheckedExecutionException;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.atomic.AtomicReference;
25 import junit.framework.TestCase;
26 
27 /**
28  * Unit test for {@link AbstractLoadingCache}.
29  *
30  * @author Charles Fry
31  */
32 public class AbstractLoadingCacheTest extends TestCase {
33 
testGetUnchecked_checked()34   public void testGetUnchecked_checked() {
35     final Exception cause = new Exception();
36     final AtomicReference<Object> valueRef = new AtomicReference<>();
37     LoadingCache<Object, Object> cache =
38         new AbstractLoadingCache<Object, Object>() {
39           @Override
40           public Object get(Object key) throws ExecutionException {
41             Object v = valueRef.get();
42             if (v == null) {
43               throw new ExecutionException(cause);
44             }
45             return v;
46           }
47 
48           @Override
49           public Object getIfPresent(Object key) {
50             return valueRef.get();
51           }
52         };
53 
54     try {
55       cache.getUnchecked(new Object());
56       fail();
57     } catch (UncheckedExecutionException expected) {
58       assertThat(expected).hasCauseThat().isEqualTo(cause);
59     }
60 
61     Object newValue = new Object();
62     valueRef.set(newValue);
63     assertSame(newValue, cache.getUnchecked(new Object()));
64   }
65 
testGetUnchecked_unchecked()66   public void testGetUnchecked_unchecked() {
67     final RuntimeException cause = new RuntimeException();
68     final AtomicReference<Object> valueRef = new AtomicReference<>();
69     LoadingCache<Object, Object> cache =
70         new AbstractLoadingCache<Object, Object>() {
71           @Override
72           public Object get(Object key) throws ExecutionException {
73             Object v = valueRef.get();
74             if (v == null) {
75               throw new ExecutionException(cause);
76             }
77             return v;
78           }
79 
80           @Override
81           public Object getIfPresent(Object key) {
82             return valueRef.get();
83           }
84         };
85 
86     try {
87       cache.getUnchecked(new Object());
88       fail();
89     } catch (UncheckedExecutionException expected) {
90       assertThat(expected).hasCauseThat().isEqualTo(cause);
91     }
92 
93     Object newValue = new Object();
94     valueRef.set(newValue);
95     assertSame(newValue, cache.getUnchecked(new Object()));
96   }
97 
testGetUnchecked_error()98   public void testGetUnchecked_error() {
99     final Error cause = new Error();
100     final AtomicReference<Object> valueRef = new AtomicReference<>();
101     LoadingCache<Object, Object> cache =
102         new AbstractLoadingCache<Object, Object>() {
103           @Override
104           public Object get(Object key) throws ExecutionException {
105             Object v = valueRef.get();
106             if (v == null) {
107               throw new ExecutionError(cause);
108             }
109             return v;
110           }
111 
112           @Override
113           public Object getIfPresent(Object key) {
114             return valueRef.get();
115           }
116         };
117 
118     try {
119       cache.getUnchecked(new Object());
120       fail();
121     } catch (ExecutionError expected) {
122       assertThat(expected).hasCauseThat().isEqualTo(cause);
123     }
124 
125     Object newValue = new Object();
126     valueRef.set(newValue);
127     assertSame(newValue, cache.getUnchecked(new Object()));
128   }
129 
testGetUnchecked_otherThrowable()130   public void testGetUnchecked_otherThrowable() {
131     final Throwable cause = new Throwable();
132     final AtomicReference<Object> valueRef = new AtomicReference<>();
133     LoadingCache<Object, Object> cache =
134         new AbstractLoadingCache<Object, Object>() {
135           @Override
136           public Object get(Object key) throws ExecutionException {
137             Object v = valueRef.get();
138             if (v == null) {
139               throw new ExecutionException(cause);
140             }
141             return v;
142           }
143 
144           @Override
145           public Object getIfPresent(Object key) {
146             return valueRef.get();
147           }
148         };
149 
150     try {
151       cache.getUnchecked(new Object());
152       fail();
153     } catch (UncheckedExecutionException expected) {
154       assertThat(expected).hasCauseThat().isEqualTo(cause);
155     }
156 
157     Object newValue = new Object();
158     valueRef.set(newValue);
159     assertSame(newValue, cache.getUnchecked(new Object()));
160   }
161 }
162