fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedDoublePredicateMemoizerTest.java
blob70b49972e01e2283b401966f146232c3e63b5502
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;
13 import java.util.function.DoublePredicate;
15 import javax.cache.Cache;
17 import org.junit.Assert;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.ExpectedException;
21 import org.mockito.Mockito;
23 import de.xn__ho_hia.memoization.shared.MemoizationException;
24 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
26 /**
29 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
30 public class JCacheBasedDoublePredicateMemoizerTest {
32 /** Captures expected exceptions. */
33 @Rule
34 public ExpectedException thrown = ExpectedException.none();
36 /**
39 @Test
40 public void shouldMemoizePredicate() {
41 // given
42 final DoublePredicate predicate = a -> true;
43 final DoubleFunction<String> keyFunction = a -> "key";
44 try (final Cache<String, Boolean> cache = JCacheMemoize.createCache(DoublePredicate.class)) {
45 // when
46 final JCacheBasedDoublePredicateMemoizer<String> loader = new JCacheBasedDoublePredicateMemoizer<>(cache,
47 keyFunction, predicate);
49 // then
50 Assert.assertTrue("Memoized value does not match expectation", loader.test(123.456D));
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, Boolean> cache = Mockito.mock(Cache.class)) {
63 final JCacheBasedDoublePredicateMemoizer<String> loader = new JCacheBasedDoublePredicateMemoizer<>(cache,
64 keyFunction, null);
65 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
67 // when
68 thrown.expect(MemoizationException.class);
70 // then
71 loader.test(123.456D);