View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }