2 * This file is part of memoization.java. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at http://creativecommons.org/publicdomain/zero/1.0/. No part of memoization.java,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
7 package de
.xn__ho_hia
.memoization
.guava
;
9 import static org
.mockito
.BDDMockito
.given
;
10 import static org
.mockito
.Matchers
.any
;
12 import java
.util
.concurrent
.ExecutionException
;
13 import java
.util
.function
.IntFunction
;
15 import com
.google
.common
.cache
.Cache
;
16 import com
.google
.common
.cache
.CacheBuilder
;
18 import org
.junit
.Assert
;
19 import org
.junit
.Rule
;
20 import org
.junit
.Test
;
21 import org
.junit
.rules
.ExpectedException
;
22 import org
.mockito
.Mockito
;
24 import de
.xn__ho_hia
.memoization
.shared
.MemoizationException
;
25 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
31 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
32 public class GuavaCacheBasedIntFunctionMemoizerTest
{
34 /** Captures expected exceptions. */
36 public ExpectedException thrown
= ExpectedException
.none();
42 public void shouldAcceptLoadingCache() {
44 final IntFunction
<String
> function
= a
-> "test";
45 final IntFunction
<String
> keyFunction
= a
-> "key";
46 final Cache
<String
, String
> cache
= CacheBuilder
.newBuilder().build();
49 final GuavaCacheBasedIntFunctionMemoizer
<String
, String
> memoizer
= new GuavaCacheBasedIntFunctionMemoizer
<>(
50 cache
, keyFunction
, function
);
53 Assert
.assertNotNull(memoizer
);
60 public void shouldTransformInput() {
62 final IntFunction
<String
> function
= a
-> "test";
63 final IntFunction
<String
> keyFunction
= a
-> "key";
64 final Cache
<String
, String
> cache
= CacheBuilder
.newBuilder().build();
67 final GuavaCacheBasedIntFunctionMemoizer
<String
, String
> memoizer
= new GuavaCacheBasedIntFunctionMemoizer
<>(
68 cache
, keyFunction
, function
);
71 Assert
.assertEquals("test", memoizer
.apply(123));
75 * @throws ExecutionException
76 * Added for the call to 'cache.get(..)'.
79 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
80 public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException
{
82 final IntFunction
<String
> keyFunction
= a
-> "key";
83 final Cache
<String
, String
> cache
= Mockito
.mock(Cache
.class);
84 given(cache
.get(any(), any())).willThrow(ExecutionException
.class);
85 final GuavaCacheBasedIntFunctionMemoizer
<String
, String
> memoizer
= new GuavaCacheBasedIntFunctionMemoizer
<>(
86 cache
, keyFunction
, null);
89 thrown
.expect(MemoizationException
.class);