How to use method of org.jmock.example.timedcache.TimedCacheTests class

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

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

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

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)