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    *      http://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.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 }