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

File AbstractGenericTestCache.java

 

Code metrics

0
62
8
1
216
102
8
0.13
7.75
8
1

Classes

Class Line # Actions
AbstractGenericTestCache 33 62 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 7 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   
28    /**
29    * Base class for testing cache component implementation.
30    *
31    * @version $Id: f8ef43927e81f633146d73a859eb7eff1691a588 $
32    */
 
33    public abstract class AbstractGenericTestCache extends AbstractTestCache
34    {
35    /**
36    * @param roleHint the role hint of the cache component implementation to test.
37    */
 
38  11 toggle protected AbstractGenericTestCache(String roleHint)
39    {
40  11 super(roleHint);
41    }
42   
43    // ///////////////////////////////////////////////////////::
44    // Tests
45   
46    /**
47    * Validate factory initialization.
48    *
49    * @throws Exception error.
50    */
 
51  1 toggle @Test
52    public void testGetFactory() throws Exception
53    {
54  1 CacheFactory factory = getCacheFactory();
55   
56  1 CacheFactory factory2 = getCacheFactory();
57   
58  1 Assert.assertSame(factory, factory2);
59    }
60   
61    /**
62    * Validate some basic cache use case without any constraints.
63    *
64    * @throws Exception error.
65    */
 
66  1 toggle @Test
67    public void testCreateAndDestroyCacheSimple() throws Exception
68    {
69  1 CacheFactory factory = getCacheFactory();
70   
71  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
72   
73  1 Assert.assertNotNull(cache);
74   
75  1 cache.set(KEY, VALUE);
76  1 cache.set(KEY2, VALUE2);
77   
78  1 Assert.assertEquals(VALUE, cache.get(KEY));
79  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
80   
81  1 cache.dispose();
82    }
83   
84    /**
85    * Validate {@link Cache#remove(String)}.
86    *
87    * @throws Exception error.
88    */
 
89  1 toggle @Test
90    public void testRemove() throws Exception
91    {
92  1 CacheFactory factory = getCacheFactory();
93   
94  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
95   
96  1 cache.set(KEY, VALUE);
97  1 cache.set(KEY2, VALUE2);
98   
99  1 cache.remove(KEY);
100   
101  1 Assert.assertNull(cache.get(KEY));
102  1 Assert.assertEquals(VALUE2, cache.get(KEY2));
103    }
104   
105    /**
106    * Validate {@link Cache#removeAll()}.
107    *
108    * @throws Exception error.
109    */
 
110  1 toggle @Test
111    public void testRemoveAll() throws Exception
112    {
113  1 CacheFactory factory = getCacheFactory();
114   
115  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
116   
117  1 cache.set(KEY, VALUE);
118  1 cache.set(KEY2, VALUE2);
119   
120  1 cache.removeAll();
121   
122  1 Assert.assertNull(cache.get(KEY));
123  1 Assert.assertNull(cache.get(KEY2));
124    }
125   
126    /**
127    * Validate event management.
128    *
129    * @throws Exception error.
130    */
 
131  1 toggle @Test
132    public void testEvents() throws Exception
133    {
134  1 CacheFactory factory = getCacheFactory();
135   
136  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
137   
138  1 CacheEntryListenerTest eventListener = new CacheEntryListenerTest();
139   
140  1 cache.addCacheEntryListener(eventListener);
141   
142  1 cache.set(KEY, VALUE);
143   
144  1 Assert.assertNotNull(eventListener.getAddedEvent());
145  1 Assert.assertSame(cache, eventListener.getAddedEvent().getCache());
146  1 Assert.assertEquals(KEY, eventListener.getAddedEvent().getEntry().getKey());
147  1 Assert.assertEquals(VALUE, eventListener.getAddedEvent().getEntry().getValue());
148   
149  1 cache.set(KEY, VALUE2);
150   
151  1 Assert.assertNotNull(eventListener.getModifiedEvent());
152  1 Assert.assertSame(cache, eventListener.getModifiedEvent().getCache());
153  1 Assert.assertEquals(KEY, eventListener.getModifiedEvent().getEntry().getKey());
154  1 Assert.assertEquals(VALUE2, eventListener.getModifiedEvent().getEntry().getValue());
155   
156  1 cache.remove(KEY);
157  1 cache.get(KEY);
158   
159  1 Assert.assertNotNull(eventListener.getRemovedEvent());
160  1 Assert.assertSame(cache, eventListener.getRemovedEvent().getCache());
161  1 Assert.assertEquals(KEY, eventListener.getRemovedEvent().getEntry().getKey());
162  1 Assert.assertEquals(VALUE2, eventListener.getRemovedEvent().getEntry().getValue());
163    }
164   
165    /**
166    * Validate that two different caches are really different.
167    *
168    * @throws Exception error.
169    */
 
170  1 toggle @Test
171    public void testSeveralCaches() throws Exception
172    {
173  1 CacheFactory factory = getCacheFactory();
174   
175  1 Cache<Object> cache = factory.newCache(new CacheConfiguration());
176  1 Cache<Object> cache2 = factory.newCache(new CacheConfiguration());
177   
178  1 cache.set(KEY, VALUE);
179   
180  1 Assert.assertNull(cache2.get(KEY));
181    }
182   
183    /**
184    * Validate that when recreating a cache with the same id the second instance is in a proper state.
185    *
186    * @throws Exception error
187    */
 
188  1 toggle @Test
189    public void testRecreateCache() throws Exception
190    {
191  1 CacheFactory factory = getCacheFactory();
192   
193  1 CacheConfiguration configuration = new CacheConfiguration();
194  1 configuration.setConfigurationId("test");
195   
196  1 Cache<Object> cache = factory.newCache(configuration);
197   
198  1 cache.set(KEY, VALUE);
199   
200  1 Assert.assertEquals(VALUE, cache.get(KEY));
201   
202    // dispose the first cache
203   
204  1 cache.dispose();
205   
206    // recreate it
207   
208  1 cache = factory.newCache(configuration);
209   
210  1 Assert.assertNull(cache.get(KEY));
211   
212  1 cache.set(KEY, VALUE);
213   
214  1 Assert.assertEquals(VALUE, cache.get(KEY));
215    }
216    }