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
.DoubleFunction
;
13 import java
.util
.function
.DoublePredicate
;
15 import javax
.cache
.Cache
;
17 import org
.junit
.Assert
;
18 import org
.junit
.Rule
;
19 import org
.junit
.Test
;
20 import org
.junit
.rules
.ExpectedException
;
21 import org
.mockito
.Mockito
;
23 import de
.xn__ho_hia
.memoization
.shared
.MemoizationException
;
24 import de
.xn__ho_hia
.quality
.suppression
.CompilerWarnings
;
29 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
30 public class JCacheBasedDoublePredicateMemoizerTest
{
32 /** Captures expected exceptions. */
34 public ExpectedException thrown
= ExpectedException
.none();
40 public void shouldMemoizePredicate() {
42 final DoublePredicate predicate
= a
-> true;
43 final DoubleFunction
<String
> keyFunction
= a
-> "key";
44 try (final Cache
<String
, Boolean
> cache
= JCacheMemoize
.createCache(DoublePredicate
.class)) {
46 final JCacheBasedDoublePredicateMemoizer
<String
> loader
= new JCacheBasedDoublePredicateMemoizer
<>(cache
,
47 keyFunction
, predicate
);
50 Assert
.assertTrue("Memoized value does not match expectation", loader
.test(123.456D
));
58 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
59 public void shouldWrapRuntimeExceptionInMemoizationException() {
61 final DoubleFunction
<String
> keyFunction
= a
-> "key";
62 try (final Cache
<String
, Boolean
> cache
= Mockito
.mock(Cache
.class)) {
63 final JCacheBasedDoublePredicateMemoizer
<String
> loader
= new JCacheBasedDoublePredicateMemoizer
<>(cache
,
65 given(cache
.invoke(any(), any())).willThrow(RuntimeException
.class);
68 thrown
.expect(MemoizationException
.class);
71 loader
.test(123.456D
);