Created
September 5, 2012 07:40
-
-
Save nekman/3632732 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package se.labs.cache.tests; | |
| import static org.hamcrest.core.Is.is; | |
| import static org.junit.Assert.assertThat; | |
| import static org.mockito.Mockito.mock; | |
| import static org.mockito.Mockito.when; | |
| import org.junit.Test; | |
| import se.labs.cache.ItemCacheImpl; | |
| import se.labs.cache.ItemCache; | |
| import se.labs.cache.ItemCacheLoader; | |
| public class ItemCacheTest { | |
| @Test | |
| public void shouldReloadWhenExpired() throws Exception { | |
| @SuppressWarnings("unchecked") | |
| ItemCacheLoader<String> loader = mock(ItemCacheLoader.class); | |
| when(loader.reload()).thenReturn("1"); | |
| ItemCache<String> stringCache = new ItemCacheImpl<String>(loader, 1); | |
| assertThat(stringCache.getItem(), is("1")); | |
| // Next time the cache reloads, return "2". | |
| when(loader.reload()).thenReturn("2"); | |
| // Sleep two seconds, to make sure that the cache gets reloaded. | |
| Thread.sleep(2000); | |
| assertThat(stringCache.getItem(), is("2")); | |
| } | |
| @Test | |
| public void shouldReloadWhenInvalidated() throws Exception { | |
| @SuppressWarnings("unchecked") | |
| ItemCacheLoader<Integer> loader = mock(ItemCacheLoader.class); | |
| when(loader.reload()).thenReturn(1); | |
| ItemCache<Integer> intCache = new ItemCacheImpl<Integer>(loader); | |
| assertThat(intCache.getItem(), is(1)); | |
| // Next time the cache reloads, return 2. | |
| when(loader.reload()).thenReturn(2); | |
| // Call invalidate, to make sure that the cache gets reloaded. | |
| intCache.invalidate(); | |
| assertThat(intCache.getItem(), is(2)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment