fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedPredicateMemoizerTest.java
blob3d8c9af2755664a15b31883a53225322b270cfbd
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.Function;
13 import java.util.function.Predicate;
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 JCacheBasedPredicateMemoizerTest {
32 /** Captures expected exceptions. */
33 @Rule
34 public ExpectedException thrown = ExpectedException.none();
36 /**
39 @Test
40 public void shouldMemoizePredicate() {
41 // given
42 final Predicate<String> predicate = a -> true;
43 final Function<String, String> keyFunction = Function.identity();
44 try (final Cache<String, Boolean> cache = JCacheMemoize.createCache(Predicate.class)) {
45 // when
46 final JCacheBasedPredicateMemoizer<String, String> loader = new JCacheBasedPredicateMemoizer<>(cache,
47 keyFunction, predicate);
49 // then
50 Assert.assertTrue("Memoized value does not match expectation", loader.test("test"));
54 /**
57 @Test
58 @SuppressWarnings(CompilerWarnings.UNCHECKED)
59 public void shouldWrapRuntimeExceptionInMemoizationException() {
60 // given
61 final Function<String, String> keyFunction = Function.identity();
62 try (final Cache<String, Boolean> cache = Mockito.mock(Cache.class)) {
63 final JCacheBasedPredicateMemoizer<String, String> loader = new JCacheBasedPredicateMemoizer<>(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("test");