fix #146
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheMemoizeCustomKeyTest.java
blobea48201d2c6e6278b52abc83e42f456635366599
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 java.util.function.BiConsumer;
10 import java.util.function.BiFunction;
11 import java.util.function.BiPredicate;
12 import java.util.function.BooleanSupplier;
13 import java.util.function.Consumer;
14 import java.util.function.DoubleConsumer;
15 import java.util.function.DoubleFunction;
16 import java.util.function.DoublePredicate;
17 import java.util.function.DoubleSupplier;
18 import java.util.function.Function;
19 import java.util.function.IntConsumer;
20 import java.util.function.IntFunction;
21 import java.util.function.IntPredicate;
22 import java.util.function.IntSupplier;
23 import java.util.function.LongConsumer;
24 import java.util.function.LongFunction;
25 import java.util.function.LongPredicate;
26 import java.util.function.LongSupplier;
27 import java.util.function.Predicate;
28 import java.util.function.Supplier;
30 import org.junit.Assert;
31 import org.junit.Test;
33 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
35 /**
38 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
39 public class JCacheMemoizeCustomKeyTest {
41 /**
44 @Test
45 public void shouldMemoizeSupplierWithKeySupplier() {
46 // given
47 final Supplier<String> supplier = () -> "test";
48 final Supplier<String> keySupplier = () -> "key";
50 // when
51 final Supplier<String> memoize = JCacheMemoize.supplier(supplier, keySupplier);
53 // then
54 Assert.assertNotNull("Memoized Supplier is NULL", memoize);
57 /**
60 @Test
61 public void shouldMemoizeBooleanSupplierWithKeySupplier() {
62 // given
63 final BooleanSupplier supplier = () -> true;
64 final Supplier<String> keySupplier = () -> "key";
66 // when
67 final BooleanSupplier memoize = JCacheMemoize.booleanSupplier(supplier, keySupplier);
69 // then
70 Assert.assertNotNull("Memoized BooleanSupplier is NULL", memoize);
73 /**
76 @Test
77 public void shouldMemoizeDoubleSupplierWithKeyFunction() {
78 // given
79 final DoubleSupplier supplier = () -> 123.456D;
80 final Supplier<String> keySupplier = () -> "key";
82 // when
83 final DoubleSupplier memoize = JCacheMemoize.doubleSupplier(supplier, keySupplier);
85 // then
86 Assert.assertNotNull("Memoized DoubleSupplier is NULL", memoize);
89 /**
92 @Test
93 public void shouldMemoizeIntSupplierWithKeyFunction() {
94 // given
95 final IntSupplier supplier = () -> 123;
96 final Supplier<String> keySupplier = () -> "key";
98 // when
99 final IntSupplier memoize = JCacheMemoize.intSupplier(supplier, keySupplier);
101 // then
102 Assert.assertNotNull("Memoized IntSupplier is NULL", memoize);
108 @Test
109 public void shouldMemoizeLongSupplierWithKeyFunction() {
110 // given
111 final LongSupplier supplier = () -> 123L;
112 final Supplier<String> keySupplier = () -> "key";
114 // when
115 final LongSupplier memoize = JCacheMemoize.longSupplier(supplier, keySupplier);
117 // then
118 Assert.assertNotNull("Memoized LongSupplier is NULL", memoize);
124 @Test
125 public void shouldMemoizeFunctionWithKeyFunction() {
126 // given
127 final Function<String, String> function = a -> "test";
128 final Function<String, String> keyFunction = Function.identity();
130 // when
131 final Function<String, String> memoize = JCacheMemoize.function(function, keyFunction);
133 // then
134 Assert.assertNotNull("Memoized Function is NULL", memoize);
140 @Test
141 public void shouldMemoizeIntFunctionWithKeyFunction() {
142 // given
143 final IntFunction<String> function = a -> "test";
144 final IntFunction<String> keyFunction = a -> "key";
146 // when
147 final IntFunction<String> memoize = JCacheMemoize.intFunction(function, keyFunction);
149 // then
150 Assert.assertNotNull("Memoized IntFunction is NULL", memoize);
156 @Test
157 public void shouldMemoizeLongFunctionWithKeyFunction() {
158 // given
159 final LongFunction<String> function = a -> "test";
160 final LongFunction<String> keyFunction = a -> "key";
162 // when
163 final LongFunction<String> memoize = JCacheMemoize.longFunction(function, keyFunction);
165 // then
166 Assert.assertNotNull("Memoized LongFunction is NULL", memoize);
172 @Test
173 public void shouldMemoizeDoubleFunction() {
174 // given
175 final DoubleFunction<String> function = a -> "test";
176 final DoubleFunction<String> keyFunction = a -> "key";
178 // when
179 final DoubleFunction<String> memoize = JCacheMemoize.doubleFunction(function, keyFunction);
181 // then
182 Assert.assertNotNull("Memoized DoubleFunction is NULL", memoize);
188 @Test
189 public void shouldMemoizePredicateWithKeyFunction() {
190 // given
191 final Predicate<String> predicate = a -> true;
192 final Function<String, String> keyFunction = Function.identity();
194 // when
195 final Predicate<String> memoize = JCacheMemoize.predicate(predicate, keyFunction);
197 // then
198 Assert.assertNotNull("Memoized Predicate is NULL", memoize);
204 @Test
205 public void shouldMemoizeDoublePredicateWithKeyFunction() {
206 // given
207 final DoublePredicate predicate = a -> true;
208 final DoubleFunction<String> keyFunction = a -> "key";
210 // when
211 final DoublePredicate memoize = JCacheMemoize.doublePredicate(predicate, keyFunction);
213 // then
214 Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize);
220 @Test
221 public void shouldMemoizeIntPredicateWithKeyFunction() {
222 // given
223 final IntPredicate predicate = a -> true;
224 final IntFunction<String> keyFunction = a -> "key";
226 // when
227 final IntPredicate memoize = JCacheMemoize.intPredicate(predicate, keyFunction);
229 // then
230 Assert.assertNotNull("Memoized IntPredicate is NULL", memoize);
236 @Test
237 public void shouldMemoizeLongPredicateWithKeyFunction() {
238 // given
239 final LongPredicate predicate = a -> true;
240 final LongFunction<String> keyFunction = a -> "key";
242 // when
243 final LongPredicate memoize = JCacheMemoize.longPredicate(predicate, keyFunction);
245 // then
246 Assert.assertNotNull("Memoized LongPredicate is NULL", memoize);
252 @Test
253 public void shouldMemoizeConsumerWithKeyFunction() {
254 // given
255 final Consumer<String> consumer = System.out::println;
256 final Function<String, String> keyFunction = Function.identity();
258 // when
259 final Consumer<String> memoize = JCacheMemoize.consumer(consumer, keyFunction);
261 // then
262 Assert.assertNotNull("Memoized Consumer is NULL", memoize);
268 @Test
269 public void shouldMemoizeDoubleConsumerWithKeyFunction() {
270 // given
271 final DoubleConsumer consumer = System.out::println;
272 final DoubleFunction<String> keyFunction = a -> "key";
274 // when
275 final DoubleConsumer memoize = JCacheMemoize.doubleConsumer(consumer, keyFunction);
277 // then
278 Assert.assertNotNull("Memoized DoubleConsumer is NULL", memoize);
284 @Test
285 public void shouldMemoizeIntConsumerWithKeyFunction() {
286 // given
287 final IntConsumer consumer = System.out::println;
288 final IntFunction<String> keyFunction = a -> "key";
290 // when
291 final IntConsumer memoize = JCacheMemoize.intConsumer(consumer, keyFunction);
293 // then
294 Assert.assertNotNull("Memoized IntConsumer is NULL", memoize);
300 @Test
301 public void shouldMemoizeLongConsumer() {
302 // given
303 final LongConsumer consumer = System.out::println;
304 final LongFunction<String> keyFunction = a -> "key";
306 // when
307 final LongConsumer memoize = JCacheMemoize.longConsumer(consumer, keyFunction);
309 // then
310 Assert.assertNotNull("Memoized LongConsumer is NULL", memoize);
316 @Test
317 public void shouldMemoizeBiConsumerWithKeyFunction() {
318 // given
319 final BiConsumer<String, String> consumer = (a, b) -> System.out.println(a + b);
320 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
322 // when
323 final BiConsumer<String, String> memoize = JCacheMemoize.biConsumer(consumer, keyFunction);
325 // then
326 Assert.assertNotNull("Memoized BiConsumer is NULL", memoize);
332 @Test
333 public void shouldMemoizeBiFunctionWithKeyBiFunction() {
334 // given
335 final BiFunction<String, String, String> function = (first, second) -> "test";
336 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
338 // when
339 final BiFunction<String, String, String> memoize = JCacheMemoize.biFunction(function, keyFunction);
341 // then
342 Assert.assertNotNull("Memoized BiFunction is NULL", memoize);
348 @Test
349 public void shouldMemoizeBiPredicateWithKeyBiFunction() {
350 // given
351 final BiPredicate<String, String> biPredicate = (first, second) -> true;
352 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
354 // when
355 final BiPredicate<String, String> memoize = JCacheMemoize.biPredicate(biPredicate, keyFunction);
357 // then
358 Assert.assertNotNull("Memoized BiPredicate is NULL", memoize);