1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.cache.test

File AbstractEvictionGenericTestCache.java

 

Code metrics

14
86
6
1
240
135
13
0.15
14.33
6
2.17

Classes

Class Line # Actions
AbstractEvictionGenericTestCache 39 86 0% 13 11
0.896226489.6%
 

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.test;
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.test.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: 2af000e7d3e413196c5a21d72ec3f58b2104d9b8 $
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 createAndDestroyCacheLRUMaxEntries() 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 createAndDestroyCacheLRUMAxIdle() 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 createAndDestroyCacheLRULifespan() 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 createAndDestroyCacheLRUAll() 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    }
223   
224  1 cache.set(KEY, VALUE);
225   
226  1 Assert.assertEquals(VALUE, cache.get(KEY));
227   
228  1 cache.set(KEY2, VALUE2);
229   
230  1 Assert.assertNull(cache.get(KEY));
231  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
232   
233  1 Thread.sleep(1100);
234   
235  1 Assert.assertNull(cache.get(KEY));
236  1 Assert.assertNull(cache.get(KEY2));
237   
238  1 cache.dispose();
239    }
240    }