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 de
.xn__ho_hia
.memoization
.shared
.MemoizationDefaults
.hashCodeKeyFunction
;
10 import static org
.mockito
.BDDMockito
.given
;
11 import static org
.mockito
.Matchers
.any
;
13 import java
.util
.function
.BiFunction
;
14 import java
.util
.function
.BiPredicate
;
16 import javax
.cache
.Cache
;
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
;
30 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
31 public class JCacheBasedBiPredicateMemoizerTest
{
33 /** Captures expected exceptions. */
35 public ExpectedException thrown
= ExpectedException
.none();
41 public void shouldMemoizeBiPredicate() {
43 final BiPredicate
<String
, String
> biPredicate
= (first
, second
) -> true;
44 final BiFunction
<String
, String
, String
> keyfunction
= hashCodeKeyFunction();
45 try (final Cache
<String
, Boolean
> cache
= JCacheMemoize
.createCache(BiPredicate
.class)) {
47 final JCacheBasedBiPredicateMemoizer
<String
, String
, String
> loader
= new JCacheBasedBiPredicateMemoizer
<>(
48 cache
, keyfunction
, biPredicate
);
51 Assert
.assertTrue("Memoized value does not match expectation", loader
.test("first", "second"));
59 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
60 public void shouldWrapRuntimeExceptionInMemoizationException() {
62 final BiFunction
<String
, String
, String
> keyfunction
= hashCodeKeyFunction();
63 try (final Cache
<String
, Boolean
> cache
= Mockito
.mock(Cache
.class)) {
64 final JCacheBasedBiPredicateMemoizer
<String
, String
, String
> loader
= new JCacheBasedBiPredicateMemoizer
<>(
65 cache
, keyfunction
, null);
66 given(cache
.invoke(any(), any())).willThrow(RuntimeException
.class);
69 thrown
.expect(MemoizationException
.class);
72 loader
.test("first", "second");