fix #68
[memoization.java.git] / memoization-guava / src / test / java / de / xn__ho_hia / memoization / guava / GuavaCacheBasedConsumerMemoizerTest.java
blob82b4355e2ac32d6f57c5092d211fa5e02441f990
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.Consumer;
14 import java.util.function.Function;
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 GuavaCacheBasedConsumerMemoizerTest {
35 /** Captures expected exceptions. */
36 @Rule
37 public ExpectedException thrown = ExpectedException.none();
39 /**
42 @Test
43 public void shouldAcceptCacheAndKeyFunctionAndConsumer() {
44 // given
45 final Function<String, String> keyFunction = Function.identity();
46 final Consumer<String> consumer = System.out::println;
47 final Cache<String, String> cache = CacheBuilder.newBuilder().build();
49 // when
50 final GuavaCacheBasedConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedConsumerMemoizer<>(cache,
51 keyFunction, consumer);
53 // then
54 Assert.assertNotNull(memoizer);
57 /**
60 @Test
61 @SuppressWarnings(CompilerWarnings.UNCHECKED)
62 public void shouldConsumeGivenValue() {
63 // given
64 final Function<String, String> keyFunction = Function.identity();
65 final Consumer<String> consumer = Mockito.mock(Consumer.class);
66 final Cache<String, String> cache = CacheBuilder.newBuilder().build();
68 // when
69 final GuavaCacheBasedConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedConsumerMemoizer<>(cache,
70 keyFunction, consumer);
72 // then
73 memoizer.accept("value");
74 Mockito.verify(consumer).accept("value");
77 /**
78 * @throws ExecutionException
79 * Added for the call to 'cache.get(..)'.
81 @Test
82 @SuppressWarnings(CompilerWarnings.UNCHECKED)
83 public void shouldWrapExecutionExceptionInMemoizationException() throws ExecutionException {
84 // given
85 final Function<String, String> keyFunction = Function.identity();
86 final Consumer<String> consumer = System.out::println;
87 final Cache<String, String> cache = Mockito.mock(Cache.class);
88 given(cache.get(any(), any())).willThrow(ExecutionException.class);
89 final GuavaCacheBasedConsumerMemoizer<String, String> memoizer = new GuavaCacheBasedConsumerMemoizer<>(cache,
90 keyFunction, consumer);
92 // when
93 thrown.expect(MemoizationException.class);
95 // then
96 memoizer.accept("test");