1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.cache.tests

File AbstractEvictionGenericTestCache.java

 

Code metrics

14
87
6
1
242
137
13
0.15
14.5
6
2.17

Classes

Class Line # Actions
AbstractEvictionGenericTestCache 39 87 0% 13 12
0.8878504688.8%
 

Contributing tests

This file is covered by 4 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.cache.tests;
21   
22    import org.junit.Assert;
23    import org.junit.Test;
24    import org.xwiki.cache.Cache;
25    import org.xwiki.cache.CacheFactory;
26    import org.xwiki.cache.config.CacheConfiguration;
27    import org.xwiki.cache.config.LRUCacheConfiguration;
28    import org.xwiki.cache.eviction.EntryEvictionConfiguration;
29    import org.xwiki.cache.eviction.LRUEvictionConfiguration;
30    import org.xwiki.cache.tests.CacheEntryListenerTest.EventType;
31   
32    /**
33    * Base class for testing cache component implementation. Also test eviction.
34    * <p>
35    * Implementations with asynch eviction system should write there own tests.
36    *
37    * @version $Id: fc993231f4dbb12ce89b6a44042e944788a51a29 $
38    */
 
39    public abstract class AbstractEvictionGenericTestCache extends AbstractGenericTestCache
40    {
41    /**
42    * Indicate if the cache implementation send event for evicted entries.
43    */
44    private boolean supportEvictionEvent;
45   
46    /**
47    * @param roleHint the role hint of the cache component implementation to test.
48    * @param supportEvictionEvent indicate of the cache implementation send eviction related events
49    */
 
50  11 toggle protected AbstractEvictionGenericTestCache(String roleHint, boolean supportEvictionEvent)
51    {
52  11 super(roleHint);
53   
54  11 this.supportEvictionEvent = supportEvictionEvent;
55    }
56   
 
57  0 toggle protected void customizeEviction(EntryEvictionConfiguration eviction)
58    {
59   
60    }
61   
62    // ///////////////////////////////////////////////////////::
63    // Tests
64   
65    /**
66    * Validate the maximum cache entries constraint.
67    *
68    * @throws Exception error
69    */
 
70  1 toggle @Test
71    public void testCreateAndDestroyCacheLRUMaxEntries() throws Exception
72    {
73  1 CacheFactory factory = getCacheFactory();
74   
75  1 CacheConfiguration conf = new CacheConfiguration();
76  1 LRUEvictionConfiguration lec = new LRUEvictionConfiguration();
77  1 lec.setMaxEntries(1);
78  1 customizeEviction(lec);
79  1 conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec);
80   
81  1 Cache<Object> cache = factory.newCache(conf);
82   
83  1 Assert.assertNotNull(cache);
84   
85  1 CacheEntryListenerTest eventListener;
86  1 if (this.supportEvictionEvent) {
87  1 eventListener = new CacheEntryListenerTest();
88  1 cache.addCacheEntryListener(eventListener);
89    } else {
90  0 eventListener = null;
91    }
92   
93  1 cache.set(KEY, VALUE);
94   
95  1 Assert.assertEquals(VALUE, cache.get(KEY));
96   
97  1 cache.set(KEY2, VALUE2);
98   
99  1 if (eventListener != null) {
100  1 Assert.assertTrue("No value has been evicted from the cache",
101    eventListener.waitForEntryEvent(EventType.REMOVE));
102  1 Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue());
103    }
104   
105  1 Assert.assertNull(cache.get(KEY));
106  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
107   
108  1 cache.dispose();
109    }
110   
111    /**
112    * Validate the maximum time to live constraint.
113    *
114    * @throws Exception error
115    */
 
