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
.jcache
;
9 import static org
.mockito
.BDDMockito
.given
;
10 import static org
.mockito
.Matchers
.any
;
12 import java
.util
.function
.IntConsumer
;
13 import java
.util
.function
.IntFunction
;
15 import javax
.cache
.Cache
;
17 import org
.junit
.Rule
;
18 import org
.junit
.Test
;
19 import org
.junit
.rules
.ExpectedException
;
20 import org
.mockito
.Mockito
;
22 import de
.xn__ho_hia
.memoization
.shared
.MemoizationException
;
23 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
28 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
, CompilerWarnings
.UNCHECKED
})
29 public class JCacheBasedIntConsumerMemoizerTest
{
31 /** Captures expected exceptions. */
33 public ExpectedException thrown
= ExpectedException
.none();
39 public void shouldMemoizeConsumer() {
41 final IntConsumer consumer
= Mockito
.mock(IntConsumer
.class);
42 final IntFunction
<String
> keyFunction
= a
-> "key";
43 try (final Cache
<String
, Integer
> cache
= JCacheMemoize
.createCache(IntConsumer
.class)) {
45 final JCacheBasedIntConsumerMemoizer
<String
> loader
= new JCacheBasedIntConsumerMemoizer
<>(cache
,
46 keyFunction
, consumer
);
50 Mockito
.verify(consumer
).accept(123);
58 public void shouldMemoizeConsumerOnce() {
60 final IntConsumer consumer
= Mockito
.mock(IntConsumer
.class);
61 final IntFunction
<String
> keyFunction
= a
-> "key";
62 try (final Cache
<String
, Integer
> cache
= JCacheMemoize
.createCache(IntConsumer
.class)) {
64 final JCacheBasedIntConsumerMemoizer
<String
> loader
= new JCacheBasedIntConsumerMemoizer
<>(cache
,
65 keyFunction
, consumer
);
70 Mockito
.verify(consumer
).accept(123);
78 public void shouldWrapRuntimeExceptionInMemoizationException() {
80 final IntFunction
<String
> keyFunction
= a
-> "key";
81 try (final Cache
<String
, Integer
> cache
= Mockito
.mock(Cache
.class)) {
82 final JCacheBasedIntConsumerMemoizer
<String
> loader
= new JCacheBasedIntConsumerMemoizer
<>(cache
,
84 given(cache
.invoke(any(), any())).willThrow(RuntimeException
.class);
87 thrown
.expect(MemoizationException
.class);