2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.cache;
18 import static org.mockito.Mockito.*;
20 import java.util.ArrayList;
21 import java.util.List;
23 import junit.framework.TestCase;
25 import org.mockito.invocation.InvocationOnMock;
26 import org.mockito.stubbing.Answer;
27 import org.wamblee.cache.ComputedValue.Computation;
29 public class ComputedValueTest extends TestCase {
31 private Computation<Integer> computation;
32 private ComputedValue<Integer> guard;
36 protected void setUp() throws Exception {
37 computation = mock(Computation.class);
40 public void testComputeOutOfDate() {
44 private void initGuard() {
45 guard = new ComputedValue<Integer>(this, computation);
47 assertNull(guard.getCached());
49 when(computation.isOutOfDate()).thenReturn(true);
50 when(computation.compute()).thenReturn(10);
52 int value = guard.get();
53 assertEquals(10, value);
54 verify(computation).compute();
58 public void testGetCached() {
60 assertEquals(10, (int)guard.getCached());
61 verifyNoMoreInteractions(computation);
64 public void testNoComputationWhenNotOutOfDate() {
66 when(computation.isOutOfDate()).thenReturn(false);
67 assertEquals(10, (int) guard.get());
68 verify(computation, never()).compute();
71 public void testOnlyOneConcurrentComputation() throws Exception {
73 final int computeTime = 500;
74 when(computation.isOutOfDate()).thenReturn(true);
75 when(computation.compute()).thenAnswer(new Answer<Integer>() {
77 public Integer answer(InvocationOnMock aInvocation)
79 Thread.sleep(computeTime);
84 final List<Integer> results = new ArrayList<Integer>();
86 Runnable task = new Runnable() {
89 int res = guard.get();
94 // concurrent computation in two threads.
95 Thread t1 = new Thread(task);
96 Thread t2 = new Thread(task);
98 Thread.sleep(computeTime / 2);
99 // second task will return old value 10 because first one is still busy.
103 assertEquals(2, results.size());
104 assertEquals(10, (int) results.get(0));
105 assertEquals(100, (int) results.get(1));
106 verify(computation, times(2)).isOutOfDate();
109 public void testExceptionWhileComputing() {
111 when(computation.isOutOfDate()).thenReturn(true);
112 when(computation.compute()).thenThrow(new RuntimeException("xx"));
116 } catch (RuntimeException e) {
117 assertEquals("xx", e.getMessage());
121 public void testExceptionWhileCheckingOutOfDate() {
123 when(computation.isOutOfDate()).thenThrow(new RuntimeException("xx"));
127 } catch (RuntimeException e) {
128 assertEquals("xx", e.getMessage());