1 package com.atlassian.cache.ehcache;
2
3 import com.atlassian.cache.CacheException;
4 import com.atlassian.cache.CacheSettingsBuilder;
5 import com.atlassian.cache.ehcache.wrapper.MockWrappedValue;
6 import com.atlassian.cache.ehcache.wrapper.WrapperTestUtils;
7 import com.atlassian.cache.impl.ReferenceKey;
8 import net.sf.ehcache.Cache;
9 import net.sf.ehcache.CacheManager;
10 import net.sf.ehcache.Element;
11 import net.sf.ehcache.config.CacheConfiguration;
12 import net.sf.ehcache.config.Configuration;
13 import net.sf.ehcache.config.MemoryUnit;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 import static org.junit.Assert.assertSame;
19
20 public class DelegatingCachedReferenceWrapperTest {
21 private DelegatingCachedReference<Object> testedDecorator;
22 private Cache testedUnderlyingCache;
23
24 private CacheManager cacheManager;
25
26 @Before
27 public void init() {
28 cacheManager = new CacheManager(new Configuration().name(DelegatingCacheWrapperTest.class.getName()));
29
30 testedUnderlyingCache = new Cache(new CacheConfiguration().name("tested").maxBytesLocalHeap(10, MemoryUnit.MEGABYTES));
31 testedDecorator = DelegatingCachedReference.create(testedUnderlyingCache, new CacheSettingsBuilder().local().build(), WrapperTestUtils.getValueProcessor());
32
33 cacheManager.removeAllCaches();
34 cacheManager.addCache(testedUnderlyingCache);
35 }
36
37 @After
38 public void destroy() {
39 cacheManager.shutdown();
40 }
41
42 @Test
43 public void testGet() {
44 Object key = ReferenceKey.KEY;
45 Object value = new Object();
46
47 testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
48
49 Object retrievedValue = testedDecorator.get();
50 assertSame(value, retrievedValue);
51 }
52
53 @Test(expected = CacheException.class)
54 public void testClear() {
55 Object key = ReferenceKey.KEY;
56 Object value = new Object();
57
58 testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
59
60 assertSame(value, testedDecorator.get());
61 testedDecorator.clear();
62 testedDecorator.get();
63 }
64
65 @Test(expected = CacheException.class)
66 public void testReset() {
67 Object key = ReferenceKey.KEY;
68 Object value = new Object();
69
70 testedUnderlyingCache.put(new Element(new MockWrappedValue(key), new MockWrappedValue(value)));
71
72 assertSame(value, testedDecorator.get());
73 testedDecorator.reset();
74 testedDecorator.get();
75 }
76
77
78 }