1 package com.atlassian.cache.vcache;
2
3 import java.util.Collection;
4
5 import com.atlassian.cache.Cache;
6 import com.atlassian.cache.CacheSettings;
7 import com.atlassian.cache.CacheSettingsBuilder;
8 import com.atlassian.cache.CachedReference;
9 import com.atlassian.cache.ManagedCache;
10
11 import org.junit.Test;
12
13 import static org.hamcrest.MatcherAssert.assertThat;
14 import static org.hamcrest.Matchers.emptyIterable;
15 import static org.hamcrest.Matchers.is;
16 import static org.hamcrest.Matchers.notNullValue;
17 import static org.hamcrest.Matchers.nullValue;
18
19 public class VCacheCacheManagerTest
20 {
21 @Test
22 public void testFlushCaches()
23 {
24 VCacheCacheManager mcm = TestUtils.createCacheFactory();
25 Cache<String, String> lcache = mcm.getCache(
26 "test-local-cache", null, new CacheSettingsBuilder().local().build());
27 lcache.put("key", "value");
28
29 Cache<String, String> rcache = mcm.getCache(
30 "test-remote-cache", null, new CacheSettingsBuilder().remote().build());
31 rcache.put("key", "value");
32
33 assertThat(rcache.containsKey("key"), is(true));
34
35 mcm.flushCaches();
36 assertThat(lcache.getKeys().size(), is(0));
37 assertThat(rcache.containsKey("key"), is(false));
38 }
39
40 @Test
41 public void testManagedCacheExists()
42 {
43 VCacheCacheManager mcm = TestUtils.createCacheFactory();
44 Cache<String, String> c = mcm.getCache("test-cache");
45
46 ManagedCache mc = mcm.getManagedCache("test-cache");
47 assertThat(mc, notNullValue());
48 assertThat(mc.getName(), is("test-cache"));
49 }
50
51 @Test
52 public void testManagedCacheMissing()
53 {
54 VCacheCacheManager mcm = TestUtils.createCacheFactory();
55 Cache<String, String> c = mcm.getCache("test-cache");
56
57 ManagedCache mc = mcm.getManagedCache("unknown-cache");
58 assertThat(mc, nullValue());
59 }
60
61 @Test
62 public void testManagedCachesEmpty()
63 {
64 VCacheCacheManager mcm = TestUtils.createCacheFactory();
65
66 Collection<ManagedCache> mcs = mcm.getManagedCaches();
67 assertThat(mcs, notNullValue());
68 assertThat(mcs.size(), is(0));
69 assertThat(mcs, emptyIterable());
70 }
71
72 @Test
73 public void testManagedCachesExists()
74 {
75 VCacheCacheManager mcm = TestUtils.createCacheFactory();
76 Cache<String, String> c = mcm.getCache("test-cache");
77
78 Collection<ManagedCache> mcs = mcm.getManagedCaches();
79 assertThat(mcs, notNullValue());
80 assertThat(mcs.size(), is(1));
81 assertThat(mcs.iterator().next().getName(), is("test-cache"));
82 }
83
84 @Test
85 public void testGetCaches()
86 {
87 VCacheCacheManager mcm = TestUtils.createCacheFactory();
88 Cache<String, String> c = mcm.getCache("test-cache");
89 CacheSettings localSettings = new CacheSettingsBuilder().local().build();
90 CachedReference<String> cref = mcm.getCachedReference(
91 Object.class, "sob", () -> "Never called", localSettings);
92
93 Collection<Cache<?, ?>> caches = mcm.getCaches();
94 assertThat(caches, notNullValue());
95 assertThat(caches.size(), is(1));
96 assertThat(caches.iterator().next().getName(), is("test-cache"));
97 }
98 }