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
.IntConsumer
;
14 import java
.util
.function
.IntFunction
;
16 import com
.google
.common
.cache
.Cache
;
17 import com
.google
.common
.cache
.CacheBuilder
;
19 import org
.junit
.Assert
;
20 import org
.junit
.Rule
;
21 import org
.junit
.Test
;
22 import org
.junit
.rules
.ExpectedException
;
23 import org
.mockito
.Mockito
;
25 import de
.xn__ho_hia
.memoization
.shared
.MemoizationException
;
26 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
32 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
33 public class GuavaCacheBasedIntConsumerMemoizerTest
{
35 /** Captures expected exceptions. */
37 public ExpectedException thrown
= ExpectedException
.none();
43 public void shouldAcceptCacheAndKeyFunctionAndConsumer() {
45 final IntFunction
<String
> keyFunction
= a
-> "key";
46 final IntConsumer consumer
= System
.out
::println
;
47 final Cache
<String
, Integer
> cache
= CacheBuilder
.newBuilder().build();
50 final GuavaCacheBasedIntConsumerMemoizer
<String
> memoizer
= new GuavaCacheBasedIntConsumerMemoizer
<>(
51 cache
, keyFunction
, consumer
);
54 Assert
.assertNotNull(memoizer
);
61 public void shouldConsumeGivenValue() {
63 final IntFunction
<String
> keyFunction
= a
-> "key";
64 final IntConsumer consumer
= Mockito
.mock(IntConsumer
.class);
65 final Cache
<String
, Integer
> cache
= CacheBuilder
.newBuilder().build();
68 final GuavaCacheBasedIntConsumerMemoizer
<String
> memoizer
= new GuavaCacheBasedIntConsumerMemoizer
<>(
69 cache
, keyFunction
, consumer
);
73 Mockito
.verify(consumer
).accept(123);
77 * @throws ExecutionException
78 * Added for the call to 'cache.get(..)'.
81 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
82 public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException
{
84 final IntFunction
<String
> keyFunction
= a
-> "key";
85 final IntConsumer consumer
= System
.out
::println
;
86 final Cache
<String
, Integer
> cache
= Mockito
.mock(Cache
.class);
87 given(cache
.get(any(), any())).willThrow(ExecutionException
.class);
88 final GuavaCacheBasedIntConsumerMemoizer
<String
> memoizer
= new GuavaCacheBasedIntConsumerMemoizer
<>(
89 cache
, keyFunction
, consumer
);
92 thrown
.expect(MemoizationException
.class);