How to use TimedCache class of org.jmock.example.timedcache package

Best Jmock-library code snippet using org.jmock.example.timedcache.TimedCache

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*  Copyright (c) 2000-2004 jMock.org
2 */
3package org.jmock.example.timedcache;
4
5import java.util.Calendar;
6import java.util.Date;
7
8import junit.framework.TestCase;
9
10import org.jmock.Expectations;
11import org.jmock.Mockery;
12import org.jmock.integration.junit3.JUnit3ErrorTranslator;
13
14
15public class TimedCacheTests extends TestCase {
16    final private Object KEY = "key";
17    final private Object VALUE = "value";
18    final private Object NEW_VALUE = "newValue";
19
20    private Mockery context = new Mockery() {{
21        setExpectationErrorTranslator(JUnit3ErrorTranslator.INSTANCE);
22    }};
23    
24    private Clock clock = context.mock(Clock.class);
25    private ObjectLoader loader = context.mock(ObjectLoader.class, "loader");
26    private ReloadPolicy reloadPolicy = context.mock(ReloadPolicy.class);
27
28    private TimedCache cache = new TimedCache(loader, clock, reloadPolicy);
29
30    private Date loadTime = time(1);
31    private Date fetchTime = time(2);
32
33    public void testLoadsObjectThatIsNotCached() {
34        final Object VALUE1 = "value1";
35        final Object VALUE2 = "value2";
36
37        context.checking(new Expectations() {{
38            allowing (clock).time(); will(returnValue(loadTime));
39
40            oneOf (loader).load("key1"); will(returnValue(VALUE1));
41            oneOf (loader).load("key2"); will(returnValue(VALUE2));
42        }});
43
44        Object actualValue1 = cache.lookup("key1");
45        Object actualValue2 = cache.lookup("key2");
46        
47        context.assertIsSatisfied();
48        assertEquals("lookup with key1", VALUE1, actualValue1);
49        assertEquals("lookup with key2", VALUE2, actualValue2);
50    }
51
52    public void testReturnsCachedObjectWithinTimeout() {
53        context.checking(new Expectations() {{
54            oneOf (clock).time(); will(returnValue(loadTime));
55            oneOf (clock).time(); will(returnValue(fetchTime));
56            
57            allowing (reloadPolicy).shouldReload(loadTime, fetchTime); will(returnValue(false));
58            
59            oneOf (loader).load(KEY); will(returnValue(VALUE));
60        }});
61        
62        Object actualValueFromFirstLookup = cache.lookup(KEY);
63        Object actualValueFromSecondLookup = cache.lookup(KEY);
64        
65        context.assertIsSatisfied();
66        assertSame("should be loaded object", VALUE, actualValueFromFirstLookup);
67        assertSame("should be cached object", VALUE, actualValueFromSecondLookup);
68    }
69
70    public void testReloadsCachedObjectAfterTimeout() {
71        context.checking(new Expectations() {{
72            allowing (reloadPolicy).shouldReload(loadTime, fetchTime); will(returnValue(true));
73            
74            oneOf (clock).time(); will(returnValue(loadTime));
75            oneOf (loader).load(KEY); will(returnValue(VALUE));
76            
77            oneOf (clock).time(); will(returnValue(fetchTime));
78            oneOf (loader).load(KEY); will(returnValue(NEW_VALUE));
79        }});
80        
81        Object actualValueFromFirstLookup = cache.lookup(KEY);
82        Object actualValueFromSecondLookup = cache.lookup(KEY);
83
84        context.assertIsSatisfied();
85        
86        assertSame("should be loaded object", VALUE, actualValueFromFirstLookup);
87        assertSame("should be reloaded object", NEW_VALUE, actualValueFromSecondLookup);
88    }
89
90    private Date time(int i) {
91        Calendar calendar = Calendar.getInstance();
92        calendar.clear();
93        calendar.set(Calendar.DAY_OF_YEAR, i);
94        return calendar.getTime();
95    }
96}
97
Full Screen
copy
1package org.jmock.example.timedcache;
2
3import java.util.Date;
4import java.util.HashMap;
5import java.util.Map;
6
7public class TimedCache {
8    private ObjectLoader loader;
9    private Clock clock;
10    private ReloadPolicy reloadPolicy;
11    private Map<Object,TimestampedValue> cache = new HashMap<Object,TimestampedValue>();
12    
13    public TimedCache(ObjectLoader loader, Clock clock, ReloadPolicy reloadPolicy) {
14        this.loader = loader;
15        this.clock = clock;
16        this.reloadPolicy = reloadPolicy;
17    }
18
19    public Object lookup(Object key) {
20        Date fetchTime = clock.time();
21        TimestampedValue cached = cache.get(key);
22        
23        if (cached == null || reloadPolicy.shouldReload(cached.loadTime, fetchTime)) {
24            Object value = loader.load(key);
25            cached = new TimestampedValue(value, fetchTime);
26            cache.put(key, cached);
27        }
28        
29        return cached.value;
30    }
31    
32    private class TimestampedValue {
33        public final Object value;
34        public final Date loadTime;
35        
36        public TimestampedValue(Object value, Date timestamp) {
37            this.value = value;
38            this.loadTime = timestamp;
39        }
40    }
41}
42
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on TimedCache

    No relevant questions found for this class 😞

Most used methods in TimedCache

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)