computation = mock(CachedObject.Computation.class);
when(computation.getObject(anyInt())).thenAnswer(new Answer<Integer>() {
public Integer answer(
- org.mockito.invocation.InvocationOnMock invocation)
+ org.mockito.invocation.InvocationOnMock aInvocation)
throws Throwable {
ncomputations++;
- return compute((Integer) invocation.getArguments()[0]);
+ return compute((Integer) aInvocation.getArguments()[0]);
}
});
ncomputations = 0;
assertEquals(101, getComputationCount());
}
- public void testPreviousValueWhileComputationBeingDone() throws Exception {
- final CachedObject<Integer,Integer> cached = createCached(new ZeroCache<Integer, Integer>());
+ public void testPreviousValueWhileComputationBeingDone() throws Exception {
+ final CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
reset(computation);
when(computation.getObject(anyInt())).thenReturn(1).thenAnswer(
new Answer<Integer>() {
TimingUtils.sleep(1000);
return 2;
}
- }
- ).thenReturn(3);
-
+ }).thenReturn(3);
+
assertEquals(1, cached.get().intValue());
Thread recompute = new Thread(new Runnable() {
-
+
@Override
public void run() {
- // will sleep for 1 second.
+ // will sleep for 1 second.
assertEquals(2, cached.get().intValue());
}
});
recompute.start();
- // Now asking the value should not recompute but should return the old value.
+ // Now asking the value should not recompute but should return the old
+ // value.
TimingUtils.sleep(500);
assertEquals(1, cached.get().intValue());
recompute.join();
}
-
- public void testNullValueWhenComputationReturnsNull() throws Exception {
- final CachedObject<Integer,Integer> cached = createCached(new ZeroCache<Integer, Integer>());
+
+ public void testNullValueWhenComputationReturnsNull() throws Exception {
+ final CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
reset(computation);
- when(computation.getObject(anyInt())).thenReturn(1).thenReturn(null).thenReturn(2);
-
+ when(computation.getObject(anyInt())).thenReturn(1).thenReturn(null)
+ .thenReturn(2);
+
assertEquals(1, cached.get().intValue());
assertNull(cached.get());
assertEquals(2, cached.get().intValue());
}
-
- public void testPreviousValueWhenComputationThrowsException() throws Exception {
- final CachedObject<Integer,Integer> cached = createCached(new ZeroCache<Integer, Integer>());
+
+ public void testPreviousValueWhenComputationThrowsException()
+ throws Exception {
+ final CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
reset(computation);
- when(computation.getObject(anyInt())).thenReturn(1).thenThrow(new RuntimeException()).thenReturn(2);
-
+ when(computation.getObject(anyInt())).thenReturn(1).thenThrow(
+ new RuntimeException()).thenReturn(2);
+
assertEquals(1, cached.get().intValue());
assertEquals(1, cached.get().intValue());
assertEquals(2, cached.get().intValue());