fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedBiPredicateMemoizerTest.java
blobca50ee6516f2caeed405247547bbf38b285587aa
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 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.BiFunction;
14 import java.util.function.BiPredicate;
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;
27 /**
30 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
31 public class JCacheBasedBiPredicateMemoizerTest {
33 /** Captures expected exceptions. */
34 @Rule
35 public ExpectedException thrown = ExpectedException.none();
37 /**
40 @Test
41 public void shouldMemoizeBiPredicate() {
42 // given
43 final BiPredicate<String, String> biPredicate = (first, second) -> true;
44 final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction();
45 try (final Cache<String, Boolean> cache = JCacheMemoize.createCache(BiPredicate.class)) {
46 // when
47 final JCacheBasedBiPredicateMemoizer<String, String, String> loader = new JCacheBasedBiPredicateMemoizer<>(
48 cache, keyfunction, biPredicate);
50 // then
51 Assert.assertTrue("Memoized value does not match expectation", loader.test("first", "second"));
55 /**
58 @Test
59 @SuppressWarnings(CompilerWarnings.UNCHECKED)
60 public void shouldWrapRuntimeExceptionInMemoizationException() {
61 // given
62 final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction();
63 try (final Cache<String, Boolean> cache = Mockito.mock(Cache.class)) {
64 final JCacheBasedBiPredicateMemoizer<String, String, String> loader = new JCacheBasedBiPredicateMemoizer<>(
65 cache, keyfunction, null);
66 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
68 // when
69 thrown.expect(MemoizationException.class);
71 // then
72 loader.test("first", "second");