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