fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheBasedBiConsumerMemoizerTest.java
blobbb3b7676b966a2c6008a145400dada89bbeb62c8
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 de.xn__ho_hia.memoization.shared.MemoizationDefaults.hashCodeKeyFunction;
10 import static org.mockito.BDDMockito.given;
11 import static org.mockito.Matchers.any;
13 import java.util.function.BiConsumer;
14 import java.util.function.BiFunction;
16 import javax.cache.Cache;
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 /**
30 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
31 public class JCacheBasedBiConsumerMemoizerTest {
33 /** Captures expected exceptions. */
34 @Rule
35 public ExpectedException thrown = ExpectedException.none();
37 /**
40 @Test
41 public void shouldMemoizeBiConsumer() {
42 // given
43 final BiConsumer<String, String> biConsumer = (first, second) -> System.out.println(first + second);
44 final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction();
45 try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) {
46 // when
47 final JCacheBasedBiConsumerMemoizer<String, String, String> memoizer = new JCacheBasedBiConsumerMemoizer<>(
48 cache, keyfunction, biConsumer);
50 // then
51 Assert.assertNotNull("Memoizer is NULL", memoizer);
55 /**
58 @Test
59 @SuppressWarnings(CompilerWarnings.UNCHECKED)
60 public void shouldAcceptInput() {
61 // given
62 final BiConsumer<String, String> biConsumer = Mockito.mock(BiConsumer.class);
63 final BiFunction<String, String, String> keyFunction = (first, second) -> second + first;
64 try (final Cache<String, String> cache = JCacheMemoize.createCache(BiConsumer.class)) {
65 // when
66 final JCacheBasedBiConsumerMemoizer<String, String, String> memoizer = new JCacheBasedBiConsumerMemoizer<>(
67 cache, keyFunction, biConsumer);
69 // then
70 memoizer.accept("first", "second");
71 Mockito.verify(biConsumer).accept("first", "second");
75 /**
78 @Test
79 @SuppressWarnings(CompilerWarnings.UNCHECKED)
80 public void shouldWrapRuntimeExceptionInMemoizationException() {
81 // given
82 final BiFunction<String, String, String> keyfunction = hashCodeKeyFunction();
83 try (final Cache<String, String> cache = Mockito.mock(Cache.class)) {
84 final JCacheBasedBiConsumerMemoizer<String, String, String> loader = new JCacheBasedBiConsumerMemoizer<>(
85 cache, keyfunction, null);
86 given(cache.invoke(any(), any())).willThrow(RuntimeException.class);
88 // when
89 thrown.expect(MemoizationException.class);
91 // then
92 loader.accept("first", "second");