fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedDoubleFunctionMemoizerTest.java
blob0529cc88e3e2313d00389665e206520a6870f94d
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.DoubleFunction;
14 import javax.cache.Cache;
16 import org.junit.Assert;
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 })
29 public class JCacheBasedDoubleFunctionMemoizerTest {
31 /** Captures expected exceptions. */
32 @Rule
33 public ExpectedException thrown = ExpectedException.none();
35 /**
38 @Test
39 public void shouldMemoizeFunction() {
40 // given
41 final DoubleFunction<String> function = a -> "test";
42 final DoubleFunction<String> keyFunction = a -> "key";
43 try (final Cache<String, String> cache = JCacheMemoize.createCache(DoubleFunction.class)) {
44 // when
45 final JCacheBasedDoubleFunctionMemoizer<String, String> loader = new JCacheBasedDoubleFunctionMemoizer<>(
46 cache,
47 keyFunction, function);
49 // then
50 Assert.assertEquals("Memoized value does not match expectation", "test", loader.apply(123));
54 /**
57 @Test
58 @SuppressWarnings(CompilerWarnings.UNCHECKED)
59 public void shouldWrapRuntimeExceptionInMemoizationException() {
60 // given
61 final DoubleFunction<String> keyFunction = a -> "key";
62 try (final Cache<String, String> cache = Mockito.mock(Cache.class)) {
63 final JCacheBasedDoubleFunctionMemoizer<String, String> loader = new JCacheBasedDoubleFunctionMemoizer<>(
64 cache,
65 keyFunction, null);
66 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
68 // when
69 thrown.expect(MemoizationException.class);
71 // then
72 loader.apply(123);