1 package net.sf.ehcache.terracotta;
2
3 import net.sf.ehcache.Cache;
4 import net.sf.ehcache.CacheException;
5 import net.sf.ehcache.CacheManager;
6 import net.sf.ehcache.Ehcache;
7 import net.sf.ehcache.Status;
8 import net.sf.ehcache.config.CacheConfiguration;
9 import net.sf.ehcache.config.TerracottaConfiguration;
10 import org.junit.Test;
11 import org.mockito.Matchers;
12
13 import java.io.IOException;
14 import java.util.Arrays;
15 import java.util.HashSet;
16 import java.util.List;
17
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.never;
20 import static org.mockito.Mockito.times;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23
24 /***
25 * @author Alex Snaps
26 */
27 public class TerracottaBootstrapCacheLoaderTest {
28
29 private static final String DIRECTORY = "dumps";
30 private static final String MOCKED_CACHE_NAME = "MockedCache";
31
32 private final TerracottaBootstrapCacheLoader cacheLoader = new TerracottaBootstrapCacheLoader(false, DIRECTORY, false);
33
34 @Test
35 public void testComplainsOnNonTcClusteredCacheButDoesNotFail() {
36 cacheLoader.load(new Cache(new CacheConfiguration("test", 0)));
37 }
38
39 @Test(expected = CacheException.class)
40 public void testFailsWhenCacheIsNotAlive() {
41 cacheLoader.load(new Cache(new CacheConfiguration("test", 0).terracotta(new TerracottaConfiguration())));
42 }
43
44 @Test
45 public void testBootstrapsWhenNoSnapshotPresent() {
46 final Ehcache cache = mockCacheToBootStrap();
47 cacheLoader.load(cache);
48 verify(cache, never()).get(Matchers.anyObject());
49 }
50
51 @Test
52 public void testDisposesProperly() {
53 final Ehcache cache = mockCacheToBootStrap();
54 cacheLoader.dispose();
55 cacheLoader.load(cache);
56 cacheLoader.dispose();
57 }
58
59 @Test
60 public void testBootstrapsWhenSnapshotPresent() throws IOException {
61 RotatingSnapshotFile file = new RotatingSnapshotFile(DIRECTORY, MOCKED_CACHE_NAME);
62
63
64 final List<Integer> localKeys = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9);
65 file.writeAll(localKeys);
66 final Ehcache cache = mockCacheToBootStrap();
67 cacheLoader.load(cache);
68 verify(cache, times(new HashSet<Integer>(localKeys).size())).get(Matchers.anyObject());
69 for (Integer localKey : localKeys) {
70 verify(cache, times(1)).get((Object) localKey);
71 }
72 file.currentSnapshotFile().delete();
73 }
74
75 private Ehcache mockCacheToBootStrap() {
76 final Ehcache cache = mock(Ehcache.class);
77 CacheConfiguration cacheConfiguration = mock(CacheConfiguration.class);
78 when(cacheConfiguration.isTerracottaClustered()).thenReturn(true);
79 when(cache.getName()).thenReturn(MOCKED_CACHE_NAME);
80 when(cache.getCacheConfiguration()).thenReturn(cacheConfiguration);
81 when(cache.getStatus()).thenReturn(Status.STATUS_ALIVE);
82 CacheManager cacheManager = mock(CacheManager.class);
83 when(cache.getCacheManager()).thenReturn(cacheManager);
84 when(cache.get(Matchers.<Object>anyObject())).thenReturn(null);
85 return cache;
86 }
87
88 }