View Javadoc

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