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