116  1 toggle @Test
117    public void testCreateAndDestroyCacheLRUMAxIdle() throws Exception
118    {
119  1 CacheFactory factory = getCacheFactory();
120   
121  1 CacheConfiguration conf = new CacheConfiguration();
122  1 LRUEvictionConfiguration lec = new LRUEvictionConfiguration();
123  1 lec.setMaxIdle(1);
124  1 customizeEviction(lec);
125  1 conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec);
126   
127  1 Cache<Object> cache = factory.newCache(conf);
128   
129  1 Assert.assertNotNull(cache);
130   
131  1 CacheEntryListenerTest eventListener;
132  1 if (this.supportEvictionEvent) {
133  1 eventListener = new CacheEntryListenerTest();
134  1 cache.addCacheEntryListener(eventListener);
135    } else {
136  0 eventListener = null;
137    }
138   
139  1 cache.set(KEY, VALUE);
140   
141  1 Assert.assertEquals(VALUE, cache.get(KEY));
142   
143  1 if (eventListener != null) {
144  1 Assert.assertTrue("No value has expired from the cache after provided max idle time",
145    eventListener.waitForEntryEvent(EventType.REMOVE));
146  1 Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue());
147    }
148   
149  1 Assert.assertNull(cache.get(KEY));
150   
151  1 cache.dispose();
152    }
153   
154    /**
155    * Validate the maximum time to live constraint.
156    *
157    * @throws Exception error
158    */
 
159  1 toggle @Test
160    public void testCreateAndDestroyCacheLRULifespan() throws Exception
161    {
162  1 CacheFactory factory = getCacheFactory();
163   
164  1 CacheConfiguration conf = new CacheConfiguration();
165  1 LRUEvictionConfiguration lec = new LRUEvictionConfiguration();
166  1 lec.setLifespan(1);
167  1 customizeEviction(lec);
168  1 conf.put(LRUEvictionConfiguration.CONFIGURATIONID, lec);
169   
170  1 Cache<Object> cache = factory.newCache(conf);
171   
172  1 Assert.assertNotNull(cache);
173   
174  1 CacheEntryListenerTest eventListener;
175  1 if (this.supportEvictionEvent) {
176  1 eventListener = new CacheEntryListenerTest();
177  1 cache.addCacheEntryListener(eventListener);
178    } else {
179  0 eventListener = null;
180    }
181   
182  1 cache.set(KEY, VALUE);
183   
184  1 Assert.assertEquals(VALUE, cache.get(KEY));
185   
186  1 if (eventListener != null) {
187  1 Assert.assertTrue("No value has expired from the cache after provide lifespan",
188    eventListener.waitForEntryEvent(EventType.REMOVE));
189  1 Assert.assertSame(VALUE, eventListener.getRemovedEvent().getEntry().getValue());
190    }
191   
192  1 Assert.assertNull(cache.get(KEY));
193   
194  1 cache.dispose();
195    }
196   
197    /**
198    * Validate the combination of maximum time to live and maximum cache entries constraints.
199    *
200    * @throws Exception error
201    */
 
202  1 toggle @Test
203    public void testCreateAndDestroyCacheLRUAll() throws Exception
204    {
205  1 CacheFactory factory = getCacheFactory();
206   
207  1 LRUCacheConfiguration conf = new LRUCacheConfiguration();
208  1 LRUEvictionConfiguration lec = conf.getLRUEvictionConfiguration();
209  1 lec.setMaxEntries(1);
210  1 lec.setMaxIdle(1);
211  1 lec.setLifespan(1);
212  1 customizeEviction(lec);
213   
214  1 Cache<Object> cache = factory.newCache(conf);
215   
216  1 Assert.assertNotNull(cache);
217   
218  1 CacheEntryListenerTest eventListener;
219  1 if (this.supportEvictionEvent) {
220  1 eventListener = new CacheEntryListenerTest();
221  1 cache.addCacheEntryListener(eventListener);
222    } else {
223  0 eventListener = null;
224    }
225   
226  1 cache.set(KEY, VALUE);
227   
228  1 Assert.assertEquals(VALUE, cache.get(KEY));
229   
230  1 cache.set(KEY2, VALUE2);
231   
232  1 Assert.assertNull(cache.get(KEY));
233  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
234   
235  1 Thread.sleep(1100);
236   
237  1 Assert.assertNull(cache.get(KEY));
238  1 Assert.assertNull(cache.get(KEY2));
239   
240  1 cache.dispose();
241    }
242    }