fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedLongConsumerMemoizerTest.java
blob54d624980bf3519cb8e7eebf7545751c4cb7e5bf
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.LongConsumer;
13 import java.util.function.LongFunction;
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 JCacheBasedLongConsumerMemoizerTest {
31 /** Captures expected exceptions. */
32 @Rule
33 public ExpectedException thrown = ExpectedException.none();
35 /**
38 @Test
39 public void shouldMemoizeConsumer() {
40 // given
41 final LongConsumer consumer = Mockito.mock(LongConsumer.class);
42 final LongFunction<String> keyFunction = a -> "key";
43 try (final Cache<String, Long> cache = JCacheMemoize.createCache(LongConsumer.class)) {
44 // when
45 final JCacheBasedLongConsumerMemoizer<String> loader = new JCacheBasedLongConsumerMemoizer<>(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 LongConsumer consumer = Mockito.mock(LongConsumer.class);
61 final LongFunction<String> keyFunction = a -> "key";
62 try (final Cache<String, Long> cache = JCacheMemoize.createCache(LongConsumer.class)) {
63 // when
64 final JCacheBasedLongConsumerMemoizer<String> loader = new JCacheBasedLongConsumerMemoizer<>(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 LongFunction<String> keyFunction = a -> "key";
81 try (final Cache<String, Long> cache = Mockito.mock(Cache.class)) {
82 final JCacheBasedLongConsumerMemoizer<String> loader = new JCacheBasedLongConsumerMemoizer<>(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);