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