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