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
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
;
28 @SuppressWarnings({ CompilerWarnings
.NLS
, CompilerWarnings
.STATIC_METHOD
})
29 public class JCacheBasedFunctionMemoizerTest
{
31 /** Captures expected exceptions. */
33 public ExpectedException thrown
= ExpectedException
.none();
39 public void shouldMemoizeFunction() {
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)) {
45 final JCacheBasedFunctionMemoizer
<String
, String
, String
> loader
= new JCacheBasedFunctionMemoizer
<>(cache
,
46 keyFunction
, function
);
49 Assert
.assertEquals("Memoized value does not match expectation", "test", loader
.apply("test"));
57 @SuppressWarnings(CompilerWarnings
.UNCHECKED
)
58 public void shouldWrapRuntimeExceptionInMemoizationException() {
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
,
64 given(cache
.invoke(any(), any())).willThrow(RuntimeException
.class);
67 thrown
.expect(MemoizationException
.class);