fix #68
[memoization.java.git] / memoization-guava / src / test / java / de / xn__ho_hia / memoization / guava / GuavaCacheBasedIntFunctionMemoizerTest.java
blob03047ebb6ada679fc5c771ee4c2962d449bd0045
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.guava;
9 import static org.mockito.BDDMockito.given;
10 import static org.mockito.Matchers.any;
12 import java.util.concurrent.ExecutionException;
13 import java.util.function.IntFunction;
15 import com.google.common.cache.Cache;
16 import com.google.common.cache.CacheBuilder;
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 /**
31 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
32 public class GuavaCacheBasedIntFunctionMemoizerTest {
34 /** Captures expected exceptions. */
35 @Rule
36 public ExpectedException thrown = ExpectedException.none();
38 /**
41 @Test
42 public void shouldAcceptLoadingCache() {
43 // given
44 final IntFunction<String> function = a -> "test";
45 final IntFunction<String> keyFunction = a -> "key";
46 final Cache<String, String> cache = CacheBuilder.newBuilder().build();
48 // when
49 final GuavaCacheBasedIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedIntFunctionMemoizer<>(
50 cache, keyFunction, function);
52 // then
53 Assert.assertNotNull(memoizer);
56 /**
59 @Test
60 public void shouldTransformInput() {
61 // given
62 final IntFunction<String> function = a -> "test";
63 final IntFunction<String> keyFunction = a -> "key";
64 final Cache<String, String> cache = CacheBuilder.newBuilder().build();
66 // when
67 final GuavaCacheBasedIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedIntFunctionMemoizer<>(
68 cache, keyFunction, function);
70 // then
71 Assert.assertEquals("test", memoizer.apply(123));
74 /**
75 * @throws ExecutionException
76 * Added for the call to 'cache.get(..)'.
78 @Test
79 @SuppressWarnings(CompilerWarnings.UNCHECKED)
80 public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException {
81 // given
82 final IntFunction<String> keyFunction = a -> "key";
83 final Cache<String, String> cache = Mockito.mock(Cache.class);
84 given(cache.get(any(), any())).willThrow(ExecutionException.class);
85 final GuavaCacheBasedIntFunctionMemoizer<String, String> memoizer = new GuavaCacheBasedIntFunctionMemoizer<>(
86 cache, keyFunction, null);
88 // when
89 thrown.expect(MemoizationException.class);
91 // then
92 memoizer.apply(123);