fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedFunctionMemoizerTest.java
blob59afe955bdacf57528b9f2d08da7262f7164eb9c
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;
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 JCacheBasedFunctionMemoizerTest {
31 /** Captures expected exceptions. */
32 @Rule
33 public ExpectedException thrown = ExpectedException.none();
35 /**
38 @Test
39 public void shouldMemoizeFunction() {
40 // given
41 final Function<String, String> function = Function.identity();
42 final Function<String, String> keyFunction = Function.identity();
43 try (final Cache<String, String> cache = JCacheMemoize.createCache(Function.class)) {
44 // when
45 final JCacheBasedFunctionMemoizer<String, String, String> loader = new JCacheBasedFunctionMemoizer<>(cache,
46 keyFunction, function);
48 // then
49 Assert.assertEquals("Memoized value does not match expectation", "test", loader.apply("test"));
53 /**
56 @Test
57 @SuppressWarnings(CompilerWarnings.UNCHECKED)
58 public void shouldWrapRuntimeExceptionInMemoizationException() {
59 // given
60 final Function<String, String> keyFunction = Function.identity();
61 try (final Cache<String, String> cache = Mockito.mock(Cache.class)) {
62 final JCacheBasedFunctionMemoizer<String, String, String> loader = new JCacheBasedFunctionMemoizer<>(cache,
63 keyFunction, null);
64 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
66 // when
67 thrown.expect(MemoizationException.class);
69 // then
70 loader.apply("test");