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