fix #151
[memoization.java.git] / memoization-jcache / src / test / java / de / xn__ho_hia / memoization / jcache / JCacheMemoizeCustomKeyTest.java
blob6a3ba8ea7a5834e1547f21f1704864006c1f96fc
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.DoubleBinaryOperator;
15 import java.util.function.DoubleConsumer;
16 import java.util.function.DoubleFunction;
17 import java.util.function.DoublePredicate;
18 import java.util.function.DoubleSupplier;
19 import java.util.function.Function;
20 import java.util.function.IntBinaryOperator;
21 import java.util.function.IntConsumer;
22 import java.util.function.IntFunction;
23 import java.util.function.IntPredicate;
24 import java.util.function.IntSupplier;
25 import java.util.function.LongBinaryOperator;
26 import java.util.function.LongConsumer;
27 import java.util.function.LongFunction;
28 import java.util.function.LongPredicate;
29 import java.util.function.LongSupplier;
30 import java.util.function.ObjDoubleConsumer;
31 import java.util.function.ObjIntConsumer;
32 import java.util.function.Predicate;
33 import java.util.function.Supplier;
35 import org.junit.Assert;
36 import org.junit.Test;
38 import de.xn__ho_hia.memoization.shared.DoubleBinaryFunction;
39 import de.xn__ho_hia.memoization.shared.IntBinaryFunction;
40 import de.xn__ho_hia.memoization.shared.LongBinaryFunction;
41 import de.xn__ho_hia.memoization.shared.ObjDoubleFunction;
42 import de.xn__ho_hia.memoization.shared.ObjIntFunction;
43 import de.xn__ho_hia.quality.suppression.CompilerWarnings;
45 /**
48 @SuppressWarnings({ CompilerWarnings.NLS, CompilerWarnings.STATIC_METHOD })
49 public class JCacheMemoizeCustomKeyTest {
51 /**
54 @Test
55 public void shouldMemoizeBiConsumerWithKeyFunction() {
56 // given
57 final BiConsumer<String, String> consumer = (a, b) -> System.out.println(a + b);
58 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
60 // when
61 final BiConsumer<String, String> memoize = JCacheMemoize.biConsumer(consumer, keyFunction);
63 // then
64 Assert.assertNotNull("Memoized BiConsumer is NULL", memoize);
67 /**
70 @Test
71 public void shouldMemoizeBiFunctionWithKeyBiFunction() {
72 // given
73 final BiFunction<String, String, String> function = (first, second) -> "test";
74 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
76 // when
77 final BiFunction<String, String, String> memoize = JCacheMemoize.biFunction(function, keyFunction);
79 // then
80 Assert.assertNotNull("Memoized BiFunction is NULL", memoize);
83 /**
86 @Test
87 public void shouldMemoizeBiPredicateWithKeyBiFunction() {
88 // given
89 final BiPredicate<String, String> biPredicate = (first, second) -> true;
90 final BiFunction<String, String, String> keyFunction = (first, second) -> "key";
92 // when
93 final BiPredicate<String, String> memoize = JCacheMemoize.biPredicate(biPredicate, keyFunction);
95 // then
96 Assert.assertNotNull("Memoized BiPredicate is NULL", memoize);
99 /**
102 @Test
103 public void shouldMemoizeBooleanSupplierWithKeySupplier() {
104 // given
105 final BooleanSupplier supplier = () -> true;
106 final Supplier<String> keySupplier = () -> "key";
108 // when
109 final BooleanSupplier memoize = JCacheMemoize.booleanSupplier(supplier, keySupplier);
111 // then
112 Assert.assertNotNull("Memoized BooleanSupplier is NULL", memoize);
118 @Test
119 public void shouldMemoizeConsumerWithKeyFunction() {
120 // given
121 final Consumer<String> consumer = System.out::println;
122 final Function<String, String> keyFunction = Function.identity();
124 // when
125 final Consumer<String> memoize = JCacheMemoize.consumer(consumer, keyFunction);
127 // then
128 Assert.assertNotNull("Memoized Consumer is NULL", memoize);
134 @Test
135 public void shouldMemoizeDoubleBinaryOperatorWithKeyFunction() {
136 // given
137 final DoubleBinaryOperator operator = (a, b) -> 123.456D;
138 final DoubleBinaryFunction<String> keyFunction = (a, b) -> "key";
140 // when
141 final DoubleBinaryOperator memoize = JCacheMemoize.doubleBinaryOperator(operator, keyFunction);
143 // then
144 Assert.assertNotNull("Memoized DoubleBinaryOperator is NULL", memoize);
150 @Test
151 public void shouldMemoizeDoubleConsumerWithKeyFunction() {
152 // given
153 final DoubleConsumer consumer = System.out::println;
154 final DoubleFunction<String> keyFunction = a -> "key";
156 // when
157 final DoubleConsumer memoize = JCacheMemoize.doubleConsumer(consumer, keyFunction);
159 // then
160 Assert.assertNotNull("Memoized DoubleConsumer is NULL", memoize);
166 @Test
167 public void shouldMemoizeDoubleFunction() {
168 // given
169 final DoubleFunction<String> function = a -> "test";
170 final DoubleFunction<String> keyFunction = a -> "key";
172 // when
173 final DoubleFunction<String> memoize = JCacheMemoize.doubleFunction(function, keyFunction);
175 // then
176 Assert.assertNotNull("Memoized DoubleFunction is NULL", memoize);
182 @Test
183 public void shouldMemoizeDoublePredicateWithKeyFunction() {
184 // given
185 final DoublePredicate predicate = a -> true;
186 final DoubleFunction<String> keyFunction = a -> "key";
188 // when
189 final DoublePredicate memoize = JCacheMemoize.doublePredicate(predicate, keyFunction);
191 // then
192 Assert.assertNotNull("Memoized DoublePredicate is NULL", memoize);
198 @Test
199 public void shouldMemoizeDoubleSupplierWithKeyFunction() {
200 // given
201 final DoubleSupplier supplier = () -> 123.456D;
202 final Supplier<String> keySupplier = () -> "key";
204 // when
205 final DoubleSupplier memoize = JCacheMemoize.doubleSupplier(supplier, keySupplier);
207 // then
208 Assert.assertNotNull("Memoized DoubleSupplier is NULL", memoize);
214 @Test
215 public void shouldMemoizeFunctionWithKeyFunction() {
216 // given
217 final Function<String, String> function = a -> "test";
218 final Function<String, String> keyFunction = Function.identity();
220 // when
221 final Function<String, String> memoize = JCacheMemoize.function(function, keyFunction);
223 // then
224 Assert.assertNotNull("Memoized Function is NULL", memoize);
230 @Test
231 public void shouldMemoizeIntBinaryOperatorWithKeyFunction() {
232 // given
233 final IntBinaryOperator operator = (a, b) -> 123;
234 final IntBinaryFunction<String> keyFunction = (a, b) -> "key";
236 // when
237 final IntBinaryOperator memoize = JCacheMemoize.intBinaryOperator(operator, keyFunction);
239 // then
240 Assert.assertNotNull("Memoized IntBinaryOperator is NULL", memoize);
246 @Test
247 public void shouldMemoizeIntConsumerWithKeyFunction() {
248 // given
249 final IntConsumer consumer = System.out::println;
250 final IntFunction<String> keyFunction = a -> "key";
252 // when
253 final IntConsumer memoize = JCacheMemoize.intConsumer(consumer, keyFunction);
255 // then
256 Assert.assertNotNull("Memoized IntConsumer is NULL", memoize);
262 @Test
263 public void shouldMemoizeIntFunctionWithKeyFunction() {
264 // given
265 final IntFunction<String> function = a -> "test";
266 final IntFunction<String> keyFunction = a -> "key";
268 // when
269 final IntFunction<String> memoize = JCacheMemoize.intFunction(function, keyFunction);
271 // then
272 Assert.assertNotNull("Memoized IntFunction is NULL", memoize);
278 @Test
279 public void shouldMemoizeIntPredicateWithKeyFunction() {
280 // given
281 final IntPredicate predicate = a -> true;
282 final IntFunction<String> keyFunction = a -> "key";
284 // when
285 final IntPredicate memoize = JCacheMemoize.intPredicate(predicate, keyFunction);
287 // then
288 Assert.assertNotNull("Memoized IntPredicate is NULL", memoize);
294 @Test
295 public void shouldMemoizeIntSupplierWithKeyFunction() {
296 // given
297 final IntSupplier supplier = () -> 123;
298 final Supplier<String> keySupplier = () -> "key";
300 // when
301 final IntSupplier memoize = JCacheMemoize.intSupplier(supplier, keySupplier);
303 // then
304 Assert.assertNotNull("Memoized IntSupplier is NULL", memoize);
310 @Test
311 public void shouldMemoizeLongBinaryOperatorWithKeyFunction() {
312 // given
313 final LongBinaryOperator operator = (a, b) -> 123L;
314 final LongBinaryFunction<String> keyFunction = (a, b) -> "key";
316 // when
317 final LongBinaryOperator memoize = JCacheMemoize.longBinaryOperator(operator, keyFunction);
319 // then
320 Assert.assertNotNull("Memoized LongBinaryOperator is NULL", memoize);
326 @Test
327 public void shouldMemoizeLongConsumer() {
328 // given
329 final LongConsumer consumer = System.out::println;
330 final LongFunction<String> keyFunction = a -> "key";
332 // when
333 final LongConsumer memoize = JCacheMemoize.longConsumer(consumer, keyFunction);
335 // then
336 Assert.assertNotNull("Memoized LongConsumer is NULL", memoize);
342 @Test
343 public void shouldMemoizeLongFunctionWithKeyFunction() {
344 // given
345 final LongFunction<String> function = a -> "test";
346 final LongFunction<String> keyFunction = a -> "key";
348 // when
349 final LongFunction<String> memoize = JCacheMemoize.longFunction(function, keyFunction);
351 // then
352 Assert.assertNotNull("Memoized LongFunction is NULL", memoize);
358 @Test
359 public void shouldMemoizeLongPredicateWithKeyFunction() {
360 // given
361 final LongPredicate predicate = a -> true;
362 final LongFunction<String> keyFunction = a -> "key";
364 // when
365 final LongPredicate memoize = JCacheMemoize.longPredicate(predicate, keyFunction);
367 // then
368 Assert.assertNotNull("Memoized LongPredicate is NULL", memoize);
374 @Test
375 public void shouldMemoizeLongSupplierWithKeyFunction() {
376 // given
377 final LongSupplier supplier = () -> 123L;
378 final Supplier<String> keySupplier = () -> "key";
380 // when
381 final LongSupplier memoize = JCacheMemoize.longSupplier(supplier, keySupplier);
383 // then
384 Assert.assertNotNull("Memoized LongSupplier is NULL", memoize);
390 @Test
391 public void shouldMemoizeObjDoubleConsumerWithKeyFunction() {
392 // given
393 final ObjDoubleConsumer<String> consumer = (a, b) -> System.out.println(a + b);
394 final ObjDoubleFunction<String, String> keyFunction = (a, b) -> "key";
396 // when
397 final ObjDoubleConsumer<String> memoize = JCacheMemoize.objDoubleConsumer(consumer, keyFunction);
399 // then
400 Assert.assertNotNull("Memoized ObjDoubleConsumer is NULL", memoize);
406 @Test
407 public void shouldMemoizeObjIntConsumerWithKeyFunction() {
408 // given
409 final ObjIntConsumer<String> consumer = (a, b) -> System.out.println(a + b);
410 final ObjIntFunction<String, String> keyFunction = (a, b) -> "key";
412 // when
413 final ObjIntConsumer<String> memoize = JCacheMemoize.objIntConsumer(consumer, keyFunction);
415 // then
416 Assert.assertNotNull("Memoized ObjIntConsumer is NULL", memoize);
422 @Test
423 public void shouldMemoizePredicateWithKeyFunction() {
424 // given
425 final Predicate<String> predicate = a -> true;
426 final Function<String, String> keyFunction = Function.identity();
428 // when
429 final Predicate<String> memoize = JCacheMemoize.predicate(predicate, keyFunction);
431 // then
432 Assert.assertNotNull("Memoized Predicate is NULL", memoize);
438 @Test
439 public void shouldMemoizeSupplierWithKeySupplier() {
440 // given
441 final Supplier<String> supplier = () -> "test";
442 final Supplier<String> keySupplier = () -> "key";
444 // when
445 final Supplier<String> memoize = JCacheMemoize.supplier(supplier, keySupplier);
447 // then
448 Assert.assertNotNull("Memoized Supplier is NULL", memoize);