1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3.concurrent;
18
19 import static org.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import java.util.function.Function;
26
27 import org.apache.commons.lang3.AbstractLangTest;
28 import org.easymock.EasyMock;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31
32 class MemoizerFunctionTest extends AbstractLangTest {
33
34 private Function<Integer, Integer> function;
35
36 @BeforeEach
37 public void setUpComputableMock() {
38 function = EasyMock.mock(Function.class);
39 }
40
41 @Test
42 void testDefaultBehaviourNotToRecalculateExecutionExceptions() throws Exception {
43 final Integer input = 1;
44 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function);
45 final IllegalArgumentException interruptedException = new IllegalArgumentException();
46 expect(function.apply(input)).andThrow(interruptedException);
47 replay(function);
48
49 assertThrows(Throwable.class, () -> memoizer.compute(input));
50 assertIllegalArgumentException(() -> memoizer.compute(input));
51 }
52
53 @Test
54 void testDoesNotRecalculateWhenSetToFalse() throws Exception {
55 final Integer input = 1;
56 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function, false);
57 final IllegalArgumentException interruptedException = new IllegalArgumentException();
58 expect(function.apply(input)).andThrow(interruptedException);
59 replay(function);
60
61 assertThrows(Throwable.class, () -> memoizer.compute(input));
62 assertIllegalArgumentException(() -> memoizer.compute(input));
63 }
64
65 @Test
66 void testDoesRecalculateWhenSetToTrue() throws Exception {
67 final Integer input = 1;
68 final Integer answer = 3;
69 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function, true);
70 final IllegalArgumentException interruptedException = new IllegalArgumentException();
71 expect(function.apply(input)).andThrow(interruptedException).andReturn(answer);
72 replay(function);
73
74 assertThrows(Throwable.class, () -> memoizer.compute(input));
75 assertEquals(answer, memoizer.compute(input));
76 }
77
78 @Test
79 void testOnlyCallComputableOnceIfDoesNotThrowException() throws Exception {
80 final Integer input = 1;
81 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function);
82 expect(function.apply(input)).andReturn(input);
83 replay(function);
84
85 assertEquals(input, memoizer.compute(input), "Should call computable first time");
86 assertEquals(input, memoizer.compute(input), "Should not call the computable the second time");
87 }
88
89 @Test
90 void testWhenComputableThrowsError() throws Exception {
91 final Integer input = 1;
92 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function);
93 final Error error = new Error();
94 expect(function.apply(input)).andThrow(error);
95 replay(function);
96
97 assertThrows(Error.class, () -> memoizer.compute(input));
98 }
99
100 @Test
101 void testWhenComputableThrowsRuntimeException() throws Exception {
102 final Integer input = 1;
103 final Memoizer<Integer, Integer> memoizer = new Memoizer<>(function);
104 final RuntimeException runtimeException = new RuntimeException("Some runtime exception");
105 expect(function.apply(input)).andThrow(runtimeException);
106 replay(function);
107
108 assertThrows(RuntimeException.class, () -> memoizer.compute(input));
109 }
110 }