fix #68
[memoization.java.git] / memoization-guava / src / test / java / de / xn__ho_hia / memoization / guava / GuavaCacheBasedDoubleToIntFunctionMemoizerTest.java
blob5a73eaf19bd86c8de7f0bdb692612359cc3fe57d
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.DoubleFunction;
14 import java.util.function.DoubleToIntFunction;
16 import com.google.common.cache.Cache;
17 import com.google.common.cache.CacheBuilder;
19 import org.junit.Assert;
20 import org.junit.Rule;
21 import org.junit.Test;
22 import org.junit.rules.ExpectedException;
23 import org.mockito.Mockito;
25 import de.xn__ho_hia.memoization.shared.MemoizationException;
26 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
28 /**
32 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
33 public class GuavaCacheBasedDoubleToIntFunctionMemoizerTest {
35 /** Captures expected exceptions. */
36 @Rule
37 public ExpectedException thrown = ExpectedException.none();
39 /**
42 @Test
43 public void shouldAcceptLoadingCache() {
44 // given
45 final DoubleToIntFunction function = a -> 123;
46 final DoubleFunction<Double> keyFunction = Double::valueOf;
47 final Cache<Double, Integer> cache = CacheBuilder.newBuilder().build();
49 // when
50 final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>(
51 cache, keyFunction, function);
53 // then
54 Assert.assertNotNull(memoizer);
57 /**
60 @Test
61 public void shouldTransformInput() {
62 // given
63 final DoubleToIntFunction function = a -> 123;
64 final DoubleFunction<Double> keyFunction = Double::valueOf;
65 final Cache<Double, Integer> cache = CacheBuilder.newBuilder().build();
67 // when
68 final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>(
69 cache, keyFunction, function);
71 // then
72 Assert.assertEquals("Memoized value does not match expectation", 123, memoizer.applyAsInt(789D));
75 /**
76 * @throws ExecutionException
77 * Added for the call to 'cache.get(..)'.
79 @Test
80 @SuppressWarnings(CompilerWarnings.UNCHECKED)
81 public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException {
82 // given
83 final DoubleFunction<Double> keyFunction = Double::valueOf;
84 final Cache<Double, Integer> cache = Mockito.mock(Cache.class);
85 given(cache.get(any(), any())).willThrow(ExecutionException.class);
86 final GuavaCacheBasedDoubleToIntFunctionMemoizer<Double> memoizer = new GuavaCacheBasedDoubleToIntFunctionMemoizer<>(
87 cache, keyFunction, null);
89 // when
90 thrown.expect(MemoizationException.class);
92 // then
93 memoizer.applyAsInt(789D);