1 package com.atlassian.cache;
2
3 import org.junit.Test;
4
5 import static org.hamcrest.core.IsEqual.equalTo;
6 import static org.hamcrest.core.IsInstanceOf.instanceOf;
7 import static org.junit.Assert.assertNotSame;
8 import static org.junit.Assert.assertThat;
9 import static org.junit.Assert.fail;
10
11
12
13
14
15
16 public abstract class AbstractLazyReferenceTest extends AbstractCacheTest
17 {
18 @Test
19 public void testGetName() throws Exception
20 {
21 CachedReference<Long> ref = makeReference();
22
23 assertThat(((ManagedCache) ref).getName(), equalTo("mycache"));
24 }
25
26 @Test
27 public void testFactoryGeneratedName() throws Exception
28 {
29 CachedReference<Long> ref = factory.getCachedReference(Object.class, "mycache", nullSupplier());
30
31 assertThat(((ManagedCache) ref).getName(), equalTo("java.lang.Object.mycache"));
32 }
33
34 @Test
35 public void testGet() throws Exception
36 {
37 CachedReference<Long> ref = makeReference();
38
39 assertThat(ref.get(), equalTo(100L));
40 assertThat(ref.get(), equalTo(100L));
41 assertThat(ref.get(), equalTo(100L));
42 }
43
44
45 @Test
46 public void testReset() throws Exception
47 {
48 CachedReference<Long> ref = makeReference();
49
50 assertThat(ref.get(), equalTo(100L));
51
52 ref.reset();
53
54
55 assertThat(ref.get(), equalTo(101L));
56 }
57
58 @Test
59 public void testRemoveTwice() throws Exception
60 {
61 CachedReference<Long> ref = makeReference();
62
63 assertThat(ref.get(), equalTo(100L));
64 assertThat(ref.get(), equalTo(100L));
65
66 ref.reset();
67 ref.reset();
68
69
70 assertThat(ref.get(), equalTo(102L));
71 assertThat(ref.get(), equalTo(102L));
72
73 ref.reset();
74
75
76 assertThat(ref.get(), equalTo(103L));
77 assertThat(ref.get(), equalTo(103L));
78 }
79
80 @Test
81 public void testClear() throws Exception
82 {
83 CachedReference<Long> ref = makeReference();
84
85 assertThat(ref.get(), equalTo(100L));
86
87 ((ManagedCache) ref).clear();
88
89
90 assertThat(ref.get(), equalTo(101L));
91 }
92
93 @Test(expected = CacheException.class)
94 public void testNullHandling() throws Exception
95 {
96 makeNullReference().get();
97 }
98
99 @Test
100 public void testExceptionHandling() throws Exception
101 {
102 final CachedReference<Long> ref = makeExceptionalReference();
103 try
104 {
105 ref.get();
106 fail("Should throw CacheException when the loader throws an IllegalArgumentException");
107 }
108 catch (CacheException ex)
109 {
110 assertThat("This exception should wrap the original Exception", ex.getCause(), instanceOf(IllegalArgumentException.class));
111 }
112 assertThat("The exception should not be sticky", ref.get(), equalTo(42L));
113 }
114
115 @Test
116 public void testNewInstanceForEveryGet()
117 {
118 CachedReference<Long> ref1 = makeReference();
119 CachedReference<Long> ref2 = makeReference();
120
121 assertNotSame("The cache manager should not return the same cache twice", ref1, ref2);
122
123 }
124
125
126
127 protected CachedReference<Long> makeReference()
128 {
129
130 Supplier<Long> supplier = new Supplier<Long>()
131 {
132 private long l = 100;
133 @Override
134 public Long get()
135 {
136 return l++;
137 }
138 };
139 return factory.getCachedReference("mycache", supplier, settingsBuilder().build());
140 }
141
142 private CachedReference<Long> makeNullReference()
143 {
144 return factory.getCachedReference("mycache", nullSupplier(), settingsBuilder().build());
145 }
146
147 private CachedReference<Long> makeExceptionalReference()
148 {
149 Supplier<Long> supplier = new Supplier<Long>()
150 {
151 private boolean called = false;
152
153 @Override
154 public Long get()
155 {
156 if (called)
157 {
158 return 42L;
159 }
160 called = true;
161 throw new IllegalArgumentException();
162 }
163 };
164 return factory.getCachedReference("mycache", supplier, settingsBuilder().build());
165 }
166
167 protected CacheSettingsBuilder settingsBuilder()
168 {
169 return new CacheSettingsBuilder();
170 }
171
172
173 private static Supplier<Long> nullSupplier()
174 {
175 return new Supplier<Long>()
176 {
177 @Override
178 public Long get()
179 {
180 return null;
181 }
182 };
183 }
184 }