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
.BiConsumer
;
14 import java
.util
.function
.BiFunction
;
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 JCacheBasedBiConsumerMemoizerTest
{
33 /** Captures expected exceptions. */
35 public ExpectedException thrown
= ExpectedException
.none();
41 public void shouldMemoizeBiConsumer() {
43 final BiConsumer
<String
, String
> biConsumer
= (first
, second
) -> System
.out
.println(first
+ second
);
44 final BiFunction
<String
, String
, String
> keyfunction
= hashCodeKeyFunction();
45 try (final Cache
<String
, String
> cache
= JCacheMemoize
.createCache(BiConsumer
.class)) {
47 final JCacheBasedBiConsumerMemoizer
<String
, String
, String
> memoizer
= new JCacheBasedBiConsumerMemoizer
<>(
48 cache
, keyfunction
, biConsumer
);
51 Assert
.assertNotNull("Memoizer is NULL", memoizer
);
59 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
60 public void shouldAcceptInput() {
62 final BiConsumer
<String
, String
> biConsumer
= Mockito
.mock(BiConsumer
.class);
63 final BiFunction
<String
, String
, String
> keyFunction
= (first
, second
) -> second
+ first
;
64 try (final Cache
<String
, String
> cache
= JCacheMemoize
.createCache(BiConsumer
.class)) {
66 final JCacheBasedBiConsumerMemoizer
<String
, String
, String
> memoizer
= new JCacheBasedBiConsumerMemoizer
<>(
67 cache
, keyFunction
, biConsumer
);
70 memoizer
.accept("first", "second");
71 Mockito
.verify(biConsumer
).accept("first", "second");
79 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
80 public void shouldWrapRuntimeExceptionInMemoizationException() {
82 final BiFunction
<String
, String
, String
> keyfunction
= hashCodeKeyFunction();
83 try (final Cache
<String
, String
> cache
= Mockito
.mock(Cache
.class)) {
84 final JCacheBasedBiConsumerMemoizer
<String
, String
, String
> loader
= new JCacheBasedBiConsumerMemoizer
<>(
85 cache
, keyfunction
, null);
86 given(cache
.invoke(any(), any())).willThrow(RuntimeException
.class);
89 thrown
.expect(MemoizationException
.class);
92 loader
.accept("first", "second");