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