fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedIntConsumerMemoizerTest.java
blob768c4b72ffcdee9bca9bc18fc1f8ccc074a79eb0
1 /*
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
5 * in the LICENSE file.
6 */
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;
25 /**
28 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD, CompilerWarnings.UNCHECKED })
29 public class JCacheBasedIntConsumerMemoizerTest {
31 /** Captures expected exceptions. */
32 @Rule
33 public ExpectedException thrown = ExpectedException.none();
35 /**
38 @Test
39 public void shouldMemoizeConsumer() {
40 // given
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)) {
44 // when
45 final JCacheBasedIntConsumerMemoizer<String> loader = new JCacheBasedIntConsumerMemoizer<>(cache,
46 keyFunction, consumer);
48 // then
49 loader.accept(123);
50 Mockito.verify(consumer).accept(123);
54 /**
57 @Test
58 public void shouldMemoizeConsumerOnce() {
59 // given
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)) {
63 // when
64 final JCacheBasedIntConsumerMemoizer<String> loader = new JCacheBasedIntConsumerMemoizer<>(cache,
65 keyFunction, consumer);
67 // then
68 loader.accept(123);
69 loader.accept(123);
70 Mockito.verify(consumer).accept(123);
74 /**
77 @Test
78 public void shouldWrapRuntimeExceptionInMemoizationException() {
79 // given
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,
83 keyFunction, null);
84 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
86 // when
87 thrown.expect(MemoizationException.class);
89 // then
90 loader.accept(123);