1 /* 2 * Copyright (C) 2009 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 org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import com.google.common.collect.ImmutableList; 24 import com.google.common.collect.ImmutableMap; 25 import java.util.concurrent.ExecutionException; 26 import junit.framework.TestCase; 27 28 /** 29 * Unit test for {@link ForwardingLoadingCache}. 30 * 31 * @author Charles Fry 32 */ 33 public class ForwardingLoadingCacheTest extends TestCase { 34 private LoadingCache<String, Boolean> forward; 35 private LoadingCache<String, Boolean> mock; 36 37 @SuppressWarnings({"unchecked", "DoNotMock"}) // mock 38 @Override setUp()39 public void setUp() throws Exception { 40 super.setUp(); 41 /* 42 * Class parameters must be raw, so we can't create a proxy with generic 43 * type arguments. The created proxy only records calls and returns null, so 44 * the type is irrelevant at runtime. 45 */ 46 mock = mock(LoadingCache.class); 47 forward = 48 new ForwardingLoadingCache<String, Boolean>() { 49 @Override 50 protected LoadingCache<String, Boolean> delegate() { 51 return mock; 52 } 53 }; 54 } 55 testGet()56 public void testGet() throws ExecutionException { 57 when(mock.get("key")).thenReturn(Boolean.TRUE); 58 assertSame(Boolean.TRUE, forward.get("key")); 59 } 60 testGetUnchecked()61 public void testGetUnchecked() { 62 when(mock.getUnchecked("key")).thenReturn(Boolean.TRUE); 63 assertSame(Boolean.TRUE, forward.getUnchecked("key")); 64 } 65 testGetAll()66 public void testGetAll() throws ExecutionException { 67 when(mock.getAll(ImmutableList.of("key"))).thenReturn(ImmutableMap.of("key", Boolean.TRUE)); 68 assertEquals(ImmutableMap.of("key", Boolean.TRUE), forward.getAll(ImmutableList.of("key"))); 69 } 70 testApply()71 public void testApply() { 72 when(mock.apply("key")).thenReturn(Boolean.TRUE); 73 assertSame(Boolean.TRUE, forward.apply("key")); 74 } 75 testInvalidate()76 public void testInvalidate() { 77 forward.invalidate("key"); 78 verify(mock).invalidate("key"); 79 } 80 testRefresh()81 public void testRefresh() throws ExecutionException { 82 forward.refresh("key"); 83 verify(mock).refresh("key"); 84 } 85 testInvalidateAll()86 public void testInvalidateAll() { 87 forward.invalidateAll(); 88 verify(mock).invalidateAll(); 89 } 90 testSize()91 public void testSize() { 92 when(mock.size()).thenReturn(0L); 93 forward.size(); 94 } 95 testStats()96 public void testStats() { 97 when(mock.stats()).thenReturn(null); 98 assertNull(forward.stats()); 99 } 100 testAsMap()101 public void testAsMap() { 102 when(mock.asMap()).thenReturn(null); 103 assertNull(forward.asMap()); 104 } 105 testCleanUp()106 public void testCleanUp() { 107 forward.cleanUp(); 108 verify(mock).cleanUp(); 109 } 110 111 /** Make sure that all methods are forwarded. */ 112 private static class OnlyGet<K, V> extends ForwardingLoadingCache<K, V> { 113 @Override delegate()114 protected LoadingCache<K, V> delegate() { 115 return null; 116 } 117 } 118 } 119