1 3 package test.jmock.examples.timedcache; 4 5 import org.jmock.Mock; 6 import org.jmock.MockObjectTestCase; 7 import org.jmock.core.InvocationMatcher; 8 import org.jmock.core.Stub; 9 import org.jmock.examples.timedcache.*; 10 11 12 public class TimedCacheTest extends MockObjectTestCase 13 { 14 15 final private Object KEY = newDummy("key"); 16 final private Object VALUE = newDummy("value"); 17 final private Object NEW_VALUE = newDummy("newValue"); 18 19 private Mock mockClock = mock(Clock.class); 20 private Mock mockLoader = mock(ObjectLoader.class); 21 private Mock mockReloadPolicy = mock(ReloadPolicy.class); 22 private TimedCache cache = new TimedCache((ObjectLoader)mockLoader.proxy(), 23 (Clock)mockClock.proxy(), 24 (ReloadPolicy)mockReloadPolicy.proxy()); 25 private Timestamp loadTime = (Timestamp)newDummy(Timestamp.class, "loadTime"); 26 private Timestamp fetchTime = (Timestamp)newDummy(Timestamp.class, "fetchTime"); 27 private Timestamp reloadTime = (Timestamp)newDummy(Timestamp.class, "reloadTime"); 28 29 public void testLoadsObjectThatIsNotCached() { 30 mockLoader.expects(once()).method("load").with(eq(KEY)) 31 .will(returnValue(VALUE)); 32 mockLoader.expects(once()).method("load").with(eq("key2")) 33 .will(returnValue("value2")); 34 mockClock.expects(atLeastOnce()).method("getCurrentTime").withNoArguments() 35 .will(returnValue(loadTime)); 36 37 assertSame("first object", VALUE, cache.lookup(KEY)); 38 assertSame("second object", "value2", cache.lookup("key2")); 39 } 40 41 public void xtestCachedObjectsAreNotReloaded() { 42 mockLoader.expects(once()).method("load").with(eq(KEY)) 43 .will(returnValue(VALUE)); 44 45 assertSame("loaded object", VALUE, cache.lookup(KEY)); 46 assertSame("cached object", VALUE, cache.lookup(KEY)); 47 } 48 49 public void testReturnsCachedObjectWithinTimeout() { 50 mockLoader.expects(once()).method("load").with(eq(KEY)) 51 .will(returnValue(VALUE)); 52 53 mockClock.expects(atLeastOnce()).method("getCurrentTime").withNoArguments() 54 .after(mockLoader, "load") 55 .will(returnValues(loadTime, fetchTime)); 56 57 mockReloadPolicy.expects(atLeastOnce()).method("shouldReload").with(eq(loadTime), eq(fetchTime)) 58 .will(returnValue(false)); 59 60 assertSame("should be loaded object", VALUE, cache.lookup(KEY)); 61 assertSame("should be cached object", VALUE, cache.lookup(KEY)); 62 } 63 64 public void testReloadsCachedObjectAfterTimeout() { 65 mockClock.expects(times(3)).method("getCurrentTime").withNoArguments() 66 .will(returnValues(loadTime, fetchTime, reloadTime)); 67 68 mockLoader.expects(times(2)) 69 .method("load").with(eq(KEY)) 70 .will(returnValues(VALUE, NEW_VALUE)); 71 72 mockReloadPolicy.expects(atLeastOnce()) 73 .method("shouldReload").with(eq(loadTime), eq(fetchTime)) 74 .will(returnValue(true)); 75 76 77 assertSame("should be loaded object", VALUE, cache.lookup(KEY)); 78 assertSame("should be reloaded object", NEW_VALUE, cache.lookup(KEY)); 79 } 80 81 private Stub returnValues( Object value1, Object value2 ) { 82 return new StubsSequence(StubsSequence.asList(returnValue(value1), returnValue(value2))); 83 } 84 85 private Stub returnValues( Object value1, Object value2, Object value3 ) { 86 return new StubsSequence(StubsSequence.asList(returnValue(value1), returnValue(value2), returnValue(value3))); 87 } 88 89 private InvocationMatcher times( int expectedTimes ) { 90 return new InvokeCountMatcher(expectedTimes); 91 } 92 } 93 | Popular Tags